From 1e64031ec3eaf1284b3723d2661f42f42939cb2f Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Thu, 17 Apr 2014 16:27:15 -0300 Subject: [PATCH] options for running with local peerJs server. Ux fixes --- index.html | 11 +++++++++-- js/config.js | 7 ++++++- js/controllers/header.js | 6 ++---- js/controllers/signin.js | 6 ++++-- js/models/core/Wallet.js | 30 +++++++++++++++++----------- js/models/network/WebRTC.js | 20 +++++++++++++++---- js/services/controllerUtils.js | 36 +++++++++++++++++++++++++++------- 7 files changed, 85 insertions(+), 31 deletions(-) diff --git a/index.html b/index.html index 7a81329cb..6b106937e 100644 --- a/index.html +++ b/index.html @@ -52,11 +52,18 @@ -
+

NOTE: Your wallet is not complete yet. - {{$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers() }} keys are missing. Ask your copayers to join your session: {{$root.wallet.network.peerId}} + + {{$root.wallet.publicKeyRing.totalCopayers - $root.wallet.publicKeyRing.registeredCopayers() }} keys are + + + One key is + + +missing. Ask your copayers to join your session: {{$root.wallet.network.peerId}}

diff --git a/js/config.js b/js/config.js index c60630981..2b3907516 100644 --- a/js/config.js +++ b/js/config.js @@ -3,7 +3,12 @@ var config = { networkName: 'testnet', network: { - apiKey: 'lwjd5qra8257b9', +// apiKey: 'lwjd5qra8257b9', + // This is for running local peerJs with params: ./peerjs -p 10009 -k 'sdfjhwefh' + apiKey: 'sdfjhwefh', + host: 'localhost', + port: 10009, + path: '/', maxPeers: 3, debug: 3, }, diff --git a/js/controllers/header.js b/js/controllers/header.js index 1ac0931b2..d6680e76b 100644 --- a/js/controllers/header.js +++ b/js/controllers/header.js @@ -1,7 +1,7 @@ 'use strict'; angular.module('copay.header').controller('HeaderController', - function($scope, $rootScope, $location, walletFactory) { + function($scope, $rootScope, $location, walletFactory, controllerUtils) { $scope.menu = [{ 'title': 'Home', 'icon': 'fi-home', @@ -39,9 +39,7 @@ angular.module('copay.header').controller('HeaderController', var w = $rootScope.wallet; if (w) { w.disconnect(); - delete $rootScope['wallet']; - $rootScope.totalBalance = 0; - $location.path('signin'); + controllerUtils.logout(); } }; diff --git a/js/controllers/signin.js b/js/controllers/signin.js index 357cc7acc..260fbb07b 100644 --- a/js/controllers/signin.js +++ b/js/controllers/signin.js @@ -26,10 +26,12 @@ angular.module('copay.signin').controller('SigninController', }; $scope.join = function(cid) { -console.log('[signin.js.42:join:]'); //TODO $scope.loading = true; + walletFactory.network.on('openError', function() { + controllerUtils.onError($scope); + $rootScope.$digest(); + }); walletFactory.connectTo(cid, function(w) { -console.log('[signin.js.50]'); //TODO controllerUtils.setupUxHandlers(w); w.netStart(); }); diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index b30de0771..54b49ae69 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -96,8 +96,11 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) { Wallet.prototype._handleData = function(senderId, data, isInbound) { - if (this.id !== data.walletId) - throw new Error('wrong message received: Bad wallet ID'); + if (this.id !== data.walletId) { + this.emit('badMessage',senderId); + this.log('badMessage FROM:', senderId); //TODO + return; + } this.log('[Wallet.js.98]' , data.type); //TODO @@ -108,18 +111,16 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) { case 'txProposals': this._handleTxProposals(senderId, data, isInbound); break; - case 'abort': - this.emit('abort'); - break; } }; Wallet.prototype._handleNetworkChange = function(newPeer) { - if (!newPeer) return; - this.log('#### Setting new PEER:', newPeer); - this.sendWalletId(newPeer); - this.sendPublicKeyRing(newPeer); - this.sendTxProposals(newPeer); + if (newPeer) { + this.log('#### Setting new PEER:', newPeer); + this.sendWalletId(newPeer); + this.sendPublicKeyRing(newPeer); + this.sendTxProposals(newPeer); + } this.emit('refresh'); }; @@ -129,7 +130,13 @@ Wallet.prototype.netStart = function() { net.on('networkChange', self._handleNetworkChange.bind(self) ); net.on('data', self._handleData.bind(self) ); net.on('open', function() {}); // TODO - net.on('close', function() {}); // TODO + net.on('openError', function() { +console.log('[Wallet.js.132:openError:] GOT openError'); //TODO + self.emit('openError'); + }); + net.on('close', function() { + self.emit('close'); + }); net.start(function(peerId) { self.emit('created'); }); @@ -185,6 +192,7 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) { this.emit('publicKeyRingUpdated', this.publicKeyRing); }; + Wallet.prototype.generateAddress = function() { var addr = this.publicKeyRing.generateAddress(); this.store(); diff --git a/js/models/network/WebRTC.js b/js/models/network/WebRTC.js index e61fbf2bf..d4d5728b7 100644 --- a/js/models/network/WebRTC.js +++ b/js/models/network/WebRTC.js @@ -21,6 +21,12 @@ function Network(opts) { this.apiKey = opts.apiKey || 'lwjd5qra8257b9'; this.debug = opts.debug || 3; this.maxPeers = opts.maxPeers || 5; + + // For using your own peerJs server + this.port = opts.port; + this.host = opts.host; + this.path = opts.path; + this.connectedPeers = []; } @@ -150,14 +156,17 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) { dataConn.on('error', function(e) { console.log('### DATA ERROR',e ); //TODO + self.emit('openError'); }); dataConn.on('close', function() { if (self.closing) return; - self.closing=1; console.log('### CLOSE RECV FROM:', dataConn.peer); self._onClose(dataConn.peer); - this.emit('close'); + if (! isInbound) { +console.log('[WebRTC.js.163] EMIT openError'); //TODO + self.emit('openError'); + } }); }; @@ -182,7 +191,7 @@ Network.prototype._setupPeerHandlers = function(openCallback) { self.peer.disconnect(); self.peer.destroy(); self.peer = null; - this.emit('abort'); + self.emit('openError'); }); p.on('connection', function(dataConn) { @@ -207,6 +216,9 @@ Network.prototype.start = function(openCallback) { this.peer = new Peer(this.peerId, { key: this.apiKey, // TODO: we need our own PeerServer KEY (http://peerjs.com/peerserver) + host: this.host, + port: this.port, + path: this.path, debug: this.debug, }); @@ -234,6 +246,7 @@ Network.prototype._sendToOne = function(peerId, data, cb) { Network.prototype.send = function(peerIds, data, cb) { var self=this; +console.log('[WebRTC.js.242] SENDING ', data.type); //TODO if (!peerIds) { peerIds = this.connectedPeers; data.isBroadcast = 1; @@ -270,7 +283,6 @@ Network.prototype.connectTo = function(peerId) { Network.prototype.disconnect = function(cb) { var self = this; self.closing = 1; - this.send(null, { type: 'disconnect' }, function() { self.connectedPeers = []; self.peerId = null; diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index 7167381d0..1c5cde907 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -2,7 +2,32 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($rootScope, $location, Socket) { var root = {}; + + root.logout = function(scope) { + delete $rootScope['wallet']; + $rootScope.totalBalance = 0; + $location.path('signin'); + }; + + root.onError = function(scope) { + if (scope) scope.loading = false; + $rootScope.flashMessage = {type:'error', message: 'Could not connect to peer'}; + root.logout(); + } + + + root.onErrorDigest = function(scope) { + root.onError(scope); + $rootScope.$digest(); + } + + root.setupUxHandlers = function(w) { + + w.on('badMessage', function(peerId) { + $rootScope.flashMessage = {type:'error', message: 'Received wrong message from peer id:' + peerId}; + }); + w.on('created', function() { $location.path('peer'); $rootScope.wallet = w; @@ -14,14 +39,11 @@ angular.module('copay.controllerUtils').factory('controllerUtils', function ($ro }); }); w.on('refresh', function() { - console.log('[controllerUtils.js] RECEIVED REFRESH'); //TODO - }); - - w.on('openError', function(){ - $scope.loading = false; - $rootScope.flashMessage = {type:'error', message: 'Wallet not found'}; - $location.path('signin'); + console.log('[controllerUtils.js] Refreshing'); //TODO + $rootScope.$digest(); }); + w.on('openError', root.onErrorDigest); + w.on('close', root.onErrorDigest); }; root.handleTransactionByAddress = function(scope) {