Skip to content

Commit

Permalink
Added distinct L2LClients to the channels and unit tests to make sure…
Browse files Browse the repository at this point in the history
… clients are added correctly
  • Loading branch information
m-hemmings committed Jan 20, 2017
1 parent 41ff294 commit ccbb70e
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 107 deletions.
2 changes: 0 additions & 2 deletions channel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { string, num, promise, fun } from "lively.lang";
import { L2LChannel } from "lively.sync/l2lchannel.js";
import L2LClient from "lively.2lively/client.js";
var debug = false;

// -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Expand Down
96 changes: 58 additions & 38 deletions l2lchannel.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,72 @@
import L2LClient from "lively.2lively/client.js";
import L2LTracker from "lively.2lively/tracker.js";
import { string, promise } from "lively.lang";
import { string, num, promise, fun } from "lively.lang";

export class L2LChannel {
constructor(sender, master){

export class Channel {

This comment has been minimized.

Copy link
@rksm

rksm Jan 21, 2017

Member

@m-hemmings is it possible to simply inherit from the original channel instead of copying the setup code?

constructor(senderRecvrA, onReceivedMethodA, senderRecvrB, onReceivedMethodB){
if (!senderRecvrA) throw new Error("no sender / receiver a!");
if (!senderRecvrB) throw new Error("no sender / receiver b!");
if (typeof senderRecvrA[onReceivedMethodA] !== "function") throw new Error(`sender a has no receive method ${onReceivedMethodA}!`);
if (typeof senderRecvrB[onReceivedMethodB] !== "function") throw new Error(`sender b has no receive method ${onReceivedMethodB}!`);
var l2lA = Channel.makeL2LClient(),
l2lB = Channel.makeL2LClient();
this.senderRecvrA = senderRecvrA;
this.senderRecvrA.l2lclient = l2lA
this.onReceivedMethodA = onReceivedMethodA;
this.onReceivedMethodB = onReceivedMethodB;
this.senderRecvrB = senderRecvrB;
this.senderRecvrB.l2lclient = l2lB
this.queueAtoB = [];
this.queueBtoA = [];
this.delayAtoB = 0;
this.delayBtoA = 0;
this.online = false;
this.lifetime = 100;
// this._watchdogProcess = null
this.goOnline();
}

static makeMaster(hostname,port, namespace,io){
var path;
hostname ? {} : hostname = 'localhost'
port ? {} : port = '9011'
namespace ? {} : namespace = '/l2l'
io ? path = io.path() : path = '/lively-socket.io'
var origin = `http://${hostname}:${port}`,
master = new L2LClient(origin,path,namespace)
master.open();
master.register();
return master;
static makeL2LClient(hostname,port, namespace,io){
var client = L2LClient.forceNew({})
return client
}

toString() {
return `<channel ${this.senderRecvrA}.${this.onReceivedMethodA}${this.senderRecvrB}.${this.onReceivedMethodB}>`
}

isOnline() { return this.online; }
goOffline() { this.online = false; }
goOnline() { this.online = true; this.watchdogProcess(); }

goOffline() {
console.log(this)
this.online = false;
var status = {senderRecvrA : null,senderRecvrB : null}
if (this.senderRecvrA && this.senderRecvrA.l2lclient) {
this.senderRecvrA.l2lclient.remove()
console.log('senderRecvrA disconnected')
status.senderRecvrA = 'offline'
} else {
status.senderRecvrA = 'online'
console.log('senderRecvrA not disconnected');

}
if (this.senderRecvrB && this.senderRecvrB.l2lclient) {
this.senderRecvrB.l2lclient.remove()
console.log('senderRecvrB disconnected')
status.senderRecvrB = 'offline'
} else {
console.log('senderRecvrB not disconnected');
status.senderRecvrB = 'online'
}
return status
}

async goOnline() {
await this.senderRecvrA.l2lclient.whenRegistered(300)
await this.senderRecvrB.l2lclient.whenRegistered(300)

this.online = true;
this.watchdogProcess();
}

watchdogProcess() {
if (!this.isOnline() || this._watchdogProcess) return;
Expand Down Expand Up @@ -57,27 +97,7 @@ export class L2LChannel {

deliver(sender) {
}

static async create(client,master,options){
if (options){
var {hostname, port, namespace, io} = options;
}
var connection = new this(client,master,options);
await connection.master.whenRegistered(300)
connection.master.sendTo(connection.master.trackerId,'joinRoom',{roomName: connection.master.socketId.split('#')[1]})
connection.sender.sendTo(connection.sender.trackerId,'joinRoom',{roomName: connection.master.socketId.split('#')[1]})

connection.online = true;
return connection;
}

isOnline() { return this.online; }
getMembers(){ return {sender: this.sender, master: this.master}}

async close(){
this.sender.sendTo(this.sender.trackerId,'leaveRoom',{roomName: this.master.socketId.split('#')[1]})
this.master.sendTo(this.master.trackerId,'leaveRoom',{roomName: this.master.socketId.split('#')[1]})
await this.master.remove();
this.online = false;
}
}
96 changes: 29 additions & 67 deletions tests/sync-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { disconnect, disconnectAll, connect } from "lively.bindings";
import { buildTestWorld, destroyTestWorld } from "./helper.js";
import { Client } from "../client.js";
import { Master } from "../master.js";
import { L2LChannel } from "../l2lchannel.js";
import { Channel as L2LChannel } from "../l2lchannel.js";
import L2LClient from "lively.2lively/client.js";
import L2LTracker from "lively.2lively/tracker.js";

Expand Down Expand Up @@ -87,72 +87,34 @@ describe("messaging between master and client", () => {

});

// describe("lively2lively backchannel tests", function() {
// this.timeout(1*1000)
//
// beforeEach(async () => setup(2));
// afterEach(async () => teardown());
//
// it("set up socket.io backchannel for messaging", async () => {
// var url = 'http://localhost:9011/lively-socket.io',
// namespace = '/l2l',
// hostname = 'localhost',
// port = '9011',
// namespace = '/l2l',
// path = '/lively-socket.io',
// origin = `http://${hostname}:${port}`,
// //Create new client for test
// client = new L2LClient(origin,path,namespace)
// client.open();
// client.register();
// await client.whenRegistered(300)
// //Create channel
// var testChannel = await L2LChannel.create(client)
//
// //Verify that client is the 'sender'
// expect(client).equals(testChannel.sender);
// //Verify that both master and sender are instances of L2LClient
// expect((testChannel.master instanceof L2LClient) && (testChannel.sender instanceof L2LClient)).equals(true);
// //tear down channel and extra client
// testChannel.close();
// await client.remove()
// });
// it("create channel with master and two clients", async () => {
//
// var url = 'http://localhost:9011/lively-socket.io',
// namespace = '/l2l',
// hostname = 'localhost',
// port = '9011',
// namespace = '/l2l',
// path = '/lively-socket.io',
// origin = `http://${hostname}:${port}`,
// client = new L2LClient(origin,path,namespace)
// client.open();
// client.register();
// await client.whenRegistered(300)
// var client2 = new L2LClient(origin,path,namespace);
// client2.open();
// client2.register();
// await client2.whenRegistered(300)
//
// var testChannel1 = await L2LChannel.create(client)
// var testChannel2 = await L2LChannel.create(client2,testChannel1.master)
//
// // console.log(testChannel1.master.socketId.split('#')[1])
// var roomContents;
// client.sendTo(client.trackerId,'listRoom',{roomName: testChannel1.master.socketId.split('#')[1]},(a) => roomContents = a.data.sockets)
// await promise.waitFor(200, () => roomContents);
// expect(roomContents.hasOwnProperty(client.socketId)).equals(true)
// expect(roomContents.hasOwnProperty(client2.socketId)).equals(true)
// expect(roomContents.hasOwnProperty(testChannel1.master.socketId)).equals(true)
// testChannel1.close();
// testChannel2.close();
//
// await client.remove();
// await client2.remove();
// })
//
// })


describe("lively2lively backchannel tests", function() {
this.timeout(1*1000)

beforeEach(async () => setup(2));
afterEach(async () => teardown());

it("ensure clients have l2l clients", async () => {

var {world1, masterWorld, client1, master} = state;
var testChannel = new L2LChannel(client1, "receiveOpsFromMaster", master, "receiveOpsFromClient")

expect((testChannel.senderRecvrA.l2lclient) && (testChannel.senderRecvrA.l2lclient instanceof L2LClient)).equals(true,'client A not L2LClient')
expect((testChannel.senderRecvrB.l2lclient) && (testChannel.senderRecvrB.l2lclient instanceof L2LClient)).equals(true,'client B not L2LClient')

await testChannel.senderRecvrA.l2lclient.whenRegistered(300)
await testChannel.senderRecvrB.l2lclient.whenRegistered(300)

window.testChannel = testChannel;
console.log(testChannel);
window.testChannel = testChannel;

testChannel.goOffline();
})

})


describe("syncing master with two clients", function() {

Expand Down

0 comments on commit ccbb70e

Please sign in to comment.