/
server.coffee
48 lines (40 loc) · 1.13 KB
/
server.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
41
42
43
44
45
46
47
48
path = require 'path'
dnode = require 'dnode'
fs = require 'fs'
join = require('path').join
Shared = require join(__dirname, 'shared')
Hash = require 'hashish'
module.exports = class Server
@include Shared
@subscriptions = {}
constructor: (@options={}) ->
@name = @options.name
@remoteMethods = @options.remoteMethods
@cwd = @options.cwd || process.cwd()
connect: (cb) ->
@setPidfile (err) =>
if err then throw err
@initSignals()
@initRPC()
@socket = @rpc.listen @getSocket()
cb() if cb?
setPidfile: (cb) ->
fs.writeFile @getPidfile(), process.pid, cb
initSignals: ->
process.on 'SIGINT', -> process.exit()
process.on 'exit', =>
@cleanPidfile()
@cleanSocket()
initRPC: ->
subs = {}
self = @
@rpc = dnode (client, conn) ->
@call = (method, args...) ->
self.remoteMethods[method](args...)
@subscribe = (emit) ->
Server.subscriptions[conn.id] = emit
conn.on 'end', => delete Server.subscriptions[conn.id]
publish: ->
args = arguments
Hash(Server.subscriptions).forEach (emit) ->
emit.apply emit, args