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