added lwes-filter-listener
[lwes.git] / src / lwes-event-printing-listener.c
blob12f10ccbab4388c018550851cb06d6dfb3c6ee12
1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
18 * Boston, MA 02110-1301 USA. *
19 *======================================================================*/
20 #include "lwes_listener.h"
22 #ifdef HAVE_GETOPT_H
23 #include <getopt.h>
24 #endif
26 #include <signal.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <stdio.h>
34 /* prototypes */
35 static void signal_handler(int sig);
37 /* global variable used to indicate what signal (if any) has been caught */
38 static volatile int done = 0;
40 static const char help[] =
41 "lwes-event-printing-listener [options]" "\n"
42 "" "\n"
43 " where options are:" "\n"
44 "" "\n"
45 " -m [one argument]" "\n"
46 " The multicast ip address to listen on." "\n"
47 " (default: 224.1.1.11)" "\n"
48 "" "\n"
49 " -p [one argument]" "\n"
50 " The ip port to listen on." "\n"
51 " (default: 12345)" "\n"
52 "" "\n"
53 " -i [one argument]" "\n"
54 " The interface to listen on." "\n"
55 " (default: 0.0.0.0)" "\n"
56 "" "\n"
57 " -h" "\n"
58 " show this message" "\n"
59 "" "\n"
60 " arguments are specified as -option value or -optionvalue" "\n"
61 "" "\n";
63 int
64 lwes_U_INT_16_to_stream
65 (LWES_U_INT_16 a_uint16,
66 FILE *stream)
68 return fprintf (stream,"%hu",a_uint16);
71 int
72 lwes_INT_16_to_stream
73 (LWES_INT_16 an_int16,
74 FILE *stream)
76 return fprintf (stream,"%hd",an_int16);
79 int
80 lwes_U_INT_32_to_stream
81 (LWES_U_INT_32 a_uint32,
82 FILE *stream)
84 return fprintf (stream,"%u",a_uint32);
87 int
88 lwes_INT_32_to_stream
89 (LWES_INT_32 an_int32,
90 FILE *stream)
92 return fprintf (stream,"%d",an_int32);
95 int
96 lwes_U_INT_64_to_stream
97 (LWES_U_INT_64 a_uint64,
98 FILE *stream)
100 return fprintf (stream,"%llu",a_uint64);
104 lwes_INT_64_to_stream
105 (LWES_INT_64 an_int64,
106 FILE *stream)
108 return fprintf (stream,"%lld",an_int64);
112 lwes_BOOLEAN_to_stream
113 (LWES_BOOLEAN a_boolean,
114 FILE *stream)
116 return fprintf (stream,"%s",((a_boolean==1)?"true":"false"));
120 lwes_IP_ADDR_to_stream
121 (LWES_IP_ADDR an_ipaddr,
122 FILE *stream)
124 return fprintf (stream,"%s",inet_ntoa (an_ipaddr));
128 lwes_SHORT_STRING_to_stream
129 (LWES_SHORT_STRING a_string,
130 FILE *stream)
132 return fprintf (stream,"%s",a_string);
136 lwes_LONG_STRING_to_stream
137 (LWES_LONG_STRING a_string,
138 FILE *stream)
140 return fprintf (stream,"%s",a_string);
144 lwes_event_attribute_to_stream
145 (struct lwes_event_attribute *attribute,
146 FILE *stream)
148 if (attribute->type == LWES_U_INT_16_TOKEN)
150 lwes_U_INT_16_to_stream (*((LWES_U_INT_16 *)attribute->value),stream);
152 else if (attribute->type == LWES_INT_16_TOKEN)
154 lwes_INT_16_to_stream (*((LWES_INT_16 *)attribute->value),stream);
156 else if (attribute->type == LWES_U_INT_32_TOKEN)
158 lwes_U_INT_32_to_stream (*((LWES_U_INT_32 *)attribute->value),stream);
160 else if (attribute->type == LWES_INT_32_TOKEN)
162 lwes_INT_32_to_stream (*((LWES_INT_32 *)attribute->value),stream);
164 else if (attribute->type == LWES_U_INT_64_TOKEN)
166 lwes_U_INT_64_to_stream (*((LWES_U_INT_64 *)attribute->value),stream);
168 else if (attribute->type == LWES_INT_64_TOKEN)
170 lwes_INT_64_to_stream (*((LWES_INT_64 *)attribute->value),stream);
172 else if (attribute->type == LWES_BOOLEAN_TOKEN)
174 lwes_BOOLEAN_to_stream (*((LWES_BOOLEAN *)attribute->value),stream);
176 else if (attribute->type == LWES_IP_ADDR_TOKEN)
178 lwes_IP_ADDR_to_stream (*((LWES_IP_ADDR *)attribute->value),stream);
180 else if (attribute->type == LWES_STRING_TOKEN)
182 lwes_LONG_STRING_to_stream ((LWES_LONG_STRING)attribute->value,stream);
184 else
186 /* should really do something here, but not sure what */
188 return 0;
192 lwes_event_to_stream
193 (struct lwes_event *event,
194 FILE *stream)
196 struct lwes_event_attribute *tmp;
197 struct lwes_hash_enumeration e;
199 lwes_SHORT_STRING_to_stream (event->eventName,stream);
200 fprintf (stream,"[");
201 fflush (stream);
202 lwes_U_INT_16_to_stream (event->number_of_attributes,stream);
203 fprintf (stream,"]");
204 fflush (stream);
205 fprintf (stream,"\n");
206 fflush (stream);
207 fprintf (stream,"{\n");
208 fflush (stream);
210 if (lwes_hash_keys (event->attributes, &e))
212 while (lwes_hash_enumeration_has_more_elements (&e))
214 LWES_SHORT_STRING tmpAttrName =
215 lwes_hash_enumeration_next_element (&e);
217 tmp =
218 (struct lwes_event_attribute *)lwes_hash_get (event->attributes,
219 tmpAttrName);
221 fprintf (stream,"\t");
222 fflush (stream);
223 lwes_SHORT_STRING_to_stream (tmpAttrName,stream);
224 fflush (stream);
225 fprintf (stream," = ");
226 fflush (stream);
227 lwes_event_attribute_to_stream (tmp,stream);
228 fflush (stream);
229 fprintf (stream,";\n");
230 fflush (stream);
233 fprintf (stream,"}\n");
234 fflush (stream);
235 return 0;
238 int main (int argc,
239 char *argv[])
241 const char *mcast_ip = "224.1.1.11";
242 const char *mcast_iface = NULL;
243 int mcast_port = 12345;
245 sigset_t fullset;
246 struct sigaction act;
248 struct lwes_listener * listener;
250 opterr = 0;
251 while (1)
253 char c = getopt (argc, argv, "m:p:i:h");
255 if (c == -1)
257 break;
260 switch (c)
262 case 'm':
263 mcast_ip = optarg;
265 break;
267 case 'p':
268 mcast_port = atoi(optarg);
270 break;
272 case 'h':
273 fprintf (stderr, "%s", help);
275 return 1;
277 case 'i':
278 mcast_iface = optarg;
280 break;
282 default:
283 fprintf (stderr,
284 "error: unrecognized command line option -%c\n",
285 optopt);
287 return 1;
291 sigfillset (&fullset);
292 sigprocmask (SIG_SETMASK, &fullset, NULL);
294 memset (&act, 0, sizeof (act));
295 act.sa_handler = signal_handler;
296 sigfillset (&act.sa_mask);
298 sigaction (SIGINT, &act, NULL);
299 sigaction (SIGTERM, &act, NULL);
300 sigaction (SIGPIPE, &act, NULL);
302 sigdelset (&fullset, SIGINT);
303 sigdelset (&fullset, SIGTERM);
304 sigdelset (&fullset, SIGPIPE);
306 sigprocmask (SIG_SETMASK, &fullset, NULL);
308 listener = lwes_listener_create ( (LWES_SHORT_STRING) mcast_ip,
309 (LWES_SHORT_STRING) mcast_iface,
310 (LWES_U_INT_32) mcast_port );
312 while ( ! done )
314 struct lwes_event *event = lwes_event_create_no_name ( NULL );
316 if ( event != NULL )
318 int ret = lwes_listener_recv ( listener, event);
319 if ( ret > 0 )
321 lwes_event_to_stream (event, stdout);
324 lwes_event_destroy (event);
327 lwes_listener_destroy (listener);
329 return 0;
332 static void signal_handler(int sig)
334 (void)sig; /* appease compiler */
335 done = 1;