Class: EnsimeBridge
- Inherits:
-
Object
- Object
- EnsimeBridge
- Defined in:
- lib/ensime_bridge.rb
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#quiet ⇒ Object
Returns the value of attribute quiet.
-
#socket ⇒ Object
Returns the value of attribute socket.
Instance Method Summary collapse
- #connect_to_ensime ⇒ Object
- #get_socket(file) ⇒ Object
-
#initialize(path) ⇒ EnsimeBridge
constructor
A new instance of EnsimeBridge.
- #is_running?(file = nil) ⇒ Boolean
- #remote_stop ⇒ Object
- #run ⇒ Object
- #run_ensime_connection ⇒ Object
- #run_forwarder ⇒ Object
- #send_command(command) ⇒ Object
- #send_result(result) ⇒ Object
- #stop ⇒ Object
- #unqueue ⇒ Object
- #wait_for_ensime ⇒ Object
Constructor Details
#initialize(path) ⇒ EnsimeBridge
Returns a new instance of EnsimeBridge.
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/ensime_bridge.rb', line 24 def initialize path @quiet = false @cache = "#{path}_cache/" @bridge_file = "#{@cache}bridge" @http_file = "#{@cache}http" if not File.exists? path @no_ensime_config = true return end @queue = Queue.new Dir.mkdir @cache if not File.exists?(@cache) @logger = Logger.new(@cache+"bridge.log", 2, 100000) end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
10 11 12 |
# File 'lib/ensime_bridge.rb', line 10 def cache @cache end |
#quiet ⇒ Object
Returns the value of attribute quiet.
9 10 11 |
# File 'lib/ensime_bridge.rb', line 9 def quiet @quiet end |
#socket ⇒ Object
Returns the value of attribute socket.
9 10 11 |
# File 'lib/ensime_bridge.rb', line 9 def socket @socket end |
Instance Method Details
#connect_to_ensime ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ensime_bridge.rb', line 48 def connect_to_ensime url = "ws://127.0.0.1:#{File.read("#{@cache}http").chomp}/jerky" @socket = WebSocket::EventMachine::Client.connect(:uri => url) @socket.onopen do @logger.info "Connected to ensime!" end @socket.onerror do |err| @logger.error err end @socket. do |msg, type| @logger.info "Received message: #{msg}, type #{type}" @queue << msg end @socket.onclose do |code, reason| @logger.info "Disconnected with status code: #{code} #{reason}" end end |
#get_socket(file) ⇒ Object
11 12 13 |
# File 'lib/ensime_bridge.rb', line 11 def get_socket file TCPSocket.open("localhost", File.read(file).chomp) end |
#is_running?(file = nil) ⇒ Boolean
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/ensime_bridge.rb', line 14 def is_running? file = nil file = file.nil? ? @bridge_file : file return false if not File.exists? file begin get_socket(file).close rescue => e return false end true end |
#remote_stop ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/ensime_bridge.rb', line 37 def remote_stop if is_running? s = get_socket(@bridge_file) s.puts "self.stop" s.close end end |
#run ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/ensime_bridge.rb', line 125 def run return if @no_ensime_config if is_running? @logger.info "bridge is already running" return end wait_for_ensime @logger.info "ensime is ready" run_ensime_connection run_forwarder end |
#run_ensime_connection ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/ensime_bridge.rb', line 87 def run_ensime_connection Thread.new do EventMachine.run do connect_to_ensime end end end |
#run_forwarder ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ensime_bridge.rb', line 111 def run_forwarder server = TCPServer.new "localhost", 0 File.write(@bridge_file, server.addr[1]) while @client = server.accept begin command = @client.readline.chomp send_command command @client.close rescue => e @logger.error e @logger.error e.backtrace end end end |
#send_command(command) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/ensime_bridge.rb', line 94 def send_command command while true result = nil @logger.info "command: #{command}" if command.start_with? "{" @logger.info "direct send #{command}" @socket.send command else result = instance_eval command end if command == "unqueue" break if not send_result result else break end end end |
#send_result(result) ⇒ Object
77 78 79 80 81 82 83 84 85 86 |
# File 'lib/ensime_bridge.rb', line 77 def send_result result if not result.nil? and not result.empty? @client.puts result.gsub("\n", "") else @client.puts "nil" return false end @logger.info result.gsub("\n", "") return true end |
#stop ⇒ Object
44 45 46 47 |
# File 'lib/ensime_bridge.rb', line 44 def stop File.delete @bridge_file if File.exists? @bridge_file exit end |
#unqueue ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/ensime_bridge.rb', line 65 def unqueue if @queue.size == 0 nil else @queue.pop(true) end end |
#wait_for_ensime ⇒ Object
72 73 74 75 76 |
# File 'lib/ensime_bridge.rb', line 72 def wait_for_ensime while not is_running? @http_file sleep 0.2 end end |