Permalink
Browse files

Added "name" parameter to alarm.

  • Loading branch information...
1 parent 5548cd7 commit bf8973284bc5fdfb624eef9e9b7a88655a5f696c Nathan Fritz committed with dustin Aug 26, 2009
Showing with 46 additions and 9 deletions.
  1. +3 −1 alarm.c
  2. +6 −1 alarm.h
  3. +1 −1 examples/bot.c
  4. +33 −6 tests/check_alarm.c
  5. +3 −0 xmpp.c
View
4 alarm.c
@@ -26,9 +26,10 @@ alarm_t get_alarm(alarm_queue_t *queue)
}
/* add an alarm to the queue */
-bool add_alarm(alarm_queue_t *queue, const char *msg)
+bool add_alarm(alarm_queue_t *queue, const char *name, const char *msg)
{
assert(queue);
+ assert(name);
assert(msg);
bool rv = false;
@@ -37,6 +38,7 @@ bool add_alarm(alarm_queue_t *queue, const char *msg)
alarm_t *alarm = &queue->queue[queue->in];
alarm->open = true;
alarm->num = queue->num;
+ strncpy(alarm->name, name, ALARM_NAME_MAXLEN);
strncpy(alarm->msg, msg, ALARM_MSG_MAXLEN);
queue->size++;
queue->in++;
View
7 alarm.h
@@ -11,6 +11,7 @@
#define ALARM_QUEUE_SIZE 100
#define ALARM_MSG_MAXLEN 255
+#define ALARM_NAME_MAXLEN 25
/**
* \addtogroup Alarm
@@ -34,6 +35,10 @@ typedef struct {
* alarm message
*/
char msg[ALARM_MSG_MAXLEN + 1];
+ /**
+ * alarm name
+ */
+ char name[ALARM_NAME_MAXLEN + 1];
} alarm_t;
/**
@@ -80,7 +85,7 @@ alarm_t get_alarm(alarm_queue_t *queue);
* @param msg message of <=255 characters for alarm
* @return true iff the alarm was enqueued
*/
-bool add_alarm(alarm_queue_t *queue, const char *msg)
+bool add_alarm(alarm_queue_t *queue, const char *name, const char *msg)
__attribute__ ((warn_unused_result));
/**
View
2 examples/bot.c
@@ -122,7 +122,7 @@ static enum conflate_mgmt_cb_result process_alarm_create(void *opaque,
kvpair_t *form,
conflate_form_result *r)
{
- if(add_alarm(handle->alarms, "This is a test alarm!")) {
+ if(add_alarm(handle->alarms, "test", "This is a test alarm!")) {
fprintf(stderr, "Created alarm!\n");
} else {
fprintf(stderr, "Error queueing an alarm.\n");
View
39 tests/check_alarm.c
@@ -24,24 +24,27 @@ START_TEST(test_simple_alarm)
{
alarm_t in_alarm;
- fail_unless(add_alarm(alarmqueue, "This is a test message 1"),
+ fail_unless(add_alarm(alarmqueue, "test", "This is a test message 1"),
"Failed to alarm.");
in_alarm = get_alarm(alarmqueue);
fail_unless(in_alarm.open == 1, "Didn't receive alarm message 1.");
+ fail_unless(strcmp(in_alarm.name, "test") == 0, "Alarm name didn't match");
fail_unless(strcmp(in_alarm.msg, "This is a test message 1") == 0,
"Didn't get the right message for message 1.");
- fail_unless(add_alarm(alarmqueue, "This is a test message 2"),
+ fail_unless(add_alarm(alarmqueue, "test2", "This is a test message 2"),
"Failed to alarm.");
- fail_unless(add_alarm(alarmqueue, "This is a test message 3"),
+ fail_unless(add_alarm(alarmqueue, "test3", "This is a test message 3"),
"Failed to alarm.");
in_alarm = get_alarm(alarmqueue);
fail_unless(in_alarm.open == 1, "Didn't receive alarm message 2.");
+ fail_unless(strcmp(in_alarm.name, "test2") == 0, "test2 didn't match");
fail_unless(strcmp(in_alarm.msg, "This is a test message 2") == 0,
"Didn't get the right message for message 2.");
in_alarm = get_alarm(alarmqueue);
fail_unless(in_alarm.open == 1, "Didn't receive alarm message 1.");
+ fail_unless(strcmp(in_alarm.name, "test3") == 0, "test3 didn't match");
fail_unless(strcmp(in_alarm.msg, "This is a test message 3") == 0,
"Didn't get the right message for message 3.");
in_alarm = get_alarm(alarmqueue);
@@ -60,26 +63,49 @@ START_TEST(test_giant_alarm)
fail_unless(strlen(msg) > ALARM_MSG_MAXLEN,
"Message string was too short to blow up.");
- fail_unless(add_alarm(alarmqueue, msg), "Failed to alarm.");
+ fail_unless(add_alarm(alarmqueue, "bigass", msg), "Failed to alarm.");
alarm_t in_alarm = get_alarm(alarmqueue);
fail_unless(in_alarm.open, "Didn't receive a large alarm.");
fail_unless(strlen(in_alarm.msg) == ALARM_MSG_MAXLEN,
"Alarm message is too long.");
+ fail_unless(strcmp("bigass", in_alarm.name) == 0,
+ "Alarm name didn't match.");
fail_unless(strncmp(msg, in_alarm.msg, ALARM_MSG_MAXLEN) == 0,
"Alarm message didn't match.");
}
END_TEST
+START_TEST(test_giant_name)
+{
+ const char *name = "this name should exceed the max length";
+ fail_unless(strlen(name) > ALARM_NAME_MAXLEN,
+ "Name wasn't too big enough.");
+
+ fail_unless(add_alarm(alarmqueue, name, "some message"),
+ "Failed to alarm.");
+
+ alarm_t in_alarm = get_alarm(alarmqueue);
+ fail_unless(in_alarm.open, "Didn't receive an alarm.");
+
+ fail_unless(strlen(in_alarm.name) == ALARM_NAME_MAXLEN,
+ "Alarm name is too long.");
+ fail_unless(strncmp(name, in_alarm.name, ALARM_NAME_MAXLEN) == 0,
+ "Alarm name didn't match.");
+ fail_unless(strcmp("some message", in_alarm.msg) == 0,
+ "Alarm message didn't match.");
+}
+END_TEST
+
START_TEST(test_full_queue)
{
for (int i = 0; i < ALARM_QUEUE_SIZE; i++) {
- fail_unless(add_alarm(alarmqueue, "Test alarm message."),
+ fail_unless(add_alarm(alarmqueue, "add", "Test alarm message."),
"Failed to add alarm.");
}
- fail_if(add_alarm(alarmqueue, "Test failing alarm."),
+ fail_if(add_alarm(alarmqueue, "fail", "Test failing alarm."),
"Should have failed to add another alarm.");
}
END_TEST
@@ -93,6 +119,7 @@ static Suite* alarm_suite(void)
tcase_add_test(tc, test_simple_alarm);
tcase_add_test(tc, test_giant_alarm);
+ tcase_add_test(tc, test_giant_name);
tcase_add_test(tc, test_full_queue);
suite_add_tcase(s, tc);
View
3 xmpp.c
@@ -493,6 +493,7 @@ static int alarmqueue_handler(xmpp_conn_t * const conn, void * const userdata)
char open[2] = { 0, 0 };
char amsg[256];
char body[1500];
+ char num[255];
while (handle->alarms->size > 0)
{
alarm = get_alarm(handle->alarms);
@@ -518,6 +519,7 @@ static int alarmqueue_handler(xmpp_conn_t * const conn, void * const userdata)
assert(mbody);
xmpp_stanza_set_name(mbody, "body");
snprintf(body, sizeof(body), "Alert\n%s", amsg);
+ snprintf(num, sizeof(num), "%d", alarm.num);
xmpp_stanza_set_text(mbody, body);
add_and_release(msg, mbody);
@@ -527,6 +529,7 @@ static int alarmqueue_handler(xmpp_conn_t * const conn, void * const userdata)
xmpp_stanza_set_attribute(alert, "xmlns", "http://northscale.net/protocol/alerts");
xmpp_stanza_set_attribute(alert, "open", open);
xmpp_stanza_set_attribute(alert, "msg", amsg);
+ xmpp_stanza_set_attribute(alert, "num", num);
add_and_release(msg, alert);
xmpp_send(conn, msg);

0 comments on commit bf89732

Please sign in to comment.