]> mj.ucw.cz Git - libucw.git/blob - ucw/main-test.c
1df357bb02b982def134f3c709b2b1fb2f742d2c
[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
25 static byte rb[16];
26
27 static void dread(struct main_block_io *bio)
28 {
29   if (bio->rpos < bio->rlen)
30     {
31       msg(L_INFO, "Read EOF");
32       block_io_del(bio);
33     }
34   else
35     {
36       msg(L_INFO, "Read done");
37       block_io_read(bio, rb, sizeof(rb));
38     }
39 }
40
41 static void derror(struct main_block_io *bio, int cause)
42 {
43   msg(L_INFO, "Error: %m !!! (cause %d)", cause);
44   block_io_del(bio);
45 }
46
47 static void dwrite(struct main_block_io *bio UNUSED)
48 {
49   msg(L_INFO, "Write done");
50 }
51
52 static int dhook(struct main_hook *ho UNUSED)
53 {
54   msg(L_INFO, "Hook called");
55   return 0;
56 }
57
58 static void dtimer(struct main_timer *tm)
59 {
60   msg(L_INFO, "Timer tick");
61   timer_add_rel(tm, 11000);
62   timer_add_rel(tm, 10000);
63 }
64
65 static void dentry(void)
66 {
67   log_fork();
68   msg(L_INFO, "*** SUBPROCESS START ***");
69   sleep(2);
70   msg(L_INFO, "*** SUBPROCESS FINISH ***");
71   exit(0);
72 }
73
74 static void dexit(struct main_process *pr)
75 {
76   msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
77 }
78
79 static void dsignal(struct main_signal *sg UNUSED)
80 {
81   msg(L_INFO, "SIGINT received (use Ctrl-\\ to really quit)");
82 }
83
84 int
85 main(void)
86 {
87   log_init(NULL);
88   main_init();
89
90   fin.read_done = dread;
91   fin.error_handler = derror;
92   block_io_add(&fin, 0);
93   block_io_read(&fin, rb, sizeof(rb));
94
95   fout.write_done = dwrite;
96   fout.error_handler = derror;
97   block_io_add(&fout, 1);
98   block_io_write(&fout, "Hello, world!\n", 14);
99
100   hook.handler = dhook;
101   hook_add(&hook);
102
103   tm.handler = dtimer;
104   timer_add_rel(&tm,  1000);
105
106   sg.signum = SIGINT;
107   sg.handler = dsignal;
108   signal_add(&sg);
109
110   mp.handler = dexit;
111   if (!process_fork(&mp))
112     dentry();
113
114   main_debug();
115
116   main_loop();
117   msg(L_INFO, "Finished.");
118 }
119
120 #endif