blob: 6410a6043db7b7b74c81ac09a8489ab011f5f9a4 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080016 *
17 */
18
19#include <grpc/grpc.h>
20
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080021#include <string.h>
22
Craig Tiller618e67d2016-10-26 21:08:10 -070023#include <grpc/support/alloc.h>
Mark D. Roth53bd6932016-12-01 08:00:38 -080024#include <grpc/support/string_util.h>
Craig Tiller91624662015-06-25 16:31:02 -070025
Craig Tiller9eb0fde2017-03-31 16:59:30 -070026#include "src/core/ext/filters/client_channel/client_channel.h"
27#include "src/core/ext/filters/client_channel/resolver_registry.h"
Mark D. Roth8686cab2016-11-17 13:12:17 -080028#include "src/core/ext/transport/chttp2/client/chttp2_connector.h"
Craig Tiller9533d042016-03-25 17:11:06 -070029#include "src/core/lib/channel/channel_args.h"
Craig Tiller9533d042016-03-25 17:11:06 -070030#include "src/core/lib/surface/api_trace.h"
31#include "src/core/lib/surface/channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080032
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070033static void client_channel_factory_ref(
Mark D. Roth98abfd32016-10-21 08:10:51 -070034 grpc_client_channel_factory *cc_factory) {}
Craig Tiller5f84c842015-06-26 16:08:21 -070035
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070036static void client_channel_factory_unref(
Mark D. Roth98abfd32016-10-21 08:10:51 -070037 grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory) {}
Craig Tillerf7afa1f2015-06-26 09:02:20 -070038
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070039static grpc_subchannel *client_channel_factory_create_subchannel(
40 grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory,
Mark D. Rothe3a21002016-10-24 13:29:05 -070041 const grpc_subchannel_args *args) {
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080042 grpc_connector *connector = grpc_chttp2_connector_create();
Mark D. Roth8686cab2016-11-17 13:12:17 -080043 grpc_subchannel *s = grpc_subchannel_create(exec_ctx, connector, args);
44 grpc_connector_unref(exec_ctx, connector);
Craig Tillerf7afa1f2015-06-26 09:02:20 -070045 return s;
Craig Tiller91624662015-06-25 16:31:02 -070046}
47
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070048static grpc_channel *client_channel_factory_create_channel(
49 grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory,
David Garcia Quintas0b868c72016-04-01 14:28:22 -070050 const char *target, grpc_client_channel_type type,
Mark D. Rothe3a21002016-10-24 13:29:05 -070051 const grpc_channel_args *args) {
Mark D. Roth30d3a682017-02-01 09:15:46 -080052 if (target == NULL) {
53 gpr_log(GPR_ERROR, "cannot create channel with NULL target name");
54 return NULL;
55 }
Mark D. Rothf75d2692016-12-12 08:41:42 -080056 // Add channel arg containing the server URI.
Mark D. Roth8d5e60b2017-06-09 09:08:23 -070057 grpc_arg arg = grpc_channel_arg_string_create(
Yash Tibrewal9eb86722017-09-17 23:43:30 -070058 (char *)GRPC_ARG_SERVER_URI,
Mark D. Roth8d5e60b2017-06-09 09:08:23 -070059 grpc_resolver_factory_add_default_prefix_if_needed(exec_ctx, target));
David Garcia Quintasbcd5f122017-02-22 14:32:56 -080060 const char *to_remove[] = {GRPC_ARG_SERVER_URI};
61 grpc_channel_args *new_args =
62 grpc_channel_args_copy_and_add_and_remove(args, to_remove, 1, &arg, 1);
Mark D. Rothe4534042017-02-01 07:39:08 -080063 gpr_free(arg.value.string);
Mark D. Rothf75d2692016-12-12 08:41:42 -080064 grpc_channel *channel = grpc_channel_create(exec_ctx, target, new_args,
65 GRPC_CLIENT_CHANNEL, NULL);
Craig Tiller4cc1c352016-12-27 08:48:01 -080066 grpc_channel_args_destroy(exec_ctx, new_args);
Mark D. Rothf75d2692016-12-12 08:41:42 -080067 return channel;
David Garcia Quintasfcf7ad62016-03-29 21:55:34 -070068}
69
70static const grpc_client_channel_factory_vtable client_channel_factory_vtable =
71 {client_channel_factory_ref, client_channel_factory_unref,
72 client_channel_factory_create_subchannel,
73 client_channel_factory_create_channel};
Craig Tiller91624662015-06-25 16:31:02 -070074
Mark D. Roth98abfd32016-10-21 08:10:51 -070075static grpc_client_channel_factory client_channel_factory = {
76 &client_channel_factory_vtable};
77
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080078/* Create a client channel:
79 Asynchronously: - resolve target
80 - connect to it (trying alternatives as presented)
81 - perform handshakes */
Craig Tillera82950e2015-09-22 12:33:20 -070082grpc_channel *grpc_insecure_channel_create(const char *target,
83 const grpc_channel_args *args,
84 void *reserved) {
Craig Tillerf5768a62015-09-22 10:54:34 -070085 grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT;
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070086 GRPC_API_TRACE(
Jan Tattermusch86839112017-05-10 15:22:13 +020087 "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3,
Masood Malekghassemi76c3d742015-08-19 18:22:53 -070088 (target, args, reserved));
Mark D. Rothf77a8ff2016-11-17 11:39:46 -080089 GPR_ASSERT(reserved == NULL);
Mark D. Rothf75d2692016-12-12 08:41:42 -080090 // Add channel arg containing the client channel factory.
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080091 grpc_arg arg =
92 grpc_client_channel_factory_create_channel_arg(&client_channel_factory);
Mark D. Rothf75d2692016-12-12 08:41:42 -080093 grpc_channel_args *new_args = grpc_channel_args_copy_and_add(args, &arg, 1);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -080094 // Create channel.
David Garcia Quintas0b868c72016-04-01 14:28:22 -070095 grpc_channel *channel = client_channel_factory_create_channel(
Mark D. Roth1f0f23c2017-01-06 13:07:19 -080096 &exec_ctx, &client_channel_factory, target,
97 GRPC_CLIENT_CHANNEL_TYPE_REGULAR, new_args);
Mark D. Roth21d4b2d2016-11-18 09:53:41 -080098 // Clean up.
Craig Tiller4cc1c352016-12-27 08:48:01 -080099 grpc_channel_args_destroy(&exec_ctx, new_args);
Craig Tillera82950e2015-09-22 12:33:20 -0700100 grpc_exec_ctx_finish(&exec_ctx);
Craig Tillerfc98f922016-04-13 08:45:06 -0700101 return channel != NULL ? channel : grpc_lame_client_channel_create(
102 target, GRPC_STATUS_INTERNAL,
103 "Failed to create client channel");
Craig Tiller190d3602015-02-18 09:23:38 -0800104}