diff --git a/app/controllers/status.js b/app/controllers/status.js index edc5051..5a509b5 100644 --- a/app/controllers/status.js +++ b/app/controllers/status.js @@ -50,3 +50,8 @@ exports.sync = function(req, res) { if (req.historicSync) res.jsonp(req.historicSync.info()); }; + +exports.peer = function(req, res) { + if (req.peerSync) + res.jsonp(req.peerSync.info()); +}; diff --git a/config/express.js b/config/express.js index 4b96d0f..ae0eae9 100644 --- a/config/express.js +++ b/config/express.js @@ -7,13 +7,17 @@ var express = require('express'), helpers = require('view-helpers'), config = require('./config'); -module.exports = function(app, historicSync) { +module.exports = function(app, historicSync, peerSync) { //custom middleware function setHistoric(req, res, next) { req.historicSync = historicSync; next(); } + function setPeer(req, res, next) { + req.peerSync = peerSync; + next(); + } app.set('showStackError', true); @@ -28,6 +32,7 @@ module.exports = function(app, historicSync) { app.enable('jsonp callback'); app.use('/api/sync', setHistoric); + app.use('/api/peer', setPeer); app.use(express.logger('dev')); app.use(express.json()); app.use(express.urlencoded()); diff --git a/config/routes.js b/config/routes.js index 317e19d..44a3591 100644 --- a/config/routes.js +++ b/config/routes.js @@ -29,6 +29,7 @@ module.exports = function(app) { app.get('/api/status', st.show); app.get('/api/sync', st.sync); + app.get('/api/peer', st.peer); // Currency var currency = require('../app/controllers/currency'); diff --git a/insight.js b/insight.js index fc0969f..92dbdd7 100644 --- a/insight.js +++ b/insight.js @@ -43,8 +43,11 @@ walk(models_path); /** * p2pSync process */ + +var peerSync = new PeerSync(); + if (!config.disableP2pSync) { - var ps = new PeerSync(); + var ps = peerSync; ps.init({ shouldBroadcast: true, }, function() { @@ -83,7 +86,7 @@ if (!config.disableHistoricSync) { //express settings -require('./config/express')(expressApp, historicSync); +require('./config/express')(expressApp, historicSync, peerSync); //Bootstrap routes require('./config/routes')(expressApp); diff --git a/lib/PeerSync.js b/lib/PeerSync.js index c565176..ae9a850 100644 --- a/lib/PeerSync.js +++ b/lib/PeerSync.js @@ -11,12 +11,12 @@ function spec() { var networks = require('bitcore/networks'); var peerdb_fn = 'peerdb.json'; - function PeerSync() {} PeerSync.prototype.init = function(opts, cb) { if (!opts) opts = {}; + this.connected = false; this.peerdb = undefined; this.allowReorgs = false; @@ -41,6 +41,12 @@ function spec() { fs.writeFileSync(peerdb_fn, JSON.stringify(this.peerdb)); }; + PeerSync.prototype.info = function() { + return { + connected: this.connected + }; + }; + PeerSync.prototype.handleInv = function(info) { var invs = info.message.invs; invs.forEach(function(inv) { @@ -98,12 +104,17 @@ function spec() { }); this.peerman.on('connection', function(conn) { + self.connected = true; conn.on('inv', self.handleInv.bind(self)); conn.on('block', self.handleBlock.bind(self)); conn.on('tx', self.handleTx.bind(self)); }); this.peerman.on('connect', self.handle_connected.bind(self)); + this.peerman.on('netDisconnected', function() { + self.connected = false; + }); + this.peerman.start(); }; diff --git a/public/src/js/controllers/connection.js b/public/src/js/controllers/connection.js index b30850a..2c3198a 100644 --- a/public/src/js/controllers/connection.js +++ b/public/src/js/controllers/connection.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('insight.connection').controller('ConnectionController', -function($scope, $window, Status, Sync, getSocket) { +function($scope, $window, Status, getSocket, PeerSync) { // Set initial values $scope.apiOnline = true; @@ -21,9 +21,9 @@ function($scope, $window, Status, Sync, getSocket) { // Check for the api connection $scope.getConnStatus = function() { - Sync.get({}, - function(sync) { - $scope.apiOnline = (sync.status !== 'aborted' && sync.status !== 'error'); + PeerSync.get({}, + function(peer) { + $scope.apiOnline = peer.connected; }, function() { $scope.apiOnline = false; diff --git a/public/src/js/services/status.js b/public/src/js/services/status.js index e8f0f24..307d66d 100644 --- a/public/src/js/services/status.js +++ b/public/src/js/services/status.js @@ -3,11 +3,15 @@ angular.module('insight.status') .factory('Status', function($resource) { - return $resource('/api/status', { - q: '@q' - }); - }) + return $resource('/api/status', { + q: '@q' + }); + }) .factory('Sync', function($resource) { return $resource('/api/sync'); - }); + }) + .factory('PeerSync', + function($resource) { + return $resource('api/peer'); + });