event.h 11.4 KB
Newer Older
Leigh B. Stoller's avatar
Leigh B. Stoller committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2007 University of Utah and the Flux Group.
Leigh B. Stoller's avatar
Leigh B. Stoller committed
4 5 6
 * All rights reserved.
 */

Ian Murdock's avatar
Ian Murdock committed
7 8 9 10 11 12 13 14 15 16 17
/*
 * event.h --
 *
 *      Definitions for the testbed event library.
 *
 */

#ifndef __EVENT_H__
#define __EVENT_H__

#include <stdio.h>
Timothy Stack's avatar
 
Timothy Stack committed
18
#include <stdarg.h>
19 20 21
#include <stdlib.h>
#include <sys/time.h>
#include <pubsub/pubsub.h>
Ian Murdock's avatar
Ian Murdock committed
22

Ian Murdock's avatar
Ian Murdock committed
23 24 25 26
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif /* MAXHOSTNAMELEN */

Timothy Stack's avatar
 
Timothy Stack committed
27 28 29 30
#ifdef __cplusplus
extern "C" {
#endif

Ian Murdock's avatar
Ian Murdock committed
31 32
/* Handle to the event server: */
struct event_handle {
33 34
    pubsub_handle_t *server;
    pubsub_error_t status;
35 36
    unsigned char *keydata;
    int keylen;
Timothy Stack's avatar
 
Timothy Stack committed
37
    int do_loop;
38
    /* API function pointers: */
39 40 41 42 43 44 45 46 47 48 49 50 51
    int (*connect)(char *server, int portnum, pubsub_handle_t **);
    int (*disconnect)(pubsub_handle_t *handle);
    int (*mainloop)(pubsub_handle_t *handle, int *go, pubsub_error_t *error);
    int (*notify)(pubsub_handle_t *handle, pubsub_notification_t *notification,
		  pubsub_error_t *error);
    pubsub_subscription_t *(*subscribe)(pubsub_handle_t *handle,
					char *expression,
					pubsub_notify_callback_t notify_cb,
					void *notify_arg,
					pubsub_error_t *error);
    int (*unsubscribe)(pubsub_handle_t *handle,
		       pubsub_subscription_t *subscription,
		       pubsub_error_t *error);
Ian Murdock's avatar
Ian Murdock committed
52 53 54 55
};
typedef struct event_handle * event_handle_t;

/* Event notification: */
56
struct event_notification {
57
	pubsub_notification_t *pubsub_notification;
58 59 60 61
	int		     has_hmac;
	
};
typedef struct event_notification *event_notification_t;
Ian Murdock's avatar
Ian Murdock committed
62 63

/* Event subscription: */
64
typedef pubsub_subscription_t *event_subscription_t;
Ian Murdock's avatar
Ian Murdock committed
65

66 67 68 69
/*
 * A tuple defines the target of the event, or if you are a subscriber,
 * what events you want to subscribe to.
 */
70
typedef struct _address_tuple {
71 72 73 74 75 76 77 78
	char		*site;		/* Which Emulab site. God only */
	char		*expt;		/* Project and experiment IDs */
	char		*group;		/* User defined group of nodes */
	char		*host;		/* A specific host */		
	char		*objtype;	/* LINK, TRAFGEN, etc ... */
        char		*objname;	/* link0, cbr0, cbr1, etc ... */
        char		*eventtype;	/* START, STOP, UP, DOWN, etc ... */
	int		scheduler;	/* A dynamic event to schedule */
Timothy Stack's avatar
 
Timothy Stack committed
79
	char		*timeline;	/* The timeline to schedule under */
80 81 82 83
} address_tuple, *address_tuple_t;
#define ADDRESSTUPLE_ANY	NULL
#define ADDRESSTUPLE_ALL	"*"
#define OBJECTTYPE_TESTBED	"TBCONTROL"
Mike Hibler's avatar
Mike Hibler committed
84
#define OBJECTTYPE_TRAFGEN	"TRAFGEN"
85 86 87 88

address_tuple_t	address_tuple_alloc(void);
int		address_tuple_free(address_tuple_t);

89
#ifndef SWIG /* SWIG doesn't handle these, so we have to hide them from it */
90
#ifndef NO_EVENT_MACROS
91 92 93 94 95 96 97 98 99 100 101 102 103 104
#define event_notification_get_site(handle, note, buf, len) \
        event_notification_get_string(handle, note, "SITE", buf, len)
#define event_notification_get_expt(handle, note, buf, len) \
        event_notification_get_string(handle, note, "EXPT", buf, len)
#define event_notification_get_group(handle, note, buf, len) \
        event_notification_get_string(handle, note, "GROUP", buf, len)
#define event_notification_get_host(handle, note, buf, len) \
        event_notification_get_string(handle, note, "HOST", buf, len)
#define event_notification_get_objtype(handle, note, buf, len) \
        event_notification_get_string(handle, note, "OBJTYPE", buf, len)
#define event_notification_get_objname(handle, note, buf, len) \
        event_notification_get_string(handle, note, "OBJNAME", buf, len)
#define event_notification_get_eventtype(handle, note, buf, len) \
        event_notification_get_string(handle, note, "EVENTTYPE", buf, len)
105 106 107 108
#define event_notification_get_arguments(handle, note, buf, len) \
        event_notification_get_string(handle, note, "ARGS", buf, len)
#define event_notification_set_arguments(handle, note, buf) \
        event_notification_put_string(handle, note, "ARGS", buf)
Timothy Stack's avatar
 
Timothy Stack committed
109 110
#define event_notification_get_timeline(handle, note, buf, len) \
        event_notification_get_string(handle, note, "TIMELINE", buf, len)
111

112 113 114 115 116 117 118 119 120 121 122
/*
 * For dynamic events.
 */
#define event_notification_clear_host(handle, note) \
	event_notification_remove(handle, note, "HOST")
#define event_notification_set_host(handle, note, buf) \
        event_notification_put_string(handle, note, "HOST", buf)
#define event_notification_clear_objtype(handle, note) \
	event_notification_remove(handle, note, "OBJTYPE")
#define event_notification_set_objtype(handle, note, buf) \
        event_notification_put_string(handle, note, "OBJTYPE", buf)
Leigh B. Stoller's avatar
Leigh B. Stoller committed
123 124 125 126
#define event_notification_clear_objname(handle, note) \
	event_notification_remove(handle, note, "OBJNAME")
#define event_notification_set_objname(handle, note, buf) \
        event_notification_put_string(handle, note, "OBJNAME", buf)
127 128 129 130 131 132 133 134 135

/*
 * Event library sets this field. Holds the sender of the event, as 
 * determined by the library when it is initialized. 
 */
#define event_notification_get_sender(handle, note, buf, len) \
        event_notification_get_string(handle, note, "___SENDER___", buf, len)
#define event_notification_set_sender(handle, note, buf) \
        event_notification_put_string(handle, note, "___SENDER___", buf)
136 137
#define event_notification_clr_sender(handle, note) \
        event_notification_remove(handle, note, "___SENDER___")
138
#endif /* ifndef NO_EVENT_MACROS */
139
#endif /* ifndef SWIG */
140

141

Ian Murdock's avatar
Ian Murdock committed
142 143 144
/* The "any host" string: */
#define EVENT_HOST_ANY "*"

Ian Murdock's avatar
Ian Murdock committed
145 146
/* Supported event types: */
typedef enum {
Ian Murdock's avatar
Ian Murdock committed
147
    EVENT_NULL,
Ian Murdock's avatar
Ian Murdock committed
148
    EVENT_TEST,
149
    EVENT_SCHEDULE,
Ian Murdock's avatar
Ian Murdock committed
150 151 152 153
    EVENT_TRAFGEN_START,
    EVENT_TRAFGEN_STOP
} event_type_t;

154 155 156 157
/* Event notification callback function.  Passed to event_subscribe
   and called whenever the specified event is triggered.
   HANDLE is the handle to the event server, NOTIFICATION is the event
   notification itself, and DATA is an arbitrary value passed to
158 159
   event_subscribe (argument 4).
 */
160 161 162 163
typedef void (*event_notify_callback_t)(event_handle_t handle,
                                        event_notification_t notification,
                                        void *data);

164 165 166 167 168
typedef void (*event_subscription_callback_t)(event_handle_t handle,
					      int result,
					      event_subscription_t es,
					      void *data);

Ian Murdock's avatar
Ian Murdock committed
169 170 171 172 173
/*
 * Function prototypes:
 */

/* event.c */
174
event_handle_t event_register(char *name, int threaded);
175 176 177 178
event_handle_t event_register_withkeyfile(char *name, int threaded,
					  char *keyfile);
event_handle_t event_register_withkeydata(char *name, int threaded,
					  unsigned char *keydata, int len);
Sachin Goyal's avatar
 
Sachin Goyal committed
179 180 181 182 183
event_handle_t event_register_withkeyfile_withretry(char *name, int threaded,
					  char *keyfile, int retrycount);
event_handle_t event_register_withkeydata_withretry(char *name, int threaded,
					  unsigned char *keydata, int len,
					  int retrycount);
Ian Murdock's avatar
Ian Murdock committed
184
int event_unregister(event_handle_t handle);
Mike Hibler's avatar
Mike Hibler committed
185
int event_poll(event_handle_t handle);
186
int event_poll_blocking(event_handle_t handle, unsigned int timeout);
Ian Murdock's avatar
Ian Murdock committed
187
int event_main(event_handle_t handle);
Timothy Stack's avatar
 
Timothy Stack committed
188
int event_stop_main(event_handle_t handle);
Ian Murdock's avatar
Ian Murdock committed
189
int event_notify(event_handle_t handle, event_notification_t notification);
190 191
int event_schedule(event_handle_t handle, event_notification_t notification,
                   struct timeval *time);
Ian Murdock's avatar
Ian Murdock committed
192
event_notification_t event_notification_alloc(event_handle_t handle,
193
                                              address_tuple_t tuple);
Ian Murdock's avatar
Ian Murdock committed
194 195
int event_notification_free(event_handle_t handle,
                            event_notification_t notification);
196 197
event_notification_t event_notification_clone(event_handle_t handle,
					event_notification_t notification);
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
int event_notification_get_double(event_handle_t handle,
                                  event_notification_t notification,
                                  char *name, double *value);
int event_notification_get_int32(event_handle_t handle,
                                 event_notification_t notification,
                                 char *name, int32_t *value);
int event_notification_get_int64(event_handle_t handle,
                                 event_notification_t notification,
                                 char *name, int64_t *value);
int event_notification_get_opaque(event_handle_t handle,
                                  event_notification_t notification,
                                  char *name, void *buffer, int length);
int event_notification_get_string(event_handle_t handle,
                                  event_notification_t notification,
                                  char *name, char *buffer, int length);
int event_notification_put_double(event_handle_t handle,
                                  event_notification_t notification,
                                  char *name, double value);
int event_notification_put_int32(event_handle_t handle,
                                 event_notification_t notification,
218
                                 char *name, int value);
219 220 221 222 223 224 225 226 227 228 229
int event_notification_put_int64(event_handle_t handle,
                                 event_notification_t notification,
                                 char *name, int64_t value);
int event_notification_put_opaque(event_handle_t handle,
                                  event_notification_t notification,
                                  char *name, void *buffer, int length);
int event_notification_put_string(event_handle_t handle,
                                  event_notification_t notification,
                                  char *name, char *value);
int event_notification_remove(event_handle_t handle,
                              event_notification_t notification, char *name);
Ian Murdock's avatar
Ian Murdock committed
230 231
event_subscription_t event_subscribe(event_handle_t handle,
                                     event_notify_callback_t callback,
232
                                     address_tuple_t tuple, void *data);
Timothy Stack's avatar
 
Timothy Stack committed
233 234 235 236
event_subscription_t event_subscribe_auth(event_handle_t handle,
					  event_notify_callback_t callback,
					  address_tuple_t tuple, void *data,
					  int do_auth);
237 238 239 240 241 242 243 244
int event_async_subscribe(event_handle_t handle,
			  event_notify_callback_t callback,
			  address_tuple_t tuple, void *data,
			  event_subscription_callback_t scb,
			  void *scb_data,
			  int do_auth);
int event_unsubscribe(event_handle_t handle, event_subscription_t es);
int event_async_unsubscribe(event_handle_t handle, event_subscription_t es);
245 246
int event_notification_insert_hmac(event_handle_t handle,
				   event_notification_t notification);
247
#ifdef NOTYET
248 249 250 251 252 253
int event_notification_pack(event_handle_t handle,
			    event_notification_t notification,
			    unsigned char *data, int *len);
int event_notification_unpack(event_handle_t handle,
			    event_notification_t *notification,
			    unsigned char *data, int len);
254
#endif
Sachin Goyal's avatar
 
Sachin Goyal committed
255
int event_set_idle_period(event_handle_t handle, int seconds) ;
Kirk Webb's avatar
 
Kirk Webb committed
256
int event_set_failover(event_handle_t handle, int dofail) ;
Ian Murdock's avatar
Ian Murdock committed
257

Timothy Stack's avatar
 
Timothy Stack committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
int event_arg_get(char *args, char *key, char **value);
int event_arg_dup(char *args, char *key, char **value);

typedef enum {
    EA_TAG_DONE,
    EA_Site,
    EA_Experiment,
    EA_Group,
    EA_Host,
    EA_Type,
    EA_Name,
    EA_Event,
    EA_Arguments,
    EA_ArgInteger,
    EA_ArgFloat,
    EA_ArgString,
    EA_When,
} ea_tag_t;

Timothy Stack's avatar
 
Timothy Stack committed
277 278 279 280 281 282 283
event_notification_t event_notification_create_v(event_handle_t handle,
						 struct timeval **when_out,
						 ea_tag_t tag,
						 va_list args);
event_notification_t event_notification_create(event_handle_t handle,
					       ea_tag_t tag,
					       ...);
Timothy Stack's avatar
 
Timothy Stack committed
284 285 286
int event_do_v(event_handle_t handle, ea_tag_t tag, va_list args);
int event_do(event_handle_t handle, ea_tag_t tag, ...);

Ian Murdock's avatar
Ian Murdock committed
287 288 289
/* util.c */
void *xmalloc(int size);
void *xrealloc(void *p, int size);
Kevin Atkinson's avatar
 
Kevin Atkinson committed
290
void make_timestamp(char * buf, const struct timeval * t_timeval);
Ian Murdock's avatar
Ian Murdock committed
291

Timothy Stack's avatar
 
Timothy Stack committed
292 293 294 295
#ifdef __cplusplus
}
#endif

Ian Murdock's avatar
Ian Murdock committed
296
#endif /* __EVENT_H__ */