Class: EnsimeBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/ensime_bridge.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#cacheObject (readonly)

Returns the value of attribute cache.



10
11
12
# File 'lib/ensime_bridge.rb', line 10

def cache
  @cache
end

#quietObject

Returns the value of attribute quiet.



9
10
11
# File 'lib/ensime_bridge.rb', line 9

def quiet
  @quiet
end

#socketObject

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_ensimeObject



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.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) ⇒ 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

Returns:

  • (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_stopObject



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

#runObject



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_connectionObject



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_forwarderObject



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

#stopObject



44
45
46
47
# File 'lib/ensime_bridge.rb', line 44

def stop
    File.delete @bridge_file if File.exists?  @bridge_file
    exit
end

#unqueueObject



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_ensimeObject



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