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> |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 39 | #include <unistd.h> |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 40 | |
Nicolas "Pixel" Noble | ba60820 | 2015-02-20 02:48:03 +0100 | [diff] [blame] | 41 | #include <gflags/gflags.h> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 42 | #include <grpc/grpc.h> |
| 43 | #include <grpc/support/log.h> |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 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> |
Abhishek Kumar | 1b3e3cd | 2015-04-16 20:10:29 -0700 | [diff] [blame^] | 51 | #include "test/proto/test.grpc.pb.h" |
Nicolas "Pixel" Noble | 0caebbf | 2015-04-09 23:08:51 +0200 | [diff] [blame] | 52 | #include "test/cpp/interop/empty.grpc.pb.h" |
| 53 | #include "test/cpp/interop/messages.grpc.pb.h" |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 54 | #include "test/cpp/interop/server_helper.h" |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 55 | #include "test/cpp/util/test_config.h" |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 56 | |
| 57 | DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls."); |
| 58 | DEFINE_int32(port, 0, "Server port."); |
| 59 | |
| 60 | using grpc::Server; |
| 61 | using grpc::ServerBuilder; |
| 62 | using grpc::ServerContext; |
| 63 | using grpc::ServerCredentials; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 64 | using grpc::ServerReader; |
| 65 | using grpc::ServerReaderWriter; |
| 66 | using grpc::ServerWriter; |
| 67 | using grpc::SslServerCredentialsOptions; |
| 68 | using grpc::testing::Payload; |
| 69 | using grpc::testing::PayloadType; |
| 70 | using grpc::testing::SimpleRequest; |
| 71 | using grpc::testing::SimpleResponse; |
| 72 | using grpc::testing::StreamingInputCallRequest; |
| 73 | using grpc::testing::StreamingInputCallResponse; |
| 74 | using grpc::testing::StreamingOutputCallRequest; |
| 75 | using grpc::testing::StreamingOutputCallResponse; |
| 76 | using grpc::testing::TestService; |
| 77 | using grpc::Status; |
| 78 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 79 | static bool got_sigint = false; |
| 80 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 81 | bool SetPayload(PayloadType type, int size, Payload* payload) { |
| 82 | PayloadType response_type = type; |
| 83 | // TODO(yangg): Support UNCOMPRESSABLE payload. |
| 84 | if (type != PayloadType::COMPRESSABLE) { |
| 85 | return false; |
| 86 | } |
| 87 | payload->set_type(response_type); |
| 88 | std::unique_ptr<char[]> body(new char[size]()); |
| 89 | payload->set_body(body.get(), size); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | class TestServiceImpl : public TestService::Service { |
| 94 | public: |
| 95 | Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request, |
| 96 | grpc::testing::Empty* response) { |
| 97 | return Status::OK; |
| 98 | } |
| 99 | |
| 100 | Status UnaryCall(ServerContext* context, const SimpleRequest* request, |
| 101 | SimpleResponse* response) { |
| 102 | if (request->has_response_size() && request->response_size() > 0) { |
| 103 | if (!SetPayload(request->response_type(), request->response_size(), |
| 104 | response->mutable_payload())) { |
| 105 | return Status(grpc::StatusCode::INTERNAL, "Error creating payload."); |
| 106 | } |
| 107 | } |
| 108 | return Status::OK; |
| 109 | } |
| 110 | |
| 111 | Status StreamingOutputCall( |
| 112 | ServerContext* context, const StreamingOutputCallRequest* request, |
| 113 | ServerWriter<StreamingOutputCallResponse>* writer) { |
| 114 | StreamingOutputCallResponse response; |
| 115 | bool write_success = true; |
| 116 | response.mutable_payload()->set_type(request->response_type()); |
| 117 | for (int i = 0; write_success && i < request->response_parameters_size(); |
| 118 | i++) { |
| 119 | response.mutable_payload()->set_body( |
| 120 | grpc::string(request->response_parameters(i).size(), '\0')); |
| 121 | write_success = writer->Write(response); |
| 122 | } |
| 123 | if (write_success) { |
| 124 | return Status::OK; |
| 125 | } else { |
| 126 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | Status StreamingInputCall(ServerContext* context, |
| 131 | ServerReader<StreamingInputCallRequest>* reader, |
| 132 | StreamingInputCallResponse* response) { |
| 133 | StreamingInputCallRequest request; |
| 134 | int aggregated_payload_size = 0; |
| 135 | while (reader->Read(&request)) { |
| 136 | if (request.has_payload() && request.payload().has_body()) { |
| 137 | aggregated_payload_size += request.payload().body().size(); |
| 138 | } |
| 139 | } |
| 140 | response->set_aggregated_payload_size(aggregated_payload_size); |
| 141 | return Status::OK; |
| 142 | } |
| 143 | |
| 144 | Status FullDuplexCall( |
| 145 | ServerContext* context, |
| 146 | ServerReaderWriter<StreamingOutputCallResponse, |
| 147 | StreamingOutputCallRequest>* stream) { |
| 148 | StreamingOutputCallRequest request; |
| 149 | StreamingOutputCallResponse response; |
| 150 | bool write_success = true; |
| 151 | while (write_success && stream->Read(&request)) { |
| 152 | response.mutable_payload()->set_type(request.payload().type()); |
| 153 | if (request.response_parameters_size() == 0) { |
| 154 | return Status(grpc::StatusCode::INTERNAL, |
| 155 | "Request does not have response parameters."); |
| 156 | } |
| 157 | response.mutable_payload()->set_body( |
| 158 | grpc::string(request.response_parameters(0).size(), '\0')); |
| 159 | write_success = stream->Write(response); |
| 160 | } |
| 161 | if (write_success) { |
| 162 | return Status::OK; |
| 163 | } else { |
| 164 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | Status HalfDuplexCall( |
| 169 | ServerContext* context, |
| 170 | ServerReaderWriter<StreamingOutputCallResponse, |
| 171 | StreamingOutputCallRequest>* stream) { |
| 172 | std::vector<StreamingOutputCallRequest> requests; |
| 173 | StreamingOutputCallRequest request; |
| 174 | while (stream->Read(&request)) { |
| 175 | requests.push_back(request); |
| 176 | } |
| 177 | |
| 178 | StreamingOutputCallResponse response; |
| 179 | bool write_success = true; |
| 180 | for (unsigned int i = 0; write_success && i < requests.size(); i++) { |
| 181 | response.mutable_payload()->set_type(requests[i].payload().type()); |
| 182 | if (requests[i].response_parameters_size() == 0) { |
| 183 | return Status(grpc::StatusCode::INTERNAL, |
| 184 | "Request does not have response parameters."); |
| 185 | } |
| 186 | response.mutable_payload()->set_body( |
| 187 | grpc::string(requests[i].response_parameters(0).size(), '\0')); |
| 188 | write_success = stream->Write(response); |
| 189 | } |
| 190 | if (write_success) { |
| 191 | return Status::OK; |
| 192 | } else { |
| 193 | return Status(grpc::StatusCode::INTERNAL, "Error writing response."); |
| 194 | } |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | void RunServer() { |
| 199 | std::ostringstream server_address; |
Yang Gao | dab7095 | 2015-01-23 16:06:36 -0800 | [diff] [blame] | 200 | server_address << "0.0.0.0:" << FLAGS_port; |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 201 | TestServiceImpl service; |
| 202 | |
| 203 | SimpleRequest request; |
| 204 | SimpleResponse response; |
| 205 | |
| 206 | ServerBuilder builder; |
Craig Tiller | f8ac5d8 | 2015-02-09 16:24:20 -0800 | [diff] [blame] | 207 | builder.RegisterService(&service); |
Yang Gao | a400207 | 2015-04-09 23:25:21 -0700 | [diff] [blame] | 208 | std::shared_ptr<ServerCredentials> creds = |
| 209 | grpc::testing::CreateInteropServerCredentials(); |
Nicolas Noble | cfd6073 | 2015-03-18 16:27:43 -0700 | [diff] [blame] | 210 | builder.AddListeningPort(server_address.str(), creds); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 211 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 212 | 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] | 213 | while (!got_sigint) { |
Craig Tiller | cf133f4 | 2015-02-26 14:05:56 -0800 | [diff] [blame] | 214 | sleep(5); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 218 | static void sigint_handler(int x) { got_sigint = true; } |
| 219 | |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 220 | int main(int argc, char** argv) { |
| 221 | grpc_init(); |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 222 | grpc::testing::InitTest(&argc, &argv, true); |
Craig Tiller | 2f3e2ec | 2015-02-20 13:07:50 -0800 | [diff] [blame] | 223 | signal(SIGINT, sigint_handler); |
yangg | 0617057 | 2015-01-12 13:12:45 -0800 | [diff] [blame] | 224 | |
| 225 | GPR_ASSERT(FLAGS_port != 0); |
| 226 | RunServer(); |
| 227 | |
| 228 | grpc_shutdown(); |
| 229 | return 0; |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 230 | } |