-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.js
138 lines (111 loc) · 3.48 KB
/
udp.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
'use strict';
const dgram = require('dgram');
const config = require('../lib/config.js');
const watch = require('../lib/watch.js');
const cluster = require('cluster');
function input(name) {
const log = config.log.child({module: 'input/udp', input: name});
function digestConfig(config) {
var result = {
script: config.script || null,
port: config.port,
host: config.host || null,
};
return result;
}
var oldConfig = digestConfig({});
var host = null, port = null;
var script = {};
var scriptWatcher = null;
var server = null;
var _firstScript = true;
var _receivedCount = 0, _droppedCount = 0, _errorCount = 0;
function startServer(newConfig) {
if(cluster.isMaster)
return;
if(!server) {
var address = null;
server = dgram.createSocket('udp4');
server.on('listening', () => {
address = server.address();
});
server.on('error', err => {
log.error({messageId: 'input/udp/server-error', err: err}, 'UDP socket error.');
server.close();
startServer(newConfig);
});
server.on('message', (msg, rinfo) => {
_receivedCount++;
var ctx = new config.messageContext(script, {remoteAddress: rinfo.address, localPort: address.port, receiveTime: new Date()});
try {
if(script.preprocess) {
msg = script.preprocess(ctx, msg);
}
else {
msg = {remoteAddress: rinfo.address, localPort: address.port, receiveTime: ctx.meta.receiveTime, msg: msg.toString('utf8')};
}
if(!msg) {
_droppedCount++;
return;
}
}
catch(err) {
log.error({messageId: 'input/udp/preprocess-error', err: err, msg: msg}, 'Error while preparing message.');
_errorCount++;
return;
}
config.queue(ctx, msg);
});
server.bind(newConfig.port, newConfig.host || '0.0.0.0');
}
}
this.config = function takeConfig(newConfig) {
newConfig = digestConfig(newConfig);
if(oldConfig.script !== newConfig.script) {
if(scriptWatcher) {
scriptWatcher.close();
scriptWatcher = null;
}
}
if(oldConfig.host !== newConfig.host || oldConfig.port !== newConfig.port) {
// Leave all existing connections open, but reconfigure the socket
if(server) {
server.close();
server = null;
}
}
if(!scriptWatcher) {
scriptWatcher = watch.watchScript(newConfig.script);
scriptWatcher.on('change', newScript => {
(_firstScript ? log.info : log.warn).call(log, {messageId: 'input/udp/new-script', script: newConfig.script},
`Loading new script ${newConfig.script} for UDP input ${name}.`);
script = newScript;
_firstScript = false;
});
scriptWatcher.on('script-error', err => {
log.warn({messageId: 'input/udp/script-error', script: newConfig.script, err: err},
`Failed to load new script ${newConfig.script}.`)
});
}
startServer(newConfig);
oldConfig = newConfig;
}
this.getStats = function getStats() {
const result = {
receivedCount: _receivedCount,
droppedCount: _droppedCount,
errorCount: _errorCount,
};
_receivedCount = 0;
_droppedCount = 0;
_errorCount = 0;
return result;
};
this.close = function close() {
if(server) {
server.close();
server = null;
}
};
}
module.exports.create = name => new input(name);