diff --git a/app.js b/app.js index 88614cd..bb3b348 100644 --- a/app.js +++ b/app.js @@ -1,19 +1,73 @@ var express = require('express.io'); var path = require('path'); var favicon = require('serve-favicon'); -var logger = require('morgan'); var bodyParser = require('body-parser'); -var fs = require('fs'); -var config; - -if(fs.existsSync('./config/nodes.js')){ - config = require('./config/nodes'); -} else { - config = require('./config/nodes.default'); -} - var Node = require('./models/node'); +var Primus = require('primus'), + http = require('http'), + server, + api, + client; + +var Collection = require('./models/collection'); + +var Nodes = new Collection(); + +server = http.createServer(); +api = new Primus(server, { + transformer: 'websockets', + pathname: '/api', + parser: 'JSON' +}); + +api.use('emit', require('primus-emit')); + +api.on('connection', function(spark) { + console.log(spark.id); + console.log(spark.address); + console.log(spark.query); + + spark.on('hello', function(data){ + console.log('got hello data from ' + spark.id); + console.log(data); + Nodes.add(data); + }); + + spark.on('update', function(data){ + console.log('got update from ' + spark.id); + console.log(data); + }); +}); + +var client = new Primus(server, { + transformer: 'websockets', + pathname: '/primus', + parser: 'JSON' +}); + +client.use('emit', require('primus-emit')); + +client.on('connection', function(spark) { + console.log(spark); + + console.log(spark.id); + console.log(spark.headers); + console.log(spark.address); + console.log(spark.query); + spark.write({give: 'identity'}); +}); + +server.listen(process.env.API_PORT || 3000); + +// if(fs.existsSync('./config/nodes.js')){ +// config = require('./config/nodes'); +// } else { +// config = require('./config/nodes.default'); +// } + +// var Node = require('./models/node'); + var app = express(); app.http().io(); @@ -23,37 +77,36 @@ app.set('view engine', 'jade'); // uncomment after placing favicon in /public //app.use(favicon(__dirname + '/public/favicon.ico')); -app.use(logger('dev')); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(express.static(path.join(__dirname, 'public'))); -var nodes = [], - nodeStatus = [], - nodeInterval; +// var nodes = [], +// nodeStatus = [], +// nodeInterval; -for(i in config) { - nodes[i] = new Node(config[i], i); - console.log(nodes[i]); - nodeStatus[i] = nodes[i].update(); -} +// for(i in config) { +// nodes[i] = new Node(config[i], i); +// console.log(nodes[i]); +// nodeStatus[i] = nodes[i].update(); +// } -nodeInterval = setInterval(function(){ - for(i in nodes){ - app.io.broadcast('update', nodes[i].update()); - } -}, 10000); +// nodeInterval = setInterval(function(){ +// for(i in nodes){ +// app.io.broadcast('update', nodes[i].update()); +// } +// }, 10000); -app.get('/', function(req, res) { - res.render('index', { title: 'Ethereum Network Status' }); -}); +// app.get('/', function(req, res) { +// res.render('index', { title: 'Ethereum Network Status' }); +// }); -app.io.route('ready', function(req) { - req.io.emit('init', { - nodes: nodeStatus - }); - console.log('emited'); -}); +// app.io.route('ready', function(req) { +// req.io.emit('init', { +// nodes: nodeStatus +// }); +// console.log('emited'); +// }); // catch 404 and forward to error handler app.use(function(req, res, next) { diff --git a/models/collection.js b/models/collection.js new file mode 100644 index 0000000..b18becb --- /dev/null +++ b/models/collection.js @@ -0,0 +1,53 @@ +var _ = require('lodash'); +var Node = require('./node'); + +var Collection = function Collection() +{ + this._list = []; + + return this; +} + +Collection.prototype.add = function(data) +{ + if(typeof data.id == 'undefined') + return false; + + var node = this.getNodeOrNew({ id : data.id }); + + node.info = data.info; +} + +Collection.prototype.get = function(id) +{ + return this.getNode(id); +} + +Collection.prototype.getIndex = function(search) +{ + return _.findIndex(this._list, search); + + return (index >= 0 ? index : false); +} + +Collection.prototype.getNode = function(search) +{ + var index = this.getIndex(search); + + if(index) + return this._list[index]; + + return false; +} + +Collection.prototype.getIndexOrNew = function(search) +{ + var index = this.getIndex(search) || this._list.push(new Node()); +} + +Collection.prototype.getNodeOrNew = function(search) +{ + return this.getNode(this.getIndexOrNew(search)); +} + +module.exports = Collection; \ No newline at end of file diff --git a/models/node.js b/models/node.js index 7acacdd..d69bea1 100644 --- a/models/node.js +++ b/models/node.js @@ -1,68 +1,34 @@ var geoip = require('geoip-lite'); -var Node = function Node(options, id) +var Node = function Node() { - this.options = options; - this.info = { - name: options.name, - ip: options.rpcHost, - type: options.type, - os: options.os - }; - - this.info.geo = geoip.lookup(this.info.ip); - this.info.id = parseInt(id); - this.info.stats = { + this.id = null; + this.info = {}; + this.geo = {} + this.stats = { active: false, - peers: 0, + listening: false, mining: false, - block: { - number: 0, - hash: '?', - timestamp: 0 - }, + peers: 0, + pending: 0, + gasPrice: 0, + block: {}, + blocktimeAvg: 0, + difficulty: [], uptime: { down: 0, inc: 0, total: 0 - } - } - - this.web3 = require('ethereum.js'); + }, + lastUpdate: 0 + }; return this; } -Node.prototype.update = function() +Node.prototype.setGeo = function() { - var sock = new this.web3.providers.HttpSyncProvider('http://' + this.options.rpcHost + ':' + this.options.rpcPort); - this.web3.setProvider(sock); - - var eth = this.web3.eth; - - try { - this.info.stats.peers = eth.peerCount; - } - catch (err) { - this.info.stats.peers = null; - } - - if(this.info.stats.peers != null) { - this.info.stats.block = eth.block(parseInt(eth.number)); - if(this.info.stats.block.hash != '?' && typeof this.info.stats.block.difficulty !== 'undefined'){ - this.info.stats.block.difficulty = this.web3.toDecimal(this.info.stats.block.difficulty); - } - this.info.stats.mining = eth.mining; - this.info.stats.active = true; - } else { - this.info.stats.active = false; - this.info.stats.uptime.down++; - } - - this.info.stats.uptime.inc++; - this.info.stats.uptime.total = ((this.info.stats.uptime.inc - this.info.stats.uptime.down) / this.info.stats.uptime.inc) * 100; - - return this.info; -}; + this.geo = geoip.lookup(ip); +} module.exports = Node; diff --git a/package.json b/package.json index 7200f76..7d8047a 100644 --- a/package.json +++ b/package.json @@ -16,8 +16,12 @@ "express.io": "^1.1.13", "geoip-lite": "^1.1.4", "jade": "~1.6.0", + "lodash": "^3.2.0", "morgan": "~1.3.0", + "primus": "^2.4.12", + "primus-emit": "^0.1.2", "serve-favicon": "~2.1.3", - "stylus": "0.42.3" + "stylus": "0.42.3", + "ws": "^0.7.1" } }