1 /*======================================================================*
2 * Copyright (C) 2008 Light Weight Event System *
3 * All rights reserved. *
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. *
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. *
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"
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
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"
43 " where options are:" "\n"
45 " -m [one argument]" "\n"
46 " The multicast ip address to listen on." "\n"
47 " (default: 224.1.1.11)" "\n"
49 " -p [one argument]" "\n"
50 " The ip port to listen on." "\n"
51 " (default: 12345)" "\n"
53 " -i [one argument]" "\n"
54 " The interface to listen on." "\n"
55 " (default: 0.0.0.0)" "\n"
58 " show this message" "\n"
60 " arguments are specified as -option value or -optionvalue" "\n"
64 lwes_U_INT_16_to_stream
65 (LWES_U_INT_16 a_uint16
,
68 return fprintf (stream
,"%hu",a_uint16
);
73 (LWES_INT_16 an_int16
,
76 return fprintf (stream
,"%hd",an_int16
);
80 lwes_U_INT_32_to_stream
81 (LWES_U_INT_32 a_uint32
,
84 return fprintf (stream
,"%u",a_uint32
);
89 (LWES_INT_32 an_int32
,
92 return fprintf (stream
,"%d",an_int32
);
96 lwes_U_INT_64_to_stream
97 (LWES_U_INT_64 a_uint64
,
100 return fprintf (stream
,"%llu",a_uint64
);
104 lwes_INT_64_to_stream
105 (LWES_INT_64 an_int64
,
108 return fprintf (stream
,"%lld",an_int64
);
112 lwes_BOOLEAN_to_stream
113 (LWES_BOOLEAN a_boolean
,
116 return fprintf (stream
,"%s",((a_boolean
==1)?"true":"false"));
120 lwes_IP_ADDR_to_stream
121 (LWES_IP_ADDR an_ipaddr
,
124 return fprintf (stream
,"%s",inet_ntoa (an_ipaddr
));
128 lwes_SHORT_STRING_to_stream
129 (LWES_SHORT_STRING a_string
,
132 return fprintf (stream
,"%s",a_string
);
136 lwes_LONG_STRING_to_stream
137 (LWES_LONG_STRING a_string
,
140 return fprintf (stream
,"%s",a_string
);
144 lwes_event_attribute_to_stream
145 (struct lwes_event_attribute
*attribute
,
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
);
186 /* should really do something here, but not sure what */
193 (struct lwes_event
*event
,
196 struct lwes_event_attribute
*tmp
;
197 struct lwes_hash_enumeration e
;
199 lwes_SHORT_STRING_to_stream (event
->eventName
,stream
);
200 fprintf (stream
,"[");
202 lwes_U_INT_16_to_stream (event
->number_of_attributes
,stream
);
203 fprintf (stream
,"]");
205 fprintf (stream
,"\n");
207 fprintf (stream
,"{\n");
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
);
218 (struct lwes_event_attribute
*)lwes_hash_get (event
->attributes
,
221 fprintf (stream
,"\t");
223 lwes_SHORT_STRING_to_stream (tmpAttrName
,stream
);
225 fprintf (stream
," = ");
227 lwes_event_attribute_to_stream (tmp
,stream
);
229 fprintf (stream
,";\n");
233 fprintf (stream
,"}\n");
241 const char *mcast_ip
= "224.1.1.11";
242 const char *mcast_iface
= NULL
;
243 int mcast_port
= 12345;
246 struct sigaction act
;
248 struct lwes_listener
* listener
;
253 char c
= getopt (argc
, argv
, "m:p:i:h");
268 mcast_port
= atoi(optarg
);
273 fprintf (stderr
, "%s", help
);
278 mcast_iface
= optarg
;
284 "error: unrecognized command line option -%c\n",
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
);
314 struct lwes_event
*event
= lwes_event_create_no_name ( NULL
);
318 int ret
= lwes_listener_recv ( listener
, event
);
321 lwes_event_to_stream (event
, stdout
);
324 lwes_event_destroy (event
);
327 lwes_listener_destroy (listener
);
332 static void signal_handler(int sig
)
334 (void)sig
; /* appease compiler */