00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <core/threading/thread.h>
00027 #include <utils/time/clock.h>
00028 #include <utils/time/time.h>
00029 #include <utils/time/wait.h>
00030 #include <utils/system/signal.h>
00031
00032 #include <unistd.h>
00033
00034 #include <iostream>
00035
00036 using namespace std;
00037 using namespace fawkes;
00038
00039
00040 class QaTestWait
00041 {
00042 public:
00043 QaTestWait()
00044 {
00045 __clock = Clock::instance();
00046 __until = new Time();
00047 }
00048
00049
00050 void mark_start()
00051 {
00052 __clock->get_time(__until);
00053 *__until += (long int)30000;
00054 }
00055
00056 void wait()
00057 {
00058 Time now;
00059 printf("Now at %p\n", &now);
00060 __clock->get_time(&now);
00061 usleep(0);
00062 long int remaining_usec = (*__until - now).in_usec();
00063 while ( remaining_usec > 0 ) {
00064 usleep(remaining_usec);
00065 __clock->get_time(&now);
00066 remaining_usec = (*__until - now).in_usec();
00067
00068 }
00069 }
00070
00071 Clock *__clock;
00072 Time *__until;
00073 };
00074
00075 class QaSignalHandler : public SignalHandler
00076 {
00077 public:
00078 QaSignalHandler(Thread *thread)
00079 {
00080 this->thread = thread;
00081 }
00082
00083 virtual void handle_signal(int signum)
00084 {
00085 thread->cancel();
00086 }
00087
00088 Thread *thread;
00089 };
00090
00091 class QaTestThread : public Thread
00092 {
00093 public:
00094 QaTestThread() : Thread("QaTestThread")
00095 {
00096 timewait = new TimeWait(Clock::instance(), 30000);
00097 testwait = new QaTestWait();
00098 }
00099
00100 virtual void loop()
00101 {
00102 printf("Loop running\n");
00103 timewait->mark_start();
00104 timewait->wait();
00105
00106
00107 }
00108
00109 QaTestWait *testwait;
00110 TimeWait *timewait;
00111 };
00112
00113 int main(int argc, char** argv)
00114 {
00115 QaTestThread t;
00116 t.start();
00117
00118 QaSignalHandler h(&t);
00119 SignalManager::register_handler(SIGINT, &h);
00120
00121 t.join();
00122
00123 return 0;
00124 }
00125
00126