Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add initial implementation

  • Loading branch information...
commit 659081fbcc3a436b57f9a891e403ad516b66262e 1 parent 8812523
@tj authored
View
3  index.js
@@ -1,2 +1,3 @@
-module.exports = require('./lib/axon-rpc');
+exports.Server = require('./lib/server');
+exports.Client = require('./lib/client');
View
0  lib/axon-rpc.js
No changes.
View
25 lib/client.js
@@ -0,0 +1,25 @@
+
+module.exports = Client;
+
+function Client(sock) {
+ sock.format('json');
+ this.sock = sock;
+}
+
+Client.prototype.call = function(name){
+ var args = [].slice.call(arguments, 1, -1);
+ var fn = arguments[arguments.length - 1];
+
+ this.sock.send({
+ type: 'call',
+ method: name,
+ args: args
+ }, function(msg){
+ if (msg.error) {
+ fn(new Error(msg.error));
+ } else {
+ msg.args.unshift(null);
+ fn.apply(null, msg.args);
+ }
+ });
+};
View
29 lib/server.js
@@ -0,0 +1,29 @@
+
+module.exports = Server;
+
+function Server(sock) {
+ sock.format('json');
+ this.sock = sock;
+ this.methods = {};
+ this.sock.on('message', this.onmessage.bind(this));
+}
+
+Server.prototype.onmessage = function(msg, reply){
+ var meth = msg.method;
+ var fn = this.methods[meth];
+ if (!fn) return reply({ error: 'method "' + meth + '" does not exist' });
+ var args = msg.args;
+
+ args.push(function(err){
+ if (err) return reply({ error: err.message });
+ var args = [].slice.call(arguments, 1);
+ reply({ args: args });
+ });
+
+ fn.apply(null, args);
+};
+
+
+Server.prototype.expose = function(name, fn){
+ this.methods[name] = fn;
+};
View
7 package.json
@@ -5,11 +5,12 @@
"keywords": ["axon", "rpc", "cloud"],
"author": "TJ Holowaychuk <tj@learnboost.com>",
"dependencies": {
- "axon": "0.4.2"
+ "debug": "*"
},
"devDependencies": {
- "mocha": "*",
- "should": "*"
+ "axon": "0.4.2",
+ "better-assert": "*",
+ "mocha": "*"
},
"main": "index"
}
View
31 test/index.js
@@ -0,0 +1,31 @@
+
+/**
+ * Module dependencies.
+ */
+
+var rpc = require('..')
+ , axon = require('axon')
+ , assert = require('better-assert');
+
+var rep = axon.socket('rep');
+var req = axon.socket('req');
+
+rep.bind(4000);
+req.connect(4000);
+
+var server = new rpc.Server(rep);
+var client = new rpc.Client(req);
+
+describe('Server#expose(name, fn)', function(){
+ it('should expose a single function', function(done){
+ server.expose('add', function(a, b, fn){
+ fn(null, a + b);
+ });
+
+ client.call('add', 1, 2, function(err, n){
+ assert(!err);
+ assert(3 === n);
+ done();
+ });
+ })
+})
Please sign in to comment.
Something went wrong with that request. Please try again.