1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert  * XXX This sample code was once meant to show how to use the basic Libevent
3*a466cc55SCy Schubert  * interfaces, but it never worked on non-Unix platforms, and some of the
4*a466cc55SCy Schubert  * interfaces have changed since it was first written.  It should probably
5*a466cc55SCy Schubert  * be removed or replaced with something better.
6*a466cc55SCy Schubert  *
7*a466cc55SCy Schubert  * Compile with:
8*a466cc55SCy Schubert  * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
9*a466cc55SCy Schubert  */
10*a466cc55SCy Schubert 
11*a466cc55SCy Schubert #include <sys/types.h>
12*a466cc55SCy Schubert 
13*a466cc55SCy Schubert #include <event2/event-config.h>
14*a466cc55SCy Schubert 
15*a466cc55SCy Schubert #include <sys/stat.h>
16*a466cc55SCy Schubert #ifndef _WIN32
17*a466cc55SCy Schubert #include <sys/queue.h>
18*a466cc55SCy Schubert #include <unistd.h>
19*a466cc55SCy Schubert #endif
20*a466cc55SCy Schubert #include <time.h>
21*a466cc55SCy Schubert #ifdef EVENT__HAVE_SYS_TIME_H
22*a466cc55SCy Schubert #include <sys/time.h>
23*a466cc55SCy Schubert #endif
24*a466cc55SCy Schubert #include <fcntl.h>
25*a466cc55SCy Schubert #include <stdlib.h>
26*a466cc55SCy Schubert #include <stdio.h>
27*a466cc55SCy Schubert #include <string.h>
28*a466cc55SCy Schubert #include <errno.h>
29*a466cc55SCy Schubert 
30*a466cc55SCy Schubert #include <event2/event.h>
31*a466cc55SCy Schubert #include <event2/event_struct.h>
32*a466cc55SCy Schubert #include <event2/util.h>
33*a466cc55SCy Schubert 
34*a466cc55SCy Schubert #ifdef _WIN32
35*a466cc55SCy Schubert #include <winsock2.h>
36*a466cc55SCy Schubert #endif
37*a466cc55SCy Schubert 
38*a466cc55SCy Schubert struct timeval lasttime;
39*a466cc55SCy Schubert 
40*a466cc55SCy Schubert int event_is_persistent;
41*a466cc55SCy Schubert 
42*a466cc55SCy Schubert static void
timeout_cb(evutil_socket_t fd,short event,void * arg)43*a466cc55SCy Schubert timeout_cb(evutil_socket_t fd, short event, void *arg)
44*a466cc55SCy Schubert {
45*a466cc55SCy Schubert 	struct timeval newtime, difference;
46*a466cc55SCy Schubert 	struct event *timeout = arg;
47*a466cc55SCy Schubert 	double elapsed;
48*a466cc55SCy Schubert 
49*a466cc55SCy Schubert 	evutil_gettimeofday(&newtime, NULL);
50*a466cc55SCy Schubert 	evutil_timersub(&newtime, &lasttime, &difference);
51*a466cc55SCy Schubert 	elapsed = difference.tv_sec +
52*a466cc55SCy Schubert 	    (difference.tv_usec / 1.0e6);
53*a466cc55SCy Schubert 
54*a466cc55SCy Schubert 	printf("timeout_cb called at %d: %.3f seconds elapsed.\n",
55*a466cc55SCy Schubert 	    (int)newtime.tv_sec, elapsed);
56*a466cc55SCy Schubert 	lasttime = newtime;
57*a466cc55SCy Schubert 
58*a466cc55SCy Schubert 	if (! event_is_persistent) {
59*a466cc55SCy Schubert 		struct timeval tv;
60*a466cc55SCy Schubert 		evutil_timerclear(&tv);
61*a466cc55SCy Schubert 		tv.tv_sec = 2;
62*a466cc55SCy Schubert 		event_add(timeout, &tv);
63*a466cc55SCy Schubert 	}
64*a466cc55SCy Schubert }
65*a466cc55SCy Schubert 
66*a466cc55SCy Schubert int
main(int argc,char ** argv)67*a466cc55SCy Schubert main(int argc, char **argv)
68*a466cc55SCy Schubert {
69*a466cc55SCy Schubert 	struct event timeout;
70*a466cc55SCy Schubert 	struct timeval tv;
71*a466cc55SCy Schubert 	struct event_base *base;
72*a466cc55SCy Schubert 	int flags;
73*a466cc55SCy Schubert 
74*a466cc55SCy Schubert #ifdef _WIN32
75*a466cc55SCy Schubert 	WORD wVersionRequested;
76*a466cc55SCy Schubert 	WSADATA wsaData;
77*a466cc55SCy Schubert 
78*a466cc55SCy Schubert 	wVersionRequested = MAKEWORD(2, 2);
79*a466cc55SCy Schubert 
80*a466cc55SCy Schubert 	(void)WSAStartup(wVersionRequested, &wsaData);
81*a466cc55SCy Schubert #endif
82*a466cc55SCy Schubert 
83*a466cc55SCy Schubert 	if (argc == 2 && !strcmp(argv[1], "-p")) {
84*a466cc55SCy Schubert 		event_is_persistent = 1;
85*a466cc55SCy Schubert 		flags = EV_PERSIST;
86*a466cc55SCy Schubert 	} else {
87*a466cc55SCy Schubert 		event_is_persistent = 0;
88*a466cc55SCy Schubert 		flags = 0;
89*a466cc55SCy Schubert 	}
90*a466cc55SCy Schubert 
91*a466cc55SCy Schubert 	/* Initialize the event library */
92*a466cc55SCy Schubert 	base = event_base_new();
93*a466cc55SCy Schubert 
94*a466cc55SCy Schubert 	/* Initialize one event */
95*a466cc55SCy Schubert 	event_assign(&timeout, base, -1, flags, timeout_cb, (void*) &timeout);
96*a466cc55SCy Schubert 
97*a466cc55SCy Schubert 	evutil_timerclear(&tv);
98*a466cc55SCy Schubert 	tv.tv_sec = 2;
99*a466cc55SCy Schubert 	event_add(&timeout, &tv);
100*a466cc55SCy Schubert 
101*a466cc55SCy Schubert 	evutil_gettimeofday(&lasttime, NULL);
102*a466cc55SCy Schubert 
103*a466cc55SCy Schubert 	setbuf(stdout, NULL);
104*a466cc55SCy Schubert 	setbuf(stderr, NULL);
105*a466cc55SCy Schubert 
106*a466cc55SCy Schubert 	event_base_dispatch(base);
107*a466cc55SCy Schubert 
108*a466cc55SCy Schubert 	return (0);
109*a466cc55SCy Schubert }
110*a466cc55SCy Schubert 
111