forked from IvanSanchez/artemis-glitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
173 lines (138 loc) · 4.7 KB
/
app.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Module dependencies.
*/
var express = require('express')
, opener = require('opener')
, routes = require('./routes')
, artemisNet = require('./artemisNet')
, artemisModel = require('./public/javascripts/worldmodel');
var app = module.exports = express();
var tcpPort = 3000;
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
console.log(routes.index);
app.get('/', routes.index);
app.get('/model', artemisModel.returnModelAsJSON);
app.get('/map', routes.map);
app.get('/bearing-table', routes.bearingTable);
app.get('/proximity', routes.proximity);
// Set up the socket.io stuff
// This basically will relay all artemis server packets to any browser
// listening to the appropiate socket.io sockets.
// Socket.io is clever enough to not send unneeded messages to
// clients
var server = require('http').Server(app);
var io = require('socket.io').listen(server);
io.set('log level', 1); // reduce logging
server.listen(tcpPort);
// When socket.io is ready: for every packet we receive from the
// Artemis server, send a websocket to all connected browsers.
io.sockets.on('connection', function (s){
artemisNet.on('packet', function(data, packetType){
if (packetType == 'unknownUpdate') {return;}
s.emit(packetType, data);
});
});
// Inform the browsers of connections/disconnections to/from Artemis server
artemisNet.on('connected', function(){
console.log('We seem to have connected.')
io.sockets.emit('connected');
});
artemisNet.on('disconnected', function(){
io.sockets.emit('disconnected');
});
function grabStations() {
/// FIXME!!! This is the console selection code, and perhaps should not
/// be placed here.
artemisNet.emit('setStation', {station:0, selected:1}); // Main Scr
// artemisNet.emit('setStation', {station:1, selected:1}); // Helm
// artemisNet.emit('setStation', {station:2, selected:1}); // Weap
// artemisNet.emit('setStation', {station:3, selected:1}); // Engine
artemisNet.emit('setStation', {station:4, selected:1}); // Sci
artemisNet.emit('setStation', {station:5, selected:1}); // Comms
artemisNet.emit('setStation', {station:6, selected:1}); // Observ
// artemisNet.emit('setStation', {station:7, selected:1}); // Capt
// artemisNet.emit('setStation', {station:8, selected:1}); // GM
artemisNet.emit('ready');
console.log('Consoles have been requested.')
}
artemisNet.on('welcome', function(){
console.log('We seem to have been welcomed.')
grabStations();
});
// Functionality for connecting/disconnecting to/from the server
// and knowing our own public IP address (which is different from
// 'localhost', which is used in the internal web browser).
var artemisServerAddr = null;
app.get('/connect/:server', function(req,res){
artemisNet.connect(req.params.server, 10);
artemisServerAddr = req.params.server;
res.end();
});
app.get('/disconnect', function(req,res){
artemisNet.disconnect();
res.end();
});
app.get('/artemis-server', function(req,res){
if (artemisServerAddr) {
res.write(artemisServerAddr);
}
res.end();
});
app.get('/glitter-address', function(req,res){
var publicIPs = [];
// Network interface detection is done every time, so that
// the glitter server can accommodate changes to its
// network profile, e.g. connect to a wifi network
// With inspiration from https://stackoverflow.com/questions/3653065/get-local-ip-address-in-node-js
var os=require('os');
var ifaces=os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach(function(details){
if (details.family=='IPv4') {
if (details.address.toString().substr(0,3)!=="127") {
publicIPs.push(details.address);
}
}
});
}
res.write(JSON.stringify(publicIPs));
res.end();
});
app.get('/ship-select/:shipIndex', function(req,res){
artemisNet.emit('shipSelect', {shipIndex:req.params.shipIndex});
grabStations();
res.end();
});
var headless = false;
var autoConnect = false;
for (var i in process.argv) {
if (process.argv[i] == '--headless') {
headless = true;
}
if (process.argv[i] == '--server' && process.argv.length > i) {
autoConnect = process.argv[parseInt(i)+1];
console.log('Will autoconnect to server at ' + autoConnect);
}
}
if (autoConnect) {
artemisNet.connect(autoConnect,5);
}
// Once everything's ready, try open the default browser with the main page.
if (!headless) {
opener('http://localhost:' + tcpPort);
}