server.cc 8.2 KB

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