xdasd_trigger.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00039 #include "xdasd_trigger.h"
00040 #include "xdasd_exec.h"
00041 #include "xdasd_parse.h"
00042 #include "xdasd_mqueue.h"
00043 #include "xdasd_list.h"
00044 #include "xdasd_log.h"
00045
00046 #include <stdio.h>
00047 #include <stdlib.h>
00048 #include <string.h>
00049
00050
00051 static XDList s_list_trigger;
00052 static MsgQueue s_mq_trigger;
00062 static int tr_put(void * msg)
00063 {
00064 Parsed * parsed = (Parsed *)msg;
00065 Parsed * cpy;
00066 int i;
00067
00068
00069 if ((cpy = (Parsed *)malloc(parsed->structsz)) == 0)
00070 return -1;
00071
00072
00073 memcpy(cpy, parsed, offsetof(Parsed, parsed));
00074 memset(&parsed->triggers, 0, sizeof(parsed->triggers));
00075
00076
00077 memcpy(cpy->msg, parsed->msg, parsed->parsed[XDAS_FIELD_COUNT] - parsed->msg);
00078
00079
00080 for (i = 0; i < XDAS_FIELD_COUNT + 1; i++)
00081 cpy->parsed[i] = cpy->msg + (parsed->parsed[i] - parsed->msg);
00082
00083 xdasd_list_link_tail(&s_list_trigger, &cpy->elem);
00084
00085 return 0;
00086 }
00087
00095 static void * tr_get(void)
00096 {
00097 return s_list_trigger.count != 0?
00098 xdasd_list_unlink(&s_list_trigger, s_list_trigger.tail):
00099 0;
00100 }
00101
00108 static void tr_process(void * msg)
00109 {
00110 Parsed * parsed = (Parsed *)msg;
00111 Trigger * t = (Trigger *)parsed->triggers.head;
00112 while (t)
00113 {
00114 if (xdasd_exec(t->script, parsed->parsed) != 0)
00115 xdasd_log(0, "trigger: Error processing script for message:\n[ %s ]\n",
00116 parsed->msg);
00117 t = (Trigger *)t->elem.next;
00118 }
00119 xdasd_parse_free(parsed);
00120 }
00121
00122
00123
00151 int xdasd_trigger_append(Parsed * parsed)
00152 {
00153 return xdasd_mqueue_append(s_mq_trigger, parsed);
00154 }
00155
00162 int xdasd_trigger_init(void)
00163 {
00164 memset(&s_list_trigger, 0, sizeof(s_list_trigger));
00165 if ((s_mq_trigger = xdasd_mqueue_create(tr_put, tr_get, tr_process)) == 0)
00166 {
00167 xdasd_log(0, "trigger: Unable to create trigger queue.\n");
00168 return -1;
00169 }
00170 return 0;
00171 }
00172
00177 void xdasd_trigger_exit(void)
00178 {
00179 xdasd_mqueue_destroy(s_mq_trigger);
00180 }
00181