-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmac.js
More file actions
93 lines (78 loc) · 2.65 KB
/
mac.js
File metadata and controls
93 lines (78 loc) · 2.65 KB
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
'use strict';
var util = require('util');
var base = require('./base');
const {getFloatOrUnknown} = require('../utils');
/**
* @module ping/parser/mac
*/
/**
* @class MacParser
* @param {string} addr - Hostname or ip addres
* @param {import('../index').PingConfig} config - Config object in probe()
*/
function MacParser(addr, config) {
base.call(this, addr, config);
}
util.inherits(MacParser, base);
/**
* Process output's header
* @param {string} line - A line from system ping
*/
MacParser.prototype._processHeader = function (line) {
// Get host and numeric_host
var tokens = line.split(' ');
this._response.host = tokens[1];
this._response.numeric_host = tokens[2].slice(1, -2);
this._changeState(base.STATES.BODY);
};
/**
* Process output's body
* @param {string} line - A line from system ping
*/
MacParser.prototype._processBody = function (line) {
// XXX: Assume there is at least 3 '=' can be found
var count = (line.match(/=/g) || []).length;
if (count >= 3) {
var regExp = /([0-9.]+)[ ]*ms/;
var match = regExp.exec(line);
// XXX: This option is only provided in linux builder, but the parser is shared.
// This option should have no effect on macOS,
// as ping on macOS does not accept replies from a different address
if (!this._pingConfig.ignoreDifferentAddressReply || line.includes(this._response.numeric_host)) {
this._times.push(getFloatOrUnknown(match[1]));
}
}
// Change state if it see a '---'
if (line.indexOf('---') >= 0) {
this._changeState(base.STATES.FOOTER);
}
};
/**
* Process output's footer
* @param {string} line - A line from system ping
*/
MacParser.prototype._processFooter = function (line) {
var packetLoss = line.match(/ ([\d.]+(\.?[\d]*))%/);
if (packetLoss) {
this._response.packetLoss = getFloatOrUnknown(packetLoss[1]);
}
// XXX: Assume number of keywords '/' more than 3
var count = (line.match(/[/]/g) || []).length;
if (count >= 3) {
var regExp = /(([0-9.]+)|nan)/g;
// XXX: Assume min avg max stddev
var m1 = regExp.exec(line);
var m2 = regExp.exec(line);
var m3 = regExp.exec(line);
var m4 = regExp.exec(line);
if (m1 && m2 && m3 && m4) {
this._response.min = getFloatOrUnknown(m1[1]);
this._response.avg = getFloatOrUnknown(m2[1]);
this._response.max = getFloatOrUnknown(m3[1]);
this._response.stddev = getFloatOrUnknown(m4[1]);
this._changeState(base.STATES.END);
}
this._changeState(base.STATES.END);
}
};
module.exports = MacParser;