Class: OpenAgent::Client
- Inherits:
-
Object
- Object
- OpenAgent::Client
- Includes:
- XMLHelpers
- Defined in:
- lib/openagent/client.rb
Constant Summary collapse
- ZIS_SUCCESS =
0
- ZIS_NO_MESSAGES =
9
- Message =
SIF::Infra::Common::Message
- MessageRepresenter =
::SIF::Representation::Infra::Common::Message
Constants included from XMLHelpers
XMLHelpers::PP_XSL, XMLHelpers::PP_XSLT
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#builder ⇒ Object
readonly
Returns the value of attribute builder.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
-
#zone ⇒ Object
readonly
Returns the value of attribute zone.
Class Method Summary collapse
Instance Method Summary collapse
- #callback(name, &block) ⇒ Object
-
#initialize(opts = {}) ⇒ Client
constructor
A new instance of Client.
- #log(name, body) ⇒ Object
-
#method_missing(method, *args, &block) ⇒ Object
Proxy to OpenAgent::MessageBuilder, e.g.
- #run(wait_period = 30, cycles = nil) ⇒ Object
- #run_loop(wait_period = 30) ⇒ Object
- #run_once ⇒ Object
- #trigger(name, *args) ⇒ Object
Methods included from XMLHelpers
#formatted_xml, #parse_well_formed_xml, #pretty_xml
Constructor Details
#initialize(opts = {}) ⇒ Client
Returns a new instance of Client.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/openagent/client.rb', line 30 def initialize(opts={}) @callbacks = { :receive_message => [], :each_loop => [] } opts = ActiveSupport::HashWithIndifferentAccess.new(opts) @name = opts.delete(:name) @url = opts.delete(:url) @pretty_print = opts.has_key?(:pretty_print) ? opts.delete(:pretty_print) : true @agent_opts = opts[:agent_opts] || {} @agent_opts[:name] = @name if @name @zone_opts = opts[:zone_opts] || {} @zone_opts[:uri] = @url if @url @log = opts[:logger] || Logger.new(opts[:logfile] || STDOUT, 'daily') @agent = opts[:agent] || OpenAgent::Agent.new(@agent_opts) @zone = opts[:zone] || OpenAgent::Zone.new(@zone_opts) @builder = OpenAgent::MessageBuilder.new(@agent, @zone) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Proxy to OpenAgent::MessageBuilder, e.g. ack, request, event, provision, register, unregister, ping, sleep, wakeup, get_message, get_zone_status, get_agent_acl
62 63 64 65 66 67 68 69 |
# File 'lib/openagent/client.rb', line 62 def method_missing(method, *args, &block) if @builder.respond_to?(method) = @builder.send(method, *args) (, &block) else super end end |
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
15 16 17 |
# File 'lib/openagent/client.rb', line 15 def agent @agent end |
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
15 16 17 |
# File 'lib/openagent/client.rb', line 15 def builder @builder end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
14 15 16 |
# File 'lib/openagent/client.rb', line 14 def name @name end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
14 15 16 |
# File 'lib/openagent/client.rb', line 14 def url @url end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
15 16 17 |
# File 'lib/openagent/client.rb', line 15 def zone @zone end |
Class Method Details
Instance Method Details
#callback(name, &block) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/openagent/client.rb', line 71 def callback(name, &block) if cbs = @callbacks[name.to_sym] cbs << block else raise "Can't register callback #{name}. " + "Available callbacks: #{@callbacks.keys.inspect}" end end |
#log(name, body) ⇒ Object
55 56 57 |
# File 'lib/openagent/client.rb', line 55 def log(name, body) @log.info "#{name}\n#{body}\n" end |
#run(wait_period = 30, cycles = nil) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/openagent/client.rb', line 122 def run(wait_period=30, cycles=nil) if cycles cycles.times(&run_loop(wait_period)) else loop(&run_loop(wait_period)) end end |
#run_loop(wait_period = 30) ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/openagent/client.rb', line 108 def run_loop(wait_period=30) Proc.new do begin run_once sleep wait_period rescue OpenAgent::ResponseError => e @log.error e rescue Net::HTTPError => e @log.error e sleep(5) end end end |
#run_once ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/openagent/client.rb', line 89 def run_once trigger(:each_loop) msg = nil begin do |, outgoing, xml| msg = # capture msg for outside of block if inner = . trigger(:receive_message, , outgoing, xml) # We send an Ack for both an Event and a Response if .status_code == ZIS_SUCCESS ack(inner.source_id, inner.msg_id) end end end # @log.warn "retval: #{retval.inspect} #{message.nil?.inspect} #{message ? message.status_code : '-'}" end while msg && msg.status_code == ZIS_SUCCESS end |
#trigger(name, *args) ⇒ Object
80 81 82 83 84 85 86 87 |
# File 'lib/openagent/client.rb', line 80 def trigger(name, *args) if cbs = @callbacks[name.to_sym] cbs.each { |cb| cb.call(*args) } else raise "Can't trigger callback #{name}. " + "Available callbacks: #{@callbacks.keys.inspect}" end end |