yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 0605995 | 2015-02-18 08:34:56 -0800 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
yangg | 0617057 | 2015-01-12 13:12:45 -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 | |
| 34 | #include <memory> |
| 35 | #include <sstream> |
| 36 | #include <thread> |
| 37 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 38 | #include <signal.h> |
| 39 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 40 | #include <gflags/gflags.h> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 41 | #include <grpc/grpc.h> |
| 42 | #include <grpc/support/log.h> |
| 43 | #include "test/core/end2end/data/ssl_test_data.h" |
| 44 | #include <grpc++/config.h> |
| 45 | #include <grpc++/server.h> |
| 46 | #include <grpc++/server_builder.h> |
| 47 | #include <grpc++/server_context.h> |
| 48 | #include <grpc++/server_credentials.h> |
| 49 | #include <grpc++/status.h> |
| 50 | #include <grpc++/stream.h> |
| 51 | #include "test/cpp/interop/test.pb.h" |
| 52 | #include "test/cpp/interop/empty.pb.h" |
| 53 | #include "test/cpp/interop/messages.pb.h" |
| 54 | |
| 55 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
| 56 | DEFINE_int32(port, 0, "Server port."); |
| 57 | |
| 58 | using grpc::Server; |
| 59 | using grpc::ServerBuilder; |
| 60 | using grpc::ServerContext; |
| 61 | using grpc::ServerCredentials; |
| 62 | using grpc::ServerCredentialsFactory; |
| 63 | using grpc::ServerReader; |
| 64 | using grpc::ServerReaderWriter; |
| 65 | using grpc::ServerWriter; |
| 66 | using grpc::SslServerCredentialsOptions; |
| 67 | using grpc::testing::Payload; |
| 68 | using grpc::testing::PayloadType; |
| 69 | using grpc::testing::SimpleRequest; |
| 70 | using grpc::testing::SimpleResponse; |
| 71 | using grpc::testing::StreamingInputCallRequest; |
| 72 | using grpc::testing::StreamingInputCallResponse; |
| 73 | using grpc::testing::StreamingOutputCallRequest; |
| 74 | using grpc::testing::StreamingOutputCallResponse; |
| 75 | using grpc::testing::TestService; |
| 76 | using grpc::Status; |
| 77 | |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 78 | // In some distros, gflags is in the namespace google, and in some others, |
| 79 | // in gflags. This hack is enabling us to find both. |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 80 | namespace google {} |
| 81 | namespace gflags {} |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 82 | using namespace google; |
| 83 | using namespace gflags; |
| 84 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 85 | static bool got_sigint = false; |
| 86 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 87 | bool SetPayload(PayloadType type, int size, Payload* payload) { |
| 88 | PayloadType response_type = type; |
| 89 | // TODO(yangg): Support UNCOMPRESSABLE payload. |
| 90 | if (type != PayloadType::COMPRESSABLE) { |
| 91 | return false; |
| 92 | } |
| 93 | payload->set_type(response_type); |
| 94 | std::unique_ptr<char[]> body(new char[size]()); |
| 95 | payload->set_body(body.get(), size); |
| 96 | return true; |
| 97 | } |
| 98 | |
| 99 | class TestServiceImpl : public TestService::Service { |
| 100 | public: |
| 101 | Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request, |
| 102 | grpc::testing::Empty* response) { |
| 103 | return Status::OK; |
| 104 | } |
| 105 | |
| 106 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
| 107 | SimpleResponse* response) { |
| 108 | if (request->has_response_size() && request->response_size() > 0) { |
| 109 | if (!SetPayload(request->response_type(), request->response_size(), |
| 110 | response->mutable_payload())) { |
| 111 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 112 | } |
| 113 | } |
| 114 | return Status::OK; |
| 115 | } |
| 116 | |
| 117 | Status StreamingOutputCall( |
| 118 | ServerContext* context, const StreamingOutputCallRequest* request, |
| 119 | ServerWriter<StreamingOutputCallResponse>* writer) { |
| 120 | StreamingOutputCallResponse response; |
| 121 | bool write_success = true; |
| 122 | response.mutable_payload()->set_type(request->response_type()); |
| 123 | for (int i = 0; write_success && i < request->response_parameters_size(); |
| 124 | i++) { |
| 125 | response.mutable_payload()->set_body( |
| 126 | grpc::string(request->response_parameters(i).size(), '\0')); |
| 127 | write_success = writer->Write(response); |
| 128 | } |
| 129 | if (write_success) { |
| 130 | return Status::OK; |
| 131 | } else { |
| 132 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | Status StreamingInputCall(ServerContext* context, |
| 137 | ServerReader<StreamingInputCallRequest>* reader, |
| 138 | StreamingInputCallResponse* response) { |
| 139 | StreamingInputCallRequest request; |
| 140 | int aggregated_payload_size = 0; |
| 141 | while (reader->Read(&request)) { |
| 142 | if (request.has_payload() && request.payload().has_body()) { |
| 143 | aggregated_payload_size += request.payload().body().size(); |
| 144 | } |
| 145 | } |
| 146 | response->set_aggregated_payload_size(aggregated_payload_size); |
| 147 | return Status::OK; |
| 148 | } |
| 149 | |
| 150 | Status FullDuplexCall( |
| 151 | ServerContext* context, |
| 152 | ServerReaderWriter<StreamingOutputCallResponse, |
| 153 | StreamingOutputCallRequest>* stream) { |
| 154 | StreamingOutputCallRequest request; |
| 155 | StreamingOutputCallResponse response; |
| 156 | bool write_success = true; |
| 157 | while (write_success && stream->Read(&request)) { |
| 158 | response.mutable_payload()->set_type(request.payload().type()); |
| 159 | if (request.response_parameters_size() == 0) { |
| 160 | return Status(grpc::StatusCode::INTERNAL, |
| 161 | "Request does not have response parameters."); |
| 162 | } |
| 163 | response.mutable_payload()->set_body( |
| 164 | grpc::string(request.response_parameters(0).size(), '\0')); |
| 165 | write_success = stream->Write(response); |
| 166 | } |
| 167 | if (write_success) { |
| 168 | return Status::OK; |
| 169 | } else { |
| 170 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | Status HalfDuplexCall( |
| 175 | ServerContext* context, |
| 176 | ServerReaderWriter<StreamingOutputCallResponse, |
| 177 | StreamingOutputCallRequest>* stream) { |
| 178 | std::vector<StreamingOutputCallRequest> requests; |
| 179 | StreamingOutputCallRequest request; |
| 180 | while (stream->Read(&request)) { |
| 181 | requests.push_back(request); |
| 182 | } |
| 183 | |
| 184 | StreamingOutputCallResponse response; |
| 185 | bool write_success = true; |
| 186 | for (unsigned int i = 0; write_success && i < requests.size(); i++) { |
| 187 | response.mutable_payload()->set_type(requests[i].payload().type()); |
| 188 | if (requests[i].response_parameters_size() == 0) { |
| 189 | return Status(grpc::StatusCode::INTERNAL, |
| 190 | "Request does not have response parameters."); |
| 191 | } |
| 192 | response.mutable_payload()->set_body( |
| 193 | grpc::string(requests[i].response_parameters(0).size(), '\0')); |
| 194 | write_success = stream->Write(response); |
| 195 | } |
| 196 | if (write_success) { |
| 197 | return Status::OK; |
| 198 | } else { |
| 199 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 200 | } |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | void RunServer() { |
| 205 | std::ostringstream server_address; |
Yang Gao | dab7095 | 2015-01-23 16:06:36 -0800 | [diff] [blame] | 206 | server_address << "0.0.0.0:" << FLAGS_port; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 207 | TestServiceImpl service; |
| 208 | |
| 209 | SimpleRequest request; |
| 210 | SimpleResponse response; |
| 211 | |
| 212 | ServerBuilder builder; |
| 213 | builder.AddPort(server_address.str()); |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 214 | builder.RegisterService(&service); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 215 | if (FLAGS_enable_ssl) { |
| 216 | SslServerCredentialsOptions ssl_opts = { |
Julien Boeuf | 8fbcc43 | 2015-01-15 16:44:13 -0800 | [diff] [blame] | 217 | "", {{test_server1_key, test_server1_cert}}}; |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 218 | std::shared_ptr<ServerCredentials> creds = ServerSslCredentials(ssl_opts); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 219 | builder.SetCredentials(creds); |
| 220 | } |
| 221 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 222 | gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 223 | while (!got_sigint) { |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 224 | std::this_thread::sleep_for(std::chrono::seconds(5)); |
| 225 | } |
| 226 | } |
| 227 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 228 | static void sigint_handler(int x) { got_sigint = true; } |
| 229 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 230 | int main(int argc, char** argv) { |
| 231 | grpc_init(); |
Nicolas "Pixel" Noble | 7e80efc | 2015-02-20 03:13:38 +0100 | [diff] [blame] | 232 | ParseCommandLineFlags(&argc, &argv, true); |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 233 | signal(SIGINT, sigint_handler); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 234 | |
| 235 | GPR_ASSERT(FLAGS_port != 0); |
| 236 | RunServer(); |
| 237 | |
| 238 | grpc_shutdown(); |
| 239 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 240 | } |