-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
94 lines (72 loc) · 2.36 KB
/
monitor.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
"use strict";
/*
* Realtime monitoring of processing status / evolution.
* Currently single threaded...
*/
//https://appdividend.com/2018/02/16/node-js-socket-io-tutorial/
const opn = require('opn');
const mongoUtils = require('./mongoUtils.js');
const UdpUtils = require('./udpUtils.js');
var udpUtils = new UdpUtils()
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const isJSON = require('./isJSON.js')
const server = require('http').createServer(app);
const connections = [];
var io = require('socket.io')(server);
app.use( bodyParser.json() );
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use(express.json()); // to support JSON-encoded bodies
app.use(express.urlencoded()); // to support URL-encoded bodies
app.get('/', function(req, res){
res.send('Hello World!')
})
/*
* Endpoint for job uploading.
*/
app.post('/postJob', function (req, res) {
console.log(req.body.jobData)
/*
* TODO: Upload the job to the mongodb and kick off processing if it is not already kicked off
*/
var jobData = JSON.parse(req.body.jobData)
mongoUtils.importSpheronet(jobData ,function(){
console.log('we have imported a spheronet')
//console.log('generating some offspring - finding a valid spheronetId first.')
mongoUtils.generateOffspring([0],0,jobData.options.popSize,function(){
res.send('ok - imported: ' + JSON.stringify(jobData) + " and bred: " + jobData.options.popSize + " offspring...")
})
})
})
app.use(express.static('public'))
//socket handling for client diagnostics.
io.sockets.on('connection',(socket) => {
connections.push(socket);
console.log(' %s sockets is connected', connections.length);
socket.on('disconnect', () => {
connections.splice(connections.indexOf(socket), 1);
});
socket.on('sending message', (message) => {
console.log('Message is received :', message);
io.sockets.emit('new message', {message: message});
});
});
server.listen(3000, function(){
console.log('monitor listening on port 3000!')
})
var monitor = {
init: function(){
}
}
udpUtils.on('message', function(thisMsg){
console.log(thisMsg)
io.sockets.emit('new message', {message: thisMsg});
})
mongoUtils.init(function(){
monitor.init()
// Specify the app to open in
opn('http://localhost:3000/monitor.html');
})