initial cut of event parsing
[lwes-ruby.git] / ext / lwes / event.c
blob6360e62d7d31a89061c89f480f2c921efda0ab87
1 #include "lwes_ruby.h"
2 static VALUE cLWES_Event;
4 static ID id_TYPE_DB, id_NAME;
5 static VALUE sym_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);
13 return 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)
26 struct lwes_event *e;
28 if (klass == cLWES_Event) {
29 e = lwes_event_create_no_name(NULL);
30 if (e == NULL) {
31 rb_gc();
32 e = lwes_event_create_no_name(NULL);
34 } else {
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);
41 if (e == NULL) {
42 rb_gc();
43 e = lwes_event_create(tdb, ename);
46 if (e == NULL)
47 rb_memerror();
49 return Data_Wrap_Struct(klass, NULL, event_free, e);
53 * kv - Array:
54 * key => String,
55 * key => [ numeric_type, Numeric ],
56 * key => true,
57 * key => false,
58 * memo - lwes_event pointer
60 static VALUE event_hash_iter_i(VALUE kv, VALUE memo)
62 /* TODO */
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);
70 return tdb;
73 static VALUE event_init(int argc, VALUE *argv, VALUE self)
75 VALUE hash;
76 struct lwes_event *event;
78 rb_scan_args(argc, argv, "01", &hash);
80 Data_Get_Struct(self, struct lwes_event, event);
82 return self;
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);
91 LWES_BYTE *attr_type;
93 if (ehash == NULL)
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;
127 const char *name;
129 name = inet_ntop(AF_INET, addr, RSTRING_PTR(str), len);
130 if (name == NULL)
131 rb_raise(rb_eTypeError, "invalid IP address");
132 rb_str_set_len(str, strlen(name));
133 return str;
134 } else {
136 * possible event corruption
137 * skip it like the C library does ...
140 return Qnil;
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;
155 LWES_BYTE_P bytes;
156 size_t num_bytes;
157 int rc;
159 StringValue(buf);
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);
163 if (rc < 0)
164 rb_raise(rb_eRuntimeError,
165 "failed to parse LWES event (code: %d)\n", rc);
166 return event;
169 VALUE lwesrb_event_to_hash(struct lwes_event *e)
171 VALUE rv = rb_hash_new();
172 VALUE val;
173 struct lwes_hash_enumeration hen;
174 LWES_SHORT_STRING name;
175 VALUE sym_attr_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))
184 return rv;
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);
190 if (! NIL_P(val))
191 rb_hash_aset(rv, sym_attr_name, val);
194 return rv;
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);
208 LWESRB_MKID(NAME);
209 LWESRB_MKSYM(name);