Merge pull request #276 from maraoz/bug/bitcoind-error-message

bitcoind connection status derived from PeerSync
This commit is contained in:
Gustavo Maximiliano Cortez 2014-02-12 18:40:35 -02:00
commit 78aed173fa
7 changed files with 42 additions and 13 deletions

View File

@ -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());
};

View File

@ -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());

View File

@ -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');

View File

@ -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);

View File

@ -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();
};

View File

@ -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;

View File

@ -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');
});