2 * UCW Library -- Main Loop: Testing
4 * (c) 2004--2011 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "ucw/mainloop.h"
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;
27 static void dread(struct main_block_io *bio)
29 if (bio->rpos < bio->rlen)
31 msg(L_INFO, "Read EOF");
36 msg(L_INFO, "Read done");
37 block_io_read(bio, rb, sizeof(rb));
41 static void derror(struct main_block_io *bio, int cause)
43 msg(L_INFO, "Error: %m !!! (cause %d)", cause);
47 static void dwrite(struct main_block_io *bio UNUSED)
49 msg(L_INFO, "Write done");
52 static int dhook(struct main_hook *ho UNUSED)
54 msg(L_INFO, "Hook called");
58 static void dtimer(struct main_timer *tm)
60 msg(L_INFO, "Timer tick");
61 timer_add_rel(tm, 11000);
62 timer_add_rel(tm, 10000);
65 static void dentry(void)
68 msg(L_INFO, "*** SUBPROCESS START ***");
70 msg(L_INFO, "*** SUBPROCESS FINISH ***");
74 static void dexit(struct main_process *pr)
76 msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
79 static void dsignal(struct main_signal *sg UNUSED)
81 msg(L_INFO, "SIGINT received (use Ctrl-\\ to really quit)");
90 fin.read_done = dread;
91 fin.error_handler = derror;
92 block_io_add(&fin, 0);
93 block_io_read(&fin, rb, sizeof(rb));
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);
100 hook.handler = dhook;
104 timer_add_rel(&tm, 1000);
107 sg.handler = dsignal;
111 if (!process_fork(&mp))
117 msg(L_INFO, "Finished.");