class RubyNos::Agent
Attributes
cloud[RW]
processor[RW]
rest_api[RW]
udp_rx[RW]
udp_tx[RW]
uuid[RW]
Public Instance Methods
join_cloud()
click to toggle source
# File lib/ruby_nos/agent.rb, line 79 def join_cloud send_message({type: 'PRS'}) send_message({type: 'QNE'}) unless rest_api.endpoints.empty? end
listen()
click to toggle source
# File lib/ruby_nos/agent.rb, line 39 def listen udp_rx.listen(processor) end
maintain_cloud()
click to toggle source
# File lib/ruby_nos/agent.rb, line 49 def maintain_cloud begin thread = Thread.new do i = 0 loop do i = i+1 RubyNos.logger.send(:info, "Iteration number #{i}") RubyNos.logger.send(:info, "Agents on the cloud #{cloud.list.list_of_keys.count}") send_discovery_messages send_connection_messages sleep RubyNos.time_between_messages end end thread rescue StandardError => e RubyNos.logger.send(:info, "Error executing the thread #{e.message}") end end
send_connection_messages()
click to toggle source
# File lib/ruby_nos/agent.rb, line 68 def send_connection_messages cloud.list.list_of_keys.each do |agent_uuid| last_message_exists?(agent_uuid) ? send_message({to: "AGT:#{uuid_for_message(agent_uuid)}", type: "PIN"}) : cloud.list.eliminate(agent_uuid) end end
send_desconnection_message()
click to toggle source
# File lib/ruby_nos/agent.rb, line 84 def send_desconnection_message send_message({type: "PRS", data: {present: 0}}) end
send_discovery_messages()
click to toggle source
# File lib/ruby_nos/agent.rb, line 74 def send_discovery_messages send_message({type: 'DSC'}) send_message({type: 'ENQ'}) end
send_message(args={})
click to toggle source
# File lib/ruby_nos/agent.rb, line 43 def send_message args={} message = build_message(args) udp_tx.send({host: args[:host], port: args[:port], message: message}) message end
start!()
click to toggle source
# File lib/ruby_nos/agent.rb, line 30 def start! at_exit { send_desconnection_message } listen join_cloud maintain_cloud end
Private Instance Methods
build_message(args)
click to toggle source
# File lib/ruby_nos/agent.rb, line 95 def build_message args if args[:data] data = args[:data] elsif args[:type] == "PRS" data = receptor_info elsif args[:type] == "QNE" data = rest_api.to_hash if rest_api end message_hash = {from: "AGT:#{uuid_for_message(uuid)}", to: args[:to] || "CLD:#{uuid_for_message(cloud.uuid)}", type: args[:type]} message_hash.merge!({data: data}) if data Message.new(message_hash).serialize end
formatter()
click to toggle source
# File lib/ruby_nos/agent.rb, line 115 def formatter @formatter ||= Formatter.new end
last_message_exists?(agent_uuid)
click to toggle source
# File lib/ruby_nos/agent.rb, line 90 def last_message_exists?(agent_uuid) remote_agent = cloud.list.info_for(agent_uuid) (Formatter.timestamp - remote_agent.timestamp) < RubyNos.keep_alive_time end
receptor_info()
click to toggle source
# File lib/ruby_nos/agent.rb, line 111 def receptor_info {present: 1, endpoints: ["UDP,#{udp_rx.socket.connect_address.ip_port},#{udp_rx.socket.connect_address.ip_address}"]} end
uuid_for_message(uuid)
click to toggle source
# File lib/ruby_nos/agent.rb, line 119 def uuid_for_message uuid if formatter.uuid_format?(uuid) formatter.uuid_to_string(uuid) else uuid end end