Class: Debounced::ServiceProxy
- Inherits:
-
Object
- Object
- Debounced::ServiceProxy
- Defined in:
- lib/debounced/service_proxy.rb
Overview
Ruby interface to the debounce service Input is an activity descriptor, and an object. When the activity is debounced, a callback method is invoked on the object. Assumes the object class has an initializer that accepts a hash of attributes, which are the instance variables
Constant Summary collapse
- DELIMITER =
"\f".freeze
Instance Attribute Summary collapse
-
#listening ⇒ Object
readonly
Returns the value of attribute listening.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#wait_timeout ⇒ Object
readonly
Returns the value of attribute wait_timeout.
Instance Method Summary collapse
- #debounce_activity(activity_descriptor, timeout, callback) ⇒ Object
-
#initialize ⇒ ServiceProxy
constructor
A new instance of ServiceProxy.
- #listen(abort_signal = nil) ⇒ Object
- #receive(abort_signal = nil) ⇒ Object
-
#reset_server ⇒ Object
Send message to server to reset its state.
- #stop ⇒ Object
Constructor Details
#initialize ⇒ ServiceProxy
Returns a new instance of ServiceProxy.
15 16 17 18 19 20 |
# File 'lib/debounced/service_proxy.rb', line 15 def initialize @wait_timeout = Debounced.configuration.wait_timeout @logger = Debounced.configuration.logger @listening = false @mutex = Mutex.new end |
Instance Attribute Details
#listening ⇒ Object (readonly)
Returns the value of attribute listening.
13 14 15 |
# File 'lib/debounced/service_proxy.rb', line 13 def listening @listening end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
13 14 15 |
# File 'lib/debounced/service_proxy.rb', line 13 def logger @logger end |
#wait_timeout ⇒ Object (readonly)
Returns the value of attribute wait_timeout.
13 14 15 |
# File 'lib/debounced/service_proxy.rb', line 13 def wait_timeout @wait_timeout end |
Instance Method Details
#debounce_activity(activity_descriptor, timeout, callback) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/debounced/service_proxy.rb', line 41 def debounce_activity(activity_descriptor, timeout, callback) SemanticLogger.tagged("send") do if !listening || socket.nil? logger.debug { "No connection to #{server_name}; skipping debounce step." } callback.call else logger_trace { "Sending #{activity_descriptor} to #{server_name}" } transmit(build_request(activity_descriptor, timeout, callback)) end end end |
#listen(abort_signal = nil) ⇒ Object
24 25 26 27 28 |
# File 'lib/debounced/service_proxy.rb', line 24 def listen(abort_signal = nil) Thread.new do receive(abort_signal) end end |
#receive(abort_signal = nil) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/debounced/service_proxy.rb', line 55 def receive(abort_signal = nil) @abort_signal = abort_signal || Concurrent::AtomicBoolean.new SemanticLogger.tagged("receive") do logger.info { "Listening for messages from #{server_name}..." } loop do @listening = true break if @abort_signal.true? = next unless payload = () raise SocketConflictError if payload['type'] == 'rejectClient' instantiate_callback(payload['callback']).call rescue Debounced::NoServerError => e logger_trace { e. } sleep wait_timeout end close end rescue SocketConflictError, StandardError => e logger.warn("Unable to listen for messages from #{server_name}: #{e.}") logger.warn(e.backtrace.join("\n")) ensure @listening = false end |
#reset_server ⇒ Object
Send message to server to reset its state. Useful for automated testing.
32 33 34 35 36 37 38 39 |
# File 'lib/debounced/service_proxy.rb', line 32 def reset_server if socket.nil? logger.warn("No connection to #{server_name}; unable to reset server.") else logger_trace { "Resetting #{server_name}" } transmit({ type: 'reset' }) end end |
#stop ⇒ Object
85 86 87 |
# File 'lib/debounced/service_proxy.rb', line 85 def stop @abort_signal.make_true end |