From ea12b97243f95d1830a9f70c3c176b82ef37cb04 Mon Sep 17 00:00:00 2001 From: murgatroid99 Date: Fri, 24 Jul 2015 10:43:27 -0700 Subject: Exposed channel target and call peer in Node wrapper --- src/node/ext/call.cc | 14 ++++++++++++++ src/node/ext/call.h | 1 + src/node/ext/channel.cc | 11 +++++++++++ src/node/ext/channel.h | 1 + src/node/src/client.js | 16 ++++++++++++++++ src/node/src/server.js | 16 ++++++++++++++++ src/node/test/call_test.js | 6 ++++++ src/node/test/channel_test.js | 6 ++++++ src/node/test/surface_test.js | 40 ++++++++++++++++++++++++++++++++++++++++ 9 files changed, 111 insertions(+) (limited to 'src/node') diff --git a/src/node/ext/call.cc b/src/node/ext/call.cc index 15c9b2d97d..b647735ead 100644 --- a/src/node/ext/call.cc +++ b/src/node/ext/call.cc @@ -453,6 +453,8 @@ void Call::Init(Handle exports) { NanNew(StartBatch)->GetFunction()); NanSetPrototypeTemplate(tpl, "cancel", NanNew(Cancel)->GetFunction()); + NanSetPrototypeTemplate(tpl, "getPeer", + NanNew(GetPeer)->GetFunction()); NanAssignPersistent(fun_tpl, tpl); Handle ctr = tpl->GetFunction(); ctr->Set(NanNew("WRITE_BUFFER_HINT"), @@ -608,5 +610,17 @@ NAN_METHOD(Call::Cancel) { NanReturnUndefined(); } +NAN_METHOD(Call::GetPeer) { + NanScope(); + if (!HasInstance(args.This())) { + return NanThrowTypeError("getPeer can only be called on Call objects"); + } + Call *call = ObjectWrap::Unwrap(args.This()); + char *peer = grpc_call_get_peer(call->wrapped_call); + Handle peer_value = NanNew(peer); + gpr_free(peer); + NanReturnValue(peer_value); +} + } // namespace node } // namespace grpc diff --git a/src/node/ext/call.h b/src/node/ext/call.h index 43142c7091..6acda76197 100644 --- a/src/node/ext/call.h +++ b/src/node/ext/call.h @@ -120,6 +120,7 @@ class Call : public ::node::ObjectWrap { static NAN_METHOD(New); static NAN_METHOD(StartBatch); static NAN_METHOD(Cancel); + static NAN_METHOD(GetPeer); static NanCallback *constructor; // Used for typechecking instances of this javascript class static v8::Persistent fun_tpl; diff --git a/src/node/ext/channel.cc b/src/node/ext/channel.cc index d37bf763dd..0b7333e450 100644 --- a/src/node/ext/channel.cc +++ b/src/node/ext/channel.cc @@ -76,6 +76,8 @@ void Channel::Init(Handle exports) { tpl->InstanceTemplate()->SetInternalFieldCount(1); NanSetPrototypeTemplate(tpl, "close", NanNew(Close)->GetFunction()); + NanSetPrototypeTemplate(tpl, "getTarget", + NanNew(GetTarget)->GetFunction()); NanAssignPersistent(fun_tpl, tpl); Handle ctr = tpl->GetFunction(); constructor = new NanCallback(ctr); @@ -185,5 +187,14 @@ NAN_METHOD(Channel::Close) { NanReturnUndefined(); } +NAN_METHOD(Channel::GetTarget) { + NanScope(); + if (!HasInstance(args.This())) { + return NanThrowTypeError("getTarget can only be called on Channel objects"); + } + Channel *channel = ObjectWrap::Unwrap(args.This()); + NanReturnValue(NanNew(grpc_channel_get_target(channel->wrapped_channel))); +} + } // namespace node } // namespace grpc diff --git a/src/node/ext/channel.h b/src/node/ext/channel.h index b3aa0f700f..6725ebb03f 100644 --- a/src/node/ext/channel.h +++ b/src/node/ext/channel.h @@ -66,6 +66,7 @@ class Channel : public ::node::ObjectWrap { static NAN_METHOD(New); static NAN_METHOD(Close); + static NAN_METHOD(GetTarget); static NanCallback *constructor; static v8::Persistent fun_tpl; diff --git a/src/node/src/client.js b/src/node/src/client.js index da6327b432..d89c656c07 100644 --- a/src/node/src/client.js +++ b/src/node/src/client.js @@ -187,6 +187,19 @@ ClientReadableStream.prototype.cancel = cancel; ClientWritableStream.prototype.cancel = cancel; ClientDuplexStream.prototype.cancel = cancel; +/** + * Get the endpoint this call/stream is connected to. + * @return {string} The URI of the endpoint + */ +function getPeer() { + /* jshint validthis: true */ + return this.call.getPeer(); +} + +ClientReadableStream.prototype.getPeer = getPeer; +ClientWritableStream.prototype.getPeer = getPeer; +ClientDuplexStream.prototype.getPeer = getPeer; + /** * Get a function that can make unary requests to the specified method. * @param {string} method The name of the method to request @@ -223,6 +236,9 @@ function makeUnaryRequestFunction(method, serialize, deserialize) { emitter.cancel = function cancel() { call.cancel(); }; + emitter.getPeer = function getPeer() { + return call.getPeer(); + }; this.updateMetadata(this.auth_uri, metadata, function(error, metadata) { if (error) { call.cancel(); diff --git a/src/node/src/server.js b/src/node/src/server.js index 0a3a0031bd..cb86b95f38 100644 --- a/src/node/src/server.js +++ b/src/node/src/server.js @@ -373,6 +373,19 @@ ServerDuplexStream.prototype._read = _read; ServerDuplexStream.prototype._write = _write; ServerDuplexStream.prototype.sendMetadata = sendMetadata; +/** + * Get the endpoint this call/stream is connected to. + * @return {string} The URI of the endpoint + */ +function getPeer() { + /* jshint validthis: true */ + return this.call.getPeer(); +} + +ServerReadableStream.prototype.getPeer = getPeer; +ServerWritableStream.prototype.getPeer = getPeer; +ServerDuplexStream.prototype.getPeer = getPeer; + /** * Fully handle a unary call * @param {grpc.Call} call The call to handle @@ -389,6 +402,9 @@ function handleUnary(call, handler, metadata) { call.startBatch(batch, function() {}); } }; + emitter.getPeer = function() { + return call.getPeer(); + }; emitter.on('error', function(error) { handleError(call, error); }); diff --git a/src/node/test/call_test.js b/src/node/test/call_test.js index 98158ffff3..0079144ae6 100644 --- a/src/node/test/call_test.js +++ b/src/node/test/call_test.js @@ -184,4 +184,10 @@ describe('call', function() { }); }); }); + describe('getPeer', function() { + it('should return a string', function() { + var call = new grpc.Call(channel, 'method', getDeadline(1)); + assert.strictEqual(typeof call.getPeer(), 'string'); + }); + }); }); diff --git a/src/node/test/channel_test.js b/src/node/test/channel_test.js index 33200c99ee..3e61d3bbc6 100644 --- a/src/node/test/channel_test.js +++ b/src/node/test/channel_test.js @@ -87,4 +87,10 @@ describe('channel', function() { }); }); }); + describe('getTarget', function() { + it('should return a string', function() { + var channel = new grpc.Channel('localhost', {}); + assert.strictEqual(typeof channel.getTarget(), 'string'); + }); + }); }); diff --git a/src/node/test/surface_test.js b/src/node/test/surface_test.js index 3cb68f8cd8..9005cbd505 100644 --- a/src/node/test/surface_test.js +++ b/src/node/test/surface_test.js @@ -344,6 +344,9 @@ describe('Other conditions', function() { after(function() { server.shutdown(); }); + it('channel.getTarget should be available', function() { + assert.strictEqual(typeof client.channel.getTarget(), 'string'); + }); describe('Server recieving bad input', function() { var misbehavingClient; var badArg = new Buffer([0xFF]); @@ -549,6 +552,43 @@ describe('Other conditions', function() { }); }); }); + describe('call.getPeer should return the peer', function() { + it('for a unary call', function(done) { + var call = client.unary({error: false}, function(err, data) { + assert.ifError(err); + done(); + }); + assert.strictEqual(typeof call.getPeer(), 'string'); + }); + it('for a client stream call', function(done) { + var call = client.clientStream(function(err, data) { + assert.ifError(err); + done(); + }); + assert.strictEqual(typeof call.getPeer(), 'string'); + call.write({error: false}); + call.end(); + }); + it('for a server stream call', function(done) { + var call = client.serverStream({error: false}); + assert.strictEqual(typeof call.getPeer(), 'string'); + call.on('data', function(){}); + call.on('status', function(status) { + assert.strictEqual(status.code, grpc.status.OK); + done(); + }); + }); + it('for a bidi stream call', function(done) { + var call = client.bidiStream(); + assert.strictEqual(typeof call.getPeer(), 'string'); + call.write({error: false}); + call.end(); + call.on('data', function(){}); + call.on('status', function(status) { + done(); + }); + }); + }); }); describe('Cancelling surface client', function() { var client; -- cgit v1.2.3