node-ipc/lib/client.js

174 lines
4.9 KiB
JavaScript
Raw Permalink Normal View History

2014-02-22 20:13:31 +11:00
var net = require('net'),
eventParser = require('../lib/eventParser.js'),
pubsub = require('event-pubsub'),
Message = require('js-message');
2014-02-22 20:13:31 +11:00
function init(config,log){
var client={
config : config,
socket : false,
connect : connect,
emit : emit,
log : log,
retriesRemaining:config.maxRetries||0
2014-02-22 20:13:31 +11:00
}
new pubsub(client);
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);
var message=new Message;
message.type=type;
message.data=data;
2015-08-23 15:46:55 +10:00
if(this.config.rawBuffer){
message=new Buffer(type,this.encoding);
2015-08-23 15:46:55 +10:00
}else{
message=eventParser.format(message);
2015-08-23 15:46:55 +10:00
}
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;
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;
}
2014-02-26 12:15:43 +11:00
if(!client.port){
client.log('Connecting client on Unix Socket :'.debug, client.path.variable);
if (process.platform ==='win32'){
client.path = client.path.replace(/^\//, '');
client.path = client.path.replace(/\//g, '-');
client.path= '\\\\.\\pipe\\'+client.path;
}
2014-02-26 12:15:43 +11:00
client.socket = net.connect(
{
path:client.path
}
);
}else{
client.log('Connecting client via TCP to'.debug, client.path.variable ,client.port);
2014-02-26 12:15:43 +11:00
client.socket = net.connect(
{
port:client.port,
host:client.path
}
);
}
2014-02-22 20:13:31 +11:00
client.socket.setEncoding(this.config.encoding);
2014-02-22 20:13:31 +11:00
client.socket.on(
'error',
function(err){
client.log('\n\n######\nerror: '.error, err);
}
);
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;
client.log('retrying reset')
2014-02-22 20:13:31 +11: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);
if(
2015-08-23 16:16:20 +10:00
client.config.stopRetrying || client.retriesRemaining<1
2015-08-23 15:46:55 +10:00
){
client.log(
client.config.id.variable,
'exceeded connection rety amount of'.warn,
" or stopRetrying flag set."
);
client.socket.destroy();
client=undefined;
return;
}
client.isRetrying=true;
2014-02-22 20:13:31 +11:00
setTimeout(
(
function(client){
return function(){
client.retriesRemaining--;
client.isRetrying=false;
2014-02-22 20:13:31 +11:00
client.connect();
setTimeout(
function(){
if(!client.isRetrying)
2014-09-01 03:50:34 +10:00
client.retriesRemaining=client.config.maxRetries;
},
100
)
2014-02-22 20:13:31 +11:00
}
}
)(client),
client.config.retry
);
2014-02-22 20:13:31 +11:00
client.trigger('disconnect');
}
);
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)
);
return;
}
2014-03-04 07:02:56 +11:00
if(!this.ipcBuffer)
this.ipcBuffer='';
2014-03-04 07:02:56 +11:00
data=(this.ipcBuffer+=data);
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;
}
2014-03-04 07:02:56 +11:00
this.ipcBuffer='';
2014-02-22 20:13:31 +11:00
var events = eventParser.parse(data);
var eCount = events.length;
for(var i=0; i<eCount; i++){
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(
message.type,
message.data
2014-02-22 20:13:31 +11:00
);
}
}
);
}
module.exports=init;