Skip to content

Commit

Permalink
Added equals method to each of the acequia clients to be able to uniq…
Browse files Browse the repository at this point in the history
…uely choose each connection based on certain parameters
  • Loading branch information
prgsmall committed Sep 21, 2011
1 parent ee53916 commit 80abd9a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 44 deletions.
58 changes: 21 additions & 37 deletions acequia.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ var sys = require('sys'),
netIP = require('./libs/netIP.js'),
ac = require("./client.js");

var TYP_OSC = 0,
TYP_WS = 1,
DEBUG = 1,
var DEBUG = 1,
INTERNAL_IP = '',
OSC_PORT = 9090,
WS_PORT = 9091,
Expand All @@ -22,7 +20,7 @@ var TYP_OSC = 0,
TIMEOUT = 600; //Seconds before kicking idle clients.


// Client array structure.
// The list of clients
var clients = [];

var oscServer, wsServer;
Expand All @@ -37,8 +35,9 @@ function debug(str) {
//Utility function for sending an osc message to a given client.
function msgSndOsc(to, from, title, body, tt) {
var data = [from].concat(body),
oscMsg = osc.newOsc(title, 's' + tt, data),
buffer = osc.oscToBuffer(oscMsg);
oscMsg = osc.newOsc(title, 's' + tt, data),
buffer = osc.oscToBuffer(oscMsg);

oscServer.send(buffer, 0, buffer.length, clients[to].portOut, clients[to].ip);
}

Expand All @@ -49,18 +48,19 @@ function msgSndWs(to, from, title, body) {
}


//The master sending function which takes a message meant for a client, decides which protocol to use, and calls the appropriate function.
// The master sending function which takes a message meant for a client, decides
// which protocol to use, and calls the appropriate function.
function msgSnd(to, from, title, body, tt) {
if (to === -1) {
return;
}

switch (clients[to].protocol) {
case TYP_OSC:
case ac.TYP_OSC:
msgSndOsc(to, from, title, body, tt);
break;

case TYP_WS:
case ac.TYP_WS:
msgSndWs(to, from, title, body);
break;
}
Expand All @@ -77,7 +77,7 @@ function msgSndAll(exc, mesid, data, tt) {
}
}

//Message routing goes here.
// Message routing goes here.
function msgRec(from, to, title, body, tt) {
var time = (new Date()).getTime();
debug('Received message ' + title + ' from client #' + from + ' (' + clients[from].name + ')');
Expand All @@ -100,26 +100,10 @@ function msgRec(from, to, title, body, tt) {
//Often we only know the IP and Port of the sender of a message. This function translates this data into a 'usable' client ID number.
function lookupClient(protocol, var1, var2) {
var i;
switch (protocol) {
case TYP_OSC:
for (i = 0; i < clients.length; i += 1) {
if (clients[i].protocol === TYP_OSC) {
if (clients[i].ip === var1 && clients[i].portIn === var2) {
return i;
}
}
}
break;

case TYP_WS:
for (i = 0; i < clients.length; i += 1) {
if (clients[i].protocol === TYP_WS) {
if (clients[i].id === var1) {
return i;
}
}
for (i = 0; i < clients.length; i += 1) {
if (clients[i].equals(protocol, var1, var2)) {
return i;
}
break;
}
return -1;
}
Expand Down Expand Up @@ -190,18 +174,18 @@ function start() {
case "/connect":
//the second parameter when logging in is an optional 'port to send to'.
portOut = (oscMsg.data[1] > 0) ? oscMsg.data[1] : rinfo.port;
client = new ac.OSCClient(oscMsg.data[0], rinfo.address, rinfo.port, portOut, TYP_OSC);
client = new ac.OSCClient(oscMsg.data[0], rinfo.address, rinfo.port, portOut);
clients.push(client);
debug('Added client ' + oscMsg.data[0] +
' (OSC@' + rinfo.address + ':' + rinfo.port + ', client #' + (clients.length - 1) + ')');
break;

case "/disconnect":
dropClient(lookupClient(TYP_OSC, rinfo.address, rinfo.port), "disconnect by user");
dropClient(lookupClient(ac.TYP_OSC, rinfo.address, rinfo.port), "disconnect by user");
break;

default:
from = lookupClient(TYP_OSC, rinfo.address, rinfo.port);
from = lookupClient(ac.TYP_OSC, rinfo.address, rinfo.port);
to = lookupClientUsername(oscMsg.data.shift());
title = oscMsg.address;
tt = oscMsg.typeTags.slice(1);
Expand Down Expand Up @@ -236,7 +220,7 @@ function start() {
wsServer.addListener('connection', function (con) {
con.addListener('message', function (msg) {
var message = JSON.parse(msg),
from = lookupClient(TYP_WS, con.id),
from = lookupClient(ac.TYP_WS, con.id),
to = lookupClientUsername(message.to),
title = message.title,
body = message.body;
Expand All @@ -254,7 +238,7 @@ function start() {
}
}

client = new ac.WebSocketClient(body[0], con.id, TYP_WS);
client = new ac.WebSocketClient(body[0], con.id);
clients.push(client);

msgSndWs(clients.length - 1, "SYS", "/connect", 1);
Expand All @@ -281,15 +265,15 @@ function start() {
});

con.addListener('close', function () {
dropClient(lookupClient(TYP_WS, con.id), "connection closed");
dropClient(lookupClient(ac.TYP_WS, con.id), "connection closed");
});

con.addListener('timeout', function () {
dropClient(lookupClient(TYP_WS, con.id), "connection timeout");
dropClient(lookupClient(ac.TYP_WS, con.id), "connection timeout");
});

con.addListener('error', function (e) {
dropClient(lookupClient(TYP_WS, con.id), "connection error: " + e);
dropClient(lookupClient(ac.TYP_WS, con.id), "connection error: " + e);
});
});

Expand Down
3 changes: 2 additions & 1 deletion acequiaClient.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<html>
<head>
<title>Acequia Client Test Page</title>
<script src='acequiaClient.js'></script>
<script type='text/javascript'>
acequiaClient.connect('ws://localhost:9091', "user",
acequiaClient.connect('ws://localhost:9091', "user" + Math.random().toString(),
function (from, command, body) {
switch (command){
case '/connect':
Expand Down
53 changes: 47 additions & 6 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,62 @@

/*global exports */

var WebSocketClient = function (name, id, prot) {
var TYP_OSC = 0,
TYP_WS = 1;

/**
* The base class for all acequia clients.
*/
AcequiaClient = function (name, prot) {
this.name = name;
this.id = id;
this.protocol = prot;
this.lastMessage = (new Date()).getTime();
};
AcequiaClient.prototype.equals = function (prot) {
return (this.protocol === prot);
};

var OSCClient = function (name, ip, portIn, portOut, prot) {
this.name = name;
/**
* Defines the client connected to acequia via Websockets
* @param {String} name The unique user name associated with the client.
* @param {String} id The id assigned to the connection by the websocket
* server
*/

WebSocketClient = function (name, id) {
this.id = id;
AcequiaClient.call(this, name, TYP_WS);
};
WebSocketClient.prototype = new AcequiaClient;
WebSocketClient.prototype.equals = function (prot, id) {
return (this.protocol === prot &&
this.id === id);
};

/**
* Defines the client connected to acequia via an OSC connection
* @param {String} name The unique user name associated with the client.
* @param {String} ip The ip address associated with the client.
* @param {Integer} portIn The port that messages will be coming into acequia
* from the client;
* @param {Integer} portOut The port that messages will be going out from acequia
* to the client;
*/
OSCClient = function (name, ip, portIn, portOut) {
this.ip = ip;
this.portIn = portIn;
this.portOut = portOut;
this.protocol = prot;
this.lastMessage = (new Date()).getTime();
AcequiaClient.call(this, name, TYP_OSC);
};
OSCClient.prototype = new AcequiaClient;
OSCClient.prototype.equals = function (prot, ip, portIn) {
return (this.protocol === prot &&
this.ip === ip &&
this.portIn === portIn);
};

// Export the entities that Acequia needs
exports.TYP_OSC = TYP_OSC;
exports.TYP_WS = TYP_WS;
exports.WebSocketClient = WebSocketClient;
exports.OSCClient = OSCClient;

0 comments on commit 80abd9a

Please sign in to comment.