added js-message dep and rawBuffer examples

This commit is contained in:
Brandon Nozaki Miller 2015-09-27 03:32:14 -07:00
parent 7f06fe6a14
commit ac714e91a4
14 changed files with 313 additions and 154 deletions

View File

@ -1,10 +1,10 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
*
* You should start both hello and world
* then you will see them communicating.
*
*
* *************************************/
ipc.config.id = 'goodbye';
@ -41,4 +41,4 @@ ipc.connectToNet(
}
);
}
);
);

View File

@ -1,10 +1,10 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
*
* You should start both hello and world
* then you will see them communicating.
*
*
* *************************************/
ipc.config.id = 'hello';
@ -36,7 +36,7 @@ ipc.connectToNet(
ipc.of.world.on(
'app.message',
function(data){
ipc.log('got a message from world : '.debug, data.messgae);
ipc.log('got a message from world : '.debug, data.message);
}
);
ipc.of.world.on(
@ -47,4 +47,4 @@ ipc.connectToNet(
}
);
}
);
);

View File

@ -0,0 +1,35 @@
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.config.rawBuffer=true;
ipc.config.encoding='ascii';
ipc.connectToNet(
'world',
function(){
ipc.of.world.on(
'connect',
function(){
ipc.log('## connected to world ##'.rainbow, ipc.config.delay);
ipc.of.world.emit(
'hello'
)
}
);
ipc.of.world.on(
'data',
function(data){
ipc.log('got a message from world : '.debug, data,data.toString());
}
);
}
);

View File

@ -0,0 +1,40 @@
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.config.rawBuffer=true;
ipc.config.encoding='ascii';
ipc.serveNet(
function(){
ipc.server.on(
'connect',
function(socket){
ipc.server.emit(
socket,
'hello'
);
}
);
ipc.server.on(
'data',
function(data,socket){
ipc.log('got a message'.debug, data,data.toString());
ipc.server.emit(
socket,
'goodbye'
);
}
);
}
);
ipc.server.start();

View File

@ -0,0 +1,35 @@
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.config.rawBuffer=true;
ipc.config.encoding='ascii';
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(
'data',
function(data){
ipc.log('got a message from world '.debug, data, data.toString());
}
);
ipc.server.emit(
{
address : 'localhost',
port : ipc.config.networkPort
},
'hello'
);
}
);
ipc.server.start();

View File

@ -0,0 +1,43 @@
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.
*
* Since there is no open client server
* relationship, you should start world
* first and then hello.
*
***************************************/
ipc.config.id = 'world';
ipc.config.retry= 1500;
ipc.config.rawBuffer=true;
ipc.config.encoding='ascii';
ipc.serveNet(
'udp4',
function(){
ipc.server.on(
'data',
function(data,socket){
ipc.log('got a message'.debug, data,data.toString());
ipc.server.emit(
socket,
'goodbye'
);
}
);
}
);
ipc.server.start();

View File

@ -1,10 +1,10 @@
var ipc=require('../../../node-ipc');
/***************************************\
*
*
* You should start both hello and world
* then you will see them communicating.
*
*
* *************************************/
ipc.config.id = 'hello';

View File

