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

Raises:



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 ConnectionError.new("#{self.class}#on_attach, failed to connect to transport endpoint [#{@transport}]") unless ZMQ::Util.resultcode_ok?(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.



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

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

#on_writable(socket) ⇒ Object

Just deregisters from receiving any further write events



96
97
98
# File 'lib/zm/log_client.rb', line 96

def on_writable socket
  @reactor.deregister_writable socket
end

#puts(string) ⇒ Object



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

def puts string
  write 'untagged', string
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(level, message) ⇒ 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
78
79
80
# File 'lib/zm/log_client.rb', line 74

def write level, message
  now = Time.now
  usec = sprintf "%06d", now.usec
  timestamp = now.strftime "%Y%m%d-%H:%M:%S.#{usec} %Z"
  @message_queue << [ZMQ::Message.new(level.to_s), ZMQ::Message.new(timestamp), ZMQ::Message.new(message.to_s)]
  write_queue_to_socket
end