Class: RedPack::SimpleSyncClient

Inherits:
Object
  • Object
show all
Defined in:
lib/redpack-ruby/clients.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(redis, name, transcoder, timeout = 0) ⇒ SimpleSyncClient

Returns a new instance of SimpleSyncClient.



50
51
52
53
54
55
56
57
58
# File 'lib/redpack-ruby/clients.rb', line 50

def initialize(redis, name, transcoder, timeout=0)
  @redis = redis
  @queue_name = RedPack::Consts::queue_name(name)
  @transcoder = transcoder
  @msg_id_seq = 1
  @timeout = timeout
  @response_queue_name_generator = ResponseQueueNameGenerator.new(@redis)
  @msg_id_generator = MsgIdGenerator.new
end

Instance Attribute Details

#timeoutObject

Returns the value of attribute timeout.



48
49
50
# File 'lib/redpack-ruby/clients.rb', line 48

def timeout
  @timeout
end

Instance Method Details

#call(method, *params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/redpack-ruby/clients.rb', line 69

def call(method, *params)
  msg_id = @msg_id_generator.next
  v = [RedPack::Consts::REQUEST, msg_id, method, params]
  return_queue_name = @response_queue_name_generator.new_name
  packed = @transcoder.pack({'data' => v,
                              'return' => return_queue_name,})
  @redis.multi
  @redis.rpush(@queue_name, packed)
  @redis.exec
  # TODO: refactor...
  while true do
    popped = @redis.blpop(return_queue_name, @timeout)
    if not popped.nil? then
      unpacked = @transcoder.unpack(popped[1])
      datum = unpacked['data']
      # TODO: assert(unpacked[0] == RedPack::Consts::RESPONSE)
      msg_id = datum[1]
      err = datum[2]
      retval = datum[3]
      if err.nil? then
        return retval
      else
        # FIXME: more-than-this?
        raise Exception.new(err)
      end
    else
      raise Exception.new('pop-timed-out')
    end
  end
end

#call_async(method, *params, &cb) ⇒ Object

TODO: forwarding?



62
63
64
65
66
67
# File 'lib/redpack-ruby/clients.rb', line 62

def call_async(method, *params, &cb)
  Thread.new do
    result = self.call(method, *params)
    cb.call(result)
  end
end

#notify(method, *params) ⇒ Object



101
102
103
104
# File 'lib/redpack-ruby/clients.rb', line 101

def notify(method, *params)
  v = [RedPack::Consts::NOTIFY, method, params]
  return @redis.rpush(@queue_name, @transcoder.pack({'data' => v})) > 0
end