Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <grpc/grpc.h> |
| 20 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 21 | #include <string.h> |
| 22 | |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 23 | #include <grpc/support/alloc.h> |
Mark D. Roth | 53bd693 | 2016-12-01 08:00:38 -0800 | [diff] [blame] | 24 | #include <grpc/support/string_util.h> |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 25 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 26 | #include "src/core/ext/filters/client_channel/client_channel.h" |
| 27 | #include "src/core/ext/filters/client_channel/resolver_registry.h" |
Mark D. Roth | 8686cab | 2016-11-17 13:12:17 -0800 | [diff] [blame] | 28 | #include "src/core/ext/transport/chttp2/client/chttp2_connector.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 29 | #include "src/core/lib/channel/channel_args.h" |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 30 | #include "src/core/lib/surface/api_trace.h" |
| 31 | #include "src/core/lib/surface/channel.h" |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 32 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 33 | static void client_channel_factory_ref( |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 34 | grpc_client_channel_factory *cc_factory) {} |
Craig Tiller | 5f84c84 | 2015-06-26 16:08:21 -0700 | [diff] [blame] | 35 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 36 | static void client_channel_factory_unref( |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 37 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory) {} |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 38 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 39 | static grpc_subchannel *client_channel_factory_create_subchannel( |
| 40 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory, |
Mark D. Roth | e3a2100 | 2016-10-24 13:29:05 -0700 | [diff] [blame] | 41 | const grpc_subchannel_args *args) { |
Mark D. Roth | 1f0f23c | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 42 | grpc_connector *connector = grpc_chttp2_connector_create(); |
Mark D. Roth | 8686cab | 2016-11-17 13:12:17 -0800 | [diff] [blame] | 43 | grpc_subchannel *s = grpc_subchannel_create(exec_ctx, connector, args); |
| 44 | grpc_connector_unref(exec_ctx, connector); |
Craig Tiller | f7afa1f | 2015-06-26 09:02:20 -0700 | [diff] [blame] | 45 | return s; |
Craig Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 46 | } |
| 47 | |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 48 | static grpc_channel *client_channel_factory_create_channel( |
| 49 | grpc_exec_ctx *exec_ctx, grpc_client_channel_factory *cc_factory, |
David Garcia Quintas | 0b868c7 | 2016-04-01 14:28:22 -0700 | [diff] [blame] | 50 | const char *target, grpc_client_channel_type type, |
Mark D. Roth | e3a2100 | 2016-10-24 13:29:05 -0700 | [diff] [blame] | 51 | const grpc_channel_args *args) { |
Mark D. Roth | 30d3a68 | 2017-02-01 09:15:46 -0800 | [diff] [blame] | 52 | if (target == NULL) { |
| 53 | gpr_log(GPR_ERROR, "cannot create channel with NULL target name"); |
| 54 | return NULL; |
| 55 | } |
Mark D. Roth | f75d269 | 2016-12-12 08:41:42 -0800 | [diff] [blame] | 56 | // Add channel arg containing the server URI. |
Mark D. Roth | 8d5e60b | 2017-06-09 09:08:23 -0700 | [diff] [blame] | 57 | grpc_arg arg = grpc_channel_arg_string_create( |
Yash Tibrewal | 9eb8672 | 2017-09-17 23:43:30 -0700 | [diff] [blame] | 58 | (char *)GRPC_ARG_SERVER_URI, |
Mark D. Roth | 8d5e60b | 2017-06-09 09:08:23 -0700 | [diff] [blame] | 59 | grpc_resolver_factory_add_default_prefix_if_needed(exec_ctx, target)); |
David Garcia Quintas | bcd5f12 | 2017-02-22 14:32:56 -0800 | [diff] [blame] | 60 | 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. Roth | e453404 | 2017-02-01 07:39:08 -0800 | [diff] [blame] | 63 | gpr_free(arg.value.string); |
Mark D. Roth | f75d269 | 2016-12-12 08:41:42 -0800 | [diff] [blame] | 64 | grpc_channel *channel = grpc_channel_create(exec_ctx, target, new_args, |
| 65 | GRPC_CLIENT_CHANNEL, NULL); |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 66 | grpc_channel_args_destroy(exec_ctx, new_args); |
Mark D. Roth | f75d269 | 2016-12-12 08:41:42 -0800 | [diff] [blame] | 67 | return channel; |
David Garcia Quintas | fcf7ad6 | 2016-03-29 21:55:34 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static 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 Tiller | 9162466 | 2015-06-25 16:31:02 -0700 | [diff] [blame] | 74 | |
Mark D. Roth | 98abfd3 | 2016-10-21 08:10:51 -0700 | [diff] [blame] | 75 | static grpc_client_channel_factory client_channel_factory = { |
| 76 | &client_channel_factory_vtable}; |
| 77 | |
Nicolas Noble | b7ebd3b | 2014-11-26 16:33:03 -0800 | [diff] [blame] | 78 | /* Create a client channel: |
| 79 | Asynchronously: - resolve target |
| 80 | - connect to it (trying alternatives as presented) |
| 81 | - perform handshakes */ |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 82 | grpc_channel *grpc_insecure_channel_create(const char *target, |
| 83 | const grpc_channel_args *args, |
| 84 | void *reserved) { |
Craig Tiller | f5768a6 | 2015-09-22 10:54:34 -0700 | [diff] [blame] | 85 | grpc_exec_ctx exec_ctx = GRPC_EXEC_CTX_INIT; |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 86 | GRPC_API_TRACE( |
Jan Tattermusch | 8683911 | 2017-05-10 15:22:13 +0200 | [diff] [blame] | 87 | "grpc_insecure_channel_create(target=%s, args=%p, reserved=%p)", 3, |
Masood Malekghassemi | 76c3d74 | 2015-08-19 18:22:53 -0700 | [diff] [blame] | 88 | (target, args, reserved)); |
Mark D. Roth | f77a8ff | 2016-11-17 11:39:46 -0800 | [diff] [blame] | 89 | GPR_ASSERT(reserved == NULL); |
Mark D. Roth | f75d269 | 2016-12-12 08:41:42 -0800 | [diff] [blame] | 90 | // Add channel arg containing the client channel factory. |
Mark D. Roth | 1f0f23c | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 91 | grpc_arg arg = |
| 92 | grpc_client_channel_factory_create_channel_arg(&client_channel_factory); |
Mark D. Roth | f75d269 | 2016-12-12 08:41:42 -0800 | [diff] [blame] | 93 | grpc_channel_args *new_args = grpc_channel_args_copy_and_add(args, &arg, 1); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 94 | // Create channel. |
David Garcia Quintas | 0b868c7 | 2016-04-01 14:28:22 -0700 | [diff] [blame] | 95 | grpc_channel *channel = client_channel_factory_create_channel( |
Mark D. Roth | 1f0f23c | 2017-01-06 13:07:19 -0800 | [diff] [blame] | 96 | &exec_ctx, &client_channel_factory, target, |
| 97 | GRPC_CLIENT_CHANNEL_TYPE_REGULAR, new_args); |
Mark D. Roth | 21d4b2d | 2016-11-18 09:53:41 -0800 | [diff] [blame] | 98 | // Clean up. |
Craig Tiller | 4cc1c35 | 2016-12-27 08:48:01 -0800 | [diff] [blame] | 99 | grpc_channel_args_destroy(&exec_ctx, new_args); |
Craig Tiller | a82950e | 2015-09-22 12:33:20 -0700 | [diff] [blame] | 100 | grpc_exec_ctx_finish(&exec_ctx); |
Craig Tiller | fc98f92 | 2016-04-13 08:45:06 -0700 | [diff] [blame] | 101 | return channel != NULL ? channel : grpc_lame_client_channel_create( |
| 102 | target, GRPC_STATUS_INTERNAL, |
| 103 | "Failed to create client channel"); |
Craig Tiller | 190d360 | 2015-02-18 09:23:38 -0800 | [diff] [blame] | 104 | } |