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