Used for mapping LWES events in in an ESF file to a Ruby object. LWES::Event-derived classes are more memory efficient if your event definitions have many unused fields.
LWES::TypeDB.create_classes! with :sparse set to true will create classes subclassed from LWES::Event instead of LWES::Struct.
For users unable to use LWES::Listener, LWES::Event.parse may also be used with UDPSocket (distributed with Ruby) to parse events:
receiver = UDPSocket.new receiver.bind(nil, port) buffer, addr = receiver.recvfrom(65536) event = LWES::Event.parse(buffer)
Initializes a new LWES::Event. If src is given, it must be an LWES::Event or hash which the new event takes initial values from
# File lib/lwes/event.rb, line 74 def initialize(src = nil) src and merge! src end
Parses a string buffer and returns a new LWES::Event object
static VALUE parse(VALUE self, VALUE buf)
{
struct lwes_event *e;
struct lwes_event_deserialize_tmp dtmp;
LWES_BYTE_P bytes;
size_t num_bytes;
int rc;
StringValue(buf);
bytes = (LWES_BYTE_P)RSTRING_PTR(buf);
num_bytes = (size_t)RSTRING_LEN(buf);
e = lwes_event_create_no_name(NULL);
rc = lwes_event_from_bytes(e, bytes, num_bytes, 0, &dtmp);
if (rc < 0) {
lwes_event_destroy(e);
rb_raise(rb_eRuntimeError,
"failed to parse LWES event (code: %d)", rc);
}
return lwesrb_wrap_event(self, e);
}
There is no need to call this method directly. use LWES::TypeDB.create_classes! with the :sparse flag set to true.
This takes the same options as LWES::Struct.new.
# File lib/lwes/event.rb, line 27 def self.subclass(options, &block) db = type_db(options) dump = db.to_hash klass, name, event_def = class_for(options, dump) tmp = Class.new(self) set_constants(tmp, db, klass, name, options) tmp.class_eval(&block) if block_given? meta = dump[:MetaEventInfo] || [] methods = meta + event_def methods = methods.inject("") do |str, (k,_)| str << "def #{k}; self[:#{k}]; end\n" str << "def #{k}= val; self[:#{k}] = val; end\n" end methods << "def initialize(src = nil); merge!(DEFAULTS); super; end\n" tmp.class_eval methods CLASSES[name] = tmp end
Returns the value stored with the key. This will return nil if key does not exist. key must be a Symbol or String object
static VALUE event_aref(VALUE self, VALUE key)
{
char *attr = key2attr(key);
struct lwes_event *e = lwesrb_get_event(self);
struct lwes_event_attribute *eattr;
eattr = lwes_hash_get(e->attributes, attr);
return eattr ? lwesrb_attr_to_value(eattr) : Qnil;
}
Assigns value to be stored in the event given by key. key must be a String or Symbol object.
static VALUE event_aset(VALUE self, VALUE key, VALUE val)
{
char *attr = key2attr(key);
LWES_BYTE attr_type = get_attr_type(self, attr);
struct lwes_event *e = lwesrb_get_event(self);
if (attr_type == LWES_STRING_TOKEN) {
lwes_event_set_STRING(e, attr, StringValueCStr(val));
} else if (attr_type == LWES_U_INT_16_TOKEN) {
lwes_event_set_U_INT_16(e, attr, lwesrb_uint16(val));
} else if (attr_type == LWES_INT_16_TOKEN) {
lwes_event_set_INT_16(e, attr, lwesrb_int16(val));
} else if (attr_type == LWES_U_INT_32_TOKEN) {
lwes_event_set_U_INT_32(e, attr, lwesrb_uint32(val));
} else if (attr_type == LWES_INT_32_TOKEN) {
lwes_event_set_INT_32(e, attr, lwesrb_int32(val));
} else if (attr_type == LWES_U_INT_64_TOKEN) {
lwes_event_set_U_INT_64(e, attr, lwesrb_uint64(val));
} else if (attr_type == LWES_INT_64_TOKEN) {
lwes_event_set_INT_64(e, attr, lwesrb_int64(val));
} else if (attr_type == LWES_IP_ADDR_TOKEN) {
lwes_event_set_IP_ADDR(e, attr, lwesrb_ip_addr(val));
} else if (attr_type == LWES_BOOLEAN_TOKEN) {
lwes_event_set_BOOLEAN(e, attr, lwesrb_boolean(val));
} else {
rb_raise(rb_eRuntimeError,
"unknown LWES attribute type: 0x%02x",
(unsigned)attr_type);
}
return val;
}
returns a human-readable representation of the event
# File lib/lwes/event.rb, line 47 def inspect klass = self.class if LWES::Event == klass "#<#{klass}:#{to_hash.inspect}>" else "#<#{klass}(event:#{klass.const_get(:NAME)}):#{to_hash.inspect}>" end end
duplicates the given event and overwrites the copy with values given in src
# File lib/lwes/event.rb, line 66 def merge src dup.merge! src end
overwrites the values of the existing event with those given in src
# File lib/lwes/event.rb, line 57 def merge! src src.to_hash.each { |k,v| self[k] = v } self end
Returns an LWES::Event object as a plain Ruby hash. Useful for interoperating with existing Ruby code and also when you don’t have Event Specification Files finalized (or available).
static VALUE to_hash(VALUE self)
{
struct lwes_event *e = lwesrb_get_event(self);
VALUE rv = rb_hash_new();
VALUE val;
struct lwes_hash_enumeration hen;
LWES_SHORT_STRING name;
VALUE sym_attr_name;
struct lwes_event_attribute *attr;
if (e->eventName != NULL && CLASS_OF(self) == cLWES_Event) {
val = rb_str_new2(e->eventName);
rb_hash_aset(rv, sym_name, val);
}
if (! lwes_hash_keys(e->attributes, &hen))
return rv;
while (lwes_hash_enumeration_has_more_elements(&hen)) {
name = lwes_hash_enumeration_next_element(&hen);
sym_attr_name = ID2SYM(rb_intern(name));
attr = lwes_hash_get(e->attributes, name);
if (attr == NULL)
rb_raise(rb_eRuntimeError,
"missing attr during enumeration: %s", name);
val = lwesrb_attr_to_value(attr);
if (! NIL_P(val))
rb_hash_aset(rv, sym_attr_name, val);
}
return rv;
}
Generated with the Darkfish Rdoc Generator 2.