Skip to content

Commit

Permalink
client code added with example
Browse files Browse the repository at this point in the history
  • Loading branch information
openmason committed Dec 29, 2012
1 parent da28d7b commit 906ae4a
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 4 deletions.
3 changes: 2 additions & 1 deletion History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# release 0.0.2
# release 0.0.3
* ZeroMQ client code added
* ZeroMQ Server and example added
* initial repository creation
26 changes: 26 additions & 0 deletions examples/mathclient.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Sample math JSON RPC client calling ZeroMQ server
*/

var zmqrpc = require('..').Client;

// math service have four methods
// - add, subtract, multiply, divide

// - can give a global callback
//var mathObj = new zmqrpc('tcp://127.0.0.1:12345', function(error, res) { ..global callback .. });
var mathObj = new zmqrpc('tcp://127.0.0.1:12345');

// or use a per call based callback
mathObj.add(12,3, function(error, res) {
console.log('Result of addition: '+res);
});
mathObj.subtract(12,3, function(error, res) {
console.log('Result of subtraction: '+res);
});
mathObj.multiply(12,3, function(error, res) {
console.log('Result of multiplicatin: '+res);
});
mathObj.divide(12,3, function(error, res) {
console.log('Result of multiplicatin: '+res);
});
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
// - http://en.wikipedia.org/wiki/JSON-RPC

exports.Server = require('./lib/server');
//exports.Client = require('./lib/client');
exports.Client = require('./lib/client');

// -- EOF
72 changes: 72 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* zmqrpc - client wrappers
* copyright (c) 2012 openmason.
* MIT Licensed.
*/

var zeromq = require('zmq');
var Proxy = require('node-proxy');
var handy = require('handy');

/*
* ZeroMQ & node-proxy based RPC Client.
* - Async client, so callback has to be
* given.
*/
var RpcClient = function (port, cb, debug) {
this.port = port || 'tcp://127.0.0.1:13674';
this.debug = debug || false;
this.socket = zeromq.socket('dealer');
this.socket.identity = 'zmqrpc-c-'+process.pid;
// connect right away
this.socket.connect(port);
// @todo - id to be fixed to be uniq
this.id = Math.floor(Math.random()*484837+1);
this.callbacks = {};
var self = this;
// Socket data
self.socket.on('message', function(data) {
//console.log(self.socket.identity + ': answer data ' + data);
var obj = JSON.parse(data);
/*
if(obj.error) {
console.log('error returned:'+obj.id);
} else {
console.log('result for:'+obj.id+' = '+obj.result);
}
*/
if(self.callbacks.hasOwnProperty(obj.id)) {
self.callbacks[obj.id](obj.error, obj.result);
delete self.callbacks[obj.id];
}
});
// lets create the proxy context
return Proxy.create({
get: function(rcvr, name) {
return function() {
var args = Array.prototype.slice.call(arguments);
var jsonObj ={ jsonrpc:"2.0",
method: name,
params: args,
id: self.id.toString(36)
};
// check if last argument is a function
// if so, then set that as the callback
var f = cb;
if(args && args.length>0) {
if(handy.getType(args[args.length-1])=='function') {
f = args.pop();
}
}
self.callbacks[jsonObj.id] = f;
self.socket.send(JSON.stringify(jsonObj));
self.id++;
return jsonObj;
};
}
});
};

module.exports = RpcClient;

// -- EOF
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "zmqrpc",
"description": "zeromq json rpc 2.0 server",
"homepage": "https://github.com/openmason/zmqrpc",
"version": "0.0.2",
"version": "0.0.3",
"author": "el aras <openmason@gmail.com>",
"dependencies": {
"jsonrpclib": ">= 0.1.2",
"zmq" : ">= 2.2.0"
"handy" : ">= 0.0.11",
"zmq" : ">= 2.2.0",
"node-proxy": ">= 0.6.0"
},
"devDependencies": {
"mocha" : ">= 1.7.4",
Expand Down

0 comments on commit 906ae4a

Please sign in to comment.