-
Notifications
You must be signed in to change notification settings - Fork 1
/
proxy.coffee
40 lines (28 loc) · 1.34 KB
/
proxy.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
net = require 'net'
class Proxy
constructor: (neo4j, config) ->
log = config.logger
server = net.createServer (proxySocket) ->
info = proxySocket.remoteAddress + ":" + proxySocket.remotePort
log "handle connection " + info
serverSocket = new net.Socket()
proxySocket.on "data", (data) -> neo4j.waitForServer ">" + info, -> serverSocket.write data
proxySocket.on "close", -> neo4j.waitForServer ">" + info, -> serverSocket.end()
neo4j.waitForServer "<" + info, ->
serverSocket.connect config.port, "127.0.0.1"
serverSocket.on "data", (data) -> proxySocket.write data
serverSocket.on "close", (had_error) -> proxySocket.end()
log "start " + config.proxyPort + " -> " + config.port
list = server.listen config.proxyPort
updater = setInterval neo4j.updateStatus, 5000
neo4j.updateStatus (isRunning) -> neo4j.setRunning isRunning
@running = -> neo4j.running()
@stop = ->
clearInterval updater
neo4j.stopServer()
server.close()
@startServer = (callback) ->
neo4j.startServer callback
@stopServer = (callback) ->
neo4j.stopServer callback
exports.buildFor = (neo4j, config) -> new Proxy neo4j, config