Class: RedPack::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(redis, name, transcoder, dispatcher) ⇒ Server

Returns a new instance of Server.



4
5
6
7
8
9
# File 'lib/redpack-ruby/servers.rb', line 4

def initialize(redis, name, transcoder, dispatcher)
  @queue_name = RedPack::Consts::queue_name(name)
  @redis = redis
  @transcoder = transcoder
  @dispatcher = dispatcher
end

Instance Method Details

#listen(timeout = 0) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/redpack-ruby/servers.rb', line 11

def listen(timeout=0)
  a = @redis.blpop(@queue_name, timeout)
  unless a.nil? then
    unpacked = @transcoder.unpack(a[1])
    response_queue_name = unpacked['return']
    data = unpacked['data']
    case data[0] 
    when RedPack::Consts::REQUEST then
      process_REQUEST(response_queue_name, data[1], data[2], data[3])
    when RedPack::Consts::NOTIFY then
      process_NOTIFY(data[1], data[2])
    when RedPack::Consts::RESPONSE then
      raise Exception.new('plz dont do that...')          
    else
      raise Exception.new("unknown code -- #{data[0]}")
    end
  end
end

#process_NOTIFY(method, params) ⇒ Object



35
36
37
38
# File 'lib/redpack-ruby/servers.rb', line 35

def process_NOTIFY(method, params)
  ctx = RedPack::RequestContext.new(nil, nil, RedPack::Consts::NOTIFY)
  @dispatcher.dispatch(ctx, method, params)
end

#process_REQUEST(response_queue_name, msg_id, method, params) ⇒ Object



30
31
32
33
# File 'lib/redpack-ruby/servers.rb', line 30

def process_REQUEST(response_queue_name, msg_id, method, params)
  ctx = RedPack::RequestContext.new(response_queue_name, msg_id, RedPack::Consts::REQUEST)
  @dispatcher.dispatch(ctx, method, params)
end