From 3be9d292409a491f6a23451ea630a9e237b06f29 Mon Sep 17 00:00:00 2001 From: Brandon Nozaki Miller Date: Thu, 10 Dec 2015 00:11:14 -0800 Subject: [PATCH] added sync option for all applicable socket types --- example/TCPSocket/basicSync/hello-client.js | 46 +++++++++++++++++ example/TCPSocket/basicSync/world-server.js | 45 +++++++++++++++++ example/TLSSocket/basicSync/hello-client.js | 47 ++++++++++++++++++ example/TLSSocket/basicSync/world-server.js | 48 ++++++++++++++++++ .../basicSync/hello-client.js | 49 +++++++++++++++++++ .../basicSync/world-server.js | 41 ++++++++++++++++ lib/client.js | 29 ++++++++++- lib/socketServer.js | 4 ++ node-ipc.js | 1 + package.json | 5 +- 10 files changed, 311 insertions(+), 4 deletions(-) create mode 100644 example/TCPSocket/basicSync/hello-client.js create mode 100644 example/TCPSocket/basicSync/world-server.js create mode 100644 example/TLSSocket/basicSync/hello-client.js create mode 100644 example/TLSSocket/basicSync/world-server.js create mode 100644 example/unixWindowsSocket/basicSync/hello-client.js create mode 100644 example/unixWindowsSocket/basicSync/world-server.js diff --git a/example/TCPSocket/basicSync/hello-client.js b/example/TCPSocket/basicSync/hello-client.js new file mode 100644 index 0000000..76875ec --- /dev/null +++ b/example/TCPSocket/basicSync/hello-client.js @@ -0,0 +1,46 @@ +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.sync= true; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + + //queue up a bunch of requests to be sent synchronously + for(var i=0; i<10; i++){ + ipc.of.world.emit( + 'message', + 'hello'+i + ) + } + } + ); + 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,'\n\n'); + } + ); + } +); + +console.log(ipc) diff --git a/example/TCPSocket/basicSync/world-server.js b/example/TCPSocket/basicSync/world-server.js new file mode 100644 index 0000000..347858d --- /dev/null +++ b/example/TCPSocket/basicSync/world-server.js @@ -0,0 +1,45 @@ +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.sync = true; //sync servers do not bradcast defined events + +ipc.serveNet( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.log('got a message : '.debug, data); + //fake some synch procedural code + setTimeout( + function(){ + ipc.server.emit( + socket, + 'message', + data+' world!' + ); + }, + 3000 + ); + } + ); + + ipc.server.on( + 'socket.disconnected', + function(data,socket){ + console.log(arguments) + } + ); + } +); + +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; + +ipc.server.start(); diff --git a/example/TLSSocket/basicSync/hello-client.js b/example/TLSSocket/basicSync/hello-client.js new file mode 100644 index 0000000..c7451c4 --- /dev/null +++ b/example/TLSSocket/basicSync/hello-client.js @@ -0,0 +1,47 @@ +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.sync= true; +ipc.config.tls={ + rejectUnauthorized:false +}; + +ipc.connectToNet( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + + //queue up a bunch of requests to be sent synchronously + for(var i=0; i<10; i++){ + ipc.of.world.emit( + 'message', + 'hello'+i + ) + } + } + ); + 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); + } + ); + } +); diff --git a/example/TLSSocket/basicSync/world-server.js b/example/TLSSocket/basicSync/world-server.js new file mode 100644 index 0000000..11767b6 --- /dev/null +++ b/example/TLSSocket/basicSync/world-server.js @@ -0,0 +1,48 @@ +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.sync= true; +ipc.config.tls={ + public: '../../../local-node-ipc-certs/server.pub', + private: '../../../local-node-ipc-certs/private/server.key' +} + +ipc.serveNet( + function(){ + ipc.server.on( + 'message', + function(data,socket){ + ipc.log('got a message : '.debug, data); + setTimeout( + function(){ + ipc.server.emit( + socket, + 'message', + data+' world!' + ); + }, + 3000 + ); + } + ); + + ipc.server.on( + 'socket.disconnected', + function(data,socket){ + console.log(arguments) + } + ); + } +); + +ipc.server.define.listen.message='This event type listens for message strings as value of data key.'; + +ipc.server.start(); diff --git a/example/unixWindowsSocket/basicSync/hello-client.js b/example/unixWindowsSocket/basicSync/hello-client.js new file mode 100644 index 0000000..1bd5dc9 --- /dev/null +++ b/example/unixWindowsSocket/basicSync/hello-client.js @@ -0,0 +1,49 @@ +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 = 1000; +ipc.config.sync= true; + +ipc.connectTo( + 'world', + function(){ + ipc.of.world.on( + 'connect', + function(){ + ipc.log('## connected to world ##'.rainbow, ipc.config.delay); + + //queue up a bunch of requests to be sent synchronously + for(var i=0; i<10; i++){ + ipc.of.world.emit( + 'app.message', + { + id : ipc.config.id, + message : 'hello'+i + } + ) + } + } + ); + ipc.of.world.on( + 'disconnect', + function(){ + ipc.log('disconnected from world'.notice); + } + ); + ipc.of.world.on( + 'app.message', + function(data){ + ipc.log('got a message from world : '.debug, data); + } + ); + + console.log(ipc.of.world.destroy); + } +); diff --git a/example/unixWindowsSocket/basicSync/world-server.js b/example/unixWindowsSocket/basicSync/world-server.js new file mode 100644 index 0000000..42a0bf6 --- /dev/null +++ b/example/unixWindowsSocket/basicSync/world-server.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 = 'world'; +ipc.config.retry= 1500; +ipc.config.sync= true; + +ipc.serve( + function(){ + ipc.server.on( + 'app.message', + function(data,socket){ + //ipc.log('got a message from'.debug, (data.id).variable, (data.message).data); + + setTimeout( + function(){ + ipc.server.emit( + socket, + 'app.message', + { + id : ipc.config.id, + message : data.message+' world!' + } + ); + }, + 2000 + ); + } + ); + } +); + +ipc.server.define.listen['app.message']='This event type listens for message strings as value of data key.'; + +ipc.server.start(); diff --git a/lib/client.js b/lib/client.js index 0ebcd49..a0ba5b6 100644 --- a/lib/client.js +++ b/lib/client.js @@ -3,11 +3,13 @@ var net = require('net'), eventParser = require('../lib/eventParser.js'), pubsub = require('event-pubsub'), Message = require('js-message'), - fs = require('fs'); + fs = require('fs'), + Queue = require('js-queue'); function init(config,log){ var client={ config : config, + queue : new Queue, socket : false, connect : connect, emit : emit, @@ -32,9 +34,21 @@ function emit(type,data){ message=eventParser.format(message); } - this.socket.write(message); + if(!this.config.sync){ + this.socket.write(message); + return; + } + + this.queue.add( + syncEmit.bind(this,message) + ); }; +function syncEmit(message){ + this.log('dispatching event to '.debug, this.id.variable, this.path.variable,' : ', message.data); + this.socket.write(message); +} + function connect(){ //init client object for scope persistance especially inside of socket events. var client=this; @@ -176,6 +190,11 @@ function connect(){ 'data', new Buffer(data,this.encoding) ); + if(!client.config.sync){ + return; + } + + client.queue.next(); return; } @@ -203,6 +222,12 @@ function connect(){ message.data ); } + + if(!client.config.sync){ + return; + } + + client.queue.next(); } ); } diff --git a/lib/socketServer.js b/lib/socketServer.js index b0a84b9..973d38e 100644 --- a/lib/socketServer.js +++ b/lib/socketServer.js @@ -267,6 +267,10 @@ function init(path,config,log,port){ return; } + if(server.config.sync){ + return; + } + server.trigger( 'get.events.broadcasting', socket diff --git a/node-ipc.js b/node-ipc.js index d60e904..ab3a7d4 100644 --- a/node-ipc.js +++ b/node-ipc.js @@ -31,6 +31,7 @@ var defaults={ id : os.hostname(), encoding : 'utf8', rawBuffer : false, + sync : false, silent : false, maxConnections : 100, retry : 500, diff --git a/package.json b/package.json index 68f0066..0b6a6b2 100644 --- a/package.json +++ b/package.json @@ -6,13 +6,14 @@ "directories": { "example": "example" }, - "engines" : { - "node" : ">=1.0.0" + "engines": { + "node": ">=1.0.0" }, "dependencies": { "colors": "*", "event-pubsub": "*", "js-message": "*", + "js-queue": "^0.1.2", "node-cmd": "*" }, "devDependencies": {},