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;
}
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);

View File

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

View File

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