@ -1,6 +1,7 @@
var net = require('net'),
eventParser = require('../lib/eventParser.js'),
pubsub = require('event-pubsub');
pubsub = require('event-pubsub'),
Message = require('js-message');
function init(config,log){
var client={
@ -12,38 +13,36 @@ function init(config,log){
retriesRemaining:config.maxRetries||0
}
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;
var message=new Message;
message.type=type;
message.data=data;
if(this.config.rawBuffer){
data=new Buffer(type,this.encoding);
message=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
{
type:type,
data:data
}
);
message=eventParser.format(message);
}
this.socket.write(data);
this.socket.write(message);
};
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;
}
if(!client.port){
client.log('Connecting client on Unix Socket :'.debug, client.path.variable);
client.socket = net.connect(
@ -60,16 +59,16 @@ function connect(){
}
);
}
client.socket.setEncoding(this.config.encoding);
client.socket.on(
'error',
function(err){
client.log('\n\n######\nerror: '.error, err);
}
);
client.socket.on(
'connect',
function(){
@ -78,12 +77,12 @@ function connect(){
client.log('retrying reset')
}
);
client.socket.on(
'close',
function(){
client.log('connection closed'.notice ,client.id.variable , client.path.variable, client.retriesRemaining+' tries remaining of '+client.config.maxRetries);
if(
client.config.stopRetrying || client.retriesRemaining<1
@ -93,15 +92,15 @@ function connect(){
'exceeded connection rety amount of'.warn,
" or stopRetrying flag set."
);
client.socket.destroy();
client=undefined;
return;
}
client.isRetrying=true;
setTimeout(
(
function(client){
@ -114,18 +113,18 @@ function connect(){
if(!client.isRetrying)
client.retriesRemaining=client.config.maxRetries;
},
100
100
)
}
}
)(client),
client.config.retry
);
client.trigger('disconnect');
}
);
client.socket.on(
'data',
function(data) {
@ -140,26 +139,26 @@ function connect(){
if(!this.ipcBuffer)
this.ipcBuffer='';
data=(this.ipcBuffer+=data);
if(data.slice(-1)!=eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1){
client.log('Implementing larger buffer for this socket message. You may want to consider smaller messages'.notice);
return;
}
this.ipcBuffer='';
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);
var message=new Message;
message.load(events[i]);
client.log('detected event of type '.debug, message.type.data, message.data);
client.trigger(
e.type,
e.data
message.type,
message.data
);
}
}

View File

