Class: Lwes::Emitter

Inherits:
Object
  • Object
show all
Defined in:
lib/lwes/emitter.rb

Overview

Emit LWES events to a UDP endpoint

Constant Summary collapse

DEFAULT_SOCIKET_OPTIONS =
{
  :address_family => Socket::AF_INET,
  :host           => "127.0.0.1",
  :port           => 12345
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Emitter

Creates a new Lwes::Emitter.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :host (String)

    UDP host address, defaults to 127.0.0.1

  • :port (String, Number)

    UDP port, defaults to 12345

  • :address_family, (Number)

    Address family of the socket, defaults to Socket::AF_INET



19
20
21
22
23
# File 'lib/lwes/emitter.rb', line 19

def initialize(options={})
  self.socket_options = DEFAULT_SOCIKET_OPTIONS.merge(options)
  self.socket = UDPSocket.new(@socket_options[:address_family])
  socket.connect(@socket_options[:host], @socket_options[:port])
end

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



12
13
14
# File 'lib/lwes/emitter.rb', line 12

def socket
  @socket
end

#socket_optionsObject

Returns the value of attribute socket_options.



12
13
14
# File 'lib/lwes/emitter.rb', line 12

def socket_options
  @socket_options
end

Instance Method Details

#emit(event) ⇒ Number

Emits specified event

Parameters:

Returns:

  • (Number)

    the number of bytes sent



28
29
30
31
32
33
# File 'lib/lwes/emitter.rb', line 28

def emit(event)
  buffer = StringIO.new
  event.write(buffer)
  buffer.rewind
  socket.write(buffer.read)
end