Skip to content
This repository
  • 4 commits
  • 6 files changed
  • 0 comments
  • 1 contributor
Dec 19, 2012
service
http server
https server
7263a82
https server options fix 4b578f7
server listen params 970f3bc
Jan 08, 2013
version update fc2ff50
3  index.js
@@ -8,6 +8,9 @@ further.registery = require('./lib/registery');
8 8
 further.Serial = require('./lib/tasks/serial');
9 9
 further.Parallel = require('./lib/tasks/parallel');
10 10
 further.Controller = require('./lib/controller');
  11
+further.Service = require('./lib/service');
  12
+further.HttpServer = require('./lib/httpServer');
  13
+further.HttpsServer = require('./lib/httpsServer');
11 14
 
12 15
 further.setup = require('./lib/setup');
13 16
 further.requires = require('./lib/requires');
17  lib/httpServer.js
... ...
@@ -0,0 +1,17 @@
  1
+var Server = require("./server"),
  2
+	util = require("util"),
  3
+	http = require('http');
  4
+
  5
+
  6
+exports = module.exports = HttpServer;
  7
+
  8
+function HttpServer(app, name) {
  9
+	this.app = app;
  10
+	
  11
+	HttpServer.super_.call(this,
  12
+		name || 'HTTP Server',
  13
+		http.createServer(app),
  14
+		app.get('port') || 80
  15
+	);
  16
+};
  17
+util.inherits(HttpServer, Server);
17  lib/httpsServer.js
... ...
@@ -0,0 +1,17 @@
  1
+var Server = require("./server"),
  2
+	util = require("util"),
  3
+	https = require('https');
  4
+
  5
+
  6
+exports = module.exports = HttpsServer;
  7
+
  8
+function HttpsServer(options, app, name) {
  9
+	this.app = app;
  10
+	
  11
+	HttpsServer.super_.call(this,
  12
+		name || 'HTTP Server',
  13
+		https.createServer(options, app),
  14
+		app.get('port ssl') || 443
  15
+	);
  16
+};
  17
+util.inherits(HttpsServer, Server);
47  lib/server.js
... ...
@@ -0,0 +1,47 @@
  1
+var Service = require("./service"),
  2
+	util = require("util");
  3
+
  4
+
  5
+exports = module.exports = Server;
  6
+
  7
+function Server(name, server, port, host, backlog) {
  8
+	Server.super_.call(this, name || 'Server');
  9
+
  10
+	this.server = server;
  11
+	this._port = port;
  12
+	this._host = host || '0.0.0.0';
  13
+	this._backlog = backlog || 511;
  14
+};
  15
+util.inherits(Server, Service);
  16
+
  17
+Server.prototype._start = function(callback) {
  18
+	var self = this;
  19
+	
  20
+	this.server.listen(this._port, this._host, this._backlog, function(err){
  21
+		if(err) {
  22
+			callback.apply(self, arguments);
  23
+			return;
  24
+		}
  25
+		
  26
+		console.log(self._serviceName + ' listening on port %d', 
  27
+			self.server.address().port);
  28
+		
  29
+		self.emit('listen');
  30
+		
  31
+		callback.apply(self, arguments);
  32
+	});
  33
+	
  34
+	console.log(this._serviceName + ' started on port %d', this._port);
  35
+};
  36
+
  37
+Server.prototype._stop = function(callback) {
  38
+	var self = this;
  39
+	
  40
+	this.server.close(function(err) {
  41
+		if(!err) {
  42
+			console.log(self._serviceName + ' stopped on port %d', self._port);
  43
+		}
  44
+		
  45
+		callback.apply(self, arguments);
  46
+	});
  47
+};
208  lib/service.js
... ...
@@ -0,0 +1,208 @@
  1
+var EventEmitter = require('events').EventEmitter,
  2
+	util = require('util'),
  3
+	
  4
+	STATUS_INIT = 0,
  5
+	STATUS_INITED = 1,
  6
+	STATUS_STOP = STATUS_INITED,
  7
+	STATUS_START = 2 | STATUS_INITED,
  8
+	STATUS_BUSY = 4,
  9
+	STATUS_STARTING = STATUS_BUSY | STATUS_STOP,
  10
+	STATUS_STOPING = STATUS_BUSY | STATUS_START;
  11
+
  12
+// Expose `Service`
  13
+exports = module.exports = Service;
  14
+
  15
+
  16
+function Service(name) {
  17
+	Service.super_.call(this);
  18
+	
  19
+	this._status = STATUS_INIT;
  20
+	this._serviceName = name;
  21
+	this._onIdle = [];
  22
+};
  23
+util.inherits(Service, EventEmitter);
  24
+
  25
+
  26
+Service.prototype.isStart = function() {
  27
+	return ((this._status & STATUS_START) === STATUS_START);
  28
+};
  29
+
  30
+Service.prototype.isBusy = function() {
  31
+	return ((this._status & STATUS_BUSY) === STATUS_BUSY);
  32
+};
  33
+
  34
+
  35
