1047f369cy/*
2047f369cy * Copyright (c) 2012 Niels Provos and Nick Mathewson
3047f369cy *
4047f369cy * Redistribution and use in source and binary forms, with or without
5047f369cy * modification, are permitted provided that the following conditions
6047f369cy * are met:
7047f369cy * 1. Redistributions of source code must retain the above copyright
8047f369cy *    notice, this list of conditions and the following disclaimer.
9047f369cy * 2. Redistributions in binary form must reproduce the above copyright
10047f369cy *    notice, this list of conditions and the following disclaimer in the
11047f369cy *    documentation and/or other materials provided with the distribution.
12047f369cy * 3. The name of the author may not be used to endorse or promote products
13047f369cy *    derived from this software without specific prior written permission.
14047f369cy *
15047f369cy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16047f369cy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17047f369cy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18047f369cy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19047f369cy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20047f369cy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21047f369cy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22047f369cy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23047f369cy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24047f369cy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25047f369cy */
26047f369cy#include "util-internal.h"
27047f369cy#include "event2/event-config.h"
28047f369cy
29047f369cy#ifdef _WIN32
30047f369cy#include <winsock2.h>
31047f369cy#include <windows.h>
32047f369cy#else
33047f369cy#include <unistd.h>
34047f369cy#endif
35047f369cy
36047f369cy#include <stdio.h>
37047f369cy#include <event2/event.h>
38047f369cy#include <signal.h>
39047f369cy
40047f369cystatic void
41047f369cysock_perror(const char *s)
42047f369cy{
43047f369cy#ifdef _WIN32
44047f369cy	const char *err = evutil_socket_error_to_string(EVUTIL_SOCKET_ERROR());
45047f369cy	fprintf(stderr, "%s: %s\n", s, err);
46047f369cy#else
47047f369cy	perror(s);
48047f369cy#endif
49047f369cy}
50047f369cy
51047f369cystatic void
52047f369cycallback1(evutil_socket_t fd, short events, void *arg)
53047f369cy{
54047f369cy}
55047f369cystatic void
56047f369cycallback2(evutil_socket_t fd, short events, void *arg)
57047f369cy{
58047f369cy}
59047f369cy
60047f369cy/* Testing code for event_base_dump_events().
61047f369cy
62047f369cy   Notes that just because we have code to exercise this function,
63047f369cy   doesn't mean that *ANYTHING* about the output format is guaranteed to
64047f369cy   remain in the future.
65047f369cy */
66047f369cyint
67047f369cymain(int argc, char **argv)
68047f369cy{
69047f369cy#define N_EVENTS 13
70047f369cy	int i;
71047f369cy	struct event *ev[N_EVENTS];
72047f369cy	evutil_socket_t pair1[2];
73047f369cy	evutil_socket_t pair2[2];
74047f369cy	struct timeval tv_onesec = {1,0};
75047f369cy	struct timeval tv_two5sec = {2,500*1000};
76047f369cy	const struct timeval *tv_onesec_common;
77047f369cy	const struct timeval *tv_two5sec_common;
78047f369cy	struct event_base *base;
79047f369cy	struct timeval now;
80047f369cy
81047f369cy#ifdef _WIN32
82047f369cy	WORD wVersionRequested;
83047f369cy	WSADATA wsaData;
84047f369cy
85047f369cy	wVersionRequested = MAKEWORD(2, 2);
86047f369cy
87047f369cy	WSAStartup(wVersionRequested, &wsaData);
88047f369cy#endif
89047f369cy
90047f369cy#ifdef _WIN32
91047f369cy#define LOCAL_SOCKETPAIR_AF AF_INET
92047f369cy#else
93047f369cy#define LOCAL_SOCKETPAIR_AF AF_UNIX
94047f369cy#endif
95047f369cy
96047f369cy	if (evutil_make_internal_pipe_(pair1) < 0 ||
97047f369cy	    evutil_make_internal_pipe_(pair2) < 0) {
98047f369cy		sock_perror("evutil_make_internal_pipe_");
99047f369cy		return 1;
100047f369cy	}
101047f369cy
102047f369cy	if (!(base = event_base_new())) {
103047f369cy		fprintf(stderr,"Couldn't make event_base\n");
104047f369cy		return 2;
105047f369cy	}
106047f369cy
107047f369cy	tv_onesec_common = event_base_init_common_timeout(base, &tv_onesec);
108047f369cy	tv_two5sec_common = event_base_init_common_timeout(base, &tv_two5sec);
109047f369cy
110047f369cy	ev[0] = event_new(base, pair1[0], EV_WRITE, callback1, NULL);
111047f369cy	ev[1] = event_new(base, pair1[1], EV_READ|EV_PERSIST, callback1, NULL);
112047f369cy	ev[2] = event_new(base, pair2[0], EV_WRITE|EV_PERSIST, callback2, NULL);
113047f369cy	ev[3] = event_new(base, pair2[1], EV_READ, callback2, NULL);
114047f369cy
115047f369cy	/* For timers */
116047f369cy	ev[4] = evtimer_new(base, callback1, NULL);
117047f369cy	ev[5] = evtimer_new(base, callback1, NULL);
118047f369cy	ev[6] = evtimer_new(base, callback1, NULL);
119047f369cy	ev[7] = event_new(base, -1, EV_PERSIST, callback2, NULL);
120047f369cy	ev[8] = event_new(base, -1, EV_PERSIST, callback2, NULL);
121047f369cy	ev[9] = event_new(base, -1, EV_PERSIST, callback2, NULL);
122047f369cy
123047f369cy	/* To activate */
124047f369cy	ev[10] = event_new(base, -1, 0, callback1, NULL);
125047f369cy	ev[11] = event_new(base, -1, 0, callback2, NULL);
126047f369cy
127047f369cy	/* Signals */
128047f369cy	ev[12] = evsignal_new(base, SIGINT, callback2, NULL);
129047f369cy
130047f369cy	event_add(ev[0], NULL);
131047f369cy	event_add(ev[1], &tv_onesec);
132047f369cy	event_add(ev[2], tv_onesec_common);
133047f369cy	event_add(ev[3], tv_two5sec_common);
134047f369cy
135047f369cy	event_add(ev[4], tv_onesec_common);
136047f369cy	event_add(ev[5], tv_onesec_common);
137047f369cy	event_add(ev[6], &tv_onesec);
138047f369cy	event_add(ev[7], tv_two5sec_common);
139047f369cy	event_add(ev[8], tv_onesec_common);
140047f369cy	event_add(ev[9], &tv_two5sec);
141047f369cy
142047f369cy	event_active(ev[10], EV_READ, 1);
143047f369cy	event_active(ev[11], EV_READ|EV_WRITE|EV_TIMEOUT, 1);
144047f369cy	event_active(ev[1], EV_READ, 1);
145047f369cy
146047f369cy	event_add(ev[12], NULL);
147047f369cy
148047f369cy	evutil_gettimeofday(&now,NULL);
149047f369cy	puts("=====expected");
150047f369cy	printf("Now= %ld.%06d\n",(long)now.tv_sec,(int)now.tv_usec);
151047f369cy	puts("Inserted:");
152047f369cy	printf("  %p [fd  %ld] Write\n",ev[0],(long)pair1[0]);
153047f369cy	printf("  %p [fd  %ld] Read Persist Timeout=T+1\n",ev[1],(long)pair1[1]);
154047f369cy	printf("  %p [fd  %ld] Write Persist Timeout=T+1\n",ev[2],(long)pair2[0]);
155047f369cy	printf("  %p [fd  %ld] Read Timeout=T+2.5\n",ev[3],(long)pair2[1]);
156047f369cy	printf("  %p [fd  -1] Timeout=T+1\n",ev[4]);
157047f369cy	printf("  %p [fd  -1] Timeout=T+1\n",ev[5]);
158047f369cy	printf("  %p [fd  -1] Timeout=T+1\n",ev[6]);
159047f369cy	printf("  %p [fd  -1] Persist Timeout=T+2.5\n",ev[7]);
160047f369cy	printf("  %p [fd  -1] Persist Timeout=T+1\n",ev[8]);
161047f369cy	printf("  %p [fd  -1] Persist Timeout=T+2.5\n",ev[9]);
162047f369cy	printf("  %p [sig %d] Signal Persist\n", ev[12], (int)SIGINT);
163047f369cy
164047f369cy	puts("Active:");
165047f369cy	printf("  %p [fd  -1, priority=0] Read active\n", ev[10]);
166047f369cy	printf("  %p [fd  -1, priority=0] Read Write Timeout active\n", ev[11]);
167047f369cy	printf("  %p [fd  %ld, priority=0] Read active\n", ev[1], (long)pair1[1]);
168047f369cy
169047f369cy	puts("======received");
170047f369cy	event_base_dump_events(base, stdout);
171047f369cy
172047f369cy	for (i = 0; i < N_EVENTS; ++i) {
173047f369cy		event_free(ev[i]);
174047f369cy	}
175047f369cy	event_base_free(base);
176047f369cy
177047f369cy	return 0;
178047f369cy}
179047f369cy
180