From ec14cc4d6744b080194e305a3d410a6901adf63b Mon Sep 17 00:00:00 2001 From: cubedro Date: Fri, 3 Apr 2015 07:00:06 +0300 Subject: [PATCH] added latency update --- app.js | 17 +++++++++++++++++ models/collection.js | 10 ++++++++++ models/node.js | 12 ++++++++++++ public/js/controllers.js | 4 ++++ 4 files changed, 43 insertions(+) diff --git a/app.js b/app.js index 7cb1d22..582914e 100644 --- a/app.js +++ b/app.js @@ -79,6 +79,23 @@ api.on('connection', function(spark) { } }); + spark.on('node-ping', function(data){ + spark.emit('node-pong'); + }); + + spark.on('latency', function(data) + { + console.log('Latency: ', data.latency); + console.log('got ping from ' + spark.id); + + if(typeof data.id !== 'undefined') + { + var stats = Nodes.updateLatency(data.id, data.latency); + + client.write({action: 'latency', data: stats}); + } + }); + spark.on('end', function(data) { var stats = Nodes.inactive(spark.id); diff --git a/models/collection.js b/models/collection.js index 7d38788..b945693 100644 --- a/models/collection.js +++ b/models/collection.js @@ -26,6 +26,16 @@ Collection.prototype.update = function(id, stats) return node.setStats(stats); } +Collection.prototype.updateLatency = function(id, latency) +{ + var node = this.getNode({ id: id }); + + if(!node) + return false; + + return node.setLatency(latency); +} + Collection.prototype.inactive = function(id) { var node = this.getNode({ spark: id }); diff --git a/models/node.js b/models/node.js index b54ea87..1830e17 100644 --- a/models/node.js +++ b/models/node.js @@ -104,6 +104,18 @@ Node.prototype.setStats = function(stats) return false; } +Node.prototype.setLatency = function(latency) +{ + if(typeof latency !== 'undefined') + { + this.stats.latency = latency; + + return { id: this.id, latency: latency }; + } + + return false; +} + Node.prototype.getStats = function() { return {id: this.id, stats: this.stats}; diff --git a/public/js/controllers.js b/public/js/controllers.js index ee46b70..4f0c0e6 100644 --- a/public/js/controllers.js +++ b/public/js/controllers.js @@ -85,6 +85,10 @@ function StatsCtrl($scope, $filter, socket, _, toastr) { $scope.nodes[findIndex({id: data.id})].stats = data.stats; toastr['error']("Node "+ $scope.nodes[findIndex({id: data.id})].info.name +" went away!", "Node connection was lost!"); break; + + case "latency": + $scope.nodes[findIndex({id: data.id})].stats.latency = data.latency; + break; } updateStats();