added in remote TCP connections

This commit is contained in:
Brandon Miller 2014-02-25 17:15:43 -08:00
parent 489e66d01c
commit ec406a70bf
6 changed files with 224 additions and 19 deletions

View file

@ -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) ipc.server.start();

View file

@ -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)

View file

@ -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();

View file

@ -39,11 +39,20 @@ function connect(){
return; return;
} }
client.socket = net.connect( if(!client.port){
{ client.socket = net.connect(
path:client.path {
} path:client.path
); }
);
}else{
client.socket = net.connect(
{
port:client.port,
host:client.path
}
);
}
client.socket.setEncoding(this.config.encoding); client.socket.setEncoding(this.config.encoding);

View file

@ -36,10 +36,11 @@ function broadcast(type,data){
} }
}; };
function init(path,config,log){ function init(path,config,log,port){
var server={ var server={
config : config, config : config,
path : path, path : path,
port : port,
log : log, log : log,
server : false, server : false,
sockets : [], sockets : [],
@ -72,7 +73,7 @@ function init(path,config,log){
( (
function(server){ function(server){
return function () { 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( server.server=net.createServer(
function(socket) { function(socket) {
socket.setEncoding(server.config.encoding); 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.server.listen(
server.port,
server.path, server.path,
( started
function(server){
return function(socket){
server.onStart(socket)
}
}
)(server)
); );
server.server.maxConnections=server.maxConnections;
} }
} }
)(this) )(this)

View file

@ -22,6 +22,8 @@ var defaults={
root : process.env.HOME, root : process.env.HOME,
appspace : socketPrefix, appspace : socketPrefix,
socketRoot : '/tmp/', socketRoot : '/tmp/',
networkHost : 'localhost',
networkPort : 8000,
id : os.hostname(), id : os.hostname(),
encoding : 'utf8', encoding : 'utf8',
silent : false, silent : false,
@ -32,7 +34,9 @@ var defaults={
var ipc = { var ipc = {
config : defaults, config : defaults,
connectTo : connect, connectTo : connect,
connectToTCP: connectTCP,
serve : serve, serve : serve,
serveTCP : serveTCP,
of : {}, of : {},
server : false, server : false,
log : log log : log
@ -61,6 +65,9 @@ function serve(path,callback){
path=ipc.config.socketRoot+ipc.config.appspace+ipc.config.id; path=ipc.config.socketRoot+ipc.config.appspace+ipc.config.id;
} }
if(!callback)
callback=function(){};
ipc.server=new Server( ipc.server=new Server(
path, path,
ipc.config, 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){ function connect(id,path,callback){
if(typeof path == 'function'){ if(typeof path == 'function'){
callback=path; callback=path;
@ -121,4 +175,69 @@ function connect(id,path,callback){
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; module.exports=ipc;