Source: buni-strategy.js

/**
 * @file sensible.buni.Strategy
 * @copyright Monohm, Inc. 2014
 */

monohm.provide ("sensible.buni.Strategy");

/**
 * Implementation of Strategy for Firefox OS.
 *
 * @class
 * @constructor
 */
 
sensible.buni.Strategy = function ()
{
	sensible.Strategy.call (this);
}
monohm.inherits (sensible.buni.Strategy, sensible.Strategy);

/**
 * Open the MDNS socket and join the multicast group.
 * Note only one UDP socket per strategy instance.
 *
 * @param {function} inCallback - function to call on completion
 */
sensible.buni.Strategy.prototype.open = function (inCallback)
{
	console.log ("sensible.buni.Strategy.open()");

	var	self = this;
	
	gBuniClient.udp.open
	(
		5353,
		"224.0.0.251",
		function (inError, inResponse)
		{
			if (!inError)
			{
				self.socket = inResponse.socket;
			}
			
			inCallback (inError);
		}
	);
}

/**
 * Close the UDP socket.
 */
sensible.buni.Strategy.prototype.close = function ()
{
	gBuniClient.udp.close (this.socket);
}

/**
 * Return the host name of the machine.
 * So far, I've been unable to determine how to do this, and returning the IP number instead.
 *
 * @returns {string} host name
 */
sensible.buni.Strategy.prototype.getHostName = function (inCallback)
{
	gBuniClient.udp.getHostName
	(
		function (inError, inResponse)
		{
			console.log ("udp.getHostName() calls back with " + inResponse.hostname);
			
			inCallback (inResponse.hostname);
		}
	);
}

/**
 * Return the IP address of the machine.
 *
 * @returns {string} IP address
 */
sensible.buni.Strategy.prototype.getIPAddress = function (inCallback)
{
	gBuniClient.udp.getIPAddress
	(
		function (inError, inResponse)
		{
			console.log ("udp.getIPAddress() calls back with " + inResponse.address);

			inCallback (inResponse.address);
		}
	);
}

/**
 * Listen for packets on the UDP socket.
 *
 * @param {function} inCallback - function to call on reception
 */
sensible.buni.Strategy.prototype.listen = function (inCallback)
{
	var	self = this;
	
	gBuniClient.udp.subscribe
	(
		function (inEvent)
		{
			console.log ("sensible.buni.Strategy.listen() receives packet of type " + inEvent.buniType);
			
			if (inEvent.buniType == "buni-event-udp-receive")
			{
				console.log ("socket is " + inEvent.socket);
				console.log ("our socket is " + self.socket);
			
				if (inEvent.socket == self.socket)
				{
					try
					{
						console.log ("calling sensible back");
						inCallback (inEvent.data, inEvent.address, inEvent.port);
					}
					catch (inException)
					{
						console.error ("error calling sensible back with packet!");
						console.error (inException);
					}
				}
			}
		}
	);
}

/**
 * Send a packet on the UDP socket.
 *
 * @param {ArrayBuffer} inPacket - packet
 * @param {string} inRemoteAddress - remote address
 * @param {port} inRemotePort - remote port
 */
sensible.buni.Strategy.prototype.send = function (inPacket, inRemoteAddress, inRemotePort)
{
	gBuniClient.udp.send
	(
		this.socket,
		inPacket,
		inRemoteAddress,
		inRemotePort
	);
}