-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlwes.i
297 lines (236 loc) · 7 KB
/
lwes.i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
%module LWES
%{
#include "lwes.h"
%}
%include lwes.h
%inline %{
struct lwes_event_type_db *
create_db(char *filename)
{
return lwes_event_type_db_create(filename);
}
int
destroy_db(struct lwes_event_type_db* db)
{
return lwes_event_type_db_destroy(db);
}
struct lwes_emitter *
create_emitter(char *address, char *iface, int port,
int emit_heartbeat, short freq)
{
return lwes_emitter_create(address, iface, port, emit_heartbeat, freq);
}
struct lwes_emitter *
create_emitter_with_ttl(char *address, char *iface, int port,
int emit_heartbeat, short freq, int ttl)
{
return lwes_emitter_create_with_ttl(address, iface, port, emit_heartbeat,
freq, ttl);
}
int
emit(struct lwes_emitter *emitter, struct lwes_event *event)
{
return lwes_emitter_emit(emitter, event);
}
int
emitto(char *address, char *iface, int port, struct lwes_emitter *emitter,
struct lwes_event *event)
{
return lwes_emitter_emitto(address, iface, port, emitter, event);
}
int
destroy_emitter(struct lwes_emitter *emitter)
{
return lwes_emitter_destroy(emitter);
}
struct lwes_event *
create_event(struct lwes_event_type_db* db, char *event_name)
{
return lwes_event_create(db, event_name);
}
struct lwes_event *
create_event_with_encoding(struct lwes_event_type_db *db, char *event_name,
short encoding)
{
return lwes_event_create_with_encoding(db, event_name, encoding);
}
int
set_uint16(struct lwes_event *event, char *attribute_name,
unsigned short a_uint16)
{
return lwes_event_set_U_INT_16(event, attribute_name, a_uint16);
}
int
get_uint16(struct lwes_event* event, char* attribute_name,
unsigned short *a_uint16)
{
return lwes_event_get_U_INT_16(event, attribute_name, a_uint16);
}
int
set_int16(struct lwes_event* event, char* attribute_name,
short an_int16)
{
return lwes_event_set_INT_16(event, attribute_name, an_int16);
}
int
get_int16(struct lwes_event* event, char* attribute_name,
short* an_int16)
{
return lwes_event_get_INT_16(event, attribute_name, an_int16);
}
int
set_uint32(struct lwes_event* event, char* attribute_name,
unsigned int a_uint32)
{
return lwes_event_set_U_INT_32(event, attribute_name, a_uint32);
}
int
get_uint32(struct lwes_event* event, char* attribute_name,
unsigned int * a_uint32)
{
return lwes_event_get_U_INT_32(event, attribute_name, a_uint32);
}
int
set_int32(struct lwes_event* event, char* attribute_name,
int an_int32)
{
return lwes_event_set_INT_32(event, attribute_name, an_int32);
}
int
get_int32(struct lwes_event* event, char* attribute_name,
int* an_int32)
{
return lwes_event_get_INT_32(event, attribute_name, an_int32);
}
int
set_uint64(struct lwes_event* event, char* attribute_name,
char* a_uint64)
{
return lwes_event_set_U_INT_64_w_string(event, attribute_name, a_uint64);
}
int
get_uint64(struct lwes_event* event, char* attribute_name,
unsigned long long * a_uint64)
{
return lwes_event_get_U_INT_64(event, attribute_name, a_uint64);
}
int
set_int64(struct lwes_event* event, char* attribute_name,
char* an_int64)
{
return lwes_event_set_INT_64_w_string(event, attribute_name, an_int64);
}
int
get_int64(struct lwes_event* event, char* attribute_name,
long long* an_int64)
{
return lwes_event_get_INT_64(event, attribute_name, an_int64);
}
int
set_string(struct lwes_event* event, char* attribute_name,
char* a_string)
{
return lwes_event_set_STRING(event, attribute_name, a_string);
}
int
get_string(struct lwes_event* event, char* attribute_name,
char** a_string)
{
return lwes_event_get_STRING(event, attribute_name, a_string);
}
int
set_ip_addr(struct lwes_event* event, char* attribute_name,
char* an_ip_addr)
{
return lwes_event_set_IP_ADDR_w_string(event, attribute_name, an_ip_addr);
}
int
get_ip_addr(struct lwes_event *event, char *attribute_name,
struct in_addr *an_ip_addr)
{
return lwes_event_get_IP_ADDR(event, attribute_name, an_ip_addr);
}
int
set_boolean(struct lwes_event* event, char* attribute_name,
int a_boolean)
{
return lwes_event_set_BOOLEAN(event, attribute_name, a_boolean);
}
int
get_boolean(struct lwes_event *event, char *attribute_name,
int *a_boolean)
{
return lwes_event_get_BOOLEAN(event, attribute_name, a_boolean);
}
int
destroy_event(struct lwes_event *event)
{
return lwes_event_destroy(event);
}
void
current_time_millis(char *buffer)
{
LWES_INT_64 current_time = 0LL;
current_time = currentTimeMillisLongLong();
snprintf(buffer,17,"%016llX",current_time);
}
%}
%perlcode %{
=head1 NAME
LWES - Perl extension for the Light Weight Event System
=head1 SYNOPSIS
use LWES;
use LWES::EventParser;
use IO::Socket::Multicast;
my $LWES_ADDRESS = "224.1.1.1";
my $LWES_PORT = 9000;
# load an event schema from a file to validate events
my $event_db = LWES::create_db("eventTypes.esf");
# create an emitter for sending events
my $emitter = LWES::create_emitter($LWES_ADDRESS, 0, $LWES_PORT, 0, 60);
# create an event and validate it against the DB
my $event = LWES::create_event($event_db, "MyEvent");
# or create an unvalidated event
my $event2 = LWES::create_event(undef, "MyOtherEvent");
# set some fields
LWES::set_string($event, "MyField", "MyValue");
LWES::set_int32($event2, "MyNumber", 123);
# emit the events
LWES::emit($emitter, $event);
LWES::emit($emitter, $event2);
# listen to some events on the network
my $socket = IO::Socket::Multicast->new(LocalPort => $LWES_PORT,
Reuse => 1);
$socket->mcast_add($LWES_ADDRESS);
my ($message, $peer);
$peer = recv($socket, $message, 65535, 0);
my ($port, $peeraddr) = sockaddr_in($peer);
# deserialize the event into a perl hash
my $event = bytesToEvent($message);
# access the various event fields
my $data = $event->{'MyField'};
# cleanup
LWES::destroy_event($event);
LWES::destroy_emitter($emitter);
LWES::destroy_db($event_db);
=head1 DESCRIPTION
This is the Perl interface to the Light Weight Event System. The
Light Weight Event System is a UDP-based communication toolkit with
built-in serialization, formatting, and type-checking.
=head1 EXPORT
None by default.
=head1 AUTHOR
Anthony Molinaro, E<lt>[email protected]<gt>
Michael P. Lum, E<lt>[email protected]<gt>
=head1 COPYRIGHT AND LICENSE
Portions Copyright (c) 2008, Yahoo! Inc. All rights reserved.
Portions Copyright (c) 2010, OpenX Inc. All rights reserved.
Licensed under the New BSD License (the "License"); you may not use
this file except in compliance with the License. Unless required
by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
=cut
%}