-
Notifications
You must be signed in to change notification settings - Fork 23
/
Sniffer.js
74 lines (48 loc) · 1.25 KB
/
Sniffer.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
var _ = require('underscore'),
util = require('util'),
path = require('path'),
spawn = require('child_process').spawn,
EventEmitter = require('events').EventEmitter;
module.exports = Sniffer;
Sniffer.SCRIPT = 'build/RFSniffer';
Sniffer.process = null;
Sniffer.instance = null;
function Sniffer(options) {
EventEmitter.call(this);
//Launch Sniffer
Sniffer.process = spawn(
path.join(__dirname, Sniffer.SCRIPT), [
'--pin', options.pin
]);
//Emit data received
Sniffer.process.stdout.on(
'data',
_.debounce(
this.onData.bind(this),
options.debounceDelay,
true
)
);
//Emit error messages
Sniffer.process.stderr.on(
'data',
this.onError.bind(this)
);
};
util.inherits(Sniffer, EventEmitter);
Sniffer.getInstance = function(options) {
return Sniffer.instance || (Sniffer.instance = new Sniffer(options));
};
Sniffer.prototype.onError = function (error) {
this.emit('error', error);
};
Sniffer.prototype.onData = function (buffer) {
this.emit('data', JSON.parse(buffer));
};
//Kill sniffer process when exit
process.on('SIGINT', function() {
if(!!Sniffer.process) {
Sniffer.process.kill();
}
process.exit();
});