]> mj.ucw.cz Git - libucw.git/blob - ucw/main-test.c
Released as 6.5.16.
[libucw.git] / ucw / main-test.c
1 /*
2  *      UCW Library -- Main Loop: Testing
3  *
4  *      (c) 2004--2011 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include <ucw/lib.h>
11 #include <ucw/mainloop.h>
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #ifdef TEST
18
19 static struct main_process mp;
20 static struct main_block_io fin, fout;
21 static struct main_hook hook;
22 static struct main_timer tm;
23 static struct main_signal sg;
24 static int sig_counter;
25
26 static byte rb[16];
27
28 static void dread(struct main_block_io *bio)
29 {
30   if (bio->rpos < bio->rlen)
31     {
32       msg(L_INFO, "Read EOF");
33       bio->data = NULL; // Mark as deleted
34       block_io_del(bio);
35     }
36   else
37     {
38       msg(L_INFO, "Read done");
39       block_io_read(bio, rb, sizeof(rb));
40       block_io_set_timeout(&fin, 3000);
41     }
42 }
43
44 static void derror(struct main_block_io *bio, int cause)
45 {
46   switch (cause)
47     {
48     case BIO_ERR_READ:
49     case BIO_ERR_WRITE:
50       msg(L_INFO, "derror: %s error: %m", (cause == BIO_ERR_READ ? "read" : "write"));
51       break;
52     case BIO_ERR_TIMEOUT:
53       msg(L_INFO, "derror: Timeout");
54       break;
55     default:
56       ASSERT(0);
57     }
58   bio->data = NULL;
59   block_io_del(bio);
60 }
61
62 static void dwrite(struct main_block_io *bio UNUSED)
63 {
64   msg(L_INFO, "Write done");
65 }
66
67 static int dhook(struct main_hook *ho UNUSED)
68 {
69   msg(L_INFO, "Hook called");
70   if (sig_counter >= 3)
71     return HOOK_SHUTDOWN;
72   return 0;
73 }
74
75 static void dtimer(struct main_timer *tm)
76 {
77   msg(L_INFO, "Timer tick");
78   timer_add_rel(tm, 11000);
79   timer_add_rel(tm, 10000);
80 }
81
82 static void dentry(void)
83 {
84   log_fork();
85   main_teardown();
86   msg(L_INFO, "*** SUBPROCESS START ***");
87   sleep(2);
88   msg(L_INFO, "*** SUBPROCESS FINISH ***");
89   exit(0);
90 }
91
92 static void dexit(struct main_process *pr)
93 {
94   msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
95 }
96
97 static void dsignal(struct main_signal *sg UNUSED)
98 {
99   msg(L_INFO, "SIGINT received (send 3 times to really quit, or use Ctrl-\\)");
100   sig_counter++;
101 }
102
103 int
104 main(void)
105 {
106   log_init(NULL);
107   main_init();
108
109   fin.read_done = dread;
110   fin.error_handler = derror;
111   fin.data = "";
112   block_io_add(&fin, 0);
113   block_io_read(&fin, rb, sizeof(rb));
114
115   fout.write_done = dwrite;
116   fout.error_handler = derror;
117   fout.data = "";
118   block_io_add(&fout, 1);
119   block_io_write(&fout, "Hello, world!\n", 14);
120
121   hook.handler = dhook;
122   hook_add(&hook);
123
124   tm.handler = dtimer;
125   timer_add_rel(&tm,  1000);
126
127   sg.signum = SIGINT;
128   sg.handler = dsignal;
129   signal_add(&sg);
130
131   mp.handler = dexit;
132   if (!process_fork(&mp))
133     dentry();
134
135   main_debug();
136
137   main_loop();
138   msg(L_INFO, "Finished.");
139
140   if (fin.data)
141     block_io_del(&fin);
142   if (fout.data)
143     block_io_del(&fout);
144   hook_del(&hook);
145   signal_del(&sg);
146   timer_del(&tm);
147   main_cleanup();
148   return 0;
149 }
150
151 #endif