UNPKG

ngn-idk-core

Version:
217 lines (190 loc) 4.49 kB
/** * @class NGN.core.Server * A generic utility class representing a server in the application. * This class typically isn't invoked directly. It is designed as a base class * for different server types like NGN.http.Server, NGN.http.APIServer, etc. * @extends NGN.Class * @private */ var Class = NGN.Class.extend({ /** * @constructor * Create a new server. * @param {Object} config */ constructor: function(config){ config = config || {}; Class.super.constructor.call(this, config); Object.defineProperties(this,{ /** * @cfg {String} * The type of server. For example, `HTTP`, `DNS`, `FTP`, etc. */ type: { value: config.type || 'UNKNOWN', enumerable: true, writable: true }, /** * @cfg {String} * The purpose of the server. Typical values include: * * * WWW * * REST * * API * * DATA * * Any value will work for this. This attribute acts as a "tag" * to identify groups of servers that may serve similar purposes. */ purpose: { value: config.purpose || 'UNKNOWN', enumerable: true, writable: true }, /** * @cfg {String} * The name of the server. This can be referenced via NGN.core.Process (if used) or NGN#getServer. */ id: { value: config.id || NGN.uuid().replace(/\-/gi,''), enumerable: true, writable: true }, /** * @cfg {Number} * The port on which the server will listen/connect. */ port: { value: config.port || null, enumerable: true, writable: true, configurable:true }, /** * @property {Boolean} [running=false] * Indicates the server is currently running. * @readonly */ running: { value: false, enumerable: true, writable: true }, /** * @cfg {Boolean} * Automatically register a helper reference to the server (available via NGN#getServer or NGN#getServers). */ autoRegister: { value: NGN.coalesce(config.autoRegister,true), enumerable: true, writable: true }, /** * @cfg {Boolean} [autoStart=true] * Automatically start the server. If this is set to `false`, the * server will need to be running explicitly using the #start method. */ autoStart: { value: NGN.coalesce(config.autoStart,true), enumerable: true, writable: true }, /** * @cfg {NGN.util.Logger} * Specify a logging utility to log server activity. */ syslog: { value: config.logger || null, enumerable: false, configurable:true }, /** * @property {Boolean} * Indicates whether the server is in the process of starting. */ starting: { value: false, enumerable: true, writable: true, configurable:true } }); // Create a generic empty logger to prevent code from breaking. if (this.syslog == null){ this.syslog == { disabled: true, info: NGN.emptyFn, warn: NGN.emptyFn, debug: NGN.emptyFn, error: NGN.emptyFn }; } this.autoRegister && this.register(); this.autoStart && this.autoStart; }, /** * @method * Registers the server within the application scope by creating a pointer to it. */ register: function(){ UTIL.notImplemented(__filename,'register()'); //NGNA && NGNA.registerServer(this); }, /** * @method * Unregisters the server from the application. */ unRegister: function(){ UTIL.notImplemented(__filename,'unRegister()'); }, /** * @method * Start the server */ start: function(){ this.onStart(); }, /** * @method * Stop the server */ stop: function(){ this.onStop(); }, /** * @event start * Fired when the server begins the startup process. */ onStart: function(){ if (!this.running) { this.running = true; this.emit('start'); } }, /** * @event ready * Fired when the server is ready to process reuests. */ onReady: function(){ if (this.running) { this.starting = false; this.emit('ready',this); } }, /** * @event stop * Fired when the server stops. */ onStop: function(){ if (this.running) { this.running = false; this.starting= false; this.emit('stop',this); } }, onError: function(e){ this.fireError(e); } }); module.exports = Class;