Class: RText::Frontend::Connector
- Inherits:
-
Object
- Object
- RText::Frontend::Connector
- Includes:
- Process, MessageHelper
- Defined in:
- lib/rtext/frontend/connector.rb
Instance Method Summary collapse
- #execute_command(obj, options = {}) ⇒ Object
-
#initialize(config, options = {}) ⇒ Connector
constructor
A new instance of Connector.
- #resume ⇒ Object
- #stop ⇒ Object
Methods included from MessageHelper
#each_json_object_string, #escape_all_strings, #extract_message, #serialize_message, #unescape_all_strings
Methods included from JsonInterface
#json_to_object, #object_to_json, set_j2o_converter, set_o2j_converter
Constructor Details
#initialize(config, options = {}) ⇒ Connector
Returns a new instance of Connector.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/rtext/frontend/connector.rb', line 12 def initialize(config, ={}) @config = config @logger = [:logger] @state = :off @invocation_id = 1 @invocations = {} @busy = false @busy_start_time = nil @connection_listener = [:connect_callback] @outfile_provider = [:outfile_provider] @keep_outfile = [:keep_outfile] @connection_timeout = [:connection_timeout] || 10 end |
Instance Method Details
#execute_command(obj, options = {}) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rtext/frontend/connector.rb', line 26 def execute_command(obj, ={}) timeout = [:timeout] || 5 @busy = false if @busy_start_time && (Time.now > @busy_start_time + timeout) if @busy do_work :backend_busy elsif connected? obj["invocation_id"] = @invocation_id obj["type"] = "request" @socket.send((obj), 0) result = nil @busy = true @busy_start_time = Time.now if [:response_callback] @invocations[@invocation_id] = lambda do |r| if r["type"] == "response" || r["type"] =~ /error$/ @busy = false end [:response_callback].call(r) end @invocation_id += 1 do_work :request_pending else @invocations[@invocation_id] = lambda do |r| if r["type"] == "response" || r["type"] =~ /error$/ result = r @busy = false end end @invocation_id += 1 while !result if Time.now > @busy_start_time + timeout result = :timeout @busy = false else sleep(0.1) do_work end end result end else connect unless connecting? do_work :connecting end end |
#resume ⇒ Object
75 76 77 |
# File 'lib/rtext/frontend/connector.rb', line 75 def resume do_work end |
#stop ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rtext/frontend/connector.rb', line 79 def stop while connecting? do_work sleep(0.1) end if connected? execute_command({"type" => "request", "command" => "stop"}) while do_work sleep(0.1) end end end |