From 7a394e44d90b5dc83070bdf942685c187d9578ac Mon Sep 17 00:00:00 2001 From: Brian Cribbs Date: Mon, 9 Feb 2015 17:49:08 -0500 Subject: [PATCH 1/2] initial import of endpoint lookup stuff --- index.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/index.js b/index.js index 4215f6e..43ed163 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,12 @@ 'use strict'; var Itach; var net = require('net'); +var dgram = require('dgram'); + +//used to listen to beacon datagrams +var PORT = 9131; +var ITACHBEACONADDRESS = '239.255.250.250'; +var BEACONTIMEOUTMILLIS = 60000; var next = function (cmd, cb) { cmd.shift(); @@ -10,8 +16,68 @@ var next = function (cmd, cb) { return cb(cmd); }; +var Endpoint = function(beaconXML) { + var MODEL = /.*<-Model=(.*)><-R/; + var CONFIGURL = /.*<-Config-URL=(.*)><-P/; + var pingTime; + + this.modelNumber = MODEL.exec(beaconXML)[1]; + this.configURL = CONFIGURL.exec(beaconXML)[1]; + this.ip = this.configURL.substr(this.configURL.indexOf('://') + 3); + //console.log("creating an endpoint for: " + this.modelNumber + "@" + this.ip); + +}; +Endpoint.prototype.getIp = function() {return this.ip;}; +Endpoint.prototype.getConfigUrl = function() {return this.configURL;}; +Endpoint.prototype.setPingTime = function(timeInMillis) {this.pingTime = timeInMillis;}; +Endpoint.prototype.getPingTime = function() {return this.pingTime;}; + module.exports = Itach = function (host) { this.host = host; + this.endpoints = {}; + this.server; +}; +Itach.prototype.getEndpoints = function() {return this.endpoints;}; + +Itach.prototype.locate = function() { + this.server = dgram.createSocket('udp4'); + var _self = this; + + function checkTimeouts() { + var currentTimeMillis = new Date().getTime(); + for (var ip in _self.endpoints) { + var ep = _self.endpoints[ip]; + if (currentTimeMillis - ep.getPingTime() > BEACONTIMEOUTMILLIS) { + //console.log(ep.getIp() + " flagged for removal"); + delete _self.endpoints[ip]; + } + } + console.log(_self.endpoints); + } + + this.server.on('message', function(message) { + var ep = new Endpoint(message); + var currentTimeMillis = new Date().getTime(); + if ('' + ep.getIp() in _self.endpoints) { + var toUpdate = _self.endpoints['' + ep.getIp()]; + toUpdate.setPingTime(currentTimeMillis); + } else { + ep.setPingTime(currentTimeMillis); + _self.endpoints['' + ep.getIp()] = ep; + } + }); + + this.server.on('listening', function() { + this.setBroadcast(true); + this.addMembership(ITACHBEACONADDRESS); + }); + + this.server.bind(PORT); + setInterval(function(){checkTimeouts()}, 10000); +}; + +Itach.prototype.stopLocation = function() { + server.close(); }; Itach.prototype.send = function (command, callback) { From b67dddfd2be5918794b869a7b55bc146ad0d90db Mon Sep 17 00:00:00 2001 From: Brian Cribbs Date: Wed, 11 Feb 2015 15:36:06 -0500 Subject: [PATCH 2/2] changes to stop polling, for real --- index.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 43ed163..0121acd 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,7 @@ module.exports = Itach = function (host) { this.host = host; this.endpoints = {}; this.server; + this.interval; }; Itach.prototype.getEndpoints = function() {return this.endpoints;}; @@ -73,11 +74,13 @@ Itach.prototype.locate = function() { }); this.server.bind(PORT); - setInterval(function(){checkTimeouts()}, 10000); + this.interval = setInterval(function(){checkTimeouts()}, 10000); }; Itach.prototype.stopLocation = function() { - server.close(); + this.endpoints = {}; + this.server.close(); + clearInterval(this.interval); }; Itach.prototype.send = function (command, callback) {