blob: 8e5c03666fa3b7ddcfb02ae9bd77a9856af52351 [file] [log] [blame]
murgatroid9997d61302015-01-20 18:06:43 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
murgatroid9997d61302015-01-20 18:06:43 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
murgatroid99dca966d2015-02-19 14:37:18 -080034'use strict';
35
murgatroid99b6ab1b42015-01-21 10:30:36 -080036var fs = require('fs');
37var path = require('path');
murgatroid9997d61302015-01-20 18:06:43 -080038var _ = require('underscore');
39var grpc = require('..');
40var testProto = grpc.load(__dirname + '/test.proto').grpc.testing;
41var Server = grpc.buildServer([testProto.TestService.service]);
42
murgatroid99b6ab1b42015-01-21 10:30:36 -080043/**
44 * Create a buffer filled with size zeroes
45 * @param {number} size The length of the buffer
46 * @return {Buffer} The new buffer
47 */
murgatroid9997d61302015-01-20 18:06:43 -080048function zeroBuffer(size) {
49 var zeros = new Buffer(size);
50 zeros.fill(0);
51 return zeros;
52}
53
murgatroid99b6ab1b42015-01-21 10:30:36 -080054/**
55 * Respond to an empty parameter with an empty response.
56 * NOTE: this currently does not work due to issue #137
57 * @param {Call} call Call to handle
58 * @param {function(Error, Object)} callback Callback to call with result
59 * or error
60 */
murgatroid9997d61302015-01-20 18:06:43 -080061function handleEmpty(call, callback) {
62 callback(null, {});
63}
64
murgatroid99b6ab1b42015-01-21 10:30:36 -080065/**
66 * Handle a unary request by sending the requested payload
67 * @param {Call} call Call to handle
68 * @param {function(Error, Object)} callback Callback to call with result or
69 * error
70 */
murgatroid9997d61302015-01-20 18:06:43 -080071function handleUnary(call, callback) {
72 var req = call.request;
73 var zeros = zeroBuffer(req.response_size);
74 var payload_type = req.response_type;
75 if (payload_type === testProto.PayloadType.RANDOM) {
76 payload_type = [
77 testProto.PayloadType.COMPRESSABLE,
78 testProto.PayloadType.UNCOMPRESSABLE][Math.random() < 0.5 ? 0 : 1];
79 }
80 callback(null, {payload: {type: payload_type, body: zeros}});
81}
82
murgatroid99b6ab1b42015-01-21 10:30:36 -080083/**
84 * Respond to a streaming call with the total size of all payloads
85 * @param {Call} call Call to handle
86 * @param {function(Error, Object)} callback Callback to call with result or
87 * error
88 */
murgatroid9997d61302015-01-20 18:06:43 -080089function handleStreamingInput(call, callback) {
90 var aggregate_size = 0;
91 call.on('data', function(value) {
92 aggregate_size += value.payload.body.limit - value.payload.body.offset;
93 });
94 call.on('end', function() {
95 callback(null, {aggregated_payload_size: aggregate_size});
96 });
97}
98
murgatroid99b6ab1b42015-01-21 10:30:36 -080099/**
100 * Respond to a payload request with a stream of the requested payloads
101 * @param {Call} call Call to handle
102 */
murgatroid9997d61302015-01-20 18:06:43 -0800103function handleStreamingOutput(call) {
104 var req = call.request;
105 var payload_type = req.response_type;
106 if (payload_type === testProto.PayloadType.RANDOM) {
107 payload_type = [
108 testProto.PayloadType.COMPRESSABLE,
109 testProto.PayloadType.UNCOMPRESSABLE][Math.random() < 0.5 ? 0 : 1];
110 }
111 _.each(req.response_parameters, function(resp_param) {
112 call.write({
113 payload: {
114 body: zeroBuffer(resp_param.size),
115 type: payload_type
116 }
117 });
118 });
119 call.end();
120}
121
murgatroid99b6ab1b42015-01-21 10:30:36 -0800122/**
123 * Respond to a stream of payload requests with a stream of payload responses as
124 * they arrive.
125 * @param {Call} call Call to handle
126 */
murgatroid9997d61302015-01-20 18:06:43 -0800127function handleFullDuplex(call) {
128 call.on('data', function(value) {
129 var payload_type = value.response_type;
130 if (payload_type === testProto.PayloadType.RANDOM) {
131 payload_type = [
132 testProto.PayloadType.COMPRESSABLE,
133 testProto.PayloadType.UNCOMPRESSABLE][Math.random() < 0.5 ? 0 : 1];
134 }
135 _.each(value.response_parameters, function(resp_param) {
136 call.write({
137 payload: {
138 body: zeroBuffer(resp_param.size),
139 type: payload_type
140 }
141 });
142 });
143 });
144 call.on('end', function() {
145 call.end();
146 });
147}
148
murgatroid99b6ab1b42015-01-21 10:30:36 -0800149/**
150 * Respond to a stream of payload requests with a stream of payload responses
151 * after all requests have arrived
152 * @param {Call} call Call to handle
153 */
murgatroid9997d61302015-01-20 18:06:43 -0800154function handleHalfDuplex(call) {
155 throw new Error('HalfDuplexCall not yet implemented');
156}
157
murgatroid99b6ab1b42015-01-21 10:30:36 -0800158/**
159 * Get a server object bound to the given port
160 * @param {string} port Port to which to bind
161 * @param {boolean} tls Indicates that the bound port should use TLS
murgatroid99f034e502015-01-21 12:12:39 -0800162 * @return {{server: Server, port: number}} Server object bound to the support,
163 * and port number that the server is bound to
murgatroid99b6ab1b42015-01-21 10:30:36 -0800164 */
murgatroid9997d61302015-01-20 18:06:43 -0800165function getServer(port, tls) {
166 // TODO(mlumish): enable TLS functionality
murgatroid99b6ab1b42015-01-21 10:30:36 -0800167 var options = {};
murgatroid99da02a672015-03-02 17:28:02 -0800168 var server_creds = null;
murgatroid99b6ab1b42015-01-21 10:30:36 -0800169 if (tls) {
170 var key_path = path.join(__dirname, '../test/data/server1.key');
171 var pem_path = path.join(__dirname, '../test/data/server1.pem');
172
173 var key_data = fs.readFileSync(key_path);
174 var pem_data = fs.readFileSync(pem_path);
murgatroid99da02a672015-03-02 17:28:02 -0800175 server_creds = grpc.ServerCredentials.createSsl(null,
176 key_data,
177 pem_data);
murgatroid99b6ab1b42015-01-21 10:30:36 -0800178 }
murgatroid9997d61302015-01-20 18:06:43 -0800179 var server = new Server({
180 'grpc.testing.TestService' : {
181 emptyCall: handleEmpty,
182 unaryCall: handleUnary,
183 streamingOutputCall: handleStreamingOutput,
184 streamingInputCall: handleStreamingInput,
185 fullDuplexCall: handleFullDuplex,
186 halfDuplexCall: handleHalfDuplex
187 }
murgatroid994d2d0f02015-01-29 11:44:46 -0800188 }, null, options);
murgatroid99da02a672015-03-02 17:28:02 -0800189 var port_num = server.bind('0.0.0.0:' + port, server_creds);
murgatroid99f034e502015-01-21 12:12:39 -0800190 return {server: server, port: port_num};
murgatroid9997d61302015-01-20 18:06:43 -0800191}
192
193if (require.main === module) {
194 var parseArgs = require('minimist');
195 var argv = parseArgs(process.argv, {
196 string: ['port', 'use_tls']
197 });
murgatroid99f034e502015-01-21 12:12:39 -0800198 var server_obj = getServer(argv.port, argv.use_tls === 'true');
murgatroid9934986de2015-01-23 13:27:20 -0800199 console.log('Server attaching to port ' + argv.port);
200 server_obj.server.listen();
murgatroid9997d61302015-01-20 18:06:43 -0800201}
202
203/**
204 * See docs for getServer
205 */
Craig Tillerce5021b2015-02-18 09:25:21 -0800206exports.getServer = getServer;