Commit 737cd1a1 authored by Ian Murdock's avatar Ian Murdock

Various cleanup changes.

parent 09c62e98
...@@ -7,20 +7,15 @@ ...@@ -7,20 +7,15 @@
* @COPYRIGHT@ * @COPYRIGHT@
*/ */
static char rcsid[] = "$Id: event.c,v 1.7 2002-01-29 12:15:18 imurdock Exp $"; static char rcsid[] = "$Id: event.c,v 1.8 2002-01-31 12:42:51 imurdock Exp $";
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <elvin/elvin.h>
#include <event.h> #include <event.h>
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif /* MAXHOSTNAMELEN */
static char hostname[MAXHOSTNAMELEN]; static char hostname[MAXHOSTNAMELEN];
...@@ -53,7 +48,7 @@ event_register(char *name) ...@@ -53,7 +48,7 @@ event_register(char *name)
return 0; return 0;
} }
TRACE("registering with event system (hostname=%s)\n", hostname); TRACE("registering with event system (hostname=\"%s\")\n", hostname);
/* Initialize the elvin synchronous interface: */ /* Initialize the elvin synchronous interface: */
...@@ -103,7 +98,7 @@ event_register(char *name) ...@@ -103,7 +98,7 @@ event_register(char *name)
/* /*
* Unregister with the testbed event system. Returns non-zero if the * Unregister with the testbed event system. Returns non-zero if the
* operation is successful, 0 otherwise. * operation is successful, 0 otherwise.
*/ */
...@@ -111,11 +106,11 @@ int ...@@ -111,11 +106,11 @@ int
event_unregister(event_handle_t handle) event_unregister(event_handle_t handle)
{ {
if (!handle) { if (!handle) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("unregistering with event system (hostname=%s)\n", hostname); TRACE("unregistering with event system (hostname=\"%s\")\n", hostname);
/* Disconnect from the elvin server: */ /* Disconnect from the elvin server: */
if (elvin_sync_disconnect(handle->server, handle->status) == 0) { if (elvin_sync_disconnect(handle->server, handle->status) == 0) {
...@@ -155,7 +150,7 @@ event_main(event_handle_t handle) ...@@ -155,7 +150,7 @@ event_main(event_handle_t handle)
int loop = 1; int loop = 1;
if (!handle) { if (!handle) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
...@@ -181,12 +176,12 @@ event_main(event_handle_t handle) ...@@ -181,12 +176,12 @@ event_main(event_handle_t handle)
int int
event_notify(event_handle_t handle, event_notification_t notification) event_notify(event_handle_t handle, event_notification_t notification)
{ {
if (!handle) { if (!handle || !notification) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("sending event notification (notification=%p)\n", notification); TRACE("sending event notification %p\n", notification);
/* Send notification to Elvin server for routing: */ /* Send notification to Elvin server for routing: */
if (elvin_sync_notify(handle->server, notification, 1, NULL, if (elvin_sync_notify(handle->server, notification, 1, NULL,
...@@ -208,6 +203,10 @@ event_notify(event_handle_t handle, event_notification_t notification) ...@@ -208,6 +203,10 @@ event_notify(event_handle_t handle, event_notification_t notification)
* and may optionally have attributes added to it by * and may optionally have attributes added to it by
* event_notification_put_*. Returns non-zero if the operation * event_notification_put_*. Returns non-zero if the operation
* is successful, 0 otherwise. * is successful, 0 otherwise.
*
* This function essentially operates as a deferred event_notify.
* event_notify sends notifications immediately,
* whereas event_schedule sends notifications at some later time.
*/ */
int int
...@@ -216,14 +215,13 @@ event_schedule(event_handle_t handle, event_notification_t notification, ...@@ -216,14 +215,13 @@ event_schedule(event_handle_t handle, event_notification_t notification,
{ {
event_type_t type; event_type_t type;
if (!handle) { if (!handle || !notification || !time) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("scheduling event notification (notification=%p, " TRACE("scheduling event notification %p to be sent at time (%ld, %ld)\n",
"time=(tv_sec=%ld, tv_usec=%ld))\n", notification, notification, time->tv_sec, time->tv_usec);
time->tv_sec, time->tv_usec);
/* Change the event type to EVENT_SCHEDULE, saving the old event /* Change the event type to EVENT_SCHEDULE, saving the old event
type in the notification structure so the event scheduler can type in the notification structure so the event scheduler can
...@@ -233,12 +231,14 @@ event_schedule(event_handle_t handle, event_notification_t notification, ...@@ -233,12 +231,14 @@ event_schedule(event_handle_t handle, event_notification_t notification,
(int *) &type) (int *) &type)
== 0) == 0)
{ {
ERROR("could not get old type attribute from notification\n"); ERROR("could not get old type attribute from notification %p\n",
notification);
return 0; return 0;
} }
if (event_notification_remove(handle, notification, "type") == 0) { if (event_notification_remove(handle, notification, "type") == 0) {
ERROR("could not remove old type attribute from notification\n"); ERROR("could not remove old type attribute from notification %p\n",
notification);
return 0; return 0;
} }
...@@ -246,14 +246,16 @@ event_schedule(event_handle_t handle, event_notification_t notification, ...@@ -246,14 +246,16 @@ event_schedule(event_handle_t handle, event_notification_t notification,
EVENT_SCHEDULE) EVENT_SCHEDULE)
== 0) == 0)
{ {
ERROR("could not add new type attribute to notification\n"); ERROR("could not add new type attribute to notification %p\n",
notification);
return 0; return 0;
} }
if (event_notification_put_int32(handle, notification, "old_type", if (event_notification_put_int32(handle, notification, "old_type",
type) type)
== 0) == 0)
{ {
ERROR("could not add old type attribute to notification\n"); ERROR("could not add old type attribute to notification %p\n",
notification);
return 0; return 0;
} }
...@@ -264,7 +266,8 @@ event_schedule(event_handle_t handle, event_notification_t notification, ...@@ -264,7 +266,8 @@ event_schedule(event_handle_t handle, event_notification_t notification,
time->tv_sec) time->tv_sec)
== 0) == 0)
{ {
ERROR("could not add time.tv_sec attribute to notification\n"); ERROR("could not add time.tv_sec attribute to notification %p\n",
notification);
return 0; return 0;
} }
if (event_notification_put_int32(handle, notification, "time_usec", if (event_notification_put_int32(handle, notification, "time_usec",
...@@ -272,7 +275,8 @@ event_schedule(event_handle_t handle, event_notification_t notification, ...@@ -272,7 +275,8 @@ event_schedule(event_handle_t handle, event_notification_t notification,
== 0) == 0)
{ {
ERROR("could not add time.tv_usec attribute to notification\n"); ERROR("could not add time.tv_usec attribute to notification %p\n",
notification);
return 0; return 0;
} }
...@@ -295,12 +299,12 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type) ...@@ -295,12 +299,12 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type)
{ {
elvin_notification_t notification; elvin_notification_t notification;
if (!handle) { if (!handle || !host || !type) {
ERROR("invalid handle\n"); ERROR("invalid paramater\n");
return NULL; return NULL;
} }
TRACE("allocating notification (host=%s, type=%d)\n", host, type); TRACE("allocating notification (host=\"%s\", type=%d)\n", host, type);
notification = elvin_notification_alloc(handle->status); notification = elvin_notification_alloc(handle->status);
if (notification == NULL) { if (notification == NULL) {
...@@ -309,13 +313,14 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type) ...@@ -309,13 +313,14 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type)
return NULL; return NULL;
} }
TRACE("allocated notification (notification=%p)\n", notification); TRACE("allocated notification %p\n", notification);
/* Add hostname to notification: */ /* Add hostname to notification: */
if (event_notification_put_string(handle, notification, "host", host) if (event_notification_put_string(handle, notification, "host", host)
== 0) == 0)
{ {
ERROR("could not add host attribute to notification\n"); ERROR("could not add host attribute to notification %p\n",
notification);
return NULL; return NULL;
} }
...@@ -323,7 +328,8 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type) ...@@ -323,7 +328,8 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type)
if (event_notification_put_int32(handle, notification, "type", type) if (event_notification_put_int32(handle, notification, "type", type)
== 0) == 0)
{ {
ERROR("could not add host attribute to notification\n"); ERROR("could not add type attribute to notification %p\n",
notification);
return NULL; return NULL;
} }
...@@ -332,7 +338,7 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type) ...@@ -332,7 +338,7 @@ event_notification_alloc(event_handle_t handle, char *host, event_type_t type)
/* /*
* Free the event notification NOTIFICATION. Returns non-zero if the * Free the event notification NOTIFICATION. Returns non-zero if the
* operation is successful, 0 otherwise. * operation is successful, 0 otherwise.
*/ */
...@@ -340,12 +346,12 @@ int ...@@ -340,12 +346,12 @@ int
event_notification_free(event_handle_t handle, event_notification_free(event_handle_t handle,
event_notification_t notification) event_notification_t notification)
{ {
if (!handle) { if (!handle || !notification) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("freeing notification (notification=%p)\n", notification); TRACE("freeing notification %p\n", notification);
if (elvin_notification_free(notification, handle->status) == 0) { if (elvin_notification_free(notification, handle->status) == 0) {
ERROR("elvin_notification_free failed: "); ERROR("elvin_notification_free failed: ");
...@@ -366,8 +372,8 @@ static int attr_traverse(void *rock, char *name, elvin_basetypes_t type, ...@@ -366,8 +372,8 @@ static int attr_traverse(void *rock, char *name, elvin_basetypes_t type,
elvin_value_t value, elvin_error_t error); elvin_value_t value, elvin_error_t error);
/* /*
* Get the attribute with name NAME and type TYPE from the event * Get the attribute with name NAME from the event notification
* notification NOTIFICATION. * NOTIFICATION.
* Writes the value of the attribute to *VALUE and returns * Writes the value of the attribute to *VALUE and returns
* non-zero if the named attribute is found, 0 otherwise. * non-zero if the named attribute is found, 0 otherwise.
*/ */
...@@ -379,13 +385,8 @@ event_notification_get(event_handle_t handle, ...@@ -379,13 +385,8 @@ event_notification_get(event_handle_t handle,
{ {
struct attr_traverse_arg arg; struct attr_traverse_arg arg;
if (!handle) { if (!handle || !notification || !name || !value) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0;
}
if (!value) {
ERROR("invalid result parameter (value)\n");
return 0; return 0;
} }
...@@ -421,18 +422,13 @@ event_notification_get_double(event_handle_t handle, ...@@ -421,18 +422,13 @@ event_notification_get_double(event_handle_t handle,
{ {
elvin_value_t v; elvin_value_t v;
if (!handle) { if (!handle || !notification || !name || !value) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0;
}
if (!value) {
ERROR("invalid result parameter (value)\n");
return 0; return 0;
} }
if (event_notification_get(handle, notification, name, &v) == 0) { if (event_notification_get(handle, notification, name, &v) == 0) {
ERROR("could not get double attribute `%s' from notification %p\n", ERROR("could not get double attribute \"%s\" from notification %p\n",
name, notification); name, notification);
return 0; return 0;
} }
...@@ -457,18 +453,13 @@ event_notification_get_int32(event_handle_t handle, ...@@ -457,18 +453,13 @@ event_notification_get_int32(event_handle_t handle,
{ {
elvin_value_t v; elvin_value_t v;
if (!handle) { if (!handle || !notification || !name || !value) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0;
}
if (!value) {
ERROR("invalid result parameter (value)\n");
return 0; return 0;
} }
if (event_notification_get(handle, notification, name, &v) == 0) { if (event_notification_get(handle, notification, name, &v) == 0) {
ERROR("could not get int32 attribute `%s' from notification %p\n", ERROR("could not get int32 attribute \"%s\" from notification %p\n",
name, notification); name, notification);
return 0; return 0;
} }
...@@ -493,18 +484,13 @@ event_notification_get_int64(event_handle_t handle, ...@@ -493,18 +484,13 @@ event_notification_get_int64(event_handle_t handle,
{ {
elvin_value_t v; elvin_value_t v;
if (!handle) { if (!handle || !notification || !name || !value) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0;
}
if (!value) {
ERROR("invalid result parameter (value)\n");
return 0; return 0;
} }
if (event_notification_get(handle, notification, name, &v) == 0) { if (event_notification_get(handle, notification, name, &v) == 0) {
ERROR("could not get int64 attribute `%s' from notification %p\n", ERROR("could not get int64 attribute \"%s\" from notification %p\n",
name, notification); name, notification);
return 0; return 0;
} }
...@@ -518,8 +504,8 @@ event_notification_get_int64(event_handle_t handle, ...@@ -518,8 +504,8 @@ event_notification_get_int64(event_handle_t handle,
/* /*
* Get the opaque attribute with name NAME from the event * Get the opaque attribute with name NAME from the event
* notification NOTIFICATION. * notification NOTIFICATION.
* Writes the value of the attribute to *VALUE and returns * Writes LENGTH bytes into *BUFFER and returns non-zero if the named
* non-zero if the named attribute is found, 0 otherwise. * attribute is found, 0 otherwise.
*/ */
int int
...@@ -529,18 +515,13 @@ event_notification_get_opaque(event_handle_t handle, ...@@ -529,18 +515,13 @@ event_notification_get_opaque(event_handle_t handle,
{ {
elvin_value_t v; elvin_value_t v;
if (!handle) { if (!handle || !notification || !name || !buffer || !length) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0;
}
if (!buffer) {
ERROR("invalid result parameter (buffer)\n");
return 0; return 0;
} }
if (event_notification_get(handle, notification, name, &v) == 0) { if (event_notification_get(handle, notification, name, &v) == 0) {
ERROR("could not get opaque attribute `%s' from notification %p\n", ERROR("could not get opaque attribute \"%s\" from notification %p\n",
name, notification); name, notification);
return 0; return 0;
} }
...@@ -554,8 +535,8 @@ event_notification_get_opaque(event_handle_t handle, ...@@ -554,8 +535,8 @@ event_notification_get_opaque(event_handle_t handle,
/* /*
* Get the string attribute with name NAME from the event * Get the string attribute with name NAME from the event
* notification NOTIFICATION. * notification NOTIFICATION.
* Writes the value of the attribute to *VALUE and returns * Writes LENGTH bytes into *BUFFER and returns non-zero if the named
* non-zero if the named attribute is found, 0 otherwise. * attribute is found, 0 otherwise.
*/ */
int int
...@@ -565,18 +546,13 @@ event_notification_get_string(event_handle_t handle, ...@@ -565,18 +546,13 @@ event_notification_get_string(event_handle_t handle,
{ {
elvin_value_t v; elvin_value_t v;
if (!handle) { if (!handle || !notification || !name || !buffer || !length) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0;
}
if (!buffer) {
ERROR("invalid result parameter (buffer)\n");
return 0; return 0;
} }
if (event_notification_get(handle, notification, name, &v) == 0) { if (event_notification_get(handle, notification, name, &v) == 0) {
ERROR("could not get double attribute `%s' from notification %p\n", ERROR("could not get string attribute \"%s\" from notification %p\n",
name, notification); name, notification);
return 0; return 0;
} }
...@@ -598,13 +574,13 @@ event_notification_put_double(event_handle_t handle, ...@@ -598,13 +574,13 @@ event_notification_put_double(event_handle_t handle,
event_notification_t notification, event_notification_t notification,
char *name, double value) char *name, double value)
{ {
if (!handle) { if (!handle || !notification || !name) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("adding attribute to notification (notification=%p, " TRACE("adding attribute (name=\"%s\", value=%f) to notification %p\n",
"name=%s, value=%f)\n", notification, name, value); name, value, notification);
if (elvin_notification_add_real64(notification, name, value, if (elvin_notification_add_real64(notification, name, value,
handle->status) handle->status)
...@@ -630,13 +606,13 @@ event_notification_put_int32(event_handle_t handle, ...@@ -630,13 +606,13 @@ event_notification_put_int32(event_handle_t handle,
event_notification_t notification, event_notification_t notification,
char *name, int32_t value) char *name, int32_t value)
{ {
if (!handle) { if (!handle || !notification || !name) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("adding attribute to notification (notification=%p, " TRACE("adding attribute (name=\"%s\", value=%d) to notification %p\n",
"name=%s, value=%d)\n", notification, name, value); name, value, notification);
if (elvin_notification_add_int32(notification, name, value, if (elvin_notification_add_int32(notification, name, value,
handle->status) handle->status)
...@@ -662,13 +638,13 @@ event_notification_put_int64(event_handle_t handle, ...@@ -662,13 +638,13 @@ event_notification_put_int64(event_handle_t handle,
event_notification_t notification, event_notification_t notification,
char *name, int64_t value) char *name, int64_t value)
{ {
if (!handle) { if (!handle || !notification || !name) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("adding attribute to notification (notification=%p, " TRACE("adding attribute (name=\"%s\", value=%lld) to notification %p\n",
"name=%s, value=%lld)\n", notification, name, value); name, value, notification);
if (elvin_notification_add_int64(notification, name, value, if (elvin_notification_add_int64(notification, name, value,
handle->status) handle->status)
...@@ -695,13 +671,13 @@ event_notification_put_opaque(event_handle_t handle, ...@@ -695,13 +671,13 @@ event_notification_put_opaque(event_handle_t handle,
event_notification_t notification, event_notification_t notification,
char *name, void *buffer, int length) char *name, void *buffer, int length)
{ {
if (!handle) { if (!handle || !notification || !buffer || !length) {
ERROR("invalid handle\n"); ERROR("invalid parameter\n");
return 0; return 0;
} }
TRACE("adding attribute to notification (notification=%p, " TRACE("adding attribute (name=\"%s\", value=<opaque>) "
"name=%s, value=<opaque>)\n", notification, name); "to notification %p\n", name, notification);
if (elvin_notification_add_opaque(notification, name, buffer, length, if (elvin_notification_add_opaque(notification, name, buffer, length,
handle->status) handle->status)
...@@ -727,13 +703,13 @@ event_notification_put_string(event_handle_t handle, ...@@ -727,13 +703,13 @@ event_notification_put_string(event_handle_t handle,