Skip to content

Commit

Permalink
make the mixer connect to the CoinJoin channel in the lobby if it's i…
Browse files Browse the repository at this point in the history
…n mixing state.
  • Loading branch information
caedesvvv committed Apr 8, 2014
1 parent 8618a6d commit 5d986ca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 25 deletions.
21 changes: 14 additions & 7 deletions js/backend/services/lobby.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ function(Services, Transport, Channel) {

function LobbyService(core) {
var lobbyTransport;
var self = this;

// Transport service managing background lobby transport
Services.start('lobby',
Expand All @@ -18,14 +19,20 @@ function(Services, Transport, Channel) {
}, function(port) {
// Connected
console.log('bus: lobby client connected');
if (!lobbyTransport) {
console.log('init lobby transport');
var identity = core.getCurrentIdentity();
lobbyTransport = new Transport(identity, core.getObeliskClient());
lobbyTransport.update = function() { Services.post('gui', {'type': 'update'}) };
}

// Ensure the lobby transport is created
self.getLobbyTransport();
});
this.getLobbyTransport = function() { return lobbyTransport; }

this.getLobbyTransport = function() {
if (!lobbyTransport) {
console.log('[lobby] init lobby transport');
var identity = core.getCurrentIdentity();
lobbyTransport = new Transport(identity, core.getObeliskClient());
lobbyTransport.update = function() { Services.post('gui', {'type': 'update'}) };
}
return lobbyTransport;
}
}

return LobbyService;
Expand Down
61 changes: 43 additions & 18 deletions js/backend/services/mixer.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,60 @@
define(['backend/services'],
function(Services) {
define(['backend/services', 'backend/channels/catchan'],
function(Services, Channel) {
'use strict';

/*
* Service managing mixing.
* @constructor
*/
function MixerService(core) {
var self = this;
this.core = core;

// Port for communication with other services
Services.connect('obelisk', function(data) {
// WakeUp when connected to obelisk
if (data.type == 'connected') {
var client = core.getClient();
var identity = core.getCurrentIdentity();

// Check to see we have anything to mix
var anyMixing = false;
identity.wallet.pockets.forEach(function(pocket) {
if (pocket.mixing) {
anyMixing = true;
}
});

// If any is mixing make sure we are connected
if (anyMixing) {
self.connectMixer();
}
self.onConnect(data);
}

});
}

MixerService.prototype.connectMixer = function() {
/*
* React to a new obelisk connection
*/
MixerService.prototype.onConnect = function() {
var client = this.core.getClient();
var identity = this.core.getCurrentIdentity();

// Check to see we have anything to mix
var anyMixing = false;
identity.wallet.pockets.forEach(function(pocket) {
if (pocket.mixing) {
anyMixing = true;
}
});

// If any is mixing make sure we are connected
if (anyMixing) {
this.ensureMixing();
}
}

/*
* Initialize the mixer connection
*/
MixerService.prototype.ensureMixing = function() {
console.log("[mixer] Connect...");
var lobbyTransport = this.core.getLobbyTransport();
var channel = lobbyTransport.initChannel('CoinJoin', Channel);
channel.addCallback('lobby', function(data) { self.onLobbyMessage(data); })
}

/*
* Lobby message arrived
*/
MixerService.prototype.onLobbyMessage = function(data) {
}

return MixerService;
Expand Down

0 comments on commit 5d986ca

Please sign in to comment.