Class: Xhummingbird::Client

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/xhummingbird/client.rb

Constant Summary collapse

XH_SERVER =
'XH_SERVER'

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



7
8
9
10
11
# File 'lib/xhummingbird/client.rb', line 7

def initialize
  @pid = nil
  @socket = nil
  @active = false
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/xhummingbird/client.rb', line 21

def enabled?
  @enabled ||= !!address
end

#send(message) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/xhummingbird/client.rb', line 13

def send(message)
  if active?
    @socket.send_string(message)
  else
    Xhummingbird.debug("Xhummingbird not started.")
  end
end

#startObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/xhummingbird/client.rb', line 25

def start
  return unless enabled?

  ctx = ZMQ::Context.new
  socket = ctx.socket(ZMQ::PUSH)
  socket.connect(address)
  @socket = socket
  Xhummingbird.debug("Socket created (pid: #{$$})")

  at_exit do
    Xhummingbird.debug("at_exit started.")
    @socket.close
    Xhummingbird.debug("at_exit stopped.")
  end

  @pid = $$
  @active = true
end