class EnsimeBridge

Attributes

cache[R]
quiet[RW]
socket[RW]

Public Class Methods

new(path) click to toggle source
# 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

Public Instance Methods

connect_to_ensime() click to toggle source
# 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.onmessage 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) click to toggle source
# File lib/ensime_bridge.rb, line 11
def get_socket file
    TCPSocket.open("localhost", File.read(file).chomp)
end
is_running?(file = nil) click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/ensime_bridge.rb, line 44
def stop
    File.delete @bridge_file if File.exists?  @bridge_file
    exit
end
unqueue() click to toggle source
# File lib/ensime_bridge.rb, line 65
def unqueue
    if @queue.size == 0
        nil
    else
        @queue.pop(true)
    end
end
wait_for_ensime() click to toggle source
# File lib/ensime_bridge.rb, line 72
def wait_for_ensime
    while not is_running? @http_file
        sleep 0.2
    end
end