58f274264a7773a1eb134233975533dab1e99fd2
[platform/upstream/grpc.git] / 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   RefCountedPtr<Subchannel> CreateSubchannel(
41       const grpc_channel_args* args) override {
42     grpc_channel_args* new_args =
43         grpc_default_authority_add_if_not_present(args);
44     RefCountedPtr<Subchannel> s =
45         Subchannel::Create(MakeOrphanable<Chttp2Connector>(), new_args);
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                             grpc_error_handle* error) {
55   if (target == nullptr) {
56     gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
57     if (error != nullptr) {
58       *error = GRPC_ERROR_CREATE_FROM_STATIC_STRING("channel target is NULL");
59     }
60     return nullptr;
61   }
62   // Add channel arg containing the server URI.
63   grpc_core::UniquePtr<char> canonical_target =
64       ResolverRegistry::AddDefaultPrefixIfNeeded(target);
65   grpc_arg arg = grpc_channel_arg_string_create(
66       const_cast<char*>(GRPC_ARG_SERVER_URI), canonical_target.get());
67   const char* to_remove[] = {GRPC_ARG_SERVER_URI};
68   grpc_channel_args* new_args =
69       grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
70   grpc_channel* channel = grpc_channel_create(
71       target, new_args, GRPC_CLIENT_CHANNEL, nullptr, nullptr, error);
72   grpc_channel_args_destroy(new_args);
73   return channel;
74 }
75
76 }  // namespace
77
78 }  // namespace grpc_core
79
80 namespace {
81
82 grpc_core::Chttp2InsecureClientChannelFactory* g_factory;
83 gpr_once g_factory_once = GPR_ONCE_INIT;
84
85 void FactoryInit() {
86   g_factory = new grpc_core::Chttp2InsecureClientChannelFactory();
87 }
88
89 }  // namespace
90
91 /* Create a client channel:
92    Asynchronously: - resolve target
93                    - connect to it (trying alternatives as presented)
94                    - perform handshakes */
95 grpc_channel* grpc_insecure_channel_create(const char* target,
96                                            const grpc_channel_args* args,
97                                            void* reserved) {
98   grpc_core::ExecCtx exec_ctx;
99   GRPC_API_TRACE(
100       "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3,
101       (target, args, reserved));
102   GPR_ASSERT(reserved == nullptr);
103   // Add channel arg containing the client channel factory.
104   gpr_once_init(&g_factory_once, FactoryInit);
105   grpc_arg arg = grpc_core::ClientChannelFactory::CreateChannelArg(g_factory);
106   const char* arg_to_remove = arg.key;
107   grpc_channel_args* new_args = grpc_channel_args_copy_and_add_and_remove(
108       args, &arg_to_remove, 1, &arg, 1);
109   grpc_error_handle error = GRPC_ERROR_NONE;
110   // Create channel.
111   grpc_channel* channel = grpc_core::CreateChannel(target, new_args, &error);
112   // Clean up.
113   grpc_channel_args_destroy(new_args);
114   if (channel == nullptr) {
115     intptr_t integer;
116     grpc_status_code status = GRPC_STATUS_INTERNAL;
117     if (grpc_error_get_int(error, GRPC_ERROR_INT_GRPC_STATUS, &integer)) {
118       status = static_cast<grpc_status_code>(integer);
119     }
120     GRPC_ERROR_UNREF(error);
121     channel = grpc_lame_client_channel_create(
122         target, status, "Failed to create client channel");
123   }
124   return channel;
125 }