2014-02-22 20:13:31 +11:00
|
|
|
var os = require('os'),
|
2015-07-27 14:52:02 +10:00
|
|
|
dns = require('dns'),
|
2014-03-04 10:52:01 +11:00
|
|
|
util = require('util'),
|
2014-02-22 20:13:31 +11:00
|
|
|
colors = require('colors'),
|
|
|
|
pubsub = require('event-pubsub'),
|
|
|
|
eventParser = require('./lib/eventParser.js'),
|
|
|
|
Client = require('./lib/client.js'),
|
2014-02-28 07:04:30 +11:00
|
|
|
Server = require('./lib/socketServer.js');
|
2014-02-22 20:13:31 +11:00
|
|
|
|
|
|
|
colors.setTheme(
|
|
|
|
{
|
|
|
|
good : 'green',
|
|
|
|
notice : 'yellow',
|
|
|
|
warn : 'red',
|
|
|
|
error : 'redBG',
|
|
|
|
debug : 'magenta',
|
|
|
|
variable: 'cyan',
|
|
|
|
data : 'blue'
|
2015-09-28 14:52:16 +10:00
|
|
|
}
|
2014-02-22 20:13:31 +11:00
|
|
|
);
|
|
|
|
|
2015-07-27 15:05:07 +10:00
|
|
|
var IPType=os.networkInterfaces()[
|
|
|
|
Object.keys(os.networkInterfaces())[0]
|
|
|
|
][0].family
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
var defaults={
|
2014-02-28 07:04:30 +11:00
|
|
|
appspace : 'app.',
|
2014-02-22 20:13:31 +11:00
|
|
|
socketRoot : '/tmp/',
|
2015-07-27 15:05:07 +10:00
|
|
|
networkHost : (IPType=='IPv6')? '::1' : '127.0.0.1',
|
2014-02-26 12:15:43 +11:00
|
|
|
networkPort : 8000,
|
2014-02-22 20:13:31 +11:00
|
|
|
id : os.hostname(),
|
|
|
|
encoding : 'utf8',
|
2015-08-23 15:46:55 +10:00
|
|
|
rawBuffer : false,
|
2015-12-10 21:29:15 +11:00
|
|
|
sync : false,
|
2014-02-22 20:13:31 +11:00
|
|
|
silent : false,
|
|
|
|
maxConnections : 100,
|
2014-03-02 11:31:05 +11:00
|
|
|
retry : 500,
|
2015-08-23 16:16:20 +10:00
|
|
|
maxRetries : Infinity,
|
2015-07-27 15:05:07 +10:00
|
|
|
stopRetrying : false,
|
2015-09-28 14:52:16 +10:00
|
|
|
IPType : IPType,
|
|
|
|
tls : false
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
var ipc = {
|
|
|
|
config : defaults,
|
|
|
|
connectTo : connect,
|
2014-02-27 09:04:09 +11:00
|
|
|
connectToNet: connectNet,
|
2014-03-01 23:13:06 +11:00
|
|
|
disconnect : disconnect,
|
2014-02-22 20:13:31 +11:00
|
|
|
serve : serve,
|
2014-02-27 09:04:09 +11:00
|
|
|
serveNet : serveNet,
|
2014-02-22 20:13:31 +11:00
|
|
|
of : {},
|
|
|
|
server : false,
|
|
|
|
log : log
|
|
|
|
}
|
|
|
|
|
|
|
|
function log(){
|
2016-01-10 16:48:03 +11:00
|
|
|
if(ipc.config.silent){
|
2014-02-22 20:13:31 +11:00
|
|
|
return;
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-03-02 09:31:40 +11:00
|
|
|
var args=Array.prototype.slice.call(arguments);
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-03-02 09:31:40 +11:00
|
|
|
for(var i=0, count=args.length; i<count; i++){
|
2016-01-10 16:48:03 +11:00
|
|
|
if(typeof args[i] != 'object'){
|
2014-03-02 09:31:40 +11:00
|
|
|
continue;
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-03-04 10:52:01 +11:00
|
|
|
args[i]=util.inspect(args[i],{colors:true});
|
2014-03-02 09:31:40 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
console.log(
|
2014-03-02 09:31:40 +11:00
|
|
|
args.join(' ')
|
2014-02-22 20:13:31 +11:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
function disconnect(id){
|
2016-01-10 16:48:03 +11:00
|
|
|
if(!ipc.of[id]){
|
2014-03-01 23:13:06 +11:00
|
|
|
return;
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-09-06 12:34:51 +10:00
|
|
|
ipc.of[id].config.stopRetrying=true;
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
ipc.of[id].off('*');
|
|
|
|
if(ipc.of[id].socket){
|
2016-01-10 16:48:03 +11:00
|
|
|
if(ipc.of[id].socket.destroy){
|
2014-03-01 23:13:06 +11:00
|
|
|
ipc.of[id].socket.destroy();
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2014-03-01 23:13:06 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
delete ipc.of[id];
|
2016-01-10 16:48:03 +11:00
|
|
|
};
|
2014-03-01 23:13:06 +11:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
function serve(path,callback){
|
|
|
|
if(typeof path=='function'){
|
|
|
|
callback=path;
|
2015-09-28 14:52:16 +10:00
|
|
|
path=false;
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
if(!path){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Server path not specified, so defaulting to'.notice,
|
|
|
|
'ipc.config.socketRoot + ipc.config.appspace + ipc.config.id'.variable,
|
2014-02-22 20:13:31 +11:00
|
|
|
(ipc.config.socketRoot+ipc.config.appspace+ipc.config.id).data
|
|
|
|
);
|
|
|
|
path=ipc.config.socketRoot+ipc.config.appspace+ipc.config.id;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2016-01-10 16:48:03 +11:00
|
|
|
if(!callback){
|
2014-02-26 12:15:43 +11:00
|
|
|
callback=function(){};
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
ipc.server=new Server(
|
|
|
|
path,
|
|
|
|
ipc.config,
|
|
|
|
log
|
|
|
|
);
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
ipc.server.on(
|
|
|
|
'start',
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-27 09:04:09 +11:00
|
|
|
function serveNet(host,port,UDPType,callback){
|
2014-02-26 12:15:43 +11:00
|
|
|
if(typeof host=='number'){
|
2014-02-27 09:04:09 +11:00
|
|
|
callback=UDPType;
|
|
|
|
UDPType=port;
|
2014-02-26 12:15:43 +11:00
|
|
|
port=host;
|
2015-09-28 14:52:16 +10:00
|
|
|
host=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
|
|
|
if(typeof host=='function'){
|
|
|
|
callback=host;
|
2014-02-27 09:04:09 +11:00
|
|
|
UDPType=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
host=false;
|
2015-09-28 14:52:16 +10:00
|
|
|
port=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2014-02-27 09:04:09 +11:00
|
|
|
if(!host){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Server host not specified, so defaulting to'.notice,
|
|
|
|
'ipc.config.networkHost'.variable,
|
2014-02-27 09:04:09 +11:00
|
|
|
ipc.config.networkHost.data
|
|
|
|
);
|
|
|
|
host=ipc.config.networkHost;
|
|
|
|
}
|
|
|
|
if(host.toLowerCase()=='udp4' || host.toLowerCase()=='udp6'){
|
2014-02-26 12:15:43 +11:00
|
|
|
callback=port;
|
2014-02-27 09:04:09 +11:00
|
|
|
UDPType=host.toLowerCase();
|
|
|
|
port=false;
|
2015-09-28 14:52:16 +10:00
|
|
|
host=ipc.config.networkHost;
|
2014-02-27 09:04:09 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-27 09:04:09 +11:00
|
|
|
if(typeof port=='string'){
|
|
|
|
callback=UDPType;
|
|
|
|
UDPType=port;
|
2015-09-28 14:52:16 +10:00
|
|
|
port=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2014-02-27 09:04:09 +11:00
|
|
|
if(typeof port=='function'){
|
|
|
|
callback=port;
|
|
|
|
UDPType=false;
|
|
|
|
port=false;
|
|
|
|
}
|
2014-02-26 12:15:43 +11:00
|
|
|
if(!port){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Server port not specified, so defaulting to'.notice,
|
|
|
|
'ipc.config.networkPort'.variable,
|
2014-02-26 12:15:43 +11:00
|
|
|
ipc.config.networkPort
|
|
|
|
);
|
|
|
|
port=ipc.config.networkPort;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-27 09:04:09 +11:00
|
|
|
if(typeof UDPType=='function'){
|
|
|
|
callback=UDPType;
|
|
|
|
UDPType=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2016-01-10 16:48:03 +11:00
|
|
|
if(!callback){
|
2014-02-26 12:15:43 +11:00
|
|
|
callback=function(){};
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-07-27 14:52:02 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
ipc.server=new Server(
|
|
|
|
host,
|
|
|
|
ipc.config,
|
|
|
|
log,
|
|
|
|
port
|
|
|
|
);
|
2015-07-27 14:52:02 +10:00
|
|
|
|
2016-01-10 16:48:03 +11:00
|
|
|
if(UDPType){
|
2014-02-27 09:04:09 +11:00
|
|
|
ipc.server[UDPType]=true;
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-07-27 14:52:02 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
ipc.server.on(
|
|
|
|
'start',
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
function connect(id,path,callback){
|
|
|
|
if(typeof path == 'function'){
|
|
|
|
callback=path;
|
|
|
|
path=false;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2016-01-10 16:48:03 +11:00
|
|
|
if(!callback){
|
2014-02-22 20:13:31 +11:00
|
|
|
callback=function(){};
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
if(!id){
|
|
|
|
ipc.log(
|
|
|
|
'Service id required'.warn,
|
|
|
|
'Requested service connection without specifying service id. Aborting connection attempt'.notice
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
if(!path){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Service path not specified, so defaulting to'.notice,
|
|
|
|
'ipc.config.socketRoot + ipc.config.appspace + id'.variable,
|
2014-02-22 20:13:31 +11:00
|
|
|
(ipc.config.socketRoot+ipc.config.appspace+id).data
|
|
|
|
);
|
|
|
|
path=ipc.config.socketRoot+ipc.config.appspace+id;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
if(ipc.of[id]){
|
|
|
|
if(!ipc.of[id].socket.destroyed){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Already Connected to'.notice,
|
2014-02-22 20:13:31 +11:00
|
|
|
id.variable,
|
|
|
|
'- So executing success without connection'.notice
|
|
|
|
);
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2015-08-23 15:56:51 +10:00
|
|
|
ipc.of[id].socket.destroy();
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
ipc.of[id] = new Client(ipc.config,ipc.log);
|
|
|
|
ipc.of[id].id = id;
|
|
|
|
ipc.of[id].path = path;
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
ipc.of[id].connect();
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-07-28 04:40:52 +10:00
|
|
|
callback(ipc);
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
|
2014-02-28 07:04:30 +11:00
|
|
|
function connectNet(id,host,port,callback){
|
2014-02-26 12:15:43 +11:00
|
|
|
if(!id){
|
|
|
|
ipc.log(
|
|
|
|
'Service id required'.warn,
|
|
|
|
'Requested service connection without specifying service id. Aborting connection attempt'.notice
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(typeof host=='number'){
|
|
|
|
callback=port;
|
|
|
|
port=host;
|
2014-02-27 09:04:09 +11:00
|
|
|
host=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
|
|
|
if(typeof host=='function'){
|
|
|
|
callback=host;
|
|
|
|
host=false;
|
2015-09-28 14:52:16 +10:00
|
|
|
port=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2014-02-27 09:04:09 +11:00
|
|
|
if(!host){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Server host not specified, so defaulting to'.notice,
|
|
|
|
'ipc.config.networkHost'.variable,
|
2014-02-27 09:04:09 +11:00
|
|
|
ipc.config.networkHost.data
|
|
|
|
);
|
|
|
|
host=ipc.config.networkHost;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
if(typeof port=='function'){
|
|
|
|
callback=port;
|
2015-09-28 14:52:16 +10:00
|
|
|
port=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
|
|
|
if(!port){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Server port not specified, so defaulting to'.notice,
|
|
|
|
'ipc.config.networkPort'.variable,
|
2014-02-26 12:15:43 +11:00
|
|
|
ipc.config.networkPort
|
|
|
|
);
|
|
|
|
port=ipc.config.networkPort;
|
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-27 09:04:09 +11:00
|
|
|
if(typeof callback == 'string'){
|
|
|
|
UDPType=callback;
|
|
|
|
callback=false;
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2016-01-10 16:48:03 +11:00
|
|
|
if(!callback){
|
2014-02-26 12:15:43 +11:00
|
|
|
callback=function(){};
|
2016-01-10 16:48:03 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
if(ipc.of[id]){
|
|
|
|
if(!ipc.of[id].socket.destroyed){
|
|
|
|
ipc.log(
|
2015-09-28 14:52:16 +10:00
|
|
|
'Already Connected to'.notice,
|
2014-02-26 12:15:43 +11:00
|
|
|
id.variable,
|
|
|
|
'- So executing success without connection'.notice
|
|
|
|
);
|
|
|
|
callback();
|
|
|
|
return;
|
|
|
|
}
|
2015-08-23 15:56:51 +10:00
|
|
|
ipc.of[id].socket.destroy();
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
ipc.of[id] = new Client(ipc.config,ipc.log);
|
|
|
|
ipc.of[id].id = id;
|
|
|
|
ipc.of[id].path = host;
|
|
|
|
ipc.of[id].port = port;
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
ipc.of[id].connect();
|
2015-09-28 14:52:16 +10:00
|
|
|
|
2014-07-28 04:40:52 +10:00
|
|
|
callback(ipc);
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
|
|
|
|
2015-07-25 05:13:53 +10:00
|
|
|
module.exports=ipc;
|