1 /*
2  * Compile with:
3  * c++ -I/usr/local/include -o time-test time-test.cpp -L/usr/local/lib -levent
4  *
5  * Wed 2006-12-27 - Modified by Leandro Lucarella <llucax+eventxx@gmail.com>
6  *
7  *     Adapted to test the C++ inteface.
8  *
9  */
10 
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/time.h>
14 #include <sys/socket.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <cstdlib>
18 #include <cstdio>
19 #include <cstring>
20 #include <csignal>
21 #include <cerrno>
22 
23 #include <eventxx>
24 
25 typedef void (cb_t)(int, short);
26 
27 int pair[2];
28 int test_okay = 1;
29 int called = 0;
30 eventxx::dispatcher d;
31 eventxx::event< cb_t >* ev;
32 
33 void
write_cb(int fd,short event)34 write_cb(int fd, short event)
35 {
36 	const char* test = "test string";
37 	int len;
38 
39 	len = write(fd, test, strlen(test) + 1);
40 
41 	printf("%s: write %d%s\n", __func__,
42 	    len, len ? "" : " - means EOF");
43 
44 	if (len > 0) {
45 		if (!called)
46 			d.add(*ev);
47 		close(pair[0]);
48 	} else if (called == 1)
49 		test_okay = 0;
50 
51 	called++;
52 }
53 
54 int
main(int argc,char ** argv)55 main (int argc, char **argv)
56 {
57 	if (signal(SIGPIPE, SIG_IGN) == SIG_IGN)
58 		return (1);
59 
60 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1)
61 		return (1);
62 
63 	/* Initalize one event */
64 	ev = new eventxx::event< cb_t >(pair[1], eventxx::WRITE, write_cb);
65 
66 	d.add(*ev);
67 
68 	d.dispatch();
69 
70 	delete ev;
71 
72 	return (test_okay);
73 }
74 
75