Class: Majordomo::Broker::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/majordomo/broker/worker.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(broker, identity, address) ⇒ Worker

Returns a new instance of Worker.



6
7
8
9
10
# File 'lib/majordomo/broker/worker.rb', line 6

def initialize(broker, identity, address)
  @broker   = broker
  @identity = identity
  @address  = address
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



4
5
6
# File 'lib/majordomo/broker/worker.rb', line 4

def address
  @address
end

#brokerObject

Returns the value of attribute broker.



4
5
6
# File 'lib/majordomo/broker/worker.rb', line 4

def broker
  @broker
end

#expires_atObject

Returns the value of attribute expires_at.



4
5
6
# File 'lib/majordomo/broker/worker.rb', line 4

def expires_at
  @expires_at
end

#identityObject

Returns the value of attribute identity.



4
5
6
# File 'lib/majordomo/broker/worker.rb', line 4

def identity
  @identity
end

#serviceObject

Returns the value of attribute service.



4
5
6
# File 'lib/majordomo/broker/worker.rb', line 4

def service
  @service
end

Class Method Details

.require(broker, address) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/majordomo/broker/worker.rb', line 12

def self.require(broker, address)
  identity = address.unpack('H*').first
  worker   = broker.workers[identity]

  if worker == nil
    worker                   = self.new(broker, identity, address)
    broker.workers[identity] = worker
  end

  worker
end

Instance Method Details

#delete(disconnect) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/majordomo/broker/worker.rb', line 24

def delete(disconnect)
  if disconnect
    send_message(DISCONNECT)
  end

  if service
    service.waiting.delete(self)
    service.workers -= 1
  end
  broker.waiting.delete(self)
  broker.workers.delete(identity)
end

#send_message(command, message = []) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/majordomo/broker/worker.rb', line 37

def send_message(command, message = [])
  message.unshift(command)
  message.unshift(WORKER)
  message.unshift(nil)
  message.unshift(address)

  broker.socket.send_strings(message)
end