// implementation of SocketStrategy for Chrome sockets.udp
monohm.provide ("sensible.chrome.Strategy");
sensible.chrome.Strategy = function ()
{
}
sensible.chrome.Strategy.prototype.subscribe = function (inMulticastAddress, inCallback)
{
console.log ("sensible.chrome.Strategy.subscribe(" + inMulticastAddress + ")");
// async = virus
chrome.sockets.udp.joinGroup (this.socketID, inMulticastAddress, inCallback);
}
sensible.chrome.Strategy.prototype.open = function (inCallback)
{
console.log ("sensible.chrome.Strategy.open(" + inPort + ")");
var self = this;
chrome.sockets.udp.create
(
{},
function (inCreateInfo)
{
self.socketID = inCreateInfo.socketId;
// note that we do NOT bind to 5353 on Chrome
// as Chrome itself does not set the reuse option -- boo!
// means we can't update our caches from others' queries
// which is kinda a shame
console.log ("binding ephemeral port, as 5353 is taken on Chrome");
var self = this;
chrome.sockets.udp.bind
(
self.socketID,
"0.0.0.0",
0,
function (inResult)
{
console.log (inResult);
if (inResult >= 0)
{
chrome.sockets.udp.joinGroup (this.socketID, inMulticastAddress, inCallback);
}
else
{
console.error ("error " + inResult + " binding to port " + inPort);
}
if (inCallback)
{
inCallback ();
}
}
);
}
);
}
sensible.chrome.Strategy.prototype.close = function ()
{
chrome.sockets.udp.close (this.socketID);
}
/**
* Return the host name of the machine.
*
* @returns {string} host name
*/
sensible.chrome.Strategy.prototype.getHostName = function (inCallback)
{
console.log ("chrome.Strategy.getHostName() stubbed");
inCallback ("chrome");
}
/**
* Return the IP address of the machine.
*
* @returns {string} IP address
*/
sensible.chrome.Strategy.prototype.getIPAddress = function (inCallback)
{
console.log ("chrome.Strategy.getIPAddress() stubbed");
inCallback ("10.0.1.14");
}
sensible.chrome.Strategy.prototype.enableBroadcast = function ()
{
// no equivalent in Chrome seems like
}
sensible.chrome.Strategy.prototype.listen = function (inCallback)
{
var self = this;
chrome.sockets.udp.onReceive.addListener
(
function (inSocketID, inPacket, inRemoteHost, inRemotePort)
{
if (inSocketID == self.socketID)
{
inCallback (inPacket, inRemoteHost, inRemotePort);
}
}
);
}
// this is passed an ArrayBuffer, happily
sensible.chrome.Strategy.prototype.send = function (inPacket, inRemoteAddress, inRemotePort)
{
console.log ("chrome.Strategy.send() on socket " + this.socketID);
chrome.sockets.udp.send
(
this.socketID,
inPacket,
inRemoteAddress,
inRemotePort,
function (inSendInfo)
{
if (inSendInfo.resultCode >= 0)
{
console.log ("sent " + inSendInfo.bytesSent);
}
else
{
console.log ("error " + inSendInfo.resultCode + " sending to " + inRemoteAddress + ":" + inRemotePort);
}
}
);
}