Class: ScoutAgent::Agent::CommunicationAgent

Inherits:
ScoutAgent::Agent show all
Defined in:
lib/scout_agent/agent/communication_agent.rb

Overview

This agent manages the XMPP connection with the server. It mainly just listens for messages from the server and passes them on to matching Order instances.

Constant Summary collapse

RECONNECT_WAIT =

The number of seconds to wait before attempting another connection.

60

Instance Attribute Summary

Attributes inherited from ScoutAgent::Agent

#log

Instance Method Summary collapse

Methods inherited from ScoutAgent::Agent

#authorize, #notice_changes

Methods included from Tracked

#clear_status, #force_status_database_reload, #status, #status_database, #status_log

Constructor Details

#initializeCommunicationAgent

Prepares a log() and passses it down to the Orders, which are also loaded here. A list of trusted XMPP users is also prepared as part of this start-up.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scout_agent/agent/communication_agent.rb', line 23

def initialize
  super  # setup our log and status

  Order.log = log  # pass on our log
  Order.load_all   # load supported orders
  
  @agent_jid       = nil
  @jabber          = nil
  @roster          = nil
  @connecting      = false
  @shutdown_thread = nil
  @trusted         = Array(Plan.xmpp_trusted).map { |trusted|
    trusted_with_server = trusted.sub(/@\*\z/, "@#{jabber_server}")
    /\A#{Regexp.escape(trusted_with_server)}\b/
  }
end

Instance Method Details

#finishObject

Triggers the shutdown process.



56
57
58
59
60
61
62
63
# File 'lib/scout_agent/agent/communication_agent.rb', line 56

def finish
  log.info("Shutting down.")
  if @shutdown_thread
    @shutdown_thread.run
  else
    exit
  end
end

#runObject

This method encupsulates the process of the XMPP listener, which is pretty much: login, setup, listen for commands until told to stop, and exit.



45
46
47
48
49
50
51
52
53
# File 'lib/scout_agent/agent/communication_agent.rb', line 45

def run
  
  update_status("Online since #{Time.now.utc.to_db_s}")
  fetch_roster
  install_subscriptions_callback
  install_messages_callback
  listen
  close_connection
end