Added API for servers to provide metadata

pull/283/head
murgatroid99 10 years ago
parent 6373ba01d4
commit 4d2d0f0f3a
  1. 2
      src/node/interop/interop_server.js
  2. 10
      src/node/src/server.js
  3. 9
      src/node/src/surface_server.js
  4. 28
      src/node/test/client_server_test.js
  5. 2
      src/node/test/surface_test.js

@ -183,7 +183,7 @@ function getServer(port, tls) {
fullDuplexCall: handleFullDuplex,
halfDuplexCall: handleHalfDuplex
}
}, options);
}, null, options);
var port_num = server.bind('0.0.0.0:' + port, tls);
return {server: server, port: port_num};
}

@ -202,10 +202,13 @@ GrpcServerStream.prototype._write = function(chunk, encoding, callback) {
* Constructs a server object that stores request handlers and delegates
* incoming requests to those handlers
* @constructor
* @param {Array} options Options that should be passed to the internal server
* @param {function(string, Object<string, Array<Buffer>>):
Object<string, Array<Buffer|string>>=} getMetadata Callback that gets
* metatada for a given method
* @param {Object=} options Options that should be passed to the internal server
* implementation
*/
function Server(options) {
function Server(getMetadata, options) {
this.handlers = {};
var handlers = this.handlers;
var server = new grpc.Server(options);
@ -249,6 +252,9 @@ function Server(options) {
stream.emit('cancelled');
}
}, 0);
if (getMetadata) {
call.addMetadata(getMetadata(data.method, data.metadata));
}
call.serverEndInitialMetadata(0);
var stream = new GrpcServerStream(call, handler.serialize,
handler.deserialize);

@ -256,10 +256,13 @@ function makeServerConstructor(services) {
* @constructor
* @param {Object} service_handlers Map from service names to map from method
* names to handlers
* @param {Object} options Options to pass to the underlying server
* @param {function(string, Object<string, Array<Buffer>>):
Object<string, Array<Buffer|string>>=} getMetadata Callback that
* gets metatada for a given method
* @param {Object=} options Options to pass to the underlying server
*/
function SurfaceServer(service_handlers, options) {
var server = new Server(options);
function SurfaceServer(service_handlers, getMetadata, options) {
var server = new Server(getMetadata, options);
this.inner_server = server;
_.each(services, function(service) {
var service_name = common.fullyQualifiedName(service);

@ -84,6 +84,10 @@ function cancelHandler(stream) {
// do nothing
}
function metadataHandler(stream, metadata) {
stream.end();
}
/**
* Serialize a string to a Buffer
* @param {string} value The string to serialize
@ -106,11 +110,14 @@ describe('echo client', function() {
var server;
var channel;
before(function() {
server = new Server();
server = new Server(function getMetadata(method, metadata) {
return {method: [method]};
});
var port_num = server.bind('0.0.0.0:0');
server.register('echo', echoHandler);
server.register('error', errorHandler);
server.register('cancellation', cancelHandler);
server.register('metadata', metadataHandler);
server.start();
channel = new grpc.Channel('localhost:' + port_num);
@ -142,12 +149,19 @@ describe('echo client', function() {
done();
});
});
it('should recieve metadata set by the server', function(done) {
var stream = client.makeRequest(channel, 'metadata');
stream.on('metadata', function(metadata) {
assert.strictEqual(metadata.method[0].toString(), 'metadata');
});
stream.on('status', function(status) {
assert.equal(status.code, client.status.OK);
done();
});
stream.end();
});
it('should get an error status that the server throws', function(done) {
var stream = client.makeRequest(
channel,
'error',
null,
getDeadline(1));
var stream = client.makeRequest(channel, 'error');
stream.on('data', function() {});
stream.write(new Buffer('test'));
@ -189,7 +203,7 @@ describe('secure echo client', function() {
key_data,
pem_data);
server = new Server({'credentials' : server_creds});
server = new Server(null, {'credentials' : server_creds});
var port_num = server.bind('0.0.0.0:0', true);
server.register('echo', echoHandler);
server.start();

@ -75,7 +75,7 @@ describe('Surface server constructor', function() {
}, /math.Math/);
});
});
describe('Surface client', function() {
describe('Cancelling surface client', function() {
var client;
var server;
before(function() {

Loading…
Cancel
Save