Class: Cotcube::Helpers::JoSchClient
- Inherits:
-
Object
- Object
- Cotcube::Helpers::JoSchClient
- Defined in:
- lib/cotcube-helpers/josch_client.rb
Constant Summary collapse
- SECRETS_DEFAULT =
{ 'josch_mq_proto' => 'http', 'josch_mq_user' => 'guest', 'josch_mq_password' => 'guest', 'josch_mq_host' => 'localhost', 'josch_mq_port' => '15672', 'josch_mq_vhost' => '%2F' }.freeze
- SECRETS =
SECRETS_DEFAULT.merge( lambda { begin YAML.safe_load(File.read(Cotcube::Helpers.init[:secrets_file])) rescue StandardError {} end }.call )
Instance Attribute Summary collapse
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
-
#lock ⇒ Object
readonly
Returns the value of attribute lock.
-
#response ⇒ Object
Returns the value of attribute response.
Instance Method Summary collapse
-
#command(command, timeout: 10) ⇒ Object
(also: #send_command)
command acts a synchronizer: it sends the command and waits for the response otherwise times out — the counterpart here is the subscription within setup_reply_queue.
-
#initialize ⇒ JoSchClient
constructor
A new instance of JoSchClient.
Constructor Details
#initialize ⇒ JoSchClient
Returns a new instance of JoSchClient.
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cotcube-helpers/josch_client.rb', line 29 def initialize @connection = Bunny.new(user: SECRETS['josch_mq_user'], password: SECRETS['josch_mq_password'], vhost: SECRETS['josch_mq_vhost']) @connection.start @commands = connection.create_channel @exchange = commands.direct('josch_commands') @requests = {} @debug = false setup_reply_queue end |
Instance Attribute Details
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
77 78 79 |
# File 'lib/cotcube-helpers/josch_client.rb', line 77 def condition @condition end |
#lock ⇒ Object (readonly)
Returns the value of attribute lock.
77 78 79 |
# File 'lib/cotcube-helpers/josch_client.rb', line 77 def lock @lock end |
#response ⇒ Object
Returns the value of attribute response.
76 77 78 |
# File 'lib/cotcube-helpers/josch_client.rb', line 76 def response @response end |
Instance Method Details
#command(command, timeout: 10) ⇒ Object Also known as: send_command
command acts a synchronizer: it sends the command and waits for the response
otherwise times out --- the counterpart here is the subscription within
setup_reply_queue
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cotcube-helpers/josch_client.rb', line 46 def command(command, timeout: 10) command = { command: command.to_s } unless command.is_a? Hash command[:timestamp] ||= (Time.now.to_f * 1000).to_i request_id = Digest::SHA256.hexdigest(command.to_json)[..6] requests[request_id] = { request: command, id: request_id, lock: Mutex.new, condition: ConditionVariable.new } exchange.publish(command.to_json, routing_key: 'josch_commands', correlation_id: request_id, reply_to: reply_queue.name) # wait for the signal to continue the execution # requests[request_id][:lock].synchronize do requests[request_id][:condition].wait(requests[request_id][:lock], timeout) end # if we reached timeout, we will return nil, just for explicity response = requests[request_id][:response].dup requests.delete(request_id) response end |