forked from JustinTulloss/zeromq.node
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request JustinTulloss#99 from utvara/master
Zmq Device examples written for node
- Loading branch information
Showing
4 changed files
with
217 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* | ||
* Forwarder device | ||
* | ||
*/ | ||
|
||
var zmq = require('../../') | ||
, frontPort = 'tcp://127.0.0.1:12345' | ||
, backPort = 'tcp://127.0.0.1:12346'; | ||
|
||
function createClient (port) { | ||
var socket = zmq.socket('pub'); | ||
|
||
socket.identity = 'client' + process.pid; | ||
|
||
socket.connect(port); | ||
console.log('client connected!'); | ||
|
||
setInterval(function() { | ||
var value = Math.floor(Math.random()*100); | ||
|
||
console.log(socket.identity + ': broadcasting ' + value); | ||
socket.send(value); | ||
}, 100); | ||
}; | ||
|
||
function createWorker (port) { | ||
var socket = zmq.socket('sub'); | ||
|
||
socket.identity = 'worker' + process.pid; | ||
|
||
socket.subscribe(''); | ||
socket.on('message', function(data) { | ||
console.log(socket.identity + ': got ' + data.toString()); | ||
}); | ||
|
||
socket.connect(port, function(err) { | ||
if (err) throw err; | ||
console.log('worker connected!'); | ||
}); | ||
}; | ||
|
||
function createForwarderDevice(frontPort, backPort) { | ||
var frontSocket = zmq.socket('sub'), | ||
backSocket = zmq.socket('pub'); | ||
|
||
frontSocket.identity = 'sub' + process.pid; | ||
backSocket.identity = 'pub' + process.pid; | ||
|
||
frontSocket.subscribe(''); | ||
frontSocket.bind(frontPort, function (err) { | ||
console.log('bound', frontPort); | ||
}); | ||
|
||
frontSocket.on('message', function() { | ||
//pass to back | ||
console.log('forwarder: recasting', arguments[0].toString()); | ||
backSocket.send(Array.prototype.slice.call(arguments)); | ||
}); | ||
|
||
backSocket.bind(backPort, function (err) { | ||
console.log('bound', backPort); | ||
}); | ||
} | ||
|
||
createForwarderDevice(frontPort, backPort); | ||
|
||
createClient(frontPort); | ||
createWorker(backPort); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* | ||
* Queue device | ||
* | ||
*/ | ||
|
||
var zmq = require('../../') | ||
, frontPort = 'tcp://127.0.0.1:12345' | ||
, backPort = 'tcp://127.0.0.1:12346'; | ||
|
||
function createClient (port) { | ||
var socket = zmq.socket('req'); | ||
|
||
socket.identity = 'client' + process.pid; | ||
|
||
socket.on('message', function(data) { | ||
console.log(socket.identity + ': answer data ' + data); | ||
}); | ||
|
||
socket.connect(port); | ||
console.log('client connected!'); | ||
|
||
setInterval(function() { | ||
var value = Math.floor(Math.random()*100); | ||
|
||
console.log(socket.identity + ': asking ' + value); | ||
socket.send(value); | ||
}, 100); | ||
}; | ||
|
||
function createServer (port) { | ||
var socket = zmq.socket('rep'); | ||
|
||
socket.identity = 'server' + process.pid; | ||
|
||
socket.on('message', function(data) { | ||
console.log(socket.identity + ': received ' + data.toString()); | ||
socket.send(data * 2); | ||
}); | ||
|
||
socket.connect(port, function(err) { | ||
if (err) throw err; | ||
console.log('server connected!'); | ||
}); | ||
}; | ||
|
||
function createQueueDevice(frontPort, backPort) { | ||
var frontSocket = zmq.socket('router'), | ||
backSocket = zmq.socket('dealer'); | ||
|
||
frontSocket.identity = 'router' + process.pid; | ||
backSocket.identity = 'dealer' + process.pid; | ||
|
||
frontSocket.bind(frontPort, function (err) { | ||
console.log('bound', frontPort); | ||
}); | ||
|
||
frontSocket.on('message', function() { | ||
//pass to back | ||
console.log('router: sending to server', arguments[0].toString(), arguments[2].toString()); | ||
backSocket.send(Array.prototype.slice.call(arguments)); | ||
}); | ||
|
||
backSocket.bind(backPort, function (err) { | ||
console.log('bound', backPort); | ||
}); | ||
|
||
backSocket.on('message', function() { | ||
//pass to front | ||
console.log('dealer: sending to client', arguments[0].toString(), arguments[2].toString()); | ||
frontSocket.send(Array.prototype.slice.call(arguments)); | ||
}); | ||
} | ||
|
||
createQueueDevice(frontPort, backPort); | ||
|
||
createClient(frontPort); | ||
createServer(backPort); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* | ||
* Forwarder device | ||
* | ||
*/ | ||
|
||
var zmq = require('../../') | ||
, frontPort = 'tcp://127.0.0.1:12345' | ||
, backPort = 'tcp://127.0.0.1:12346'; | ||
|
||
function createClient (port) { | ||
var socket = zmq.socket('push'); | ||
|
||
socket.identity = 'client' + process.pid; | ||
|
||
socket.connect(port); | ||
console.log('client connected!'); | ||
|
||
setInterval(function() { | ||
var value = Math.floor(Math.random()*100); | ||
|
||
console.log(socket.identity + ': pushing ' + value); | ||
socket.send(value); | ||
}, 100); | ||
}; | ||
|
||
function createWorker (port) { | ||
var socket = zmq.socket('pull'); | ||
|
||
socket.identity = 'worker' + process.pid; | ||
|
||
socket.on('message', function(data) { | ||
console.log(socket.identity + ': pulled ' + data.toString()); | ||
}); | ||
|
||
socket.connect(port, function(err) { | ||
if (err) throw err; | ||
console.log('worker connected!'); | ||
}); | ||
}; | ||
|
||
function createStreamerDevice(frontPort, backPort) { | ||
var frontSocket = zmq.socket('pull'), | ||
backSocket = zmq.socket('push'); | ||
|
||
frontSocket.identity = 'sub' + process.pid; | ||
backSocket.identity = 'pub' + process.pid; | ||
|
||
frontSocket.bind(frontPort, function (err) { | ||
console.log('bound', frontPort); | ||
}); | ||
|
||
frontSocket.on('message', function() { | ||
//pass to back | ||
console.log('forwarder: sending downstream', arguments[0].toString()); | ||
backSocket.send(Array.prototype.slice.call(arguments)); | ||
}); | ||
|
||
backSocket.bind(backPort, function (err) { | ||
console.log('bound', backPort); | ||
}); | ||
} | ||
|
||
createStreamerDevice(frontPort, backPort); | ||
|
||
createClient(frontPort); | ||
createWorker(backPort); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters