Home | History | Annotate | Download | only in sample
      1 /*
      2  * Compile with:
      3  * cc -I/usr/local/include -o time-test time-test.c -L/usr/local/lib -levent
      4  */
      5 
      6 #include <sys/types.h>
      7 
      8 #ifdef HAVE_CONFIG_H
      9 #include "config.h"
     10 #endif
     11 
     12 #include <sys/stat.h>
     13 #ifndef WIN32
     14 #include <sys/queue.h>
     15 #include <unistd.h>
     16 #endif
     17 #include <time.h>
     18 #ifdef HAVE_SYS_TIME_H
     19 #include <sys/time.h>
     20 #endif
     21 #include <fcntl.h>
     22 #include <stdlib.h>
     23 #include <stdio.h>
     24 #include <string.h>
     25 #include <errno.h>
     26 
     27 #include <event.h>
     28 #include <evutil.h>
     29 
     30 int lasttime;
     31 
     32 static void
     33 timeout_cb(int fd, short event, void *arg)
     34 {
     35 	struct timeval tv;
     36 	struct event *timeout = arg;
     37 	int newtime = time(NULL);
     38 
     39 	printf("%s: called at %d: %d\n", __func__, newtime,
     40 	    newtime - lasttime);
     41 	lasttime = newtime;
     42 
     43 	evutil_timerclear(&tv);
     44 	tv.tv_sec = 2;
     45 	event_add(timeout, &tv);
     46 }
     47 
     48 int
     49 main (int argc, char **argv)
     50 {
     51 	struct event timeout;
     52 	struct timeval tv;
     53 
     54 	/* Initalize the event library */
     55 	event_init();
     56 
     57 	/* Initalize one event */
     58 	evtimer_set(&timeout, timeout_cb, &timeout);
     59 
     60 	evutil_timerclear(&tv);
     61 	tv.tv_sec = 2;
     62 	event_add(&timeout, &tv);
     63 
     64 	lasttime = time(NULL);
     65 
     66 	event_dispatch();
     67 
     68 	return (0);
     69 }
     70 
     71