Source: fxos-strategy.js

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

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

/**
 * Implementation of Strategy for Firefox OS.
 *
 * @class
 * @constructor
 */
 
sensible.fxos.Strategy = function ()
{
	sensible.Strategy.call (this);
}
monohm.inherits (sensible.fxos.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.fxos.Strategy.prototype.open = function (inCallback)
{
	console.log ("sensible.fxos.Strategy.open()");

	try
	{
		this.socket = new UDPSocket
		({
			addressReuse: true,
			binaryType: "arraybuffer",
			localPort: 5353
		});

		if (inCallback)
		{
			var	self = this;
			
			this.socket.opened.then
			(
				function ()
				{
					self.socket.joinMulticastGroup ("224.0.0.251");
					inCallback ();
				}
			);
		}
	}
	catch (inError)
	{
		console.error ("error opening UDPSocket");
		console.error (inError.message);
		
		inCallback (inError);
	}
}

/**
 * Close the UDP socket and bind to the specified port.
 *
 * @param {integer} inPort - port to which to bind, eg 5353 for MDNS
 * @param {function} inCallback - function to call on completion
 */
sensible.fxos.Strategy.prototype.close = function ()
{
	this.socket.close ();
}

/**
 * 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.fxos.Strategy.prototype.getHostName = function (inCallback)
{
	// no idea how to do this on FxOS
	if (navigator && navigator.mozWifiManager && navigator.mozWifiManager.connectionInformation)
	{
		inCallback (navigator.mozWifiManager.connectionInformation.ipAddress);
	}
	else
	{
		console.error ("sensible.fxos.Strategy.getHostName() can't get wifi connection info");
		inCallback ("unknown");
	}
}

/**
 * Return the IP address of the machine.
 *
 * @returns {string} IP address
 */
sensible.fxos.Strategy.prototype.getIPAddress = function ()
{
	if (navigator && navigator.mozWifiManager && navigator.mozWifiManager.connectionInformation)
	{
		inCallback (navigator.mozWifiManager.connectionInformation.ipAddress);
	}
	else
	{
		console.error ("sensible.fxos.Strategy.getIPAddress() can't get wifi connection info");
		inCallback ("127.0.0.1");
	}
}

/**
 * Listen for packets on the UDP socket.
 *
 * @param {function} inCallback - function to call on reception
 */
sensible.fxos.Strategy.prototype.listen = function (inCallback)
{
	this.socket.addEventListener
	(
		"message",
		function (inMessage)
		{
			inCallback (inMessage.data, inMessage.remoteAddress, inMessage.remotePort);
		}
	);
}

/**
 * Send a packet on the UDP socket.
 *
 * @param {ArrayBuffer} inPacket - packet
 * @param {string} inRemoteAddress - remote address
 * @param {port} inRemotePort - remote port
 */
sensible.fxos.Strategy.prototype.send = function (inPacket, inRemoteAddress, inRemotePort)
{
	if (this.socket.readyState == "closed")
	{
		console.error ("udp socket is closed, reopening");
		
		var	self = this;
		var	port = this.socket.localPort;
		
		this.open
		(
			port,
			function ()
			{
				var	result = self.socket.send (inPacket, inRemoteAddress, inRemotePort); 
				console.log ("send() returns " + result);
			}
		);
	}
	else
	{
		var	result = this.socket.send (inPacket, inRemoteAddress, inRemotePort); 
		console.log ("send() returns " + result);
	}
}