connect working

This commit is contained in:
Manuel Araoz 2014-05-09 14:35:57 -03:00
commit 55ba5e6d37
4 changed files with 24 additions and 25 deletions

View file

@ -49,12 +49,16 @@ Wallet.getRandomId = function() {
return r; return r;
}; };
Wallet.prototype.seedCopayer = function(pubKey) {
this.seededCopayerId = pubKey;
};
Wallet.prototype.connectToAll = function() { Wallet.prototype.connectToAll = function() {
var all = this.publicKeyRing.getAllCopayerIds(); var all = this.publicKeyRing.getAllCopayerIds();
this.network.connectToCopayers(all); this.network.connectToCopayers(all);
if (this.firstCopayerId) { if (this.seededCopayerId) {
this.sendWalletReady(this.firstCopayerId); this.sendWalletReady(this.seededCopayerId);
this.firstCopayerId = null; 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) { if (newCopayerId) {
this.log('#### Setting new COPAYER:', newCopayerId); this.log('#### Setting new COPAYER:', newCopayerId);
this.sendWalletId(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() { Wallet.prototype._optsToObj = function() {
var obj = { var obj = {
@ -181,9 +188,9 @@ Wallet.prototype.netStart = function() {
var self = this; var self = this;
var net = this.network; var net = this.network;
net.removeAllListeners(); 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('data', self._handleData.bind(self));
net.on('open', function() {}); // TODO
net.on('openError', function() { net.on('openError', function() {
self.log('[Wallet.js.132:openError:] GOT openError'); //TODO self.log('[Wallet.js.132:openError:] GOT openError'); //TODO
self.emit('openError'); self.emit('openError');
@ -283,7 +290,6 @@ Wallet.prototype.sendWalletReady = function(recipients) {
type: 'walletReady', type: 'walletReady',
walletId: this.id, walletId: this.id,
}); });
this.emit('walletReady');
}; };
Wallet.prototype.sendWalletId = function(recipients) { Wallet.prototype.sendWalletId = function(recipients) {

View file

@ -193,7 +193,7 @@ WalletFactory.prototype.joinCreateSession = function(secret, nickname, passphras
data.opts.passphrase = passphrase; data.opts.passphrase = passphrase;
data.opts.id = data.walletId; data.opts.id = data.walletId;
var w = self.create(data.opts); var w = self.create(data.opts);
w.firstCopayerId = s.pubKey; w.seedCopayer(s.pubKey);
return cb(null, w); return cb(null, w);
} }
}); });

View file

@ -5,7 +5,7 @@ var bitcore = require('bitcore');
var util = bitcore.util; var util = bitcore.util;
/* /*
* Emits * Emits
* 'networkChange' * 'connect'
* when network layout has change (new/lost peers, etc) * when network layout has change (new/lost peers, etc)
* *
* 'data' * 'data'
@ -117,7 +117,7 @@ Network.prototype._deletePeer = function(peerId) {
Network.prototype._onClose = function(peerId) { Network.prototype._onClose = function(peerId) {
this._deletePeer(peerId); this._deletePeer(peerId);
this._notifyNetworkChange(); this.emit('disconnect');
}; };
Network.prototype.connectToCopayers = function(copayerIds) { Network.prototype.connectToCopayers = function(copayerIds) {
@ -128,8 +128,7 @@ Network.prototype.connectToCopayers = function(copayerIds) {
if (this.allowedCopayerIds && !this.allowedCopayerIds[copayerId]) { if (this.allowedCopayerIds && !this.allowedCopayerIds[copayerId]) {
console.log('### IGNORING STRANGE COPAYER:', copayerId); console.log('### IGNORING STRANGE COPAYER:', copayerId);
this._deletePeer(this.peerFromCopayer(copayerId)); this._deletePeer(this.peerFromCopayer(copayerId));
} } else {
else {
console.log('### CONNECTING TO:', copayerId); console.log('### CONNECTING TO:', copayerId);
self.connectTo(copayerId); self.connectTo(copayerId);
} }
@ -178,8 +177,7 @@ Network.prototype._onData = function(encStr, isInbound, peerId) {
console.log('#### Peer sent hello. Setting it up.'); //TODO console.log('#### Peer sent hello. Setting it up.'); //TODO
this._addConnectedCopayer(payload.copayerId, isInbound); this._addConnectedCopayer(payload.copayerId, isInbound);
this._setInboundPeerAuth(peerId, true); this._setInboundPeerAuth(peerId, true);
this._notifyNetworkChange( isInbound ? payload.copayerId : null); this.emit('connect', payload.copayerId);
this.emit('open');
return; return;
} }
@ -227,6 +225,7 @@ Network.prototype._setupConnectionHandlers = function(dataConn, toCopayerId) {
if(toCopayerId) { if(toCopayerId) {
self._addConnectedCopayer(toCopayerId); self._addConnectedCopayer(toCopayerId);
self._sendHello(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) { Network.prototype._setupPeerHandlers = function(openCallback) {
var self = this; var self = this;
var p = this.peer; var p = this.peer;

View file

@ -10,8 +10,6 @@ angular.module('copay.controllerUtils')
var vi = $rootScope.videoInfo[copayer] var vi = $rootScope.videoInfo[copayer]
if (!vi) return; if (!vi) return;
//alert($rootScope.wallet.getOnlinePeerIDs());
//alert(copayer);
if ($rootScope.wallet.getOnlinePeerIDs().indexOf(copayer) === -1) { if ($rootScope.wallet.getOnlinePeerIDs().indexOf(copayer) === -1) {
// peer disconnected, remove his video // peer disconnected, remove his video
delete $rootScope.videoInfo[copayer] delete $rootScope.videoInfo[copayer]
@ -81,7 +79,6 @@ angular.module('copay.controllerUtils')
$rootScope.$digest(); $rootScope.$digest();
}); });
w.on('refresh', function() { w.on('refresh', function() {
//alert('refresh');
root.updateBalance(function() { root.updateBalance(function() {
$rootScope.$digest(); $rootScope.$digest();
}); });
@ -93,9 +90,10 @@ angular.module('copay.controllerUtils')
}); });
}); });
w.on('openError', root.onErrorDigest); w.on('openError', root.onErrorDigest);
w.on('peer', function(peerID) { w.on('connect', function(peerID) {
//alert('peer'); if (peerID) {
video.callPeer(peerID, handlePeerVideo); video.callPeer(peerID, handlePeerVideo);
}
$rootScope.$digest(); $rootScope.$digest();
}); });
w.on('close', root.onErrorDigest); w.on('close', root.onErrorDigest);