class Pigato::Client

Public Class Methods

new(broker, conf = {}) click to toggle source
# File lib/pigato/client.rb, line 8
def initialize broker, conf = {}
  @broker = broker

  @conf = {
    :autostart => false,
    :timeout => 2500
  }

  @conf.merge!(conf)

  init
  
  if @conf[:autostart]
    start
  end
end

Public Instance Methods

_recv(rid) click to toggle source
# File lib/pigato/client.rb, line 60
def _recv rid 
  iid = get_iid
  socket = @@sockets[iid]
  socket.rcvtimeo = 2500
  
  data = []
  
  msg = socket.recv_message()
  while 1 do
    break if msg.nil? || msg.size == 0
    data << msg.pop.data
  end

  if data[3] != rid  
    data = []
    if @conf[:logger]
      @conf[:logger].error("PigatoClient: RID mismatch #{data[3]}/#{rid}")
    end
  end

  return nil if data.length == 0

  data.shift
  return data 
end
request(service, request, opts = {}) click to toggle source
# File lib/pigato/client.rb, line 34
def request service, request, opts = {}
  request = [Oj.dump(request), Oj.dump(opts)]

  rid = SecureRandom.uuid
  request = [Pigato::C_CLIENT, Pigato::W_REQUEST, service, rid].concat(request)
  msg = ZMQ::Message.new
  request.reverse.each{|p| msg.push(ZMQ::Frame(p))}

  res = send msg
  return nil if res.nil?

  rtimer = Time.now + (@conf[:timeout] * 0.001)

  res = [] 
  while Time.now <= rtimer do
    chunk = _recv rid
    next if chunk == nil
    res << Oj.load(chunk[4])
    break if chunk[0] == Pigato::W_REPLY
  end

  return nil if res.length == 0
  return res[0] if res.length == 1
  res
end
send(msg) click to toggle source
# File lib/pigato/client.rb, line 25
def send msg
  iid = get_iid
  start if @@sockets[iid] == nil && @conf[:autostart]
  socket = get_socket
  return nil if socket.nil?
  socket.send_message msg
  true
end
start() click to toggle source
Calls superclass method Pigato::Base#start
# File lib/pigato/client.rb, line 86
def start
  stop
  sock_create
  super 
rescue ZMQ::Error => e
  puts e
end
stop() click to toggle source
Calls superclass method Pigato::Base#stop
# File lib/pigato/client.rb, line 94
def stop
  sock_close
  super 
end