Debian Codegraph @

libmowgli-2/src/examples/helpertest/helpertest.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * echoserver.c: Testing of the I/O system
4 *
5 * Copyright (c) 2012 William Pitcock <[email protected]>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include <mowgli.h>
25
26 int helper_count = 0;
27
28 void
29 timer_oneshot(mowgli_eventloop_helper_proc_t *helper)
30 {
31 mowgli_writef(helper->fd, "oneshot timer hit\n");
32 }
33
34 void
35 timer_tick(mowgli_eventloop_helper_proc_t *helper)
36 {
37 static int ticks = 0;
38
39 mowgli_writef(helper->fd, "tick: %d\n", ++ticks);
40
41 if (ticks > 10)
42 mowgli_eventloop_break(helper->eventloop);
43 }
44
45 void
46 helper_start(mowgli_eventloop_helper_proc_t *helper, void *userdata)
47 {
48 mowgli_eventloop_t *eventloop = helper->eventloop;
49
50 mowgli_writef(helper->fd, "hi from pid %d\n", getpid());
51
52 mowgli_timer_add(eventloop, "timer_tick", (mowgli_event_dispatch_func_t *) timer_tick, helper, 1);
53 mowgli_timer_add_once(eventloop, "timer_oneshot", (mowgli_event_dispatch_func_t *) timer_oneshot, helper, 5);
54
55 mowgli_eventloop_run(eventloop);
56
57 mowgli_writef(helper->fd, "eventloop halted\n");
58
59 mowgli_eventloop_destroy(eventloop);
60 }
61
62 void helper_read(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata);
63
64 void
65 helper_spawn(mowgli_eventloop_t *eventloop)
66 {
67 mowgli_eventloop_helper_proc_t *helper;
68
69 if (helper_count >= 100)
70 return;
71
72 helper = mowgli_helper_create(eventloop, helper_start, "Spawned helper", NULL);
73 mowgli_helper_set_read_cb(eventloop, helper, helper_read);
74
75 helper_count++;
76 }
77
78 void
79 helper_read(mowgli_eventloop_t *eventloop, mowgli_eventloop_io_t *io, mowgli_eventloop_io_dir_t dir, void *userdata)
80 {
81 size_t r;
82 char buf[16384];
83 mowgli_eventloop_helper_proc_t *helper = mowgli_eventloop_io_helper(io);
84
85 bzero(buf, sizeof buf);
86 r = read(helper->fd, buf, sizeof buf);
87
88 if (r > 0)
89 {
90 printf("helper %p [%d/%d]: %s", helper, helper->child->pid, helper->fd, buf);
91 }
92 else if (r <= 0)
93 {
94 helper_count--;
95 mowgli_helper_destroy(eventloop, helper);
96 }
97
98 if ((rand() % helper_count) == 0)
99 helper_spawn(eventloop);
100 }
101
102 int
103 main(int argc, char *argv[])
104 {
105 mowgli_eventloop_t *base_eventloop;
106
107 /* Bleh this is needed to ensure some systems can set the process title */
108 argv = mowgli_proctitle_init(argc, argv);
109
110 base_eventloop = mowgli_eventloop_create();
111
112 helper_spawn(base_eventloop);
113
114 mowgli_eventloop_run(base_eventloop);
115
116 return EXIT_SUCCESS;
117 }