Skip to content

Commit

Permalink
added bonjour advertising.
Browse files Browse the repository at this point in the history
jslint changes
  • Loading branch information
prgsmall committed Jan 27, 2012
1 parent e1c3b90 commit 9b5d529
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 115 deletions.
8 changes: 5 additions & 3 deletions TODO
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Acequia TODO

Add namespace to the message object

Add pub/sub to the clients object

OSCClient.prototype.send = function (message) {
// TODO: convert the message to an OSC message
// TODO: convert the message to an OSC message

Define OSC messages for connect/disconnect, etc.

rename the msg module to something else.
7 changes: 3 additions & 4 deletions examples/acequiaClient.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@
};

var ac = new AcequiaClient("http://localhost:9091", "user" + Math.random());
ac.addMessageListener(msg.MSG_CONNECT, onconnect);
ac.addMessageListener(msg.MSG_GETCLIENTS, ongetClients);
ac.addMessageListener('*', ontest);
ac.addListener(msg.MSG_CONNECT, onconnect);
ac.addListener(msg.MSG_GETCLIENTS, ongetClients);
ac.addConnectionChangeHandler(onconnectionChanged);

$(document).ready( function(){
$("button").button();
$("#disconnect").button("option", "disabled", true);
Expand Down
80 changes: 80 additions & 0 deletions examples/tuio.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<!DOCTYPE HTML>
<html>
<head>
<title>tuio</title>
<style>
#tuioCanvas {
border: 1px black solid;
border-radius: 6px;
height: 480px;
width: 320px;
}
</style>
</head>

<body>
<div id="container">
<canvas id="tuioCanvas">
</canvas>
<button onclick="clearCanvas()">Clear Canvas</button>
</div>

<script src='http://localhost:9091/acequia/acequia.js'></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<script>
var canvas;
var context;

$(document).ready( function(){
canvas = document.getElementById('tuioCanvas');
context = canvas.getContext('2d');
var ac = new AcequiaClient("http:// :9091", "TUTUTUIO-" + Math.random());
ac.addListener("/tuio/2Dcur/set", onTuio2Dcur);
ac.connect();
});

var started = false;
var timeout = null;

var onTimeout = function () {
started = false;
timeout = null;
};

var clearCanvas = function () {
started = false;

// Store the current transformation matrix
context.save();

// Use the identity matrix while clearing the canvas
context.setTransform(1, 0, 0, 1, 0, 0);
context.clearRect(0, 0, canvas.width, canvas.height);

// Restore the transform
context.restore();
};

var onTuio2Dcur = function (mmmmsg) {
if (timeout) {
clearTimeout(timeout);
}
var x = parseFloat(mmmmsg.body[1]) * canvas.width;
var y = parseFloat(mmmmsg.body[2]) * canvas.height;

if (!started) {
context.beginPath();
context.moveTo(x, y);
started = true;
} else {
context.lineTo(x, y);
context.stroke();
}
timeout = setTimeout(onTimeout, 200);
};

</script>
</body>

</html>
48 changes: 31 additions & 17 deletions lib/acequia.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var http = require("http"),
msg = require("./msg"),
tuio = require("./tuio"),
Buffer = require('buffer').Buffer,
mdns = require("mdns"),
genclient = require("./genclient");

var INTERNAL_IP = "0.0.0.0",
Expand All @@ -20,6 +21,8 @@ var INTERNAL_IP = "0.0.0.0",
TCP_PORT = 9092,
TIMEOUT = 600000; // Seconds before kicking idle clients.

var tcp_ad, osc_ad;

// The list of clients
var clients = new ac.AcequiaClients(TIMEOUT * 1000);

Expand All @@ -42,12 +45,12 @@ function startServers() {
oscServer = dgram.createSocket("udp4");

oscServer.on("message", function (data, rinfo) {
var cli, oscMsgs = osc.bufferToOsc(data), oscMsg, i;
var cli, oscMsgs = osc.bufferToOsc(data), oscMsg, i, message;

for (i in oscMsgs) {
for (i = 0; i < oscMsgs.length; i += 1) {
oscMsg = oscMsgs[i];

if (oscMsg.address.indexOf("/tuio") == 0) {
logger.trace("OSC:" + oscMsg);
if (oscMsg.address.indexOf("/tuio") === 0) {
message = tuio.toAcequiaMessage(oscMsg);
} else {
message = new msg.AcequiaMessage("", oscMsg.address, oscMsg.data);
Expand All @@ -61,6 +64,10 @@ function startServers() {

oscServer.on("listening", function () {
logger.debug("OSC Server is listening on %j", oscServer.address());

// Broadcast the presence of this server
osc_ad = mdns.createAdvertisement(mdns.udp('acequia', 'osc'), OSC_PORT, {name:"Acequia OSC Server", port: OSC_PORT});
osc_ad.start();
});

oscServer.on("close", function () {
Expand All @@ -76,19 +83,19 @@ function startServers() {
clientCode = genclient.generateClientCode();
}

httpServer = http.createServer(function (req, res){
httpServer = http.createServer(function (req, res) {
var pathName = url.parse(req.url, true).pathname,

serveClientCode = function (out) {
res.writeHead(200,{ 'Content-Type': 'text/javascript' });
res.end(out);
res.writeHead(200, { 'Content-Type': 'text/javascript' });
res.end(out);
};

console.log("HTTP server received " + pathName);

if (pathName == "/acequia/acequia.js") {
if (pathName === "/acequia/acequia.js") {
serveClientCode(clientCode.full);
} else if (pathName == "/acequia/acequia.min.js") {
} else if (pathName === "/acequia/acequia.min.js") {
serveClientCode(clientCode.minified);
}
});
Expand All @@ -102,7 +109,7 @@ function startServers() {
wsServer.set('log level', 1); // reduce logging
wsServer.set('transports', [ // enable all transports (optional if you want flashsocket)
'websocket',
'flashsocket',
//'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling'
Expand All @@ -114,7 +121,7 @@ function startServers() {

socket.on('message', function (data) {
var message = new msg.AcequiaMessage(JSON.parse(data));
logger.debug("WebSocket:" + this.id + " Received message: " + message);
logger.trace("WebSocket:" + this.id + " Received message: " + message);

clients.onMessage(ac.TYP_WS, message, socket);
});
Expand All @@ -127,26 +134,25 @@ function startServers() {
// Setup a tcp server
tcpServer = net.createServer(function (socket) {

socket.addListener("connect", function () {
socket.on("connect", function () {
logger.debug("TCP: %s:%s connect", socket.remoteAddress, socket.remotePort);
});

socket.addListener("data", function (data) {
socket.on("data", function (data) {
var index = 0, msgs = [], size, message,
buffer = new Buffer(data);

while (index < buffer.length) {
size = buffer.readInt32BE(index);
logger.debug("message size: %d", size);
index += 4;
message = buffer.slice(index, index + size);
msgs.push(new msg.AcequiaMessage(JSON.parse(message)));
index += size;
}

for (index in msgs) {
for (index = 0; index < msgs.length; index += 1) {
message = msgs[index];
logger.debug("TCP: %s:%s data : %j", socket.remoteAddress, socket.remotePort, message);
logger.trace("TCP: %s:%s data : %j", socket.remoteAddress, socket.remotePort, message);
clients.onMessage(ac.TYP_TCP, message, socket);
}
});
Expand All @@ -168,7 +174,11 @@ function startServers() {
});

tcpServer.on("listening", function () {
logger.debug("TCP Server is listening on %j", tcpServer.address());
logger.debug("TCP Server is listening on %j", tcpServer.address());

// Broadcast the presence of this server
tcp_ad = mdns.createAdvertisement(mdns.tcp('acequia'), TCP_PORT, {name:"Acequia TCP Server", port: TCP_PORT});
tcp_ad.start();
});

tcpServer.listen(TCP_PORT, INTERNAL_IP);
Expand All @@ -179,6 +189,10 @@ function startServers() {
// The exported function is called to start the server.
// It starts a server for each individual protocol.
function start() {
process.argv.forEach(function (val, index, array) {
console.log(index + ': ' + val);
});

if (process.argv.length > 2) {
INTERNAL_IP = process.argv[2];
}
Expand Down
56 changes: 21 additions & 35 deletions lib/acequiaClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ var objCallback = function (obj, func) {
};
};

// TODO: do not require the uri. Derive it from the include path, some how...
// Write it through the http request on the acequia server, in genclient...

/**
* Defines the AcequiaClient object, which is used to connect with the Acequia server
* through a WebSocket connection.
Expand Down Expand Up @@ -47,11 +50,15 @@ var AcequiaClient = function (uri, userName) {
};

AcequiaClient.prototype.getSocketIOJS = function (onload) {
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", this.uri + "/socket.io/socket.io.js");
fileref.onload = onload;
document.body.appendChild(fileref);
if (typeof(io) === "undefined") {
var fileref = document.createElement('script');
fileref.setAttribute("type", "text/javascript");
fileref.setAttribute("src", this.uri + "/socket.io/socket.io.js");
fileref.onload = onload;
document.body.appendChild(fileref);
} else {
onload();
}
};

/**
Expand All @@ -60,7 +67,7 @@ AcequiaClient.prototype.getSocketIOJS = function (onload) {
* @param {Function} callback The callback function which will be called when the
* message arrives.
*/
AcequiaClient.prototype.addMessageListener = function (msgName, callback) {
AcequiaClient.prototype.addListener = function (msgName, callback) {
if (!(msgName in this.listeners)) {
this.listeners[msgName] = [];
}
Expand All @@ -75,7 +82,7 @@ AcequiaClient.prototype.addMessageListener = function (msgName, callback) {
* @param {Function} callback The callback function which will be called when the
* message arrives.
*/
AcequiaClient.prototype.removeMessageListener = function (msgName, callback) {
AcequiaClient.prototype.removeListener = function (msgName, callback) {
var idx = this.listeners[msgName].indexOf(callback);
this.listeners[msgName].splice(idx, 1);
};
Expand All @@ -86,14 +93,12 @@ AcequiaClient.prototype.removeMessageListener = function (msgName, callback) {
AcequiaClient.prototype.connect = function () {
var onload = function (that) {
return function (evt) {
that.socket = io.connect(that.uri);
that.socket = io.connect(that.uri, {reconnect:false});
that.socket.on("connect", objCallback(that, "onConnect"));
};
};

if (!this.socket || (!this.socket.socket.connected && !this.socket.socket.connecting)) {
this.getSocketIOJS(onload(this));
}
this.getSocketIOJS(onload(this));
};

/**
Expand Down Expand Up @@ -169,25 +174,6 @@ AcequiaClient.prototype.ac_onmessage = function (message) {
return ret;
};

/**
* Calls the message listeners for the given msgName.
* @param {AcequiaMessage} msg The message to send.
* @param {String} msgName The name of the message to look for in the listeners.
*/
AcequiaClient.prototype.callListeners = function (message, msgName) {
var i;

if (typeof(msgName) === "undefined") {
msgName = message.name;
}

if (msgName in this.listeners) {
for (i in this.listeners[msgName]) {
this.listeners[msgName][i](message, this);
}
}
};

/**
* Handles the onmessage event from the WebSocket. This method calls any message listeners
* that have registered for the message. If there is a wildcard handler registered, then it
Expand All @@ -201,9 +187,9 @@ AcequiaClient.prototype.onMessage = function (data) {
return;
}

// Call the message listeners.
this.callListeners(message);

// Call the wildcard message listeners.
this.callListeners(message, "*");
if (message.name in this.listeners) {
for (i in this.listeners[message.name]) {
this.listeners[message.name][i](message, this);
}
}
};
Loading

0 comments on commit 9b5d529

Please sign in to comment.