-
Notifications
You must be signed in to change notification settings - Fork 0
/
udpserver.js
46 lines (34 loc) · 992 Bytes
/
udpserver.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
var udp = require('dgram');
var ringBuffers = require('./ringbuffers');
var RingBuffer = require('./ringbuffer');
module.exports.createServer = function(options) {
return new LogplexUDPServer(options);
};
function LogplexUDPServer(options) {
var self = this;
self.start = function() {
self.server = udp.createSocket('udp4');
self.server.bind(options.port);
self.server.on('message', function(raw,meta) {
var msg = raw.toString();
var ts = msg.substr(0,13);
var token = msg.substr(14,36);
var logmsg = msg.substring(51, msg.length -1); //remove trailing \n
var rb = ringBuffers[token];
if(!rb) {
console.log('creating ringbuffer for ' + token);
ringBuffers[token] = new RingBuffer(500);
rb = ringBuffers[token];
}
var entry = {
ts: +(ts),
token: token,
msg: logmsg
};
rb.add(entry);
});
};
self.stop = function() {
self.server.close();
};
}