@ -1,11 +1,15 @@
function formatData(data){
if(!data.data)
data.data={};
if(data.data['_maxListeners'])
delete data.data;
data=JSON.stringify(data)+parser.delimiter;
return data;
var Message = require('js-message');
function formatData(message){
if(!message.data){
message.data={};
}
if(message.data['_maxListeners']){
message.data={};
}
message=message.JSON+parser.delimiter;
return message;
};
function parseDataEvents(data){
@ -20,4 +24,4 @@ var parser={
delimiter : '\f'
}
module.exports=parser;
module.exports=parser;

View File

@ -2,70 +2,59 @@ var net = require('net'),
fs = require('fs'),
dgram = require('dgram'),
eventParser = require('../lib/eventParser.js'),
pubsub = require('event-pubsub');
pubsub = require('event-pubsub'),
Message = require('js-message');
function emit(socket, type, data){
if(!data)
data=false;
this.log('dispatching event to socket'.debug, ' : ', type.data, data);
var event={
type:type,
data:data
}
var message=new Message;
message.type=type;
message.data=data;
if(this.config.rawBuffer){
data=new Buffer(type,this.encoding);
message=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
event
);
message=eventParser.format(message);
}
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(
data,
message,
socket
);
return;
};
socket.write(data);
socket.write(message);
};
function broadcast(type,data){
this.log('broadcasting event to all known sockets listening to '.debug, this.path.variable,' : ', ((this.port)?this.port:''), type, data);
if(!data)
data=false;
var event={
type:type,
data:data
};
var message=new Message;
message.type=type;
message.data=data;
if(this.config.rawBuffer){
data=new Buffer(type,this.encoding);
message=new Buffer(type,this.encoding);
}else{
data=eventParser.format(
event
);
message=eventParser.format(message);
}
if(this.udp4 || this.udp6){
for(var i=1, count=this.sockets.length; i<count; i++){
this.server.write(data,this.sockets[i]);
this.server.write(message,this.sockets[i]);
}
}else{
for(var i=0, count=this.sockets.length; i<count; i++){
this.sockets[i].write(data);
this.sockets[i].write(message);
}
}
};
@ -89,7 +78,7 @@ function init(path,config,log,port){
},
broadcast : {
'events.broadcasting' : 'data.events is a JSON object of event definitions by type '+config.id+' will broadcast on '+path,
'events.listening' : 'data.events is a JSON object of event definitions by type '+config.id+' is listening for on '+path
'events.listening' : 'data.events is a JSON object of event definitions by type '+config.id+' is listening for on '+path
}
},
onStart : function(socket){
@ -103,14 +92,14 @@ function init(path,config,log,port){
server.log('Socket Server Path not specified, refusing to start'.warn);
return;
}
fs.unlink(
this.path,
this.path,
(
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(
serverCreated
@ -119,15 +108,15 @@ function init(path,config,log,port){
function UDPWrite(message,socket){
var data=new Buffer(message, server.config.encoding);
server.server.send(
data,
0,
data.length,
socket.port,
socket.address,
data,
0,
data.length,
socket.port,
socket.address,
function(err, bytes) {
if(err){
server.trigger(
'error',
'error',
function(err){
server.trigger('error',err);
}
@ -136,25 +125,25 @@ function init(path,config,log,port){
}
);
}
server.server=dgram.createSocket(
((server.udp4)? 'udp4':'udp6')
);
server.server.write=UDPWrite;
server.server.on(
'listening',
'listening',
function () {
serverCreated(server.server)
}
);
}
function serverCreated(socket) {
server.sockets.push(socket);
if(socket.setEncoding)
socket.setEncoding(server.config.encoding);
server.log('## socket connection to server detected ##'.rainbow);
socket.on(
'close',
@ -165,14 +154,14 @@ function init(path,config,log,port){
);
}
);
socket.on(
'error',
'error',
function(err){
server.trigger('error',err);
}
);
socket.on(
'data',
function(data,UDPSocket){
@ -191,32 +180,34 @@ function init(path,config,log,port){
this.ipcBuffer='';
data=(this.ipcBuffer+=data);
if(data.slice(-1)!=eventParser.delimiter || data.indexOf(eventParser.delimiter) == -1){
server.log('Implementing larger buffer for this socket message. You may want to consider smaller messages'.notice);
return;
}
this.ipcBuffer='';
data=eventParser.parse(data);
while(data.length>0){
var e=JSON.parse(data.shift());
server.log('received event of : '.debug,e.type.data,e.data);
if(e.data.id)
sock.id=e.data.id;
var message=new Message;
message.load(data.shift());
server.log('received event of : '.debug,message.type.data,message.data);
if(message.data.id)
sock.id=message.data.id;
server.trigger(
e.type,
e.data,
message.type,
message.data,
sock
);
}
}
);
socket.on(
'message',
function(msg,rinfo) {
@ -233,12 +224,12 @@ function init(path,config,log,port){
socket.emit('data',data,rinfo);
}
);
server.trigger(
'connect',
socket
);
if(server.config.rawBuffer){
return;
}
@ -247,28 +238,28 @@ function init(path,config,log,port){
'get.events.broadcasting',
socket
);
server.trigger(
'get.events.listening',
socket
);
}
function started(socket){
server.onStart(socket)
}
if(!port){
server.log('starting server as'.debug, 'Unix Socket'.variable);
server.server.listen(
server.path,
started
);
server.server.maxConnections=server.maxConnections;
return;
}
if(!server.udp4 && !server.udp4){
server.log('starting server as'.debug, 'TCP'.variable);
server.server.listen(
@ -278,13 +269,13 @@ function init(path,config,log,port){
);
return;
}
server.log('starting server as'.debug,((server.udp4)? 'udp4':'udp6').variable);
server.server.bind(
server.port,
server.port,
server.path
);
started(
{
address : server.path,
@ -297,9 +288,9 @@ function init(path,config,log,port){
);
}
};
new pubsub(server);
server.on(
'get.events.broadcasting',
function(socket){
@ -313,7 +304,7 @@ function init(path,config,log,port){
);
}
);
server.on(
'get.events.listening',
function(socket){
@ -326,35 +317,35 @@ function init(path,config,log,port){
}
);
}
)
)
server.on(
'close',
'close',
function(){
for(var i=0, count=server.sockets.length; i<count; i++){
var socket=server.sockets[i];
var destroyedSocketId=false;
if(socket){
if(socket.readable)
continue;
if(socket.readable)
continue;
}
if(socket.id)
destroyedSocketId=socket.id;
server.log('socket disconnected'.notice,' '+destroyedSocketId.variable);
if(socket)
socket.destroy();
server.sockets.splice(i,1);
server.trigger('socket.disconnected', socket, destroyedSocketId);
return;
}
}
}
);
return server;

4
node_modules/colors/package.json generated vendored
View File

@ -16,7 +16,7 @@
],
"repository": {
"type": "git",
"url": "http://github.com/Marak/colors.js.git"
"url": "git+ssh://git@github.com/Marak/colors.js.git"
},
"engines": {
"node": ">=0.1.90"
@ -27,7 +27,7 @@
"shasum": "2423fe6678ac0c5dae8852e5d0e5be08c997abcc",
"tarball": "http://registry.npmjs.org/colors/-/colors-0.6.2.tgz"
},
"_from": "colors@~0.6.2",
"_from": "colors@>=0.6.2 <0.7.0",
"_npmVersion": "1.2.30",
"_npmUser": {
"name": "marak",

View File

@ -11,7 +11,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/RIAEvangelist/event-pubsub.git"
"url": "git+https://github.com/RIAEvangelist/event-pubsub.git"
},
"keywords": [
"event",
@ -33,7 +33,7 @@
"shasum": "c81c49b101cdb4892d8fa2631b443184db2de6aa",
"tarball": "http://registry.npmjs.org/event-pubsub/-/event-pubsub-1.0.3.tgz"
},
"_from": "event-pubsub@~1.0.3",
"_from": "event-pubsub@>=1.0.3 <1.1.0",
"_npmVersion": "1.4.3",
"_npmUser": {
"name": "riaevangelist",

23
node_modules/node-cmd/package.json generated vendored
View File

@ -11,7 +11,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/RIAEvangelist/node-cmd.git"
"url": "git+https://github.com/RIAEvangelist/node-cmd.git"
},
"keywords": [
"commandline",
@ -29,10 +29,25 @@
"url": "https://github.com/RIAEvangelist/node-cmd/issues"
},
"homepage": "https://github.com/RIAEvangelist/node-cmd",
"readme": "#node-cmd\n-\n*Node.js commandline/terminal interface.* \n\nSimple commandline or terminal interface to allow you to run cli or bash style commands as if you were in the terminal.\n\nRun commands asynchronously, and if needed can get the output as a string.\n\n#Methods\n-\n\n|method | arguments | functionality |\n|-------|-----------|---------------|\n|run | command | runs a command asynchronously|\n|get | command,callback | runs a command asynchronously, when the command is complete all of the stdout will be passed to the callback|\n\n\n#Examples\n-\n\n var cmd=require('node-cmd');\n \n cmd.get(\n 'pwd',\n function(data){\n console.log('the current working dir is : ',data)\n }\n );\n \n cmd.run('touch example.created.file');\n \n cmd.get(\n 'ls',\n function(data){\n console.log('the current dir contains these files :\\n\\n',data)\n }\n );\n\n",
"readmeFilename": "README.md",
"gitHead": "a003426996e8594af31a6486cfb7d28d0a547bc9",
"_id": "node-cmd@1.0.2",
"_shasum": "89cdb50181476cdd127763d5c8514499fe3c038d",
"_from": "node-cmd@~1.0.1"
"_from": "node-cmd@>=1.0.1 <1.1.0",
"_npmVersion": "1.4.23",
"_npmUser": {
"name": "riaevangelist",
"email": "brandon@diginow.it"
},
"maintainers": [
{
"name": "riaevangelist",
"email": "brandon@1942design.com"
}
],
"dist": {
"shasum": "89cdb50181476cdd127763d5c8514499fe3c038d",
"tarball": "http://registry.npmjs.org/node-cmd/-/node-cmd-1.0.2.tgz"
},
"_resolved": "https://registry.npmjs.org/node-cmd/-/node-cmd-1.0.2.tgz",
"readme": "ERROR: No README data found!"
}

View File

@ -7,15 +7,12 @@
"example": "example"
},
"dependencies": {
"event-pubsub": "~1.0.3",
"colors": "~0.6.2",
"event-pubsub": "~1.0.3",
"js-message": "*",
"node-cmd": "~1.0.1"
},
"devDependencies": {
"colors": "*",
"event-pubsub": "*",
"node-cmd": "*"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},