use rake-compiler
[lwes-ruby.git] / ext / lwes_ext / event.c
blob299d303a02c24e07d401fedfd89fda736fe0ed2d
1 #include "lwes_ruby.h"
2 VALUE cLWES_Event;
4 static ID id_TYPE_DB, id_NAME;
5 static VALUE sym_name;
7 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;
144 * Returns an LWES::Event object as a plain Ruby hash
146 static VALUE to_hash(VALUE self)
148 struct lwes_event *e = lwesrb_get_event(self);
150 return lwesrb_event_to_hash(e);
154 * call-seq:
156 * receiver = UDPSocket.new
157 * receiver.bind(nil, 12345)
158 * buf, addr = receiver.recvfrom(65536)
159 * parsed = LWES::Event.parse(buf)
160 * parsed.to_hash -> hash
162 * Parses a string +buf+ and returns a new LWES::Event object
164 static VALUE parse(VALUE self, VALUE buf)
166 VALUE event = event_alloc(cLWES_Event);
167 struct lwes_event *e = lwesrb_get_event(event);
168 struct lwes_event_deserialize_tmp dtmp;
169 LWES_BYTE_P bytes;
170 size_t num_bytes;
171 int rc;
173 StringValue(buf);
174 bytes = (LWES_BYTE_P)RSTRING_PTR(buf);
175 num_bytes = (size_t)RSTRING_LEN(buf);
176 rc = lwes_event_from_bytes(e, bytes, num_bytes, 0, &dtmp);
177 if (rc < 0)
178 rb_raise(rb_eRuntimeError,
179 "failed to parse LWES event (code: %d)\n", rc);
180 return event;
183 VALUE lwesrb_event_to_hash(struct lwes_event *e)
185 VALUE rv = rb_hash_new();
186 VALUE val;
187 struct lwes_hash_enumeration hen;
188 LWES_SHORT_STRING name;
189 VALUE sym_attr_name;
190 struct lwes_event_attribute *attr;
192 if (e->eventName != NULL) {
193 val = rb_str_new2(e->eventName);
194 rb_hash_aset(rv, sym_name, val);
197 if (! lwes_hash_keys(e->attributes, &hen))
198 return rv;
199 while (lwes_hash_enumeration_has_more_elements(&hen)) {
200 name = lwes_hash_enumeration_next_element(&hen);
201 sym_attr_name = ID2SYM(rb_intern(name));
202 attr = lwes_hash_get(e->attributes, name);
203 val = lwesrb_attr_to_value(attr);
204 if (! NIL_P(val))
205 rb_hash_aset(rv, sym_attr_name, val);
208 return rv;
211 void lwesrb_init_event(void)
213 VALUE mLWES = rb_define_module("LWES");
214 cLWES_Event = rb_define_class_under(mLWES, "Event", rb_cObject);
216 rb_define_method(cLWES_Event, "initialize", event_init, -1);
217 rb_define_alloc_func(cLWES_Event, event_alloc);
218 rb_define_singleton_method(cLWES_Event, "parse", parse, 1);
219 rb_define_method(cLWES_Event, "to_hash", to_hash, 0);
221 LWESRB_MKID(TYPE_DB);
222 LWESRB_MKID(NAME);
223 LWESRB_MKSYM(name);