Class: Peeper

Inherits:
Object
  • Object
show all
Includes:
Callback
Defined in:
lib/ass/peeper.rb

Overview

TODO should prolly have the option of using non auto-delete queues. This would be useful for logger. Maybe if a peeper name is given, then create queues with options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name, callback) ⇒ Peeper

Returns a new instance of Peeper.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ass/peeper.rb', line 8

def initialize(server_name,callback)
  @server_name = server_name
  @clients = {}
  @callback = build_callback(callback)
  
  uid = "#{@server_name}.peeper.#{rand 999_999_999_999}"
  q = MQ.queue uid, :auto_delete => true
  q.bind(@server_name) # messages to the server would be duplicated here.
  q.subscribe { |info,payload|
    payload = ::Marshal.load(payload)
    # sets context, but doesn't make the call
    obj = prepare_callback(@callback,info,payload)
    # there is a specific method we want to call.
    obj.server(payload[:method],payload[:data])

    # bind to peep client message queue if we've not seen it before.
    unless @clients.has_key? info.routing_key
      @clients[info.routing_key] = true
      client_q = MQ.queue "#{uid}--#{info.routing_key}",
      :auto_delete => true
      # messages to the client would be duplicated here.
      client_q.bind("#{server_name}--", :routing_key => info.routing_key) 
      client_q.subscribe { |info,payload|
        payload = ::Marshal.load(payload)
        obj = prepare_callback(@callback,info,payload)
        obj.client(payload[:method],payload[:data])
      }
    end
  }
end

Instance Attribute Details

#server_nameObject (readonly)

Returns the value of attribute server_name.



7
8
9
# File 'lib/ass/peeper.rb', line 7

def server_name
  @server_name
end