-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdebugserver.js
56 lines (48 loc) · 1.36 KB
/
debugserver.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
var net = require('net')
var options = {
port : 5012,
host: "0.0.0.0"
}
var server = net.createServer( (sock) => {
log ("new connection: "+sock.address().address +":"+sock.address().port);
sock.setEncoding('utf8')
sock.on('data', (data) => {
handleData(sock, data)
})
sock.on('error', (err) => { log(err.toString())})
//sock.on('close', () => {})
//sock.on('end', () => {})
//sock.on('timeout', () =>{})
})
server.on ( 'error', (err) => { log(err.toString()) })
server.on ( 'listening', () => { log(JSON.stringify(server.address())) })
server.listen(options)
console.log("listening?")
var partial_data = {}
function handleData (sock, data) {
var _data = ""
if (partial_data[sock]) {
_data += partial_data[sock]
delete(partial_data[sock])
}
_data += data
var packets = _data.split(/\n/)
for (var i =0; i!=packets.length-1; ++i) {
handlePacket(packets[i])
}
// last (or only str in packets is incomplete, use it to add to subsequent data
partial_data[sock] = packets[packets.length-1]
}
var count = 0
function handlePacket(packet) {
var match = packet.match(/(\d+) ([a-f0-9:]+) (\d+) (\d+)/)
if (match) {
// "S <sampleNum> <ts> <mac> <vnox> <vred>"
log (`S ${++count} ${match[1]} ${match[2]} ${match[3]} ${match[4]}`)
} else {
log (`${packet}`)
}
}
function log (msg) {
console.log(`${Date.now()} : ${msg}`)
}