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;
24 static int sig_counter;
28 static void dread(struct main_block_io *bio)
30 if (bio->rpos < bio->rlen)
32 msg(L_INFO, "Read EOF");
33 bio->data = NULL; // Mark as deleted
38 msg(L_INFO, "Read done");
39 block_io_read(bio, rb, sizeof(rb));
40 block_io_set_timeout(&fin, 3000);
44 static void derror(struct main_block_io *bio, int cause)
50 msg(L_INFO, "derror: %s error: %m", (cause == BIO_ERR_READ ? "read" : "write"));
53 msg(L_INFO, "derror: Timeout");
62 static void dwrite(struct main_block_io *bio UNUSED)
64 msg(L_INFO, "Write done");
67 static int dhook(struct main_hook *ho UNUSED)
69 msg(L_INFO, "Hook called");
75 static void dtimer(struct main_timer *tm)
77 msg(L_INFO, "Timer tick");
78 timer_add_rel(tm, 11000);
79 timer_add_rel(tm, 10000);
82 static void dentry(void)
86 msg(L_INFO, "*** SUBPROCESS START ***");
88 msg(L_INFO, "*** SUBPROCESS FINISH ***");
92 static void dexit(struct main_process *pr)
94 msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
97 static void dsignal(struct main_signal *sg UNUSED)
99 msg(L_INFO, "SIGINT received (send 3 times to really quit, or use Ctrl-\\)");
109 fin.read_done = dread;
110 fin.error_handler = derror;
112 block_io_add(&fin, 0);
113 block_io_read(&fin, rb, sizeof(rb));
115 fout.write_done = dwrite;
116 fout.error_handler = derror;
118 block_io_add(&fout, 1);
119 block_io_write(&fout, "Hello, world!\n", 14);
121 hook.handler = dhook;
125 timer_add_rel(&tm, 1000);
128 sg.handler = dsignal;
132 if (!process_fork(&mp))
138 msg(L_INFO, "Finished.");