2014-02-22 20:13:31 +11:00
var net = require ( 'net' ) ,
eventParser = require ( '../lib/eventParser.js' ) ,
pubsub = require ( 'event-pubsub' ) ;
function init ( config , log ) {
var client = {
config : config ,
socket : false ,
connect : connect ,
emit : emit ,
2014-03-02 11:31:05 +11:00
log : log ,
2014-08-28 13:41:12 +10:00
retriesRemaining : 0
2014-02-22 20:13:31 +11:00
}
new pubsub ( client ) ;
return client ;
}
function emit ( type , data ) {
this . log ( 'dispatching event to ' . debug , this . id . variable , this . path . variable , ' : ' , type . data , ',' , data ) ;
if ( ! data )
data = false ;
this . socket . write (
eventParser . format (
{
type : type ,
data : data
}
)
) ;
} ;
function connect ( ) {
//init client object for scope persistance especially inside of socket events.
var client = this ;
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 ) {
2014-02-28 07:31:40 +11:00
client . log ( 'Connecting client on Unix Socket :' . debug , client . path . variable ) ;
2014-02-26 12:15:43 +11:00
client . socket = net . connect (
{
path : client . path
}
) ;
} else {
2014-02-28 07:31:40 +11:00
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 ) ;
client . socket . on (
'error' ,
function ( err ) {
client . log ( '\n\n######\nerror: ' . error , err ) ;
}
) ;
client . socket . on (
'connect' ,
function ( ) {
client . trigger ( 'connect' ) ;
2014-08-28 13:41:12 +10:00
client . retriesRemaining = client . config . maxRetries || 0 ;
client . log ( 'retrying reset' )
2014-02-22 20:13:31 +11:00
}
) ;
client . socket . on (
'close' ,
function ( ) {
client . log ( 'connection closed' . notice , client . id . variable , client . path . variable ) ;
2014-03-02 11:31:05 +11:00
2014-08-28 13:41:12 +10:00
if ( client . stopRetrying || client . retriesRemaining < 1 ) {
2014-09-01 03:50:34 +10:00
if ( client . config . maxRetries ) {
client . log ( client . config . id . variable , 'exceeded connection rety amount of' . warn , client . config . stopRetrying , " or stopRetrying flag set." ) ;
client . socket . destroy ( ) ;
client = undefined ;
return ;
}
2014-03-02 11:31:05 +11:00
}
client . isRetrying = true ;
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
} ,
100
)
2014-02-22 20:13:31 +11:00
}
}
) ( client ) ,
client . config . retry
) ;
client . trigger ( 'disconnect' ) ;
}
) ;
client . socket . on (
'data' ,
function ( data ) {
client . log ( '## recieved events ##' . rainbow ) ;
2014-03-04 07:02:56 +11:00
if ( ! this . ipcBuffer )
this . ipcBuffer = '' ;
data = ( this . ipcBuffer += data ) ;
if ( data . slice ( - 1 ) != eventParser . delimiter ) {
client . log ( 'Socket buffer size exceeded consider smaller messages or a larger buffer.' . warn , 'Implementing software buffer expansion for this message.' . notice ) ;
return ;
}
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 e = JSON . parse (
events [ i ]
) ;
client . log ( 'detected event of type ' . debug , e . type . data , e . data ) ;
client . trigger (
e . type ,
e . data
) ;
}
}
) ;
}
2014-02-28 07:31:40 +11:00
module . exports = init ;