murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 4 | * 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 | |
murgatroid99 | dca966d | 2015-02-19 14:37:18 -0800 | [diff] [blame] | 34 | 'use strict'; |
| 35 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 36 | var fs = require('fs'); |
| 37 | var path = require('path'); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 38 | var _ = require('underscore'); |
| 39 | var grpc = require('..'); |
| 40 | var testProto = grpc.load(__dirname + '/test.proto').grpc.testing; |
| 41 | var Server = grpc.buildServer([testProto.TestService.service]); |
| 42 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 43 | /** |
| 44 | * Create a buffer filled with size zeroes |
| 45 | * @param {number} size The length of the buffer |
| 46 | * @return {Buffer} The new buffer |
| 47 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 48 | function zeroBuffer(size) { |
| 49 | var zeros = new Buffer(size); |
| 50 | zeros.fill(0); |
| 51 | return zeros; |
| 52 | } |
| 53 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 54 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 61 | function handleEmpty(call, callback) { |
| 62 | callback(null, {}); |
| 63 | } |
| 64 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 65 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 71 | function 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 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 83 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 89 | function 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 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 99 | /** |
| 100 | * Respond to a payload request with a stream of the requested payloads |
| 101 | * @param {Call} call Call to handle |
| 102 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 103 | function 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 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 122 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 127 | function 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 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 149 | /** |
| 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 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 154 | function handleHalfDuplex(call) { |
| 155 | throw new Error('HalfDuplexCall not yet implemented'); |
| 156 | } |
| 157 | |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 158 | /** |
| 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 |
murgatroid99 | f034e50 | 2015-01-21 12:12:39 -0800 | [diff] [blame] | 162 | * @return {{server: Server, port: number}} Server object bound to the support, |
| 163 | * and port number that the server is bound to |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 164 | */ |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 165 | function getServer(port, tls) { |
| 166 | // TODO(mlumish): enable TLS functionality |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 167 | var options = {}; |
murgatroid99 | da02a67 | 2015-03-02 17:28:02 -0800 | [diff] [blame^] | 168 | var server_creds = null; |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 169 | 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); |
murgatroid99 | da02a67 | 2015-03-02 17:28:02 -0800 | [diff] [blame^] | 175 | server_creds = grpc.ServerCredentials.createSsl(null, |
| 176 | key_data, |
| 177 | pem_data); |
murgatroid99 | b6ab1b4 | 2015-01-21 10:30:36 -0800 | [diff] [blame] | 178 | } |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 179 | 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 | } |
murgatroid99 | 4d2d0f0 | 2015-01-29 11:44:46 -0800 | [diff] [blame] | 188 | }, null, options); |
murgatroid99 | da02a67 | 2015-03-02 17:28:02 -0800 | [diff] [blame^] | 189 | var port_num = server.bind('0.0.0.0:' + port, server_creds); |
murgatroid99 | f034e50 | 2015-01-21 12:12:39 -0800 | [diff] [blame] | 190 | return {server: server, port: port_num}; |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | if (require.main === module) { |
| 194 | var parseArgs = require('minimist'); |
| 195 | var argv = parseArgs(process.argv, { |
| 196 | string: ['port', 'use_tls'] |
| 197 | }); |
murgatroid99 | f034e50 | 2015-01-21 12:12:39 -0800 | [diff] [blame] | 198 | var server_obj = getServer(argv.port, argv.use_tls === 'true'); |
murgatroid99 | 34986de | 2015-01-23 13:27:20 -0800 | [diff] [blame] | 199 | console.log('Server attaching to port ' + argv.port); |
| 200 | server_obj.server.listen(); |
murgatroid99 | 97d6130 | 2015-01-20 18:06:43 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * See docs for getServer |
| 205 | */ |
Craig Tiller | ce5021b | 2015-02-18 09:25:21 -0800 | [diff] [blame] | 206 | exports.getServer = getServer; |