added UDP support and examples
This commit is contained in:
parent
ec406a70bf
commit
91b942f6e9
10 changed files with 332 additions and 97 deletions
|
@ -10,7 +10,7 @@ var ipc=require('../../../node-ipc');
|
|||
ipc.config.id = 'hello';
|
||||
ipc.config.retry= 1500;
|
||||
|
||||
ipc.connectToTCP(
|
||||
ipc.connectToNet(
|
||||
'world',
|
||||
function(){
|
||||
ipc.of.world.on(
|
|
@ -10,7 +10,7 @@ var ipc=require('../../../node-ipc');
|
|||
ipc.config.id = 'world';
|
||||
ipc.config.retry= 1500;
|
||||
|
||||
ipc.serveTCP(
|
||||
ipc.serveNet(
|
||||
function(){
|
||||
ipc.server.on(
|
||||
'message',
|
48
example/UDPSocket/basic/hello-client.js
Normal file
48
example/UDPSocket/basic/hello-client.js
Normal file
|
@ -0,0 +1,48 @@
|
|||
var ipc=require('../../../node-ipc');
|
||||
|
||||
/***************************************\
|
||||
*
|
||||
* UDP Client is really a UDP server
|
||||
*
|
||||
* Dedicated UDP sockets on the same
|
||||
* machine can not be bound to in the
|
||||
* traditional client/server method
|
||||
*
|
||||
* Every UDP socket is it's own UDP server
|
||||
* And so must have a unique port on its
|
||||
* machine, unlike TCP or Unix Sockts
|
||||
* which can share on the same machine.
|
||||
*
|
||||
* *************************************/
|
||||
|
||||
ipc.config.id = 'hello';
|
||||
ipc.config.retry= 1500;
|
||||
|
||||
ipc.serveNet(
|
||||
8001, //we set the port here because the world server is already using the default of 8000. So we can not bind to 8000 while world is using it.
|
||||
'udp4',
|
||||
function(){
|
||||
ipc.server.on(
|
||||
'message',
|
||||
function(data){
|
||||
ipc.log('got Data');
|
||||
ipc.log('got a message from '.debug, data.from.variable ,' : '.debug, data.message.variable);
|
||||
}
|
||||
);
|
||||
ipc.server.emit(
|
||||
{
|
||||
address : 'localhost',
|
||||
port : ipc.config.networkPort
|
||||
},
|
||||
'message',
|
||||
{
|
||||
from : ipc.config.id,
|
||||
message : 'Hello'
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
|
||||
|
||||
ipc.server.start();
|
38
example/UDPSocket/basic/world-server.js
Normal file
38
example/UDPSocket/basic/world-server.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
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.serveNet(
|
||||
'udp4',
|
||||
function(){
|
||||
console.log(123);
|
||||
ipc.server.on(
|
||||
'message',
|
||||
function(data,socket){
|
||||
ipc.log('got a message from '.debug, data.from.variable ,' : '.debug, data.message.variable);
|
||||
ipc.server.emit(
|
||||
socket,
|
||||
'message',
|
||||
{
|
||||
from : ipc.config.id,
|
||||
message : data.message+' world!'
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
console.log(ipc.server);
|
||||
}
|
||||
);
|
||||
|
||||
ipc.server.define.listen.message='This event type listens for message strings as value of data key.';
|
||||
|
||||
ipc.server.start();
|
|
@ -40,12 +40,14 @@ function connect(){
|
|||
}
|
||||
|
||||
if(!client.port){
|
||||
server.log('Connecting client on Unix Socket :'.debug, client.path.variable);
|
||||
client.socket = net.connect(
|
||||
{
|
||||
path:client.path
|
||||
}
|
||||
);
|
||||
}else{
|
||||
server.log('Connecting client via TCP to'.debug, client.path.variable ,client.port);
|
||||
client.socket = net.connect(
|
||||
{
|
||||
port:client.port,
|
||||
|
|
|
@ -1,26 +1,45 @@
|
|||
var net = require('net'),
|
||||
fs = require('fs'),
|
||||
dgram = require('dgram'),
|
||||
eventParser = require('../lib/eventParser.js'),
|
||||
pubsub = require('event-pubsub');
|
||||
|
||||
function emit(socket, type, data){
|
||||
if(!data)
|
||||
data=false;
|
||||
console.log(type,data)
|
||||
this.log('dispatching event to socket'.debug, ' : ', type.data, data);
|
||||
|
||||
socket.write(
|
||||
eventParser.format(
|
||||
{
|
||||
var event={
|
||||
type:type,
|
||||
data:data
|
||||
}
|
||||
|
||||
if(this.udp4 || this.udp6){
|
||||
|
||||
if(!socket.address || !socket.port){
|
||||
this.log('Attempting to emit to a single UDP socket without supplying socket address or port. Redispatching event as broadcast to all connected sockets');
|
||||
this.broadcast(type,data);
|
||||
return;
|
||||
}
|
||||
|
||||
this.server.write(
|
||||
eventParser.format(
|
||||
event
|
||||
),
|
||||
socket
|
||||
)
|
||||
return;
|
||||
};
|
||||
|
||||
socket.write(
|
||||
eventParser.format(
|
||||
event
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
function broadcast(type,data){
|
||||
this.log('broadcasting event to '.debug, this.path.variable,' : ', type.data, data);
|
||||
this.log('broadcasting event to all known sockets listening to '.debug, this.path.variable,' : ', ((this.port)?this.port:''), type.data, data);
|
||||
if(!data)
|
||||
data=false;
|
||||
|
||||
|
@ -31,9 +50,15 @@ function broadcast(type,data){
|
|||
}
|
||||
);
|
||||
|
||||
if(this.udp4 || this.udp6){
|
||||
for(var i=0, count=this.sockets.length; i<count; i++){
|
||||
this.server.write(e,this.sockets[i]);
|
||||
}
|
||||
}else{
|
||||
for(var i=0, count=this.sockets.length; i<count; i++){
|
||||
this.sockets[i].write(e);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
function init(path,config,log,port){
|
||||
|
@ -41,6 +66,8 @@ function init(path,config,log,port){
|
|||
config : config,
|
||||
path : path,
|
||||
port : port,
|
||||
udp4 : false,
|
||||
udp6 : false,
|
||||
log : log,
|
||||
server : false,
|
||||
sockets : [],
|
||||
|
@ -74,9 +101,50 @@ function init(path,config,log,port){
|
|||
function(server){
|
||||
return function () {
|
||||
server.log('starting server on '.debug,server.path.variable,((server.port)?':'+server.port:'').variable);
|
||||
|
||||
if(!server.udp4 && !server.udp6){
|
||||
server.server=net.createServer(
|
||||
function(socket) {
|
||||
serverCreated
|
||||
);
|
||||
}else{
|
||||
function UDPWrite(message,socket){
|
||||
var data=new Buffer(message, server.config.encoding);
|
||||
server.server.send(
|
||||
data,
|
||||
0,
|
||||
data.length,
|
||||
socket.port,
|
||||
socket.address,
|
||||
function(err, bytes) {
|
||||
if(err){
|
||||
server.trigger(
|
||||
'error',
|
||||
function(err){
|
||||
server.trigger('error',err);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
server.server=dgram.createSocket(
|
||||
((server.udp4)? 'udp4':'udp6')
|
||||
);
|
||||
server.server.write=UDPWrite;
|
||||
server.server.on(
|
||||
'listening',
|
||||
function () {
|
||||
serverCreated(server.server)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function serverCreated(socket) {
|
||||
|
||||
if(socket.setEncoding)
|
||||
socket.setEncoding(server.config.encoding);
|
||||
|
||||
server.log('## socket connection to server detected ##'.rainbow);
|
||||
socket.on(
|
||||
'close',
|
||||
|
@ -97,24 +165,33 @@ function init(path,config,log,port){
|
|||
|
||||
socket.on(
|
||||
'data',
|
||||
function(data){
|
||||
function(data,UDPSocket){
|
||||
data=eventParser.parse(data);
|
||||
var sock=((server.udp4 || server.udp6)? UDPSocket : socket);
|
||||
|
||||
while(data.length>0){
|
||||
var e=JSON.parse(data.shift());
|
||||
server.log('recieved event of : '.debug,e.type.data,e.data);
|
||||
server.log('received event of : '.debug,e.type.data,e.data);
|
||||
|
||||
server.sockets.push(socket);
|
||||
server.sockets.push(sock);
|
||||
|
||||
server.trigger(
|
||||
e.type,
|
||||
e.data,
|
||||
socket
|
||||
sock
|
||||
);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
socket.on(
|
||||
'message',
|
||||
function(msg,rinfo) {
|
||||
server.log('Received UDP message from '.debug, rinfo.address.variable, rinfo.port);
|
||||
socket.emit('data',msg.toString(),rinfo);
|
||||
}
|
||||
);
|
||||
|
||||
server.trigger(
|
||||
'connect',
|
||||
socket
|
||||
|
@ -130,13 +207,13 @@ function init(path,config,log,port){
|
|||
socket
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
function started(socket){
|
||||
server.onStart(socket)
|
||||
}
|
||||
|
||||
if(!port){
|
||||
server.log('starting server as'.debug, 'Unix Socket'.variable);
|
||||
server.server.listen(
|
||||
server.path,
|
||||
started
|
||||
|
@ -146,11 +223,28 @@ function init(path,config,log,port){
|
|||
return;
|
||||
}
|
||||
|
||||
if(!server.udp4 && !server.udp4){
|
||||
server.log('starting server as'.debug, 'TCP'.variable);
|
||||
server.server.listen(
|
||||
server.port,
|
||||
server.path,
|
||||
started
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
server.log('starting server as'.debug,((server.udp4)? 'udp4':'udp6').variable);
|
||||
server.server.bind(
|
||||
server.port,
|
||||
server.path
|
||||
);
|
||||
|
||||
started(
|
||||
{
|
||||
address : server.path,
|
||||
port : server.port
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
)(this)
|
||||
|
|
85
node-ipc.js
85
node-ipc.js
|
@ -34,9 +34,9 @@ var defaults={
|
|||
var ipc = {
|
||||
config : defaults,
|
||||
connectTo : connect,
|
||||
connectToTCP: connectTCP,
|
||||
connectToNet: connectNet,
|
||||
serve : serve,
|
||||
serveTCP : serveTCP,
|
||||
serveNet : serveNet,
|
||||
of : {},
|
||||
server : false,
|
||||
log : log
|
||||
|
@ -80,19 +80,42 @@ function serve(path,callback){
|
|||
);
|
||||
}
|
||||
|
||||
function serveTCP(host,port,callback){
|
||||
function serveNet(host,port,UDPType,callback){
|
||||
if(typeof host=='number'){
|
||||
callback=port;
|
||||
callback=UDPType;
|
||||
UDPType=port;
|
||||
port=host;
|
||||
host=false;
|
||||
}
|
||||
if(typeof host=='function'){
|
||||
callback=host;
|
||||
UDPType=false;
|
||||
host=false;
|
||||
port=false;
|
||||
}
|
||||
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(host.toLowerCase()=='udp4' || host.toLowerCase()=='udp6'){
|
||||
callback=port;
|
||||
UDPType=host.toLowerCase();
|
||||
port=false;
|
||||
host=ipc.config.networkHost;
|
||||
}
|
||||
|
||||
if(typeof port=='string'){
|
||||
callback=UDPType;
|
||||
UDPType=port;
|
||||
port=false;
|
||||
}
|
||||
if(typeof port=='function'){
|
||||
callback=port;
|
||||
UDPType=false;
|
||||
port=false;
|
||||
}
|
||||
if(!port){
|
||||
|
@ -103,14 +126,12 @@ function serveTCP(host,port,callback){
|
|||
);
|
||||
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(typeof UDPType=='function'){
|
||||
callback=UDPType;
|
||||
UDPType=false;
|
||||
}
|
||||
|
||||
if(!callback)
|
||||
callback=function(){};
|
||||
|
||||
|
@ -121,6 +142,11 @@ function serveTCP(host,port,callback){
|
|||
port
|
||||
);
|
||||
|
||||
if(UDPType)
|
||||
ipc.server[UDPType]=true;
|
||||
|
||||
console.log(callback.toString())
|
||||
|
||||
ipc.server.on(
|
||||
'start',
|
||||
callback
|
||||
|
@ -175,7 +201,7 @@ function connect(id,path,callback){
|
|||
callback();
|
||||
}
|
||||
|
||||
function connectTCP(id,host,port,callback){
|
||||
function connectNet(id,host,port,callback,UDPType){
|
||||
if(!id){
|
||||
ipc.log(
|
||||
'Service id required'.warn,
|
||||
|
@ -183,21 +209,43 @@ function connectTCP(id,host,port,callback){
|
|||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if(typeof host=='number'){
|
||||
UDPType=callback;
|
||||
callback=port;
|
||||
port=host;
|
||||
host=false;
|
||||
}
|
||||
if(typeof host=='function'){
|
||||
UDPType=port;
|
||||
callback=host;
|
||||
host=false;
|
||||
port=false;
|
||||
}
|
||||
if(host=='udp4' || host=='udp6'){
|
||||
UDPType=host;
|
||||
host=false;
|
||||
port=false;
|
||||
callback=false;
|
||||
}
|
||||
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(typeof port=='function'){
|
||||
UDPType=callback;
|
||||
callback=port;
|
||||
port=false;
|
||||
}
|
||||
if(typeof port == 'string'){
|
||||
UDPType=port;
|
||||
port=false;
|
||||
callback=false;
|
||||
}
|
||||
if(!port){
|
||||
ipc.log(
|
||||
'Server port not specified, so defaulting to'.notice,
|
||||
|
@ -206,13 +254,10 @@ function connectTCP(id,host,port,callback){
|
|||
);
|
||||
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(typeof callback == 'string'){
|
||||
UDPType=callback;
|
||||
callback=false;
|
||||
}
|
||||
if(!callback)
|
||||
callback=function(){};
|
||||
|
|
12
package.json
12
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "node-ipc",
|
||||
"version": "0.0.2",
|
||||
"version": "0.9.0",
|
||||
"description": "A nodejs module for local and remote Inter Process Communication (IPC) uses Unix Sockets for local communication avoiding the network card for lower overhead and latency. ## Solid but, ### Still under development and lacking documentation, but useable.",
|
||||
"main": "node-ipc.js",
|
||||
"directories": {
|
||||
|
@ -29,5 +29,13 @@
|
|||
"memory"
|
||||
],
|
||||
"author": "Brandon Nozaki Miller",
|
||||
"license": "Unlicenced"
|
||||
"license": "Unlicenced",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/RIAEvangelist/node-ipc.git"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/RIAEvangelist/node-ipc/issues"
|
||||
},
|
||||
"homepage": "https://github.com/RIAEvangelist/node-ipc"
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue