The LWES::Emitter is used for emitting LWES events to a multicast network or a single host. It can emit LWES::Event objects, LWES::Struct objects, and even plain Ruby hashes.
It is non-blocking and does not guarantee delivery.
emitter = LWES::Emitter.new(:address => '224.1.1.11', :port => 12345, :heartbeat => 30, # nil to disable :ttl => 1) # nil for default TTL(3) event = MyEvent.new event.foo = "bar" emitter << event
Since we can’t reliably map certain Ruby types to LWES types, you’ll have to specify them explicitly for IP addresses and all Integer types.
event = { :time_sec => [ :int32, Time.now.to_i ], :time_usec => [ :int32, Time.now.tv_usec ], :remote_addr => [ :ip_addr, "192.168.0.1" ], } # Strings and Boolean values are easily mapped, however: event[:field1] = "String value" event[:boolean1] = true event[:boolean2] = false # finally, we just emit the hash with any given name emitter.emit "Event3", event
creates a new Emitter object which may be used for the lifetime of the process:
LWES::Emitter.new(:address => '224.1.1.11', :iface => '0.0.0.0', :port => 12345, :heartbeat => false, # Integer for frequency :ttl => 60, # nil for no ttl)
# File lib/lwes/emitter.rb, line 47 def initialize(options = {}, &block) options[:iface] ||= '0.0.0.0' _create(options) block_given? end
Emits the given event which much be an LWES::Event or LWES::Struct-derived object
static VALUE emitter_ltlt(VALUE self, VALUE event) { if (rb_obj_is_kind_of(event, cLWES_Event)) { return emit_event(self, event); } else { Check_Type(event, T_STRUCT); return emit_struct(self, event); } }
Destroys the associated lwes_emitter and the associated socket. This method is rarely needed as Ruby garbage collection will take care of closing for you, but may be useful in odd cases when it is desirable to release file descriptors ASAP.
static VALUE emitter_close(VALUE self) { struct _rb_lwes_emitter *rle = _rle(self); if (rle->emitter) lwes_emitter_destroy(rle->emitter); rle->emitter = NULL; return Qnil; }
Emits a hash. If EventName is given as a string, it will expect a hash as its second argument and will do its best to serialize a Ruby Hash to an LWES Event. If a type is ambiguous, a two-element array may be specified as its value, including the LWES type information and the Ruby value.
If an EventClass is given, the second argument should be a hash with the values given to the class. This will emit the event named by EventClass.
If only one argument is given, it behaves just like LWES::Emitter#<<
static VALUE emitter_emit(int argc, VALUE *argv, VALUE self) { volatile VALUE raise_inspect; char *err; VALUE name = Qnil; VALUE event = Qnil; argc = rb_scan_args(argc, argv, "11", &name, &event); switch (TYPE(name)) { case T_STRING: if (TYPE(event) == T_HASH) return emit_hash(self, name, event); rb_raise(rb_eTypeError, "second argument must be a hash when first " "is a String"); case T_STRUCT: if (argc >= 2) rb_raise(rb_eArgError, "second argument not allowed when first" " is a Struct"); event = name; return emit_struct(self, event); case T_CLASS: if (TYPE(event) != T_HASH) rb_raise(rb_eTypeError, "second argument must be a Hash when first" " is a Class"); /* * we can optimize this so there's no intermediate * struct created */ event = rb_funcall(name, id_new, 1, event); if (TYPE(event) == T_STRUCT) return emit_struct(self, event); if (rb_obj_is_kind_of(event, cLWES_Event)) return emit_event(self, event); name = rb_class_name(name); err = StringValuePtr(name); rb_raise(rb_eArgError, "%s created a bad event: %s", err, RAISE_INSPECT(event)); default: if (rb_obj_is_kind_of(name, cLWES_Event)) return emit_event(self, name); rb_raise(rb_eArgError, "bad argument: %s, must be a String, Struct or Class", RAISE_INSPECT(name)); } assert(0 && "should never get here"); return event; }
Generated with the Darkfish Rdoc Generator 2.