1 /*======================================================================*
2 * Copyright (c) 2008, Yahoo! Inc. All rights reserved. *
4 * Licensed under the New BSD License (the "License"); you may not use *
5 * this file except in compliance with the License. Unless required *
6 * by applicable law or agreed to in writing, software distributed *
7 * under the License is distributed on an "AS IS" BASIS, WITHOUT *
8 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
9 * See the License for the specific language governing permissions and *
10 * limitations under the License. See accompanying LICENSE file. *
11 *======================================================================*/
13 #include "lwes_listener.h"
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
29 static void signal_handler(int sig
);
31 // global variable used to indicate what signal (if any) has been caught
32 static volatile int done
= 0;
34 static const char help
[] =
35 "lwes-event-counting-listener [options]" "\n"
37 " where options are:" "\n"
39 " -m [one argument]" "\n"
40 " The multicast ip address to listen on." "\n"
41 " (default: 224.1.1.11)" "\n"
43 " -p [one argument]" "\n"
44 " The ip port to listen on." "\n"
45 " (default: 12345)" "\n"
47 " -i [one argument]" "\n"
48 " The interface to listen on." "\n"
49 " (default: 0.0.0.0)" "\n"
52 " Quiet mode, don't actually print out the message" "\n"
55 " Count mode, count the packets which come in and print out" "\n"
56 " the count every second" "\n"
59 " show this message" "\n"
61 " arguments are specified as -option value or -optionvalue" "\n"
66 lwes_U_INT_16_to_stream
67 (LWES_U_INT_16 a_uint16
,
70 return fprintf (stream
,"%hu",a_uint16
);
76 (LWES_INT_16 an_int16
,
79 return fprintf (stream
,"%hd",an_int16
);
84 lwes_U_INT_32_to_stream
85 (LWES_U_INT_32 a_uint32
,
88 return fprintf (stream
,"%u",a_uint32
);
94 (LWES_INT_32 an_int32
,
97 return fprintf (stream
,"%d",an_int32
);
102 lwes_U_INT_64_to_stream
103 (LWES_U_INT_64 a_uint64
,
106 return fprintf (stream
,"%llu",a_uint64
);
111 lwes_INT_64_to_stream
112 (LWES_INT_64 an_int64
,
115 return fprintf (stream
,"%lld",an_int64
);
120 lwes_BOOLEAN_to_stream
121 (LWES_BOOLEAN a_boolean
,
124 return fprintf (stream
,"%s",((a_boolean
==1)?"true":"false"));
129 lwes_IP_ADDR_to_stream
130 (LWES_IP_ADDR an_ipaddr
,
133 return fprintf (stream
,"%s",inet_ntoa (an_ipaddr
));
138 lwes_SHORT_STRING_to_stream
139 (LWES_SHORT_STRING a_string
,
142 return fprintf (stream
,"%s",a_string
);
147 lwes_LONG_STRING_to_stream
148 (LWES_LONG_STRING a_string
,
151 return fprintf (stream
,"%s",a_string
);
156 lwes_event_attribute_to_stream
157 (struct lwes_event_attribute
*attribute
,
160 if (attribute
->type
== LWES_U_INT_16_TOKEN
)
162 lwes_U_INT_16_to_stream (*((LWES_U_INT_16
*)attribute
->value
),stream
);
164 else if (attribute
->type
== LWES_INT_16_TOKEN
)
166 lwes_INT_16_to_stream (*((LWES_INT_16
*)attribute
->value
),stream
);
168 else if (attribute
->type
== LWES_U_INT_32_TOKEN
)
170 lwes_U_INT_32_to_stream (*((LWES_U_INT_32
*)attribute
->value
),stream
);
172 else if (attribute
->type
== LWES_INT_32_TOKEN
)
174 lwes_INT_32_to_stream (*((LWES_INT_32
*)attribute
->value
),stream
);
176 else if (attribute
->type
== LWES_U_INT_64_TOKEN
)
178 lwes_U_INT_64_to_stream (*((LWES_U_INT_64
*)attribute
->value
),stream
);
180 else if (attribute
->type
== LWES_INT_64_TOKEN
)
182 lwes_INT_64_to_stream (*((LWES_INT_64
*)attribute
->value
),stream
);
184 else if (attribute
->type
== LWES_BOOLEAN_TOKEN
)
186 lwes_BOOLEAN_to_stream (*((LWES_BOOLEAN
*)attribute
->value
),stream
);
188 else if (attribute
->type
== LWES_IP_ADDR_TOKEN
)
190 lwes_IP_ADDR_to_stream (*((LWES_IP_ADDR
*)attribute
->value
),stream
);
192 else if (attribute
->type
== LWES_STRING_TOKEN
)
194 lwes_LONG_STRING_to_stream ((LWES_LONG_STRING
)attribute
->value
,stream
);
198 /* should really do something here, but not sure what */
206 (struct lwes_event
*event
,
209 struct lwes_event_attribute
*tmp
;
210 struct lwes_hash_enumeration e
;
212 lwes_SHORT_STRING_to_stream (event
->eventName
,stream
);
213 fprintf (stream
,"[");
215 lwes_U_INT_16_to_stream (event
->number_of_attributes
,stream
);
216 fprintf (stream
,"]");
218 fprintf (stream
,"\n");
220 fprintf (stream
,"{\n");
223 if (lwes_hash_keys (event
->attributes
, &e
))
225 while (lwes_hash_enumeration_has_more_elements (&e
))
227 LWES_SHORT_STRING tmpAttrName
=
228 lwes_hash_enumeration_next_element (&e
);
231 (struct lwes_event_attribute
*)lwes_hash_get (event
->attributes
,
234 fprintf (stream
,"\t");
236 lwes_SHORT_STRING_to_stream (tmpAttrName
,stream
);
238 fprintf (stream
," = ");
240 lwes_event_attribute_to_stream (tmp
,stream
);
242 fprintf (stream
,";\n");
246 fprintf (stream
,"}\n");
254 const char *mcast_ip
= "224.1.1.11";
255 const char *mcast_iface
= NULL
;
256 int mcast_port
= 12345;
261 struct sigaction act
;
263 struct lwes_listener
* listener
;
265 time_t start_time
= time (NULL
);
268 /* turn off error messages, I'll handle them */
272 char c
= getopt (argc
, argv
, "m:p:i:qch");
295 mcast_port
= atoi(optarg
);
300 fprintf (stderr
, "%s", help
);
305 mcast_iface
= optarg
;
311 "error: unrecognized command line option -%c\n",
318 sigfillset (&fullset
);
319 sigprocmask (SIG_SETMASK
, &fullset
, NULL
);
321 memset (&act
, 0, sizeof (act
));
322 act
.sa_handler
= signal_handler
;
323 sigfillset (&act
.sa_mask
);
325 sigaction (SIGINT
, &act
, NULL
);
326 sigaction (SIGTERM
, &act
, NULL
);
327 sigaction (SIGPIPE
, &act
, NULL
);
329 sigdelset (&fullset
, SIGINT
);
330 sigdelset (&fullset
, SIGTERM
);
331 sigdelset (&fullset
, SIGPIPE
);
333 sigprocmask (SIG_SETMASK
, &fullset
, NULL
);
335 listener
= lwes_listener_create ( (LWES_SHORT_STRING
) mcast_ip
,
336 (LWES_SHORT_STRING
) mcast_iface
,
337 (LWES_U_INT_32
) mcast_port
);
341 struct lwes_event
*event
= lwes_event_create_no_name ( NULL
);
346 int ret
= lwes_listener_recv_by (listener
, event
,1000);
355 lwes_event_to_stream (event
, stdout
);
360 current_time
= time (NULL
);
361 if ((current_time
- start_time
) >= frequency
)
364 /* HH/MM/SS DD/MM/YYYY */
365 /* 12345678901234567890 */
366 start_time
= time (NULL
);
368 strftime (timebuff
, 20, "%H:%M:%S %d/%m/%Y",
369 localtime (&start_time
));
371 printf ("%s : %d\n", timebuff
, event_count
);
376 lwes_event_destroy (event
);
379 lwes_listener_destroy (listener
);
384 static void signal_handler(int sig
)
386 (void)sig
; // appease compiler