Class: ZMQMachine::LogClient

Inherits:
Object
  • Object
show all
Defined in:
lib/zm/log_client.rb

Instance Method Summary collapse

Constructor Details

#initialize(reactor, transport) ⇒ LogClient

Returns a new instance of LogClient.



40
41
42
43
44
45
46
# File 'lib/zm/log_client.rb', line 40

def initialize reactor, transport
  @reactor = reactor
  @transport = transport
  allocate_socket
  @message_queue = []
  @timer = nil
end

Instance Method Details

#on_attach(socket) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/zm/log_client.rb', line 53

def on_attach socket
  socket.identity = "#{Kernel.rand(999_999_999)}.#{socket.kind}.log_client"

  # socket options *must* be set before we bind/connect otherwise they are ignored
  set_options socket

  #FIXME error check!
  rc = socket.connect @transport

  raise "#{self.class}#on_attach, failed to connect to transport endpoint [#{@transport}]" unless rc

  register_for_events socket
end

#on_readable(socket, messages) ⇒ Object

Prints each message when global debugging is enabled.

Forwards messages on to the :on_read callback given in the constructor.



83
84
85
# File 'lib/zm/log_client.rb', line 83

def on_readable socket, messages
  @on_read.call messages, socket
end

#on_writable(socket) ⇒ Object

Just deregisters from receiving any further write events



89
90
91
# File 'lib/zm/log_client.rb', line 89

def on_writable socket
  @reactor.deregister_writable socket
end

#shutdownObject



48
49
50
51
# File 'lib/zm/log_client.rb', line 48

def shutdown
  @timer.cancel if @timer
  @reactor.close_socket @socket
end

#write(messages) ⇒ Object

Takes an array of ZM::Message instances and writes them out to the socket. If any socket write fails, the message is saved. We will attempt to write it again in 10 milliseconds or when another message array is sent, whichever comes first.

All messages passed here are guaranteed to be written in the *order they were received*.



74
75
76
77
# File 'lib/zm/log_client.rb', line 74

def write messages
  @message_queue << messages
  write_queue_to_socket
end