-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
123 lines (92 loc) · 2.01 KB
/
index.js
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env node
/**
* Ninja Blocks WiFi Utility
* http://ninjablocks.com/
*
* Copyright (c) 2012 Ninja Blocks Inc
* Licensed under the MIT license.
*
* Emily Rose <[email protected]>
*/
;(function() {
var argv = require('optimist').argv;
var config = require('./app/config')
, fork = require('child_process').fork
, express = require('express')
, port = argv.port || 80
, confPath = argv.confPath || "/etc/wpa_supplicant.conf"
, path = require('path')
, util = require('util')
, app = express()
;
function exit(reason) {
process.stderr.write(
util.format(
"Exiting: Could not listen on port %s. %s\n"
, port
, reason
)
);
};
/**
* Messages from the monitor process
*/
function message(msg) {
if(!msg.action || typeof msg.data == 'undefined' || msg.error) {
var
stack = new Error().stack
, error
;
if(msg.error && msg.error == "unknownAction" && msg.action) {
error = util.format(
'Unknown monitor action: %s'
, msg.action
);
}
else {
error = util.format(
'Error from monitor: %s'
, msg.error || "UNKNOWN"
);
}
console.log(error);
console.log(stack);
return;
}
app.emit(msg.action, msg.data);
};
var monitor = function monitor() {
monitor.process = fork(path.resolve(__dirname, 'monitor'), [confPath]);
monitor.process
.on('message', message)
.on('exit', monitor)
.send({ action : "init" })
;
};
/**
* Messages _to_ the monitor process
*/
app.send = function send(action, data) {
monitor.process.send({
'action' : action
, 'data' : data || null
});
};
/**
* Exit if we have problems. Upstart should handle restarts.
*/
process.on("uncaughtException", function(err) {
if(err.code == "EACCES") {
exit("Access denied.");
}
else if(err.code == "EADDRINUSE") {
exit("Address in use.");
}
else {
process.stderr.write(util.format("Exiting: %s.\n", err));
}
process.exit(1);
});
config(app).listen(port);
monitor();
})();