/**
* @file NodeApplication
* @copyright Monohm, Inc. 2014
*/
monohm.provide ("sensible.node.Application");
sensible.node.Application = function (inCallback)
{
sensible.Application.call (this, inCallback);
}
monohm.inherits (sensible.node.Application, sensible.Application);
// SENSIBLE_APPLICATION IMPLEMENTATION
// override to load the config from the hardware
// we assume the load() is async for safety reasons!
sensible.node.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.node.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.node.Application.prototype.registerHost =
function node_Application_registerHost (inCallback)
{
console.log ("node.Application.registerHost()");
if (this.config.hostname)
{
this.mdns.registerHost (this.config.hostname, null);
}
if (inCallback)
{
inCallback ();
}
}
sensible.node.Application.prototype.registerService = function (inCallback)
{
var service = this.mdns.registerService
(this.config.name, this.config.type, this.config.address, this.config.port, this.config.description, this.config.ttl);
console.log ("server advertised as "
+ service.name + "." + service.type + ":" + service.host + ":" + service.port);
if (inCallback)
{
inCallback ();
}
}
// override to save config to the hardware
// this will be hopefully the same structure as retrieved using loadConfig()
sensible.node.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.node.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);
}
}
sensible.node.Application.prototype.startWebServer = function (inCallback)
{
this.server = new sensible.node.Server (this.config.port, this);
if (inCallback)
{
inCallback ();
}
}
sensible.node.Application.prototype.stop =
function node_Application_stop ()
{
this.unregisterService ();
this.stopHTTPServer ();
}
sensible.node.Application.prototype.stopHTTPServer = function ()
{
this.server.stop ();
}
sensible.node.Application.prototype.unregisterHost = function ()
{
if (this.config.hostname)
{
this.mdns.unregisterHost (this.config.hostname);
}
}
sensible.node.Application.prototype.unregisterService = function ()
{
this.mdns.unregisterService (null, this.config.port);
}
sensible.node.Application.prototype.onWebSocketOpen = function ()
{
}
sensible.node.Application.prototype.onWebSocketClose = function ()
{
}