Module: Miu::Subscriber

Defined in:
lib/miu/subscriber.rb

Defined Under Namespace

Modules: ClassMethods

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#subscriberObject (readonly)

Returns the value of attribute subscriber.



38
39
40
# File 'lib/miu/subscriber.rb', line 38

def subscriber
  @subscriber
end

Class Method Details

.included(base) ⇒ Object



23
24
25
# File 'lib/miu/subscriber.rb', line 23

def included(base)
  base.extend ClassMethods
end

.new(*args, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/miu/subscriber.rb', line 8

def new(*args, &block)
  options = Miu::Utility.extract_options! args
  host = args.shift || '127.0.0.1'
  port = args.shift || Miu.default_pub_port
  socket = options[:socket] || SubSocket

  klass = Class.new(socket, &block)
  klass.send :include, Readable

  klass.new.tap do |sub|
    address = Miu::Socket.build_address host, port
    sub.connect address
  end
end

Instance Method Details

#closeObject



45
46
47
# File 'lib/miu/subscriber.rb', line 45

def close
  @subscriber.close
end

#initialize(host, port, tag) ⇒ Object



40
41
42
43
# File 'lib/miu/subscriber.rb', line 40

def initialize(host, port, tag)
  @subscriber = Miu::Subscriber.new host, port, :socket => self.class.socket_type
  self.tag = tag
end

#runObject



59
60
61
62
63
64
65
66
67
# File 'lib/miu/subscriber.rb', line 59

def run
  @subscriber.each do |packet|
    begin
      on_packet packet
    rescue Exception => e
      Miu::Logger.exception e
    end
  end
end

#tagObject



55
56
57
# File 'lib/miu/subscriber.rb', line 55

def tag
  @tag
end

#tag=(value) ⇒ Object



49
50
51
52
53
# File 'lib/miu/subscriber.rb', line 49

def tag=(value)
  @subscriber.unsubscribe @tag if @tag
  @subscriber.subscribe value
  @tag = value
end