Class: RText::Frontend::Connector

Inherits:
Object
  • Object
show all
Includes:
Process, MessageHelper
Defined in:
lib/rtext/frontend/connector.rb

Instance Method Summary collapse

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
25
# File 'lib/rtext/frontend/connector.rb', line 12

def initialize(config, options={})
  @config = config
  @logger = options[:logger]
  @state = :off
  @invocation_id = 1
  @invocations = {}
  @busy = false
  @busy_start_time = nil
  @connection_listener = options[:connect_callback]
  @outfile_provider = options[:outfile_provider]
  @keep_outfile = options[:keep_outfile]
  @connection_timeout = options[:connection_timeout] || 10
  @process_id = nil
end

Instance Method Details

#execute_command(obj, options = {}) ⇒ Object



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
74
75
76
77
78
79
# File 'lib/rtext/frontend/connector.rb', line 27

def execute_command(obj, options={})
  timeout = options[:timeout] || 10
  @busy = false if @busy_start_time && (Time.now > @busy_start_time + timeout)
  if @busy
    do_work
    return :backend_busy 
  end
  unless connected?
    connect unless connecting?
    do_work
  end
  if connected?
    obj["invocation_id"] = @invocation_id
    obj["type"] = "request"
    @socket.send(serialize_message(obj), 0)
    @logger.debug('Sent request') if @logger
    result = nil
    @busy = true
    @busy_start_time = Time.now
    if options[:response_callback]
      @invocations[@invocation_id] = lambda do |r|
        if r["type"] == "response" || r["type"] =~ /error$/
          @busy = false
        end
        options[: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
    @logger.debug('connecting')
    :connecting
  end
end

#resumeObject



81
82
83
# File 'lib/rtext/frontend/connector.rb', line 81

def resume
  do_work
end

#stopObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/rtext/frontend/connector.rb', line 85

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
  ensure_process_cleanup(@process_id, @keep_outfile ? nil : @out_file, 10)
  @process_id = nil
end