Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / ext / transport / chttp2 / client / insecure / channel_create.cc
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 #include <grpc/support/port_platform.h>
20
21 #include <grpc/grpc.h>
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/filters/client_channel/client_channel.h"
29 #include "src/core/ext/filters/client_channel/resolver_registry.h"
30 #include "src/core/ext/transport/chttp2/client/authority.h"
31 #include "src/core/ext/transport/chttp2/client/chttp2_connector.h"
32 #include "src/core/lib/channel/channel_args.h"
33 #include "src/core/lib/surface/api_trace.h"
34 #include "src/core/lib/surface/channel.h"
35
36 namespace grpc_core {
37
38 class Chttp2InsecureClientChannelFactory : public ClientChannelFactory {
39  public:
40   Subchannel* CreateSubchannel(const grpc_channel_args* args) override {
41     grpc_channel_args* new_args =
42         grpc_default_authority_add_if_not_present(args);
43     grpc_connector* connector = grpc_chttp2_connector_create();
44     Subchannel* s = Subchannel::Create(connector, new_args);
45     grpc_connector_unref(connector);
46     grpc_channel_args_destroy(new_args);
47     return s;
48   }
49 };
50
51 namespace {
52
53 grpc_channel* CreateChannel(const char* target, const grpc_channel_args* args) {
54   if (target == nullptr) {
55     gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
56     return nullptr;
57   }
58   // Add channel arg containing the server URI.
59   UniquePtr<char> canonical_target =
60       ResolverRegistry::AddDefaultPrefixIfNeeded(target);
61   grpc_arg arg = grpc_channel_arg_string_create(
62       const_cast<char*>(GRPC_ARG_SERVER_URI), canonical_target.get());
63   const char* to_remove[] = {GRPC_ARG_SERVER_URI};
64   grpc_channel_args* new_args =
65       grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
66   grpc_channel* channel =
67       grpc_channel_create(target, new_args, GRPC_CLIENT_CHANNEL, nullptr);
68   grpc_channel_args_destroy(new_args);
69   return channel;
70 }
71
72 }  // namespace
73
74 }  // namespace grpc_core
75
76 namespace {
77
78 grpc_core::Chttp2InsecureClientChannelFactory* g_factory;
79 gpr_once g_factory_once = GPR_ONCE_INIT;
80
81 void FactoryInit() {
82   g_factory = grpc_core::New<grpc_core::Chttp2InsecureClientChannelFactory>();
83 }
84
85 }  // namespace
86
87 /* Create a client channel:
88    Asynchronously: - resolve target
89                    - connect to it (trying alternatives as presented)
90                    - perform handshakes */
91 grpc_channel* grpc_insecure_channel_create(const char* target,
92                                            const grpc_channel_args* args,
93                                            void* reserved) {
94   grpc_core::ExecCtx exec_ctx;
95   GRPC_API_TRACE(
96       "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3,
97       (target, args, reserved));
98   GPR_ASSERT(reserved == nullptr);
99   // Add channel arg containing the client channel factory.
100   gpr_once_init(&g_factory_once, FactoryInit);
101   grpc_arg arg = grpc_core::ClientChannelFactory::CreateChannelArg(g_factory);
102   grpc_channel_args* new_args = grpc_channel_args_copy_and_add(args, &arg, 1);
103   // Create channel.
104   grpc_channel* channel = grpc_core::CreateChannel(target, new_args);
105   // Clean up.
106   grpc_channel_args_destroy(new_args);
107   return channel != nullptr ? channel
108                             : grpc_lame_client_channel_create(
109                                   target, GRPC_STATUS_INTERNAL,
110                                   "Failed to create client channel");
111 }