4 static ID id_TYPE_DB
, id_NAME
;
7 struct lwes_event
* lwesrb_get_event(VALUE self
)
9 struct lwes_event
*event
;
11 Data_Get_Struct(self
, struct lwes_event
, event
);
16 static void event_free(void *ptr
)
18 struct lwes_event
*event
= ptr
;
20 lwes_event_destroy(event
);
23 static VALUE
event_alloc(VALUE klass
)
27 if (klass
== cLWES_Event
) {
28 e
= lwes_event_create_no_name(NULL
);
31 e
= lwes_event_create_no_name(NULL
);
34 VALUE type_db
= rb_const_get(klass
, id_TYPE_DB
);
35 struct lwes_event_type_db
*tdb
= lwesrb_get_type_db(type_db
);
36 VALUE name
= rb_const_get(klass
, id_NAME
);
37 const char *ename
= StringValueCStr(name
);
39 e
= lwes_event_create(tdb
, ename
);
42 e
= lwes_event_create(tdb
, ename
);
48 return Data_Wrap_Struct(klass
, NULL
, event_free
, e
);
54 * key => [ numeric_type, Numeric ],
57 * memo - lwes_event pointer
59 static VALUE
event_hash_iter_i(VALUE kv
, VALUE memo
)
64 static struct lwes_event_type_db
* get_type_db(VALUE self
)
66 VALUE type_db
= rb_const_get(CLASS_OF(self
), id_TYPE_DB
);
67 struct lwes_event_type_db
*tdb
= lwesrb_get_type_db(type_db
);
72 static VALUE
event_init(int argc
, VALUE
*argv
, VALUE self
)
75 struct lwes_event
*event
;
77 rb_scan_args(argc
, argv
, "01", &hash
);
79 Data_Get_Struct(self
, struct lwes_event
, event
);
84 static VALUE
event_aset(VALUE self
, VALUE key
, VALUE val
)
86 struct lwes_event_type_db
*tdb
= get_type_db(self
);
87 struct lwes_event
*e
= lwesrb_get_event(self
);
88 const char *attr
= StringValueCStr(key
);
89 struct lwes_hash
*ehash
= lwes_hash_get(tdb
->events
, e
->eventName
);
93 rb_raise(rb_eArgError
, "invalid event: %s", e
->eventName
);
95 attr_type
= lwes_hash_get(ehash
, attr
);
96 if (attr_type
== NULL
)
97 rb_raise(rb_eArgError
, "invalid attribute: %s", attr
);
103 static VALUE
lwesrb_attr_to_value(struct lwes_event_attribute
*attr
)
105 if (attr
->type
== LWES_STRING_TOKEN
) {
106 return rb_str_new2((const char *)attr
->value
);
107 } else if (attr
->type
== LWES_U_INT_16_TOKEN
) {
108 return UINT2NUM(*((uint16_t *)attr
->value
));
109 } else if (attr
->type
== LWES_INT_16_TOKEN
) {
110 return INT2FIX(*((int16_t *)attr
->value
));
111 } else if (attr
->type
== LWES_U_INT_32_TOKEN
) {
112 return UINT2NUM(*((uint32_t *)attr
->value
));
113 } else if (attr
->type
== LWES_INT_32_TOKEN
) {
114 return INT2NUM(*((int32_t *)attr
->value
));
115 } else if (attr
->type
== LWES_U_INT_64_TOKEN
) {
116 return ULL2NUM(*((uint64_t *)attr
->value
));
117 } else if (attr
->type
== LWES_INT_64_TOKEN
) {
118 return LL2NUM(*((int64_t *)attr
->value
));
119 } else if (attr
->type
== LWES_BOOLEAN_TOKEN
) {
120 LWES_BOOLEAN b
= *(LWES_BOOLEAN
*)attr
->value
;
121 return b
? Qtrue
: Qfalse
;
122 } else if (attr
->type
== LWES_IP_ADDR_TOKEN
) {
123 LWES_IP_ADDR
*addr
= attr
->value
;
124 VALUE str
= rb_str_new(0, INET_ADDRSTRLEN
);
125 socklen_t len
= (socklen_t
)INET_ADDRSTRLEN
;
128 name
= inet_ntop(AF_INET
, addr
, RSTRING_PTR(str
), len
);
130 rb_raise(rb_eTypeError
, "invalid IP address");
131 rb_str_set_len(str
, strlen(name
));
135 * possible event corruption
136 * skip it like the C library does ...
143 * Returns an LWES::Event object as a plain Ruby hash
145 static VALUE
to_hash(VALUE self
)
147 struct lwes_event
*e
= lwesrb_get_event(self
);
149 return lwesrb_event_to_hash(e
);
155 * receiver = UDPSocket.new
156 * receiver.bind(nil, 12345)
157 * buf, addr = receiver.recvfrom(65536)
158 * parsed = LWES::Event.parse(buf)
159 * parsed.to_hash -> hash
161 * Parses a string +buf+ and returns a new LWES::Event object
163 static VALUE
parse(VALUE self
, VALUE buf
)
165 VALUE event
= event_alloc(cLWES_Event
);
166 struct lwes_event
*e
= lwesrb_get_event(event
);
167 struct lwes_event_deserialize_tmp dtmp
;
173 bytes
= (LWES_BYTE_P
)RSTRING_PTR(buf
);
174 num_bytes
= (size_t)RSTRING_LEN(buf
);
175 rc
= lwes_event_from_bytes(e
, bytes
, num_bytes
, 0, &dtmp
);
177 rb_raise(rb_eRuntimeError
,
178 "failed to parse LWES event (code: %d)", rc
);
182 VALUE
lwesrb_event_to_hash(struct lwes_event
*e
)
184 VALUE rv
= rb_hash_new();
186 struct lwes_hash_enumeration hen
;
187 LWES_SHORT_STRING name
;
189 struct lwes_event_attribute
*attr
;
191 if (e
->eventName
!= NULL
) {
192 val
= rb_str_new2(e
->eventName
);
193 rb_hash_aset(rv
, sym_name
, val
);
196 if (! lwes_hash_keys(e
->attributes
, &hen
))
198 while (lwes_hash_enumeration_has_more_elements(&hen
)) {
199 name
= lwes_hash_enumeration_next_element(&hen
);
200 sym_attr_name
= ID2SYM(rb_intern(name
));
201 attr
= lwes_hash_get(e
->attributes
, name
);
202 val
= lwesrb_attr_to_value(attr
);
204 rb_hash_aset(rv
, sym_attr_name
, val
);
210 void lwesrb_init_event(void)
212 VALUE mLWES
= rb_define_module("LWES");
213 cLWES_Event
= rb_define_class_under(mLWES
, "Event", rb_cObject
);
215 rb_define_method(cLWES_Event
, "initialize", event_init
, -1);
216 rb_define_alloc_func(cLWES_Event
, event_alloc
);
217 rb_define_singleton_method(cLWES_Event
, "parse", parse
, 1);
218 rb_define_method(cLWES_Event
, "to_hash", to_hash
, 0);
220 LWESRB_MKID(TYPE_DB
);