|
1 | | -'use strict'; |
| 1 | +"use strict"; |
2 | 2 |
|
3 | | -const EventEmitter = require('events'); |
4 | | -const Metric = require('@metrics/metric'); |
5 | | -const abslog = require('abslog'); |
6 | | -const dgram = require('dgram'); |
| 3 | +const EventEmitter = require("events"); |
| 4 | +const Metric = require("@metrics/metric"); |
| 5 | +const abslog = require("abslog"); |
| 6 | +const dgram = require("dgram"); |
7 | 7 |
|
8 | 8 | const MetricsDaemonUdpd = class MetricsDaemonUdpd extends EventEmitter { |
9 | | - constructor({ port = 40400, address, logger } = {}) { |
10 | | - super(); |
11 | | - |
12 | | - Object.defineProperty(this, 'socket', { |
13 | | - value: dgram.createSocket('udp4'), |
14 | | - }); |
15 | | - |
16 | | - Object.defineProperty(this, 'address', { |
17 | | - value: address, |
18 | | - }); |
19 | | - |
20 | | - Object.defineProperty(this, 'port', { |
21 | | - value: port, |
22 | | - }); |
23 | | - |
24 | | - Object.defineProperty(this, 'log', { |
25 | | - value: abslog(logger), |
26 | | - }); |
27 | | - |
28 | | - this.socket.on('error', (error) => { |
29 | | - this.log.error(error); |
30 | | - this.socket.close(); |
31 | | - }); |
32 | | - |
33 | | - this.socket.on('message', (data, info) => { |
34 | | - try { |
35 | | - const obj = JSON.parse(data); |
36 | | - const metric = new Metric(obj); |
37 | | - this.emit('message', metric); |
38 | | - } catch (err) { |
39 | | - this.log.error(`Recieved non metric object from ${info.address}:${info.port}`); |
40 | | - } |
41 | | - }); |
42 | | - |
43 | | - this.socket.on('listening', () => { |
44 | | - const info = this.socket.address(); |
45 | | - this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`); |
46 | | - }); |
47 | | - } |
48 | | - |
49 | | - get [Symbol.toStringTag]() { |
50 | | - return 'MetricsDaemonUdpd'; |
51 | | - } |
52 | | - |
53 | | - listen() { |
54 | | - this.socket.bind(this.port, this.address); |
55 | | - } |
56 | | - |
57 | | - destroy() { |
58 | | - this.socket.close(); |
59 | | - } |
| 9 | + constructor({ port = 40400, address, logger } = {}) { |
| 10 | + super(); |
| 11 | + |
| 12 | + Object.defineProperty(this, "socket", { |
| 13 | + value: dgram.createSocket("udp4"), |
| 14 | + }); |
| 15 | + |
| 16 | + Object.defineProperty(this, "address", { |
| 17 | + value: address, |
| 18 | + }); |
| 19 | + |
| 20 | + Object.defineProperty(this, "port", { |
| 21 | + value: port, |
| 22 | + }); |
| 23 | + |
| 24 | + Object.defineProperty(this, "log", { |
| 25 | + value: abslog(logger), |
| 26 | + }); |
| 27 | + |
| 28 | + this.socket.on("error", (error) => { |
| 29 | + this.log.error(error); |
| 30 | + this.socket.close(); |
| 31 | + }); |
| 32 | + |
| 33 | + this.socket.on("message", (data, info) => { |
| 34 | + try { |
| 35 | + const obj = JSON.parse(data); |
| 36 | + const metric = new Metric(obj); |
| 37 | + this.emit("message", metric); |
| 38 | + } catch (err) { |
| 39 | + this.log.error(`Recieved non metric object from ${info.address}:${info.port}`); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + this.socket.on("listening", () => { |
| 44 | + const info = this.socket.address(); |
| 45 | + this.log.info(`Daemon listening on UDP on ${info.address}:${info.port}`); |
| 46 | + }); |
| 47 | + } |
| 48 | + |
| 49 | + get [Symbol.toStringTag]() { |
| 50 | + return "MetricsDaemonUdpd"; |
| 51 | + } |
| 52 | + |
| 53 | + listen() { |
| 54 | + this.socket.bind(this.port, this.address); |
| 55 | + } |
| 56 | + |
| 57 | + destroy() { |
| 58 | + this.socket.close(); |
| 59 | + } |
60 | 60 | }; |
61 | 61 |
|
62 | 62 | module.exports = MetricsDaemonUdpd; |
0 commit comments