xref: /aosp_15_r20/external/grpc-grpc/src/cpp/ext/chaotic_good.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2024 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "src/cpp/ext/chaotic_good.h"
16 
17 #include <memory>
18 
19 #include <grpc/grpc.h>
20 
21 #include "src/core/ext/transport/chaotic_good/client/chaotic_good_connector.h"
22 #include "src/core/ext/transport/chaotic_good/server/chaotic_good_server.h"
23 
24 namespace grpc {
25 
26 namespace {
27 
28 class ChaoticGoodInsecureChannelCredentialsImpl final
29     : public ChannelCredentials {
30  public:
ChaoticGoodInsecureChannelCredentialsImpl()31   ChaoticGoodInsecureChannelCredentialsImpl() : ChannelCredentials(nullptr) {}
32 
33  private:
CreateChannelWithInterceptors(const grpc::string & target,const grpc::ChannelArguments & args,std::vector<std::unique_ptr<experimental::ClientInterceptorFactoryInterface>> interceptor_creators)34   std::shared_ptr<Channel> CreateChannelWithInterceptors(
35       const grpc::string& target, const grpc::ChannelArguments& args,
36       std::vector<
37           std::unique_ptr<experimental::ClientInterceptorFactoryInterface>>
38           interceptor_creators) override {
39     grpc_channel_args channel_args;
40     args.SetChannelArgs(&channel_args);
41     auto channel = grpc::CreateChannelInternal(
42         "", grpc_chaotic_good_channel_create(target.c_str(), &channel_args),
43         std::move(interceptor_creators));
44     return channel;
45   }
46 };
47 
48 class ChaoticGoodInsecureServerCredentialsImpl final
49     : public ServerCredentials {
50  public:
ChaoticGoodInsecureServerCredentialsImpl()51   ChaoticGoodInsecureServerCredentialsImpl() : ServerCredentials(nullptr) {}
52 
AddPortToServer(const std::string & addr,grpc_server * server)53   int AddPortToServer(const std::string& addr, grpc_server* server) override {
54     return grpc_server_add_chaotic_good_port(server, addr.c_str());
55   }
56 };
57 
58 }  // namespace
59 
ChaoticGoodInsecureChannelCredentials()60 std::shared_ptr<ChannelCredentials> ChaoticGoodInsecureChannelCredentials() {
61   return std::make_shared<ChaoticGoodInsecureChannelCredentialsImpl>();
62 }
63 
ChaoticGoodInsecureServerCredentials()64 std::shared_ptr<ServerCredentials> ChaoticGoodInsecureServerCredentials() {
65   return std::make_shared<ChaoticGoodInsecureServerCredentialsImpl>();
66 }
67 
68 }  // namespace grpc
69