1 /* 2 * 3 * Copyright 2015 gRPC authors. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 */ 18 19 #ifndef GRPC_TEST_CPP_INTEROP_SERVER_HELPER_H 20 #define GRPC_TEST_CPP_INTEROP_SERVER_HELPER_H 21 22 #include <condition_variable> 23 #include <memory> 24 25 #include <grpc/compression.h> 26 #include <grpc/impl/codegen/atm.h> 27 28 #include <grpcpp/security/server_credentials.h> 29 #include <grpcpp/server.h> 30 #include <grpcpp/server_builder.h> 31 #include <grpcpp/server_context.h> 32 33 namespace grpc { 34 namespace testing { 35 36 std::shared_ptr<ServerCredentials> CreateInteropServerCredentials(); 37 38 class InteropServerContextInspector { 39 public: 40 InteropServerContextInspector(const ::grpc::ServerContext& context); 41 42 // Inspector methods, able to peek inside ServerContext, follow. 43 std::shared_ptr<const AuthContext> GetAuthContext() const; 44 bool IsCancelled() const; 45 grpc_compression_algorithm GetCallCompressionAlgorithm() const; 46 uint32_t GetEncodingsAcceptedByClient() const; 47 uint32_t GetMessageFlags() const; 48 49 private: 50 const ::grpc::ServerContext& context_; 51 }; 52 53 namespace interop { 54 55 extern gpr_atm g_got_sigint; 56 57 struct ServerStartedCondition { 58 std::mutex mutex; 59 std::condition_variable condition; 60 bool server_started = false; 61 }; 62 63 /// Run gRPC interop server using port FLAGS_port. 64 /// 65 /// \param creds The credentials associated with the server. 66 void RunServer(const std::shared_ptr<ServerCredentials>& creds); 67 68 /// Run gRPC interop server. 69 /// 70 /// \param creds The credentials associated with the server. 71 /// \param port Port to use for the server. 72 /// \param server_started_condition (optional) Struct holding mutex, condition 73 /// variable, and condition used to notify when the server has started. 74 void RunServer(const std::shared_ptr<ServerCredentials>& creds, int port, 75 ServerStartedCondition* server_started_condition); 76 77 /// Run gRPC interop server. 78 /// 79 /// \param creds The credentials associated with the server. 80 /// \param server_options List of options to set when building the server. 81 void RunServer( 82 const std::shared_ptr<ServerCredentials>& creds, 83 std::unique_ptr<std::vector<std::unique_ptr<ServerBuilderOption>>> 84 server_options); 85 86 /// Run gRPC interop server. 87 /// 88 /// \param creds The credentials associated with the server. 89 /// \param port Port to use for the server. 90 /// \param server_options List of options to set when building the server. 91 /// \param server_started_condition (optional) Struct holding mutex, condition 92 // variable, and condition used to notify when the server has started. 93 void RunServer( 94 const std::shared_ptr<ServerCredentials>& creds, const int port, 95 ServerStartedCondition* server_started_condition, 96 std::unique_ptr<std::vector<std::unique_ptr<grpc::ServerBuilderOption>>> 97 server_options); 98 99 } // namespace interop 100 } // namespace testing 101 } // namespace grpc 102 103 #endif // GRPC_TEST_CPP_INTEROP_SERVER_HELPER_H 104