This repository has been archived by the owner on Sep 28, 2021. It is now read-only.
forked from Neutron/TacMapPLI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tacmap_pli.js
372 lines (349 loc) · 13.1 KB
/
tacmap_pli.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (C) 2015 jdn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* global __dirname, require, process */
(function() {
"use strict";
var express = require('express');
var compression = require('compression');
var url = require('url');
var request = require('request');
var jsonfile = require('jsonfile');
var bodyParser = require('body-parser');
var fs = require('fs');
var cesium = require('./geoserver/cesiumserver');
//var http = require('http');
var cors = require('cors');
var https = require('https');
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
//UDP Service
var dgram = require('dgram');
var udpserver = dgram.createSocket('udp4');
var udpPort = 33333;
udpserver.bind(udpPort);
udpserver.on('message', function(message) {
var msg = message.toString();
var jsonmsg = JSON.parse(msg);
sio.emit(jsonmsg.scktmsg, {
scktid: jsonmsg.sctkid,
payload: jsonmsg.payload
});
});
var yargs = require('yargs').options({
'port': {
'default': server_port,
'description': 'Port to listen on.'
},
'public': {
'type': 'boolean',
'description': 'Run a public server that listens on all interfaces.'
},
'publicssl': {
'type': 'boolean',
'description': 'Run a public https server that listens on all interfaces.'
},
'upstream-proxy': {
'description': 'A standard proxy server that will be used to retrieve data. Specify a URL including port, e.g. "http://proxy:8000".'
},
'bypass-upstream-proxy-hosts': {
'description': 'A comma separated list of hosts that will bypass the specified upstream_proxy, e.g. "lanhost1,lanhost2"'
},
'help': {
'alias': 'h',
'type': 'boolean',
'description': 'Show this help.'
}
});
var argv = yargs.argv;
if (argv.help) {
return yargs.showHelp();
}
var app = express();
app.use(bodyParser.json());
app.use(compression());
app.use(express.static(__dirname + '/public'));
app.use(cors());
function getRemoteUrlFromParam(req) {
var remoteUrl = req.params[0];
if (remoteUrl) {
// add http:// to the URL if no protocol is present
if (!/^https?:\/\//.test(remoteUrl)) {
remoteUrl = 'http://' + remoteUrl;
}
remoteUrl = url.parse(remoteUrl);
// copy query string
remoteUrl.search = url.parse(req.url).search;
}
return remoteUrl;
}
var dontProxyHeaderRegex = /^(?:Host|Proxy-Connection|Connection|Keep-Alive|Transfer-Encoding|TE|Trailer|Proxy-Authorization|Proxy-Authenticate|Upgrade)$/i;
function filterHeaders(req, headers) {
var result = {};
// filter out headers that are listed in the regex above
Object.keys(headers).forEach(function(name) {
if (!dontProxyHeaderRegex.test(name)) {
result[name] = headers[name];
}
});
return result;
}
var upstreamProxy = argv['upstream-proxy'];
var bypassUpstreamProxyHosts = {};
if (argv['bypass-upstream-proxy-hosts']) {
argv['bypass-upstream-proxy-hosts'].split(',').forEach(function(host) {
bypassUpstreamProxyHosts[host.toLowerCase()] = true;
});
}
app.get('/proxy/*', function(req, res, next) {
// look for request like http://localhost:8080/proxy/http://example.com/file?query=1
var remoteUrl = getRemoteUrlFromParam(req);
if (!remoteUrl) {
// look for request like http://localhost:8080/proxy/?http%3A%2F%2Fexample.com%2Ffile%3Fquery%3D1
remoteUrl = Object.keys(req.query)[0];
if (remoteUrl) {
remoteUrl = url.parse(remoteUrl);
}
}
if (!remoteUrl) {
return res.send(400, 'No url specified.');
}
if (!remoteUrl.protocol) {
remoteUrl.protocol = 'http:';
}
var proxy;
if (upstreamProxy && !(remoteUrl.host in bypassUpstreamProxyHosts)) {
proxy = upstreamProxy;
}
// encoding : null means "body" passed to the callback will be raw bytes
request.get({
url: url.format(remoteUrl),
headers: filterHeaders(req, req.headers),
encoding: null,
proxy: proxy
}, function(error, response, body) {
var code = 500;
if (response) {
code = response.statusCode;
res.header(filterHeaders(req, response.headers));
}
res.send(code, body);
});
});
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var server = app.listen(server_port, argv.public ? undefined : server_ip_address, function() {
if (argv.public) {
console.log('TacMap development server running publicly. Connect to http://localhost:%d/', server.address().port);
}
else if (argv.publicssl) {
server.key = fs.readFileSync('key.pem');
server.cert = fs.readFileSync('cert.pem');
console.log('TacMap development server running publicly. Connect to https://localhost:%d/', server.address().port);
}
else {
console.log('TacMap development server running locally. Connect to http://localhost:%d/', server.address().port);
}
});
server.on('error', function(e) {
if (e.code === 'EADDRINUSE') {
console.log('Error: Port %d is already in use, select a different port.', argv.port);
console.log('Example: node server.js --port %d', argv.port + 1);
}
else if (e.code === 'EACCES') {
console.log('Error: This process does not have permission to listen on port %d.', argv.port);
if (argv.port < 1024) {
console.log('Try a port number higher than 1024.');
}
}
console.log(e);
process.exit(1);
});
server.on('close', function() {
console.log('Cesium development server stopped.');
});
process.on('SIGINT', function() {
server.close(function() {
process.exit(0);
});
});
//SOCKET IO
/**
* @param req Request
* @param res Result
* **/
app.get('/', function(req, res) {
res.sendFile(__dirname + '/public/geoview.html');
});
app.get('/node_modules/*', function(req, res) {
res.sendFile(__dirname + '/' + req.url);
});
app.get('/json/*', function(req, res) {
console.log('jsonp');
var file = __dirname + '/' + req.url;
jsonfile.readFile(file, function(err, obj) {
res.sendFile(obj);
});
});
app.post('/json/*', function(req, res) {
//console.log(request.body);
fs.writeFile(__dirname + '/public' + req.url, JSON.stringify(req.body), function() {
res.end();
});
});
app.put('/json/*', function(req, res) {
//console.log(req.body);
fs.writeFile(__dirname + '/public' + req.url, JSON.stringify(req.body), function() {
res.end();
});
});
app.get('/xml/*', function(req, res) {
res.sendFile(__dirname + '/' + req.url);
});
app.put('/xml/*', function(req, res) {
console.log("Put " + req.url);
console.log(req.body);
fs.writeFile(__dirname + '/' + req.url, req.body, function() {
res.end();
});
});
app.put('/entity/*', function(req, res) {
console.log("Put entity " + req.url);
console.log(req.body);
fs.writeFile(__dirname + '/public' + req.url, req.body, function() {
res.end();
});
});
app.post('/msg/*', cors(), function(req, res) {
var jsonmsg = req.body;
sio.emit(jsonmsg.scktmsg, {
scktid: jsonmsg.sctkid,
payload: jsonmsg.payload
});
res.send(req.body);
});
app.post('/udpmsg/*', cors(), function(req, res) {
var msg = JSON.stringify(req.body);
var jsonmsg = new Buffer(jsonmsg);
udpserver.send(jsonmsg, 0, jsonmsg.length, udpPort, '192.168.225.5');
res.send(req.body);
});
/** SocketIO Services **/
var sio = require('socket.io').listen(server);
sio.serveClient(true);
//The following code implements a peer map service that uses SockeIO namespaces
//to publish and subscribe to position reporting and other data.
/** Global SockeIO Connection **/
/** @param topsocket Top level socket connection **/
sio.of('').on('connection', function(topsocket) {
topsocket.emit('connection', {
message: 'Msg Socket Ready',
socketid: topsocket.id
});
/**
* Namespaces correspond to Map Views ..
* @param mapdta {id,netname,mapviewid}
* @param callback
* **/
topsocket.on('join namespace', function(ep, callback) {
console.log("join namespace: " + ep.user_id);
sio.of('/' + ep.user_id)
.once('connection', function(map_socket) {
console.log('user connected to ' + ep.user_id);
socketOps(map_socket);
});
callback(ep);
});
/** @param endpoint {id,netname,mapviewid}**/
topsocket.on('initial connection', function(endpoint) {
sio.to(endpoint.socketid).emit('load map', endpoint);
});
// When a Map View is created - another namespace is set up with
// SocketOps functions. Each new user has a namespace auomatically assigned
// and will be avalalabe to others to select.
// @todo Implement security to restrict/permit access to Map Views / Namespaces
// Create a mapview
/** @param mapdata {user_id,name,data} **/
topsocket.on('create map', function(mapdata) {
console.log('create map');
sio.of('/' + mapdata.id)
.once('connection', function(map_socket) {
console.log('user connected to ' + mapdata.id);
socketOps(map_socket);
});
});
// Remove a mapview
/** @param mapdata {id,name,url,data} **/
topsocket.on('remove map', function(mapdata) {
sio.emit('remove map', mapdata);
});
// Update a mapview
/** @param mapdata {id,name,url,data} **/
topsocket.on('update map', function(mapdata) {
sio.emit('update map', mapdata);
});
});
var socketOps = function(socket) {
// Disconnect endpoint. Remove from all lists
socket.once('disconnect', function() {
console.log('disconnect ' + socket.id);
sio.emit('disconnect', {
socketid: socket.id
});
});
// Join a network.
socket.on('join network', function(netdata) {
socket.join(netdata.network_id);
sio.emit('join net', netdata);
});
// Leave a network.
socket.on('leave network', function(netdata) {
socket.leave(netdata.network_id);
sio.emit('leave net', netdata);
});
// Rename a network.
socket.on('update network', function(netdata) {
sio.emit('update net', netdata);
});
// Create a network as a socketIO room
socket.on('create network', function(netdata) {
sio.emit('create net', netdata);
});
// Remove a network
socket.on('remove network', function(netdata) {
sio.emit('remove net', netdata);
});
// Synch Tracks
socket.on('Sync Tracks', function(trackdata) {
console.log('Sync Tracks');
sio.emit('update tracks', {
scktid: trackdata.scktid,
tracks: trackdata.tracks
});
});
//
//Publish message to single socketid, or to one or all network
socket.on('publish msg', function(msg) {
if (typeof msg.data.destination !== 'undefined') {
sio.to(msg.data.destination.socketid).emit(msg.scktmsg, msg.data);
}
else if (typeof msg.data.network !== 'undefined') {
sio.to(msg.data.network.id).emit(msg.scktmsg, msg.data);
}
else {
sio.emit(msg.scktmsg, msg.data);
}
});
};
})();