2014-02-22 20:13:31 +11:00
|
|
|
var net = require('net'),
|
2015-09-28 14:52:16 +10:00
|
|
|
tls = require('tls'),
|
2014-02-22 20:13:31 +11:00
|
|
|
eventParser = require('../lib/eventParser.js'),
|
2015-09-27 20:32:14 +10:00
|
|
|
pubsub = require('event-pubsub'),
|
2015-09-28 14:52:16 +10:00
|
|
|
Message = require('js-message'),
|
2015-12-10 19:11:14 +11:00
|
|
|
fs = require('fs'),
|
|
|
|
Queue = require('js-queue');
|
2014-02-22 20:13:31 +11:00
|
|
|
|
|
|
|
function init(config,log){
|
|
|
|
var client={
|
|
|
|
config : config,
|
2015-12-10 19:11:14 +11:00
|
|
|
queue : new Queue,
|
2014-02-22 20:13:31 +11:00
|
|
|
socket : false,
|
|
|
|
connect : connect,
|
|
|
|
emit : emit,
|
2014-03-02 11:31:05 +11:00
|
|
|
log : log,
|
2014-09-05 15:06:25 +10:00
|
|
|
retriesRemaining:config.maxRetries||0
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
new pubsub(client);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
return client;
|
|
|
|
}
|
|
|
|
|
|
|
|
function emit(type,data){
|
|
|
|
this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', type.data,',', data);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
|
|
|
var message=new Message;
|
|
|
|
message.type=type;
|
|
|
|
message.data=data;
|
|
|
|
|
2015-08-23 15:46:55 +10:00
|
|
|
if(this.config.rawBuffer){
|
2015-09-27 20:32:14 +10:00
|
|
|
message=new Buffer(type,this.encoding);
|
2015-08-23 15:46:55 +10:00
|
|
|
}else{
|
2015-09-27 20:32:14 +10:00
|
|
|
message=eventParser.format(message);
|
2015-08-23 15:46:55 +10:00
|
|
|
}
|
|
|
|
|
2015-12-10 19:11:14 +11:00
|
|
|
if(!this.config.sync){
|
|
|
|
this.socket.write(message);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.queue.add(
|
|
|
|
syncEmit.bind(this,message)
|
|
|
|
);
|
2014-02-22 20:13:31 +11:00
|
|
|
};
|
|
|
|
|
2015-12-10 19:11:14 +11:00
|
|
|
function syncEmit(message){
|
|
|
|
this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', message.data);
|
|
|
|
this.socket.write(message);
|
|
|
|
}
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
function connect(){
|
|
|
|
//init client object for scope persistance especially inside of socket events.
|
|
|
|
var client=this;
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.log('requested connection to '.debug, client.id.variable, client.path.variable);
|
|
|
|
if(!this.path){
|
|
|
|
client.log('\n\n######\nerror: '.error, client.id .info,' client has not specified socket path it wishes to connect to.'.error);
|
|
|
|
return;
|
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-26 12:15:43 +11:00
|
|
|
if(!client.port){
|
2014-02-28 07:31:40 +11:00
|
|
|
client.log('Connecting client on Unix Socket :'.debug, client.path.variable);
|
2015-12-03 05:45:12 +11:00
|
|
|
|
|
|
|
var path = client.path;
|
|
|
|
|
2015-11-12 22:57:17 +11:00
|
|
|
if (process.platform ==='win32' && !client.path.startsWith('\\\\.\\pipe\\')){
|
2015-12-01 06:53:06 +11:00
|
|
|
path = path.replace(/^\//, '');
|
|
|
|
path = path.replace(/\//g, '-');
|
|
|
|
path= '\\\\.\\pipe\\'+path;
|
2015-09-27 21:45:57 +10:00
|
|
|
}
|
2014-02-26 12:15:43 +11:00
|
|
|
client.socket = net.connect(
|
|
|
|
{
|
2015-12-01 06:53:06 +11:00
|
|
|
path: path
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}else{
|
2015-09-28 14:52:16 +10:00
|
|
|
if(!client.config.tls){
|
|
|
|
client.log('Connecting client via TCP to'.debug, client.path.variable ,client.port);
|
|
|
|
client.socket = net.connect(
|
|
|
|
{
|
|
|
|
port:client.port,
|
|
|
|
host:client.path
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}else{
|
|
|
|
client.log('Connecting client via TLS to'.debug, client.path.variable ,client.port,client.config.tls);
|
|
|
|
if(client.config.tls.private){
|
|
|
|
client.config.tls.key=fs.readFileSync(client.config.tls.private);
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2015-09-28 14:52:16 +10:00
|
|
|
if(client.config.tls.public){
|
|
|
|
client.config.tls.cert=fs.readFileSync(client.config.tls.public);
|
|
|
|
}
|
|
|
|
if(client.config.tls.trustedConnections){
|
|
|
|
if(typeof client.config.tls.trustedConnections === 'string'){
|
|
|
|
client.config.tls.trustedConnections=[client.config.tls.trustedConnections];
|
|
|
|
}
|
|
|
|
client.config.tls.ca=[];
|
|
|
|
for(var i=0; i<client.config.tls.trustedConnections.length; i++){
|
|
|
|
client.config.tls.ca.push(
|
|
|
|
fs.readFileSync(client.config.tls.trustedConnections[i])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
client.config.tls.host=client.path;
|
|
|
|
client.config.tls.port=client.port;
|
|
|
|
|
|
|
|
client.socket = tls.connect(
|
|
|
|
client.config.tls
|
|
|
|
);
|
|
|
|
}
|
2014-02-26 12:15:43 +11:00
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.setEncoding(this.config.encoding);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'error',
|
|
|
|
function(err){
|
|
|
|
client.log('\n\n######\nerror: '.error, err);
|
2015-12-01 12:43:43 +11:00
|
|
|
client.trigger('error', err);
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'connect',
|
|
|
|
function(){
|
|
|
|
client.trigger('connect');
|
2015-08-23 16:16:20 +10:00
|
|
|
client.retriesRemaining=client.config.maxRetries;
|
2015-12-01 12:43:43 +11:00
|
|
|
client.log('retrying reset');
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'close',
|
|
|
|
function(){
|
2015-08-23 16:16:20 +10:00
|
|
|
client.log('connection closed'.notice ,client.id.variable , client.path.variable, client.retriesRemaining+' tries remaining of '+client.config.maxRetries);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-09-07 09:28:49 +10:00
|
|
|
if(
|
2015-08-23 16:16:20 +10:00
|
|
|
client.config.stopRetrying || client.retriesRemaining<1
|
2015-08-23 15:46:55 +10:00
|
|
|
|
2014-09-07 09:28:49 +10:00
|
|
|
){
|
2015-12-03 05:05:47 +11:00
|
|
|
client.trigger('disconnect');
|
2014-09-05 15:06:25 +10:00
|
|
|
client.log(
|
|
|
|
client.config.id.variable,
|
|
|
|
'exceeded connection rety amount of'.warn,
|
|
|
|
" or stopRetrying flag set."
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-09-05 15:06:25 +10:00
|
|
|
client.socket.destroy();
|
2015-12-03 05:05:47 +11:00
|
|
|
client.trigger('destroy');
|
2014-09-05 15:06:25 +10:00
|
|
|
client=undefined;
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-09-05 15:06:25 +10:00
|
|
|
return;
|
2014-03-02 11:31:05 +11:00
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-03-02 11:31:05 +11:00
|
|
|
client.isRetrying=true;
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
setTimeout(
|
|
|
|
(
|
|
|
|
function(client){
|
|
|
|
return function(){
|
2014-08-28 13:41:12 +10:00
|
|
|
client.retriesRemaining--;
|
2014-03-02 11:31:05 +11:00
|
|
|
client.isRetrying=false;
|
2014-02-22 20:13:31 +11:00
|
|
|
client.connect();
|
2014-03-02 11:31:05 +11:00
|
|
|
setTimeout(
|
|
|
|
function(){
|
|
|
|
if(!client.isRetrying)
|
2014-09-01 03:50:34 +10:00
|
|
|
client.retriesRemaining=client.config.maxRetries;
|
2014-03-02 11:31:05 +11:00
|
|
|
},
|
2015-09-27 20:32:14 +10:00
|
|
|
100
|
2014-03-02 11:31:05 +11:00
|
|
|
)
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)(client),
|
|
|
|
client.config.retry
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.trigger('disconnect');
|
|
|
|
}
|
|
|
|
);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
client.socket.on(
|
|
|
|
'data',
|
|
|
|
function(data) {
|
|
|
|
client.log('## recieved events ##'.rainbow);
|
2015-08-23 15:46:55 +10:00
|
|
|
if(client.config.rawBuffer){
|
|
|
|
client.trigger(
|
|
|
|
'data',
|
|
|
|
new Buffer(data,this.encoding)
|
|
|
|
);
|
2015-12-10 19:11:14 +11:00
|
|
|
if(!client.config.sync){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.queue.next();
|
2015-08-23 15:46:55 +10:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-04 07:02:56 +11:00
|
|
|
if(!this.ipcBuffer)
|
|
|
|
this.ipcBuffer='';
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-03-04 07:02:56 +11:00
|
|
|
data=(this.ipcBuffer+=data);
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2015-08-12 03:11:29 +10:00
|
|
|
if(data.slice(-1)!=eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1){
|
2014-09-24 20:26:05 +10:00
|
|
|
client.log('Implementing larger buffer for this socket message. You may want to consider smaller messages'.notice);
|
2014-03-04 07:02:56 +11:00
|
|
|
return;
|
|
|
|
}
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-03-04 07:02:56 +11:00
|
|
|
this.ipcBuffer='';
|
2015-09-27 20:32:14 +10:00
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
var events = eventParser.parse(data);
|
|
|
|
var eCount = events.length;
|
|
|
|
for(var i=0; i<eCount; i++){
|
2015-09-27 20:32:14 +10:00
|
|
|
var message=new Message;
|
|
|
|
message.load(events[i]);
|
|
|
|
|
|
|
|
client.log('detected event of type '.debug, message.type.data, message.data);
|
2014-02-22 20:13:31 +11:00
|
|
|
client.trigger(
|
2015-09-27 20:32:14 +10:00
|
|
|
message.type,
|
|
|
|
message.data
|
2014-02-22 20:13:31 +11:00
|
|
|
);
|
|
|
|
}
|
2015-12-10 19:11:14 +11:00
|
|
|
|
|
|
|
if(!client.config.sync){
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
client.queue.next();
|
2014-02-22 20:13:31 +11:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-02-28 07:31:40 +11:00
|
|
|
module.exports=init;
|