Source: node-mdns-application.js

/**
 * @file NodeApplication
 * @copyright Monohm, Inc. 2014
 */

monohm.provide ("sensible.nodemdns.Application");

// can we require() here?
var	mdns = require ("mdns");

sensible.nodemdns.Application = function (inCallback)
{
	sensible.Application.call (this, inCallback);
}
monohm.inherits (sensible.nodemdns.Application, sensible.Application);

// SENSIBLE_APPLICATION IMPLEMENTATION

// override to load the config from the hardware
// we assume the load() is async for safety reasons!
sensible.nodemdns.Application.prototype.loadConfig = function (inCallback)
{
	var	self = this;
	
	fs.readFile
	(
		"sensible-config.json",
		function (inError, inContents)
		{
			var	error = inError;
			
			if (inContents)
			{
				try
				{
					self.config = JSON.parse (inContents);
				}
				catch (inError)
				{
					error = inError;
				}
			}
			
			if (inCallback)
			{
				inCallback (error);
			}
		}
	);
}

sensible.nodemdns.Application.prototype.loadProperties = function (inCallback)
{
	var	self = this;
	
	fs.readFile
	(
		"sensible-properties.json",
		function (inError, inContents)
		{
			var	error = inError;
			
			if (inContents)
			{
				try
				{
					self.properties = JSON.parse (inContents);
			
					self.propertiesByKey = new Object ();
					
					// sort the property key cache
					for (var i = 0; i < self.properties.length; i++)
					{
						var	property = self.properties [i];
						self.propertiesByKey [property.name] = property;
					}
				}
				catch (inError)
				{
					error = inError;
				}
			}
			
			if (inCallback)
			{
				inCallback (error);
			}
		}
	);
}

sensible.nodemdns.Application.prototype.registerHost =
function nodemdns_Application_registerHost (inCallback)
{
	console.log ("nodemdns.Application.registerHost() not implemented");
}

sensible.nodemdns.Application.prototype.registerService = function (inCallback)
{
	console.log ("registerService() registering " + this.config.name + " at " + this.config.port);
	
	this.advertisement = mdns.createAdvertisement (mdns.tcp ("sensible"), this.config.port);
	this.advertisement.start ();
}

// override to save config to the hardware
// this will be hopefully the same structure as retrieved using loadConfig()
sensible.nodemdns.Application.prototype.saveConfig = function ()
{
	try
	{
		// prettyprint the JSON to the file
		var	config = JSON.stringify (this.config, {}, 2);
		fs.writeFileSync ("sensible-config.json", json);
	}
	catch (inError)
	{
		console.error ("unable to write sensible-config.json");
		console.error (inError);
	}
}

// override to save properties to the hardware
// this will be hopefully the same structure as retrieved using loadProperties()
sensible.nodemdns.Application.prototype.saveProperties = function ()
{
	try
	{
		// prettyprint the JSON to the file
		var	json = JSON.stringify (this.properties, {}, 2);
		fs.writeFileSync ("sensible-properties.json", json);
	}
	catch (inError)
	{
		console.error ("unable to write sensible-properties.json");
		console.error (inError);
	}
}

// overrides the regular Node application class to NOT start our MDNS
sensible.nodemdns.Application.prototype.startMDNS = function (inCallback)
{
	var	self = this;
	
	this.registerService
	(
		function ()
		{
			inCallback.call (self);
		}
	);
}

sensible.nodemdns.Application.prototype.startWebServer = function (inCallback)
{
	this.server = new sensible.node.Server (this.config.port, this);
	
	if (inCallback)
	{
		inCallback ();
	}
}

sensible.nodemdns.Application.prototype.stop =
function nodemdns_Application_stop ()
{
	this.unregisterService ();
	this.stopHTTPServer ();
}

sensible.nodemdns.Application.prototype.stopHTTPServer = function ()
{
	this.server.stop ();
}

sensible.nodemdns.Application.prototype.unregisterHost = function ()
{
	console.log ("nodemdns.Application.unregisterHost() not implemented");
}

sensible.nodemdns.Application.prototype.unregisterService = function ()
{
	if (this.advertisement)
	{
		this.advertisement.stop ();
		this.advertisement = null;
	}
	else
	{
		console.log ("unregisterService() with no extant advertisement");
	}
}

sensible.nodemdns.Application.prototype.onWebSocketOpen = function ()
{
}

sensible.nodemdns.Application.prototype.onWebSocketClose = function ()
{
}