From 55ba5e6d3719149e0ee61a1d13d14b2cf202d0b5 Mon Sep 17 00:00:00 2001 From: Manuel Araoz Date: Fri, 9 May 2014 14:35:57 -0300 Subject: [PATCH] connect working --- js/models/core/Wallet.js | 22 ++++++++++++++-------- js/models/core/WalletFactory.js | 2 +- js/models/network/WebRTC.js | 15 +++++---------- js/services/controllerUtils.js | 10 ++++------ 4 files changed, 24 insertions(+), 25 deletions(-) diff --git a/js/models/core/Wallet.js b/js/models/core/Wallet.js index d60828c81..a906c20ed 100644 --- a/js/models/core/Wallet.js +++ b/js/models/core/Wallet.js @@ -49,12 +49,16 @@ Wallet.getRandomId = function() { return r; }; +Wallet.prototype.seedCopayer = function(pubKey) { + this.seededCopayerId = pubKey; +}; + Wallet.prototype.connectToAll = function() { var all = this.publicKeyRing.getAllCopayerIds(); this.network.connectToCopayers(all); - if (this.firstCopayerId) { - this.sendWalletReady(this.firstCopayerId); - this.firstCopayerId = null; + if (this.seededCopayerId) { + this.sendWalletReady(this.seededCopayerId); + this.seededCopayerId = null; } }; @@ -121,14 +125,17 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) { } }; -Wallet.prototype._handleNetworkChange = function(newCopayerId) { +Wallet.prototype._handleConnect = function(newCopayerId) { if (newCopayerId) { this.log('#### Setting new COPAYER:', newCopayerId); this.sendWalletId(newCopayerId); - this.emit('peer', this.network.peerFromCopayer(newCopayerId)); } + var peerID = this.network.peerFromCopayer(newCopayerId) + this.emit('connect', peerID); }; +Wallet.prototype._handleDisconnect = function(copayerID) { +}; Wallet.prototype._optsToObj = function() { var obj = { @@ -181,9 +188,9 @@ Wallet.prototype.netStart = function() { var self = this; var net = this.network; net.removeAllListeners(); - net.on('networkChange', self._handleNetworkChange.bind(self)); + net.on('connect', self._handleConnect.bind(self)); + net.on('disconnect', self._handleDisconnect.bind(self)); net.on('data', self._handleData.bind(self)); - net.on('open', function() {}); // TODO net.on('openError', function() { self.log('[Wallet.js.132:openError:] GOT openError'); //TODO self.emit('openError'); @@ -283,7 +290,6 @@ Wallet.prototype.sendWalletReady = function(recipients) { type: 'walletReady', walletId: this.id, }); - this.emit('walletReady'); }; Wallet.prototype.sendWalletId = function(recipients) { diff --git a/js/models/core/WalletFactory.js b/js/models/core/WalletFactory.js index fc59b188d..4f110a279 100644 --- a/js/models/core/WalletFactory.js +++ b/js/models/core/WalletFactory.js @@ -193,7 +193,7 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras data.opts.passphrase = passphrase; data.opts.id = data.walletId; var w = self.create(data.opts); - w.firstCopayerId = s.pubKey; + w.seedCopayer(s.pubKey); return cb(null, w); } }); diff --git a/js/models/network/WebRTC.js b/js/models/network/WebRTC.js index 6f9eeaefd..59f520935 100644 --- a/js/models/network/WebRTC.js +++ b/js/models/network/WebRTC.js @@ -5,7 +5,7 @@ var bitcore = require('bitcore'); var util = bitcore.util; /* * Emits - * 'networkChange' + * 'connect' * when network layout has change (new/lost peers, etc) * * 'data' @@ -117,7 +117,7 @@ Network.prototype._deletePeer = function(peerId) { Network.prototype._onClose = function(peerId) { this._deletePeer(peerId); - this._notifyNetworkChange(); + this.emit('disconnect'); }; Network.prototype.connectToCopayers = function(copayerIds) { @@ -128,8 +128,7 @@ Network.prototype.connectToCopayers = function(copayerIds) { if (this.allowedCopayerIds && !this.allowedCopayerIds[copayerId]) { console.log('### IGNORING STRANGE COPAYER:', copayerId); this._deletePeer(this.peerFromCopayer(copayerId)); - } - else { + } else { console.log('### CONNECTING TO:', copayerId); self.connectTo(copayerId); } @@ -178,8 +177,7 @@ Network.prototype._onData = function(encStr, isInbound, peerId) { console.log('#### Peer sent hello. Setting it up.'); //TODO this._addConnectedCopayer(payload.copayerId, isInbound); this._setInboundPeerAuth(peerId, true); - this._notifyNetworkChange( isInbound ? payload.copayerId : null); - this.emit('open'); + this.emit('connect', payload.copayerId); return; } @@ -227,6 +225,7 @@ Network.prototype._setupConnectionHandlers = function(dataConn, toCopayerId) { if(toCopayerId) { self._addConnectedCopayer(toCopayerId); self._sendHello(toCopayerId); + self.emit('connect', toCopayerId); // TODO: try to unify both 'connect' emits } } }); @@ -251,10 +250,6 @@ Network.prototype._setupConnectionHandlers = function(dataConn, toCopayerId) { }); }; -Network.prototype._notifyNetworkChange = function(newCopayerId) { - this.emit('networkChange', newCopayerId); -}; - Network.prototype._setupPeerHandlers = function(openCallback) { var self = this; var p = this.peer; diff --git a/js/services/controllerUtils.js b/js/services/controllerUtils.js index f155de2c5..76302af12 100644 --- a/js/services/controllerUtils.js +++ b/js/services/controllerUtils.js @@ -10,8 +10,6 @@ angular.module('copay.controllerUtils') var vi = $rootScope.videoInfo[copayer] if (!vi) return; - //alert($rootScope.wallet.getOnlinePeerIDs()); - //alert(copayer); if ($rootScope.wallet.getOnlinePeerIDs().indexOf(copayer) === -1) { // peer disconnected, remove his video delete $rootScope.videoInfo[copayer] @@ -81,7 +79,6 @@ angular.module('copay.controllerUtils') $rootScope.$digest(); }); w.on('refresh', function() { - //alert('refresh'); root.updateBalance(function() { $rootScope.$digest(); }); @@ -93,9 +90,10 @@ angular.module('copay.controllerUtils') }); }); w.on('openError', root.onErrorDigest); - w.on('peer', function(peerID) { - //alert('peer'); - video.callPeer(peerID, handlePeerVideo); + w.on('connect', function(peerID) { + if (peerID) { + video.callPeer(peerID, handlePeerVideo); + } $rootScope.$digest(); }); w.on('close', root.onErrorDigest);