+Service.prototype.start = function(callback) {
  36
+	if(this.isStart()) {
  37
+		if(typeof callback === "function")
  38
+			callback.call(this, null);
  39
+		return this;
  40
+	}
  41
+	
  42
+	var self = this;
  43
+	
  44
+	this.onIdle(function() {
  45
+		if(self._serviceName)
  46
+			console.log(self._serviceName + ': Starting Service...');
  47
+		
  48
+		self.emit('start');
  49
+	
  50
+		var myCallback = self.setBusy(STATUS_STARTING, function(err) {
  51
+			if(err) {
  52
+				if(typeof callback === "function")
  53
+					callback.apply(this, arguments);
  54
+				return;
  55
+			}
  56
+	
  57
+			if(self._serviceName)
  58
+				console.log(self._serviceName + ': Service Started.');
  59
+			
  60
+			self._status = STATUS_START;
  61
+			self.emit('start.post');
  62
+			
  63
+			if(typeof callback === "function")
  64
+				callback.apply(this, arguments);	
  65
+		});
  66
+		
  67
+		self._start(myCallback);
  68
+	});
  69
+	
  70
+	return this;
  71
+};
  72
+
  73
+Service.prototype._start = function(callback) {
  74
+	callback.call(this, null);
  75
+};
  76
+
  77
+
  78
+Service.prototype.stop = function(callback) {
  79
+	if(!this.isStart()) {
  80
+		if(typeof callback === "function")
  81
+			callback.call(this, null);
  82
+		return this;
  83
+	}
  84
+	
  85
+	var self = this;
  86
+
  87
+	this.onIdle(function() {
  88
+		if(self._serviceName)
  89
+			console.log(self._serviceName + ': Stopping Service...');
  90
+		
  91
+		self.emit('stop');
  92
+		
  93
+		var myCallback = self.setBusy(STATUS_STOPING, function(err) {
  94
+			if(err) {
  95
+				if(typeof callback === "function")
  96
+					callback.apply(this, arguments);
  97
+				return;
  98
+			}
  99
+	
  100
+			if(self._serviceName)
  101
+				console.log(self._serviceName + ': Service Stopped.');
  102
+			
  103
+			self._status = STATUS_STOP;
  104
+			self.emit('stop.post');
  105
+			
  106
+			if(typeof callback === "function")
  107
+				callback.apply(this, arguments);
  108
+		});
  109
+		
  110
+		self._stop(myCallback);
  111
+	});
  112
+	
  113
+	return this;
  114
+};
  115
+
  116
+Service.prototype._stop = function(callback) {
  117
+	callback.call(this, null);
  118
+};
  119
+
  120
+Service.prototype.setBusy = function(status, callback) {
  121
+	var oldStatus = this._status,
  122
+	newStatus = status || STATUS_BUSY,
  123
+	self = this,
  124
+	
  125
+	myCallback = function() {
  126
+		if(self._status === newStatus)
  127
+			self._status = oldStatus;
  128
+		
  129
+		util.debug(self._serviceName + ': Service set as idle (' + self._status + ')');
  130
+		
  131
+		
  132
+		// emit 'busy.release' event
  133
+		var args = Array.prototype.slice.apply(arguments);
  134
+		args.unshift('busy.release');
  135
+		self.emit.apply(self, args);
  136
+		
  137
+		if(typeof callback === "function")
  138
+			callback.apply(self, arguments);
  139
+		
  140
+		// call on idle funcs
  141
+		while(self._onIdle.length > 0) {
  142
+			if(self.isBusy())
  143
+				return;
  144
+			
  145
+			self._onIdle.shift().apply(arguments);
  146
+		}
  147
+	};
  148
+	
  149
+	this._status = newStatus;
  150
+	util.debug(this._serviceName + ': Service set as busy (' + this._status + ')');
  151
+	this.emit('busy');	
  152
+	
  153
+	return myCallback;
  154
+};
  155
+
  156
+Service.prototype.onIdle = function(callback) {
  157
+	if(!this.isBusy()) {
  158
+		callback.call(this, null);
  159
+		return this;
  160
+	}
  161
+	
  162
+	this._onIdle.push(callback);
  163
+	return this;
  164
+};
  165
+
  166
+
  167
+Service.prototype.restart = function(callback) {
  168
+	this.emit('restart');
  169
+	
  170
+	var self = this,
  171
+	start = function() {
  172
+		self.emit('restart.pre');
  173
+		self.start(function(err) {
  174
+			if(!err) {			
  175
+				self.emit('restart.post');
  176
+			}
  177
+			
  178
+			if(typeof callback === "function")
  179
+				callback.apply(self, arguments);
  180
+		});
  181
+	};
  182
+	
  183
+	if(this.isStart()) {
  184
+		this.stop(start);
  185
+	} else {
  186
+		start();
  187
+	}
  188
+	
  189
+	return this;
  190
+};
  191
+
  192
+
  193
+Service.queue = function(queue, callback) {
  194
+	var self = this,
  195
+	myCallback = function(err) {
  196
+		if(err || queue.length < 1) {
  197
+			callback.apply(self, arguments);
  198
+			return;
  199
+		}
  200
+		
  201
+		var next = queue.shift(),
  202
+		service = next[0], method = service[next[1]];
  203
+		
  204
+		method.apply(service, [myCallback]);
  205
+	}
  206
+	
  207
+	myCallback(null);
  208
+};
2  package.json
... ...
@@ -1,7 +1,7 @@
1 1
 {
2 2
   "name": "further",
3 3
   "description": "A lightweight library on Node.js",
4  
-  "version": "0.1.0",
  4
+  "version": "0.1.1",
5 5
   "author": {
6 6
     "name": "Moshe Simantov",
7 7
     "email": "ms@development.co.il"

No commit comments for this range

Something went wrong with that request. Please try again.