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)
46 msg(L_INFO, "Error: %m !!! (cause %d)", cause);
51 static void dwrite(struct main_block_io *bio UNUSED)
53 msg(L_INFO, "Write done");
56 static int dhook(struct main_hook *ho UNUSED)
58 msg(L_INFO, "Hook called");
64 static void dtimer(struct main_timer *tm)
66 msg(L_INFO, "Timer tick");
67 timer_add_rel(tm, 11000);
68 timer_add_rel(tm, 10000);
71 static void dentry(void)
75 msg(L_INFO, "*** SUBPROCESS START ***");
77 msg(L_INFO, "*** SUBPROCESS FINISH ***");
81 static void dexit(struct main_process *pr)
83 msg(L_INFO, "Subprocess %d exited with status %x", pr->pid, pr->status);
86 static void dsignal(struct main_signal *sg UNUSED)
88 msg(L_INFO, "SIGINT received (send 3 times to really quit, or use Ctrl-\\)");
98 fin.read_done = dread;
99 fin.error_handler = derror;
101 block_io_add(&fin, 0);
102 block_io_read(&fin, rb, sizeof(rb));
104 fout.write_done = dwrite;
105 fout.error_handler = derror;
107 block_io_add(&fout, 1);
108 block_io_write(&fout, "Hello, world!\n", 14);
110 hook.handler = dhook;
114 timer_add_rel(&tm, 1000);
117 sg.handler = dsignal;
121 if (!process_fork(&mp))
127 msg(L_INFO, "Finished.");