proxytest.c 2.89 KB
Newer Older
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2003, 2004 University of Utah and the Flux Group.
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 * All rights reserved.
 */

/*
 * Test the proxy.
 */

#include <stdio.h>
#include <ctype.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "event.h"
#include "tbdefs.h"
#include "log.h"

static char	*progname;
static int	debug = 0;

void
usage()
{
	fprintf(stderr,	"Usage: %s [-s server] [-p port]\n", progname);
	exit(-1);
}

static void
callback(event_handle_t handle,
	 event_notification_t notification, void *data);

int
main(int argc, char **argv)
{
	event_handle_t handle;
	address_tuple_t	tuple;
41
	char *server = "localhost";
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
	char *port = NULL;
	char buf[BUFSIZ];
	int c;
	
	progname = argv[0];
	
	while ((c = getopt(argc, argv, "s:p:d")) != -1) {
		switch (c) {
		case 'd':
			debug = 1;
			break;
		case 's':
			server = optarg;
			break;
		case 'p':
			port = optarg;
			break;
		default:
			usage();
		}
	}
	argc -= optind;
	argv += optind;

	/*
	 * Convert server/port to elvin thing.
	 *
	 * XXX This elvin string stuff should be moved down a layer. 
	 */
	if (server) {
		snprintf(buf, sizeof(buf), "elvin://%s%s%s",
			 server,
			 (port ? ":"  : ""),
			 (port ? port : ""));
		server = buf;
	}

	/*
	 * Construct an address tuple for generating the event.
	 */
	tuple = address_tuple_alloc();
	if (tuple == NULL) {
		fatal("could not allocate an address tuple");
	}

	/* Register with the event system: */
	handle = event_register(server, 0);
	if (handle == NULL) {
		fatal("could not register with event system");
	}

	/* Subscribe to the test event: */
	if (! event_subscribe(handle, callback, tuple, "event received")) {
		fatal("could not subscribe to event");
	}

	/* Begin the event loop, waiting to receive event notifications: */
	event_main(handle);

	/* Unregister with the event system: */
	if (event_unregister(handle) == 0) {
		fatal("could not unregister with event system");
	}

	return 0;
}

/*
 * Handle Events
 */
static void
callback(event_handle_t handle, event_notification_t notification, void *data)
{
	char		eventtype[TBDB_FLEN_EVEVENTTYPE];
	char		objecttype[TBDB_FLEN_EVOBJTYPE];
	char		objectname[TBDB_FLEN_EVOBJNAME];
	struct timeval  now, then, diff;
			
	gettimeofday(&now, NULL);

	event_notification_get_eventtype(handle,
				notification, eventtype, sizeof(eventtype));
	event_notification_get_objtype(handle,
				notification, objecttype, sizeof(objecttype));
	event_notification_get_objname(handle,
				notification, objectname, sizeof(objectname));
	event_notification_get_int32(handle, notification, "time_usec",
				     (int *) &then.tv_usec);
	event_notification_get_int32(handle, notification, "time_sec",
				     (int *) &then.tv_sec);
	timersub(&now, &then, &diff);

	info("%s %s %s %d %d %d %d %d %d\n", eventtype, objecttype,
	     objectname, now.tv_sec, now.tv_usec, then.tv_sec, then.tv_usec,
	     diff.tv_sec, diff.tv_usec);
}