blob: 91c7b35550a429636fb7e72982d2bae18b601e74 [file] [log] [blame]
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08001/*
2 *
Craig Tiller06059952015-02-18 08:34:56 -08003 * Copyright 2015, Google Inc.
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <grpc/grpc.h>
35
36#include <stdlib.h>
37#include <string.h>
38
Craig Tiller91624662015-06-25 16:31:02 -070039#include <grpc/support/alloc.h>
40
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080041#include "src/core/channel/channel_args.h"
42#include "src/core/channel/client_channel.h"
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070043#include "src/core/channel/compress_filter.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080044#include "src/core/channel/http_client_filter.h"
Craig Tillerf5f17122015-06-25 08:47:26 -070045#include "src/core/client_config/resolver_registry.h"
ctiller18b49ab2014-12-09 14:39:16 -080046#include "src/core/iomgr/tcp_client.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080047#include "src/core/surface/channel.h"
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080048#include "src/core/transport/chttp2_transport.h"
David Garcia Quintas55b4ea12015-06-16 14:27:32 -070049
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080050typedef struct {
Craig Tiller91624662015-06-25 16:31:02 -070051 grpc_connector base;
52 gpr_refcount refs;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080053
Craig Tiller5f84c842015-06-26 16:08:21 -070054 grpc_iomgr_closure *notify;
Craig Tiller04c5d4b2015-06-26 17:21:41 -070055 grpc_connect_in_args args;
56 grpc_connect_out_args *result;
Craig Tiller91624662015-06-25 16:31:02 -070057} connector;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080058
Craig Tillerf7afa1f2015-06-26 09:02:20 -070059static void connector_ref(grpc_connector *con) {
60 connector *c = (connector *)con;
61 gpr_ref(&c->refs);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080062}
63
Craig Tillerf7afa1f2015-06-26 09:02:20 -070064static void connector_unref(grpc_connector *con) {
65 connector *c = (connector *)con;
66 if (gpr_unref(&c->refs)) {
67 gpr_free(c);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080068 }
Craig Tiller91624662015-06-25 16:31:02 -070069}
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -080070
Craig Tiller5f84c842015-06-26 16:08:21 -070071static void connected(void *arg, grpc_endpoint *tcp) {
72 connector *c = arg;
73 grpc_iomgr_closure *notify;
74 if (tcp != NULL) {
Craig Tiller079a11b2015-06-30 10:07:15 -070075 c->result->transport = grpc_create_chttp2_transport(
Craig Tiller825b21c2015-07-06 08:35:02 -070076 c->args.channel_args, tcp, c->args.metadata_context, 1);
77 grpc_chttp2_transport_start_reading(c->result->transport, NULL, 0);
Craig Tiller87cc0842015-06-30 08:15:55 -070078 GPR_ASSERT(c->result->transport);
Craig Tiller079a11b2015-06-30 10:07:15 -070079 c->result->filters = gpr_malloc(sizeof(grpc_channel_filter *));
Craig Tiller5945ee12015-06-27 10:36:09 -070080 c->result->filters[0] = &grpc_http_client_filter;
81 c->result->num_filters = 1;
Craig Tiller43a2b172015-02-18 17:17:51 -080082 } else {
Craig Tiller5945ee12015-06-27 10:36:09 -070083 memset(c->result, 0, sizeof(*c->result));
Craig Tiller5f84c842015-06-26 16:08:21 -070084 }
85 notify = c->notify;
86 c->notify = NULL;
87 grpc_iomgr_add_callback(notify);
88}
89
Craig Tiller079a11b2015-06-30 10:07:15 -070090static void connector_connect(grpc_connector *con,
91 const grpc_connect_in_args *args,
92 grpc_connect_out_args *result,
93 grpc_iomgr_closure *notify) {
Craig Tiller5f84c842015-06-26 16:08:21 -070094 connector *c = (connector *)con;
95 GPR_ASSERT(c->notify == NULL);
Craig Tillerff54c922015-06-26 16:57:20 -070096 GPR_ASSERT(notify->cb);
Craig Tiller5f84c842015-06-26 16:08:21 -070097 c->notify = notify;
Craig Tiller04c5d4b2015-06-26 17:21:41 -070098 c->args = *args;
99 c->result = result;
Craig Tiller079a11b2015-06-30 10:07:15 -0700100 grpc_tcp_client_connect(connected, c, args->interested_parties, args->addr,
101 args->addr_len, args->deadline);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700102}
103
Craig Tiller079a11b2015-06-30 10:07:15 -0700104static const grpc_connector_vtable connector_vtable = {
105 connector_ref, connector_unref, connector_connect};
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700106
Craig Tiller5f84c842015-06-26 16:08:21 -0700107typedef struct {
108 grpc_subchannel_factory base;
109 gpr_refcount refs;
110 grpc_mdctx *mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700111 grpc_channel_args *merge_args;
Craig Tiller5f84c842015-06-26 16:08:21 -0700112} subchannel_factory;
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700113
Craig Tiller5f84c842015-06-26 16:08:21 -0700114static void subchannel_factory_ref(grpc_subchannel_factory *scf) {
115 subchannel_factory *f = (subchannel_factory *)scf;
116 gpr_ref(&f->refs);
117}
118
119static void subchannel_factory_unref(grpc_subchannel_factory *scf) {
120 subchannel_factory *f = (subchannel_factory *)scf;
121 if (gpr_unref(&f->refs)) {
Craig Tiller6806e1e2015-06-30 10:28:29 -0700122 grpc_channel_args_destroy(f->merge_args);
Craig Tiller5f84c842015-06-26 16:08:21 -0700123 grpc_mdctx_unref(f->mdctx);
124 gpr_free(f);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800125 }
126}
127
Craig Tiller079a11b2015-06-30 10:07:15 -0700128static grpc_subchannel *subchannel_factory_create_subchannel(
129 grpc_subchannel_factory *scf, grpc_subchannel_args *args) {
Craig Tiller5f84c842015-06-26 16:08:21 -0700130 subchannel_factory *f = (subchannel_factory *)scf;
Craig Tiller91624662015-06-25 16:31:02 -0700131 connector *c = gpr_malloc(sizeof(*c));
Craig Tillerd9a50882015-06-29 15:57:36 -0700132 grpc_channel_args *final_args =
133 grpc_channel_args_merge(args->args, f->merge_args);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700134 grpc_subchannel *s;
Craig Tiller5f84c842015-06-26 16:08:21 -0700135 memset(c, 0, sizeof(*c));
Craig Tiller91624662015-06-25 16:31:02 -0700136 c->base.vtable = &connector_vtable;
137 gpr_ref_init(&c->refs, 1);
Craig Tiller5f84c842015-06-26 16:08:21 -0700138 args->mdctx = f->mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700139 args->args = final_args;
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700140 s = grpc_subchannel_create(&c->base, args);
141 grpc_connector_unref(&c->base);
Craig Tillerd9a50882015-06-29 15:57:36 -0700142 grpc_channel_args_destroy(final_args);
Craig Tillerf7afa1f2015-06-26 09:02:20 -0700143 return s;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800144}
145
Craig Tiller079a11b2015-06-30 10:07:15 -0700146static const grpc_subchannel_factory_vtable subchannel_factory_vtable = {
147 subchannel_factory_ref, subchannel_factory_unref,
148 subchannel_factory_create_subchannel};
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800149
150/* Create a client channel:
151 Asynchronously: - resolve target
152 - connect to it (trying alternatives as presented)
153 - perform handshakes */
154grpc_channel *grpc_channel_create(const char *target,
155 const grpc_channel_args *args) {
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800156 grpc_channel *channel = NULL;
157#define MAX_FILTERS 3
158 const grpc_channel_filter *filters[MAX_FILTERS];
Craig Tillerf5f17122015-06-25 08:47:26 -0700159 grpc_resolver *resolver;
Craig Tiller5f84c842015-06-26 16:08:21 -0700160 subchannel_factory *f;
161 grpc_mdctx *mdctx = grpc_mdctx_create();
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800162 int n = 0;
Alistair Veitch9686dab2015-05-26 14:26:47 -0700163 /* TODO(census)
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800164 if (grpc_channel_args_is_census_enabled(args)) {
165 filters[n++] = &grpc_client_census_filter;
Alistair Veitch9686dab2015-05-26 14:26:47 -0700166 } */
David Garcia Quintas331d2da2015-06-24 16:17:47 -0700167 filters[n++] = &grpc_compress_filter;
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800168 filters[n++] = &grpc_client_channel_filter;
169 GPR_ASSERT(n <= MAX_FILTERS);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800170
Craig Tiller5f84c842015-06-26 16:08:21 -0700171 f = gpr_malloc(sizeof(*f));
172 f->base.vtable = &subchannel_factory_vtable;
173 gpr_ref_init(&f->refs, 1);
Craig Tiller98465032015-06-29 14:36:42 -0700174 grpc_mdctx_ref(mdctx);
Craig Tiller5f84c842015-06-26 16:08:21 -0700175 f->mdctx = mdctx;
Craig Tillerd9a50882015-06-29 15:57:36 -0700176 f->merge_args = grpc_channel_args_copy(args);
Craig Tiller5f84c842015-06-26 16:08:21 -0700177 resolver = grpc_resolver_create(target, &f->base);
Craig Tillerf5f17122015-06-25 08:47:26 -0700178 if (!resolver) {
179 return NULL;
180 }
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800181
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800182 channel = grpc_channel_create_from_filters(filters, n, args, mdctx, 1);
Craig Tiller079a11b2015-06-30 10:07:15 -0700183 grpc_client_channel_set_resolver(grpc_channel_get_channel_stack(channel),
184 resolver);
Craig Tiller98465032015-06-29 14:36:42 -0700185 GRPC_RESOLVER_UNREF(resolver, "create");
186 grpc_subchannel_factory_unref(&f->base);
Nicolas Nobleb7ebd3b2014-11-26 16:33:03 -0800187
188 return channel;
Craig Tiller190d3602015-02-18 09:23:38 -0800189}