This repository was archived by the owner on Sep 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
105 lines (90 loc) · 2.6 KB
/
index.js
File metadata and controls
105 lines (90 loc) · 2.6 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
94
95
96
97
98
99
100
101
102
103
104
var ping = require('net-ping');
var events = require('events');
var _ = require('underscore');
var ejs = require('ejs');
var fs = require('fs');
var Plugin = function() {};
Plugin.prototype = new events.EventEmitter;
Plugin.prototype.NAME = "ednt-plugin-ping";
Plugin.prototype.FRIENDLY_NAME = "ping";
Plugin.prototype.VERSION = "0.0.1";
Plugin.prototype.MOUNTPOINT = "ping";
Plugin.prototype.SU_REQUIRED = true;
Plugin.prototype.ROUTES = {
"/": {
"method": "GET",
"params": {
"target": {
"mandatory": true,
"type": "String"
},
"count": {
"type": "Integer",
"default": 5
},
"interval": {
"type": "Integer",
"default": 1000
},
"timeout": {
"type": "Integer"
}
}
}
};
Plugin.prototype.init = function(options) {
this.session = ping.createSession();
this.session.on('error', function(e) {
console.log('session error');
console.dir(e);
});
return true;
}
Plugin.prototype.newRequest = function(params) {
var self = this;
params['count'] = params['count'] || this.ROUTES['/'].params.count.default;
params['interval'] = params['interval'] || this.ROUTES['/'].params.interval.default;
if(params['target'] === undefined || params['target'] === '') {
self.emit('output', null, self.getView());
self.emit('end');
}
var count = params['count'];
self.emit('output', null, 'test');
self.ping(params, count, function(err) {
self.emit('end', null);
});
}
Plugin.prototype.ping = function(params, i, callback) {
var self = this;
var delay = (i === params['count']) ? 0 : params['interval'];
var nextCallback = _.bind(self.ping, this);
var nextPing = _.bind(self.executePing, this);
i--;
if(i < 0) callback();
else setTimeout(nextPing, delay, params, i, callback, nextCallback);
}
Plugin.prototype.executePing = function(params, i, finalCallback, callback) {
var self = this;
this.session.pingHost (params['target'], function (err, target, sent, rcvd) {
var ms = rcvd - sent;
if (err)
result = target + ": " + err.toString ();
else
result = target + ": Alive (ms=" + ms + ")";
//callback(null, result);
//console.log(result);
self.emit('output', null, result);
callback(params, i, finalCallback);
});
}
Plugin.prototype.cancelRequest = function(requestId) {
}
Plugin.prototype.getView = function(viewName) {
viewName = viewName || "default";
switch(viewName) {
case "default":
return ejs.render(fs.readFileSync(__dirname+'/views/index.ejs', 'utf8'), null);
break;
}
}
module.exports = new Plugin();