2 static VALUE cLWES_Event
;
4 static ID id_TYPE_DB
, id_NAME
;
7 static 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
);
24 static VALUE
event_alloc(VALUE klass
)
28 if (klass
== cLWES_Event
) {
29 e
= lwes_event_create_no_name(NULL
);
32 e
= lwes_event_create_no_name(NULL
);
35 VALUE type_db
= rb_const_get(klass
, id_TYPE_DB
);
36 struct lwes_event_type_db
*tdb
= lwesrb_get_type_db(type_db
);
37 VALUE name
= rb_const_get(klass
, id_NAME
);
38 const char *ename
= StringValuePtr(name
);
40 e
= lwes_event_create(tdb
, ename
);
43 e
= lwes_event_create(tdb
, ename
);
49 return Data_Wrap_Struct(klass
, NULL
, event_free
, e
);
55 * key => [ numeric_type, Numeric ],
58 * memo - lwes_event pointer
60 static VALUE
event_hash_iter_i(VALUE kv
, VALUE memo
)
65 static struct lwes_event_type_db
* get_type_db(VALUE self
)
67 VALUE type_db
= rb_const_get(CLASS_OF(self
), id_TYPE_DB
);
68 struct lwes_event_type_db
*tdb
= lwesrb_get_type_db(type_db
);
73 static VALUE
event_init(int argc
, VALUE
*argv
, VALUE self
)
76 struct lwes_event
*event
;
78 rb_scan_args(argc
, argv
, "01", &hash
);
80 Data_Get_Struct(self
, struct lwes_event
, event
);
85 static VALUE
event_aset(VALUE self
, VALUE key
, VALUE val
)
87 struct lwes_event_type_db
*tdb
= get_type_db(self
);
88 struct lwes_event
*e
= lwesrb_get_event(self
);
89 const char *attr
= StringValuePtr(key
);
90 struct lwes_hash
*ehash
= lwes_hash_get(tdb
->events
, e
->eventName
);
94 rb_raise(rb_eArgError
, "invalid event: %s\n", e
->eventName
);
96 attr_type
= lwes_hash_get(ehash
, attr
);
97 if (attr_type
== NULL
)
98 rb_raise(rb_eArgError
, "invalid attribute: %s\n", attr
);
100 switch (*attr_type
) {
104 static VALUE
lwesrb_attr_to_value(struct lwes_event_attribute
*attr
)
106 if (attr
->type
== LWES_STRING_TOKEN
) {
107 return rb_str_new2((const char *)attr
->value
);
108 } else if (attr
->type
== LWES_U_INT_16_TOKEN
) {
109 return UINT2NUM(*((uint16_t *)attr
->value
));
110 } else if (attr
->type
== LWES_INT_16_TOKEN
) {
111 return INT2FIX(*((int16_t *)attr
->value
));
112 } else if (attr
->type
== LWES_U_INT_32_TOKEN
) {
113 return UINT2NUM(*((uint32_t *)attr
->value
));
114 } else if (attr
->type
== LWES_INT_32_TOKEN
) {
115 return INT2NUM(*((int32_t *)attr
->value
));
116 } else if (attr
->type
== LWES_U_INT_64_TOKEN
) {
117 return ULL2NUM(*((uint64_t *)attr
->value
));
118 } else if (attr
->type
== LWES_INT_64_TOKEN
) {
119 return LL2NUM(*((int64_t *)attr
->value
));
120 } else if (attr
->type
== LWES_BOOLEAN_TOKEN
) {
121 LWES_BOOLEAN b
= *(LWES_BOOLEAN
*)attr
->value
;
122 return b
? Qtrue
: Qfalse
;
123 } else if (attr
->type
== LWES_IP_ADDR_TOKEN
) {
124 LWES_IP_ADDR
*addr
= attr
->value
;
125 VALUE str
= rb_str_new(0, INET_ADDRSTRLEN
);
126 socklen_t len
= (socklen_t
)INET_ADDRSTRLEN
;
129 name
= inet_ntop(AF_INET
, addr
, RSTRING_PTR(str
), len
);
131 rb_raise(rb_eTypeError
, "invalid IP address");
132 rb_str_set_len(str
, strlen(name
));
136 * possible event corruption
137 * skip it like the C library does ...
143 static VALUE
to_hash(VALUE self
)
145 struct lwes_event
*e
= lwesrb_get_event(self
);
147 return lwesrb_event_to_hash(e
);
150 static VALUE
parse(VALUE self
, VALUE buf
)
152 VALUE event
= event_alloc(cLWES_Event
);
153 struct lwes_event
*e
= lwesrb_get_event(event
);
154 struct lwes_event_deserialize_tmp dtmp
;
160 bytes
= (LWES_BYTE_P
)RSTRING_PTR(buf
);
161 num_bytes
= (size_t)RSTRING_LEN(buf
);
162 rc
= lwes_event_from_bytes(e
, bytes
, num_bytes
, 0, &dtmp
);
164 rb_raise(rb_eRuntimeError
,
165 "failed to parse LWES event (code: %d)\n", rc
);
169 VALUE
lwesrb_event_to_hash(struct lwes_event
*e
)
171 VALUE rv
= rb_hash_new();
173 struct lwes_hash_enumeration hen
;
174 LWES_SHORT_STRING name
;
176 struct lwes_event_attribute
*attr
;
178 if (e
->eventName
!= NULL
) {
179 val
= rb_str_new2(e
->eventName
);
180 rb_hash_aset(rv
, sym_name
, val
);
183 if (! lwes_hash_keys(e
->attributes
, &hen
))
185 while (lwes_hash_enumeration_has_more_elements(&hen
)) {
186 name
= lwes_hash_enumeration_next_element(&hen
);
187 sym_attr_name
= ID2SYM(rb_intern(name
));
188 attr
= lwes_hash_get(e
->attributes
, name
);
189 val
= lwesrb_attr_to_value(attr
);
191 rb_hash_aset(rv
, sym_attr_name
, val
);
197 void lwesrb_init_event(void)
199 VALUE mLWES
= rb_define_module("LWES");
200 cLWES_Event
= rb_define_class_under(mLWES
, "Event", rb_cObject
);
202 rb_define_method(cLWES_Event
, "initialize", event_init
, -1);
203 rb_define_alloc_func(cLWES_Event
, event_alloc
);
204 rb_define_singleton_method(cLWES_Event
, "parse", parse
, 1);
205 rb_define_method(cLWES_Event
, "to_hash", to_hash
, 0);
207 LWESRB_MKID(TYPE_DB
);