-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommand-server-check.coffee
56 lines (43 loc) · 1.45 KB
/
command-server-check.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
49
50
51
52
53
54
55
56
commander = require 'commander'
_ = require 'lodash'
meshblu = require 'meshblu'
url = require 'url'
DEFAULT_HOST = 'meshblu.octoblu.com'
DEFAULT_PORT = 443
DEFAULT_TIMEOUT = 10000
class ServerCheck
parseOptions: =>
commander
.option '-s, --server <host[:port]>', "Meshblu host (default #{DEFAULT_HOST}:#{DEFAULT_PORT})"
.option '-t, --timeout <milliseconds>', "Request timeout (default #{DEFAULT_TIMEOUT})"
.parse process.argv
@data = JSON.parse(commander.data) if commander.data?
@registerFileName = commander.file
parseConfig: =>
{server, port} = @parseServer()
{server: server, port: port, timeout: commander.timeout || DEFAULT_TIMEOUT}
parseServer: =>
unless commander.server?
return {server: DEFAULT_HOST, port: DEFAULT_PORT}
server = commander.server
unless _.startsWith server, 'ws'
protocol = if port == 443 then 'wss://' else 'ws://'
server = protocol + server
{hostname, port} = url.parse server
port ?= DEFAULT_PORT
{server: hostname, port: port}
run: =>
@parseOptions()
@config = @parseConfig()
console.log @config.server + ":" + @config.port
@conn = meshblu.createConnection @config
@conn.on 'ready', @onReady
@conn.on 'notReady', @notReady
setTimeout @notReady, @config.timeout
onReady: =>
console.log "online"
process.exit 0
notReady: =>
console.log "offline"
process.exit 1
(new ServerCheck()).run()