server.cc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <unistd.h>
  38. #include <gflags/gflags.h>
  39. #include <grpc/grpc.h>
  40. #include <grpc/support/log.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/proto/test.grpc.pb.h"
  49. #include "test/proto/empty.grpc.pb.h"
  50. #include "test/proto/messages.grpc.pb.h"
  51. #include "test/cpp/interop/server_helper.h"
  52. #include "test/cpp/util/test_config.h"
  53. DEFINE_bool(enable_ssl, false, "Whether to use ssl/tls.");
  54. DEFINE_int32(port, 0, "Server port.");
  55. using grpc::Server;
  56. using grpc::ServerBuilder;
  57. using grpc::ServerContext;
  58. using grpc::ServerCredentials;
  59. using grpc::ServerReader;
  60. using grpc::ServerReaderWriter;
  61. using grpc::ServerWriter;
  62. using grpc::SslServerCredentialsOptions;
  63. using grpc::testing::Payload;
  64. using grpc::testing::PayloadType;
  65. using grpc::testing::SimpleRequest;
  66. using grpc::testing::SimpleResponse;
  67. using grpc::testing::StreamingInputCallRequest;
  68. using grpc::testing::StreamingInputCallResponse;
  69. using grpc::testing::StreamingOutputCallRequest;
  70. using grpc::testing::StreamingOutputCallResponse;
  71. using grpc::testing::TestService;
  72. using grpc::Status;
  73. static bool got_sigint = false;
  74. bool SetPayload(PayloadType type, int size, Payload* payload) {
  75. PayloadType response_type = type;
  76. payload->set_type(response_type);
  77. std::unique_ptr<char[]> body(new char[size]());
  78. payload->set_body(body.get(), size);
  79. return true;
  80. }
  81. template <typename RequestType>
  82. void SetResponseCompression(ServerContext* context,
  83. const RequestType& request) {
  84. if (request.has_response_compression()) {
  85. switch (request.response_compression()) {
  86. case grpc::testing::NONE:
  87. context->set_compression_algorithm(GRPC_COMPRESS_NONE);
  88. break;
  89. case grpc::testing::GZIP:
  90. context->set_compression_algorithm(GRPC_COMPRESS_GZIP);
  91. break;
  92. case grpc::testing::DEFLATE:
  93. context->set_compression_algorithm(GRPC_COMPRESS_DEFLATE);
  94. break;
  95. }
  96. } else {
  97. context->set_compression_algorithm(GRPC_COMPRESS_NONE);
  98. }
  99. }
  100. class TestServiceImpl : public TestService::Service {
  101. public:
  102. Status EmptyCall(ServerContext* context, const grpc::testing::Empty* request,
  103. grpc::testing::Empty* response) {
  104. return Status::OK;
  105. }
  106. Status UnaryCall(ServerContext* context, const SimpleRequest* request,
  107. SimpleResponse* response) {
  108. SetResponseCompression(context, *request);
  109. if (request->has_response_size() && request->response_size() > 0) {
  110. if (!SetPayload(request->response_type(), request->response_size(),
  111. response->mutable_payload())) {
  112. return Status(grpc::StatusCode::INTERNAL, "Error creating payload.");
  113. }
  114. }
  115. return Status::OK;
  116. }
  117. Status StreamingOutputCall(
  118. ServerContext* context, const StreamingOutputCallRequest* request,
  119. ServerWriter<StreamingOutputCallResponse>* writer) {
  120. SetResponseCompression(context, *request);
  121. StreamingOutputCallResponse response;
  122. bool write_success = true;
  123. response.mutable_payload()->set_type(request->response_type());
  124. for (int i = 0; write_success && i < request->response_parameters_size();
  125. i++) {
  126. response.mutable_payload()->set_body(
  127. grpc::string(request->response_parameters(i).size(), '\0'));
  128. write_success = writer->Write(response);
  129. }
  130. if (write_success) {
  131. return Status::OK;
  132. } else {
  133. return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
  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. Status FullDuplexCall(
  150. ServerContext* context,
  151. ServerReaderWriter<StreamingOutputCallResponse,
  152. StreamingOutputCallRequest>* stream) {
  153. StreamingOutputCallRequest request;
  154. StreamingOutputCallResponse response;
  155. bool write_success = true;
  156. while (write_success && stream->Read(&request)) {
  157. SetResponseCompression(context, request);
  158. if (request.response_parameters_size() != 0) {
  159. response.mutable_payload()->set_type(request.payload().type());
  160. response.mutable_payload()->set_body(
  161. grpc::string(request.response_parameters(0).size(), '\0'));
  162. write_success = stream->Write(response);
  163. }
  164. }
  165. if (write_success) {
  166. return Status::OK;
  167. } else {
  168. return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
  169. }
  170. }
  171. Status HalfDuplexCall(
  172. ServerContext* context,
  173. ServerReaderWriter<StreamingOutputCallResponse,
  174. StreamingOutputCallRequest>* stream) {
  175. std::vector<StreamingOutputCallRequest> requests;
  176. StreamingOutputCallRequest request;
  177. while (stream->Read(&request)) {
  178. requests.push_back(request);
  179. }
  180. StreamingOutputCallResponse response;
  181. bool write_success = true;
  182. for (unsigned int i = 0; write_success && i < requests.size(); i++) {
  183. response.mutable_payload()->set_type(requests[i].payload().type());
  184. if (requests[i].response_parameters_size() == 0) {
  185. return Status(grpc::StatusCode::INTERNAL,
  186. "Request does not have response parameters.");
  187. }
  188. response.mutable_payload()->set_body(
  189. grpc::string(requests[i].response_parameters(0).size(), '\0'));
  190. write_success = stream->Write(response);
  191. }
  192. if (write_success) {
  193. return Status::OK;
  194. } else {
  195. return Status(grpc::StatusCode::INTERNAL, "Error writing response.");
  196. }
  197. }
  198. };
  199. void RunServer() {
  200. std::ostringstream server_address;
  201. server_address << "0.0.0.0:" << FLAGS_port;
  202. TestServiceImpl service;
  203. SimpleRequest request;
  204. SimpleResponse response;
  205. ServerBuilder builder;
  206. builder.RegisterService(&service);
  207. std::shared_ptr<ServerCredentials> creds =
  208. grpc::testing::CreateInteropServerCredentials();
  209. builder.AddListeningPort(server_address.str(), creds);
  210. std::unique_ptr<Server> server(builder.BuildAndStart());
  211. gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str());
  212. while (!got_sigint) {
  213. sleep(5);
  214. }
  215. }
  216. static void sigint_handler(int x) { got_sigint = true; }
  217. int main(int argc, char** argv) {
  218. grpc::testing::InitTest(&argc, &argv, true);
  219. signal(SIGINT, sigint_handler);
  220. GPR_ASSERT(FLAGS_port != 0);
  221. RunServer();
  222. return 0;
  223. }