From ec406a70bf958f39339f67b9e8fb3a165dfaac9d Mon Sep 17 00:00:00 2001 From: Brandon Miller Date: Tue, 25 Feb 2014 17:15:43 -0800 Subject: [PATCH] added in remote TCP connections --- example/local/basic/world-server.js | 4 +- example/remote/basic/hello-client.js | 41 +++++++++ example/remote/basic/world-server.js | 30 +++++++ lib/client.js | 19 +++-- lib/socketServer.js | 30 ++++--- node-ipc.js | 119 +++++++++++++++++++++++++++ 6 files changed, 224 insertions(+), 19 deletions(-) create mode 100644 example/remote/basic/hello-client.js create mode 100644 example/remote/basic/world-server.js diff --git a/example/local/basic/world-server.js b/example/local/basic/world-server.js index 271b859..b42ef0f 100644 --- a/example/local/basic/world-server.js +++ b/example/local/basic/world-server.js @@ -25,6 +25,6 @@ ipc.serve( } ); -ipc.server.start(); +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; -console.log(ipc) \ No newline at end of file +ipc.server.start(); \ No newline at end of file diff --git a/example/remote/basic/hello-client.js b/example/remote/basic/hello-client.js new file mode 100644 index 0000000..c88592a --- /dev/null +++ b/example/remote/basic/hello-client.js @@ -0,0 +1,41 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'hello'; +ipc.config.retry= 1500; + +ipc.connectToTCP( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + ipc.of.world.emit( + 'message', + 'hello' + ) + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'message', + function(data){ + ipc.log('got a message from world : '.debug, data); + } + ); + } +); + +console.log(ipc) \ No newline at end of file diff --git a/example/remote/basic/world-server.js b/example/remote/basic/world-server.js new file mode 100644 index 0000000..86b3dae --- /dev/null +++ b/example/remote/basic/world-server.js @@ -0,0 +1,30 @@ +var ipc=require('../../../node-ipc'); + +/***************************************\ + * + * You should start both hello and world + * then you will see them communicating. + * + * *************************************/ + +ipc.config.id = 'world'; +ipc.config.retry= 1500; + +ipc.serveTCP( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.log('got a message : '.debug, data); + socket.emit( + 'message', + data+' world!' + ); + } + ); + } +); + +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; + +ipc.server.start(); \ No newline at end of file diff --git a/lib/client.js b/lib/client.js index 1ea7fe0..18a0d34 100644 --- a/lib/client.js +++ b/lib/client.js @@ -39,11 +39,20 @@ function connect(){ return; } - client.socket = net.connect( - { - path:client.path - } - ); + if(!client.port){ + client.socket = net.connect( + { + path:client.path + } + ); + }else{ + client.socket = net.connect( + { + port:client.port, + host:client.path + } + ); + } client.socket.setEncoding(this.config.encoding); diff --git a/lib/socketServer.js b/lib/socketServer.js index 9892f33..85226f8 100644 --- a/lib/socketServer.js +++ b/lib/socketServer.js @@ -36,10 +36,11 @@ function broadcast(type,data){ } }; -function init(path,config,log){ +function init(path,config,log,port){ var server={ config : config, path : path, + port : port, log : log, server : false, sockets : [], @@ -72,7 +73,7 @@ function init(path,config,log){ ( function(server){ return function () { - server.log('starting server on '.debug,server.path.variable); + server.log('starting server on '.debug,server.path.variable,((server.port)?':'+server.port:'').variable); server.server=net.createServer( function(socket) { socket.setEncoding(server.config.encoding); @@ -131,20 +132,25 @@ function init(path,config,log){ } ); + function started(socket){ + server.onStart(socket) + } + + if(!port){ + server.server.listen( + server.path, + started + ); + + server.server.maxConnections=server.maxConnections; + return; + } server.server.listen( + server.port, server.path, - ( - function(server){ - return function(socket){ - server.onStart(socket) - } - } - )(server) + started ); - - server.server.maxConnections=server.maxConnections; - } } )(this) diff --git a/node-ipc.js b/node-ipc.js index 6d4ea8d..9e067b7 100644 --- a/node-ipc.js +++ b/node-ipc.js @@ -22,6 +22,8 @@ var defaults={ root : process.env.HOME, appspace : socketPrefix, socketRoot : '/tmp/', + networkHost : 'localhost', + networkPort : 8000, id : os.hostname(), encoding : 'utf8', silent : false, @@ -32,7 +34,9 @@ var defaults={ var ipc = { config : defaults, connectTo : connect, + connectToTCP: connectTCP, serve : serve, + serveTCP : serveTCP, of : {}, server : false, log : log @@ -61,6 +65,9 @@ function serve(path,callback){ path=ipc.config.socketRoot+ipc.config.appspace+ipc.config.id; } + if(!callback) + callback=function(){}; + ipc.server=new Server( path, ipc.config, @@ -73,6 +80,53 @@ function serve(path,callback){ ); } +function serveTCP(host,port,callback){ + if(typeof host=='number'){ + callback=port; + port=host; + host=false; + } + if(typeof host=='function'){ + callback=host; + host=false; + port=false; + } + if(typeof port=='function'){ + callback=port; + port=false; + } + if(!port){ + ipc.log( + 'Server port not specified, so defaulting to'.notice, + 'ipc.config.networkPort'.variable, + ipc.config.networkPort + ); + port=ipc.config.networkPort; + } + if(!host){ + ipc.log( + 'Server host not specified, so defaulting to'.notice, + 'ipc.config.networkHost'.variable, + ipc.config.networkHost.data + ); + host=ipc.config.networkHost; + } + if(!callback) + callback=function(){}; + + ipc.server=new Server( + host, + ipc.config, + log, + port + ); + + ipc.server.on( + 'start', + callback + ); +} + function connect(id,path,callback){ if(typeof path == 'function'){ callback=path; @@ -121,4 +175,69 @@ function connect(id,path,callback){ callback(); } +function connectTCP(id,host,port,callback){ + 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; + host=false; + } + if(typeof host=='function'){ + callback=host; + host=false; + port=false; + } + if(typeof port=='function'){ + callback=port; + port=false; + } + if(!port){ + ipc.log( + 'Server port not specified, so defaulting to'.notice, + 'ipc.config.networkPort'.variable, + ipc.config.networkPort + ); + port=ipc.config.networkPort; + } + if(!host){ + ipc.log( + 'Server host not specified, so defaulting to'.notice, + 'ipc.config.networkHost'.variable, + ipc.config.networkHost.data + ); + host=ipc.config.networkHost; + } + if(!callback) + callback=function(){}; + + if(ipc.of[id]){ + if(!ipc.of[id].socket.destroyed){ + ipc.log( + 'Already Connected to'.notice, + id.variable, + '- So executing success without connection'.notice + ); + callback(); + return; + } + ipc.of[id].destroy(); + } + + ipc.of[id] = new Client(ipc.config,ipc.log); + ipc.of[id].id = id; + ipc.of[id].path = host; + ipc.of[id].port = port; + + ipc.of[id].connect(); + + callback(); +} + module.exports=ipc; \ No newline at end of file