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 ⇒ 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.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/openagent/client.rb', line 29 def initialize(opts={}) @callbacks = { :receive_message => [], :each_loop => [] } @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 = Logger.new(opts["log"] || 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
59 60 61 62 63 64 65 66 |
# File 'lib/openagent/client.rb', line 59 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.
14 15 16 |
# File 'lib/openagent/client.rb', line 14 def agent @agent end |
#builder ⇒ Object (readonly)
Returns the value of attribute builder.
14 15 16 |
# File 'lib/openagent/client.rb', line 14 def builder @builder end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/openagent/client.rb', line 13 def name @name end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
13 14 15 |
# File 'lib/openagent/client.rb', line 13 def url @url end |
#zone ⇒ Object (readonly)
Returns the value of attribute zone.
14 15 16 |
# File 'lib/openagent/client.rb', line 14 def zone @zone end |
Class Method Details
Instance Method Details
#callback(name, &block) ⇒ Object
68 69 70 71 72 73 74 75 |
# File 'lib/openagent/client.rb', line 68 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
52 53 54 |
# File 'lib/openagent/client.rb', line 52 def log(name, body) @log.info "#{name}\n#{body}\n" end |
#run ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/openagent/client.rb', line 86 def run wait_short = 10 wait_long = 30 loop do begin trigger(:each_loop) wait_period = wait_long = true while do |, outgoing, xml| if inner = . trigger(:receive_message, , outgoing, xml) if inner.response case .status_code when ZIS_SUCCESS then if inner.response.more_packets? wait_period = wait_short else wait_period = wait_long end when ZIS_NO_MESSAGES then if not inner.more_packets? wait_period = wait_long end end end # We send an Ack for both an Event and a Response if .status_code == ZIS_SUCCESS ack(inner.source_id, inner.msg_id) end else if .status_code == ZIS_NO_MESSAGES = false end end end end sleep wait_period rescue ResponseError => e @log.error e end end end |
#trigger(name, *args) ⇒ Object
77 78 79 80 81 82 83 84 |
# File 'lib/openagent/client.rb', line 77 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 |