aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core
diff options
context:
space:
mode:
authorGravatar Yuchen Zeng <zyc@google.com>2016-08-08 14:16:34 -0700
committerGravatar Yuchen Zeng <zyc@google.com>2016-08-08 14:18:51 -0700
commit85750b08f26416615e005269d23eea0c71c3dd2c (patch)
tree59d5200a5a002d7bcd5dbdb825cc6af15c51ad6b /src/core
parentf0cb609e211e8b16120c70ba92dcfa158dbd6872 (diff)
Separate event driver from ares wrapper
Diffstat (limited to 'src/core')
-rw-r--r--src/core/ext/client_config/client_channel.c19
-rw-r--r--src/core/ext/resolver/dns/c_ares/dns_resolver.c9
-rw-r--r--src/core/ext/resolver/dns/c_ares/grpc_ares_ev_dirver_windows.c (renamed from src/core/ext/c-ares/wrapper.c)11
-rw-r--r--src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h43
-rw-r--r--src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c176
-rw-r--r--src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.c288
-rw-r--r--src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h6
7 files changed, 368 insertions, 184 deletions
diff --git a/src/core/ext/client_config/client_channel.c b/src/core/ext/client_config/client_channel.c
index 1fd8fd9bb3..2977639db7 100644
--- a/src/core/ext/client_config/client_channel.c
+++ b/src/core/ext/client_config/client_channel.c
@@ -36,9 +36,6 @@
#include <stdio.h>
#include <string.h>
-#include "src/core/lib/iomgr/ev_posix.h"
-#include "src/core/lib/iomgr/sockaddr.h"
-
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <grpc/support/sync.h>
@@ -85,22 +82,6 @@ typedef struct client_channel_channel_data {
grpc_pollset_set *interested_parties;
} channel_data;
-struct grpc_pollset_set {
- gpr_mu mu;
-
- size_t pollset_count;
- size_t pollset_capacity;
- grpc_pollset **pollsets;
-
- size_t pollset_set_count;
- size_t pollset_set_capacity;
- struct grpc_pollset_set **pollset_sets;
-
- size_t fd_count;
- size_t fd_capacity;
- grpc_fd **fds;
-};
-
/** We create one watcher for each new lb_policy that is returned from a
resolver,
to watch for state changes from the lb_policy. When a state change is seen,
diff --git a/src/core/ext/resolver/dns/c_ares/dns_resolver.c b/src/core/ext/resolver/dns/c_ares/dns_resolver.c
index 29caac1d20..e741f19b6f 100644
--- a/src/core/ext/resolver/dns/c_ares/dns_resolver.c
+++ b/src/core/ext/resolver/dns/c_ares/dns_resolver.c
@@ -271,6 +271,7 @@ static void dns_maybe_finish_next_locked(grpc_exec_ctx *exec_ctx,
static void dns_destroy(grpc_exec_ctx *exec_ctx, grpc_resolver *gr) {
dns_resolver *r = (dns_resolver *)gr;
gpr_mu_destroy(&r->mu);
+ grpc_ares_cleanup();
if (r->resolved_config) {
grpc_client_config_unref(exec_ctx, r->resolved_config);
}
@@ -285,6 +286,7 @@ static grpc_resolver *dns_create(grpc_resolver_args *args,
const char *default_port,
const char *lb_policy_name) {
dns_resolver *r;
+ grpc_error *error;
const char *path = args->uri->path;
if (0 != strcmp(args->uri->authority, "")) {
@@ -292,6 +294,12 @@ static grpc_resolver *dns_create(grpc_resolver_args *args,
return NULL;
}
+ error = grpc_ares_init();
+ if (error != GRPC_ERROR_NONE) {
+ GRPC_LOG_IF_ERROR("fd_orphan", error);
+ return NULL;
+ }
+
if (path[0] == '/') ++path;
r = gpr_malloc(sizeof(dns_resolver));
@@ -306,7 +314,6 @@ static grpc_resolver *dns_create(grpc_resolver_args *args,
BACKOFF_MIN_SECONDS * 1000, BACKOFF_MAX_SECONDS * 1000);
grpc_client_channel_factory_ref(r->client_channel_factory);
r->lb_policy_name = gpr_strdup(lb_policy_name);
- grpc_ares_init();
return &r->base;
}
diff --git a/src/core/ext/c-ares/wrapper.c b/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_dirver_windows.c
index 720163d23c..cd8dfbc326 100644
--- a/src/core/ext/c-ares/wrapper.c
+++ b/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_dirver_windows.c
@@ -31,13 +31,4 @@
*
*/
-#include <grpc/support/port_platform.h>
-#include <ares.h>
-
-int cares(void) {
- ares_channel channelptr;
-
- ares_init(&channelptr);
- ares_destroy(channelptr);
- return 0;
-}
+#include "src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h"
diff --git a/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h b/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h
new file mode 100644
index 0000000000..cac8a67462
--- /dev/null
+++ b/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h
@@ -0,0 +1,43 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <ares.h>
+
+#include "src/core/lib/iomgr/exec_ctx.h"
+#include "src/core/lib/iomgr/pollset_set.h"
+
+typedef struct grpc_ares_ev_driver grpc_ares_ev_driver;
+
+void grpc_ares_notify_on_event(grpc_exec_ctx *exec_ctx, grpc_ares_ev_driver *ev_driver);
+
+grpc_ares_ev_driver *grpc_ares_ev_driver_create(ares_channel *channel, grpc_pollset_set *pollset_set);
diff --git a/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c b/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c
new file mode 100644
index 0000000000..ee1c0e135a
--- /dev/null
+++ b/src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver_posix.c
@@ -0,0 +1,176 @@
+/*
+ *
+ * Copyright 2016, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+#include <grpc/support/port_platform.h>
+#ifdef GPR_POSIX_SOCKET
+
+#include "src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h"
+
+
+#include "src/core/lib/iomgr/ev_posix.h"
+#include "src/core/lib/iomgr/sockaddr.h"
+
+#include <ares.h>
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+#include <grpc/support/string_util.h>
+#include <grpc/support/time.h>
+#include <grpc/support/useful.h>
+#include "src/core/lib/iomgr/iomgr_internal.h"
+#include "src/core/lib/iomgr/sockaddr_utils.h"
+#include "src/core/lib/iomgr/unix_sockets_posix.h"
+#include "src/core/lib/support/block_annotate.h"
+#include "src/core/lib/support/string.h"
+
+typedef struct fd_pair {
+ grpc_fd *grpc_fd;
+ int fd;
+ struct fd_pair *next;
+} fd_pair;
+
+struct grpc_ares_ev_driver {
+ int id;
+ ares_socket_t socks[ARES_GETSOCK_MAXNUM];
+ int bitmask;
+ grpc_closure driver_closure;
+ grpc_pollset_set *pollset_set;
+ ares_channel *channel;
+ fd_pair *fds;
+};
+
+static fd_pair *get_fd(fd_pair **head, int fd) {
+ fd_pair dummy_head;
+ fd_pair *node;
+ fd_pair *ret;
+ dummy_head.next = *head;
+ node = &dummy_head;
+ while (node->next != NULL) {
+ if (node->next->fd == fd) {
+ ret = node->next;
+ node->next = node->next->next;
+ *head = dummy_head.next;
+ return ret;
+ }
+ }
+ return NULL;
+}
+
+static void driver_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
+ grpc_ares_ev_driver *d = arg;
+ size_t i;
+ gpr_log(GPR_ERROR, "driver_cb");
+ if (error == GRPC_ERROR_NONE) {
+ gpr_log(GPR_ERROR, "GRPC_ERROR_NONE");
+ for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
+ ares_process_fd(
+ *d->channel,
+ ARES_GETSOCK_READABLE(d->bitmask, i) ? d->socks[i] : ARES_SOCKET_BAD,
+ ARES_GETSOCK_WRITABLE(d->bitmask, i) ? d->socks[i] : ARES_SOCKET_BAD);
+ }
+ }
+ grpc_ares_notify_on_event(exec_ctx, d);
+ grpc_exec_ctx_flush(exec_ctx);
+}
+
+void grpc_ares_notify_on_event(grpc_exec_ctx *exec_ctx, grpc_ares_ev_driver *ev_driver) {
+ size_t i;
+ fd_pair *new_list = NULL;
+ ev_driver->bitmask =
+ ares_getsock(*ev_driver->channel, ev_driver->socks, ARES_GETSOCK_MAXNUM);
+ grpc_closure_init(&ev_driver->driver_closure, driver_cb, ev_driver);
+ for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
+ char *final_name;
+ gpr_asprintf(&final_name, "host1%" PRIuPTR, i);
+
+ if (ARES_GETSOCK_READABLE(ev_driver->bitmask, i) ||
+ ARES_GETSOCK_WRITABLE(ev_driver->bitmask, i)) {
+ gpr_log(GPR_ERROR, "%d", ev_driver->socks[i]);
+ fd_pair *fdp = get_fd(&ev_driver->fds, ev_driver->socks[i]);
+ if (!fdp) {
+ gpr_log(GPR_ERROR, "new fd");
+ fdp = gpr_malloc(sizeof(fd_pair));
+ fdp->grpc_fd = grpc_fd_create(ev_driver->socks[i], final_name);
+ fdp->fd = ev_driver->socks[i];
+ grpc_pollset_set_add_fd(exec_ctx, ev_driver->pollset_set, fdp->grpc_fd);
+ // new_fd_pair->grpc_fd = fd;
+ // new_fd_pair->next = ev_driver->fds;
+ }
+ fdp->next = new_list;
+ new_list = fdp;
+
+ if (ARES_GETSOCK_READABLE(ev_driver->bitmask, i)) {
+ gpr_log(GPR_ERROR, "READABLE");
+
+ grpc_fd_notify_on_read(exec_ctx, fdp->grpc_fd,
+ &ev_driver->driver_closure);
+ }
+ if (ARES_GETSOCK_WRITABLE(ev_driver->bitmask, i)) {
+ gpr_log(GPR_ERROR, "writable");
+
+ grpc_fd_notify_on_write(exec_ctx, fdp->grpc_fd,
+ &ev_driver->driver_closure);
+ }
+ }
+ gpr_free(final_name);
+ }
+
+ while (ev_driver->fds != NULL) {
+ fd_pair *cur;
+ // int fd;s
+ cur = ev_driver->fds;
+ ev_driver->fds = ev_driver->fds->next;
+ gpr_log(GPR_ERROR, "fd in ev_driver: %d\n", cur->fd);
+ grpc_pollset_set_del_fd(exec_ctx, ev_driver->pollset_set, cur->grpc_fd);
+ gpr_log(GPR_ERROR, "grpc_pollset_set_del_fd");
+ grpc_fd_shutdown(exec_ctx, cur->grpc_fd);
+ gpr_log(GPR_ERROR, "grpc_fd_shutdown");
+ grpc_fd_orphan(exec_ctx, cur->grpc_fd, NULL, NULL, "come on..");
+ gpr_log(GPR_ERROR, "grpc_fd_orphan");
+ gpr_free(cur);
+ }
+
+ ev_driver->fds = new_list;
+
+ gpr_log(GPR_ERROR, "eof notify_on_event");
+}
+
+
+
+grpc_ares_ev_driver *grpc_ares_ev_driver_create(ares_channel *channel, grpc_pollset_set *pollset_set) {
+ grpc_ares_ev_driver *ev_driver = gpr_malloc(sizeof(grpc_ares_ev_driver));
+ ev_driver->channel = channel;
+ ev_driver->pollset_set = pollset_set;
+ ev_driver->fds = NULL;
+ return ev_driver;
+}
+
+#endif
diff --git a/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.c b/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.c
index 73563eacc4..3db974bd92 100644
--- a/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.c
+++ b/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.c
@@ -33,10 +33,6 @@
#include <grpc/support/port_platform.h>
-#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h"
-#include "src/core/lib/iomgr/ev_posix.h"
-#include "src/core/lib/iomgr/sockaddr.h"
-
#ifdef GPR_POSIX_SOCKET
#include <arpa/inet.h>
#endif
@@ -45,6 +41,10 @@
#include <winsock2.h>
#endif
+#include "src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h"
+// #include "src/core/lib/iomgr/ev_posix.h"
+// #include "src/core/lib/iomgr/sockaddr.h"
+
#include <string.h>
#include <sys/types.h>
@@ -56,6 +56,7 @@
#include <grpc/support/thd.h>
#include <grpc/support/time.h>
#include <grpc/support/useful.h>
+#include "src/core/ext/resolver/dns/c_ares/grpc_ares_ev_driver.h"
#include "src/core/lib/iomgr/executor.h"
#include "src/core/lib/iomgr/iomgr_internal.h"
#include "src/core/lib/iomgr/sockaddr_utils.h"
@@ -63,22 +64,6 @@
#include "src/core/lib/support/block_annotate.h"
#include "src/core/lib/support/string.h"
-typedef struct fd_pair {
- grpc_fd *grpc_fd;
- int fd;
- struct fd_pair *next;
-} fd_pair;
-
-typedef struct {
- int id;
- ares_socket_t socks[ARES_GETSOCK_MAXNUM];
- int bitmask;
- grpc_closure driver_closure;
- grpc_pollset_set *pollset_set;
- ares_channel *channel;
- fd_pair *fds;
-} driver;
-
struct grpc_ares_request {
char *name;
char *host;
@@ -90,122 +75,122 @@ struct grpc_ares_request {
grpc_closure request_closure;
void *arg;
ares_channel channel;
- driver ev_driver;
+ grpc_ares_ev_driver* ev_driver;
};
-struct grpc_pollset_set {
- gpr_mu mu;
-
- size_t pollset_count;
- size_t pollset_capacity;
- grpc_pollset **pollsets;
-
- size_t pollset_set_count;
- size_t pollset_set_capacity;
- struct grpc_pollset_set **pollset_sets;
-
- size_t fd_count;
- size_t fd_capacity;
- grpc_fd **fds;
-};
-
-static void driver_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error);
-
-static fd_pair *get_fd(fd_pair **head, int fd) {
- fd_pair dummy_head;
- fd_pair *node;
- fd_pair *ret;
- dummy_head.next = *head;
- node = &dummy_head;
- while (node->next != NULL) {
- if (node->next->fd == fd) {
- ret = node->next;
- node->next = node->next->next;
- *head = dummy_head.next;
- return ret;
- }
- }
- return NULL;
-}
-
-static void notify_on_event(grpc_exec_ctx *exec_ctx, driver *ev_driver) {
- size_t i;
- fd_pair *new_list = NULL;
- ev_driver->bitmask =
- ares_getsock(*ev_driver->channel, ev_driver->socks, ARES_GETSOCK_MAXNUM);
- grpc_closure_init(&ev_driver->driver_closure, driver_cb, ev_driver);
- for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
- char *final_name;
- gpr_asprintf(&final_name, "host1%" PRIuPTR, i);
-
- if (ARES_GETSOCK_READABLE(ev_driver->bitmask, i) ||
- ARES_GETSOCK_WRITABLE(ev_driver->bitmask, i)) {
- gpr_log(GPR_ERROR, "%d", ev_driver->socks[i]);
- fd_pair *fdp = get_fd(&ev_driver->fds, ev_driver->socks[i]);
- if (!fdp) {
- gpr_log(GPR_ERROR, "new fd");
- fdp = gpr_malloc(sizeof(fd_pair));
- fdp->grpc_fd = grpc_fd_create(ev_driver->socks[i], final_name);
- fdp->fd = ev_driver->socks[i];
- grpc_pollset_set_add_fd(exec_ctx, ev_driver->pollset_set, fdp->grpc_fd);
- // new_fd_pair->grpc_fd = fd;
- // new_fd_pair->next = ev_driver->fds;
- }
- fdp->next = new_list;
- new_list = fdp;
-
- if (ARES_GETSOCK_READABLE(ev_driver->bitmask, i)) {
- gpr_log(GPR_ERROR, "READABLE");
-
- grpc_fd_notify_on_read(exec_ctx, fdp->grpc_fd,
- &ev_driver->driver_closure);
- }
- if (ARES_GETSOCK_WRITABLE(ev_driver->bitmask, i)) {
- gpr_log(GPR_ERROR, "writable");
-
- grpc_fd_notify_on_write(exec_ctx, fdp->grpc_fd,
- &ev_driver->driver_closure);
- }
- }
- gpr_free(final_name);
- }
-
- while (ev_driver->fds != NULL) {
- fd_pair *cur;
- // int fd;s
- cur = ev_driver->fds;
- ev_driver->fds = ev_driver->fds->next;
- gpr_log(GPR_ERROR, "fd in ev_driver: %d\n", cur->fd);
- grpc_pollset_set_del_fd(exec_ctx, ev_driver->pollset_set, cur->grpc_fd);
- gpr_log(GPR_ERROR, "grpc_pollset_set_del_fd");
- grpc_fd_shutdown(exec_ctx, cur->grpc_fd);
- gpr_log(GPR_ERROR, "grpc_fd_shutdown");
- grpc_fd_orphan(exec_ctx, cur->grpc_fd, NULL, NULL, "come on..");
- gpr_log(GPR_ERROR, "grpc_fd_orphan");
- gpr_free(cur);
- }
-
- ev_driver->fds = new_list;
-
- gpr_log(GPR_ERROR, "eof notify_on_event");
-}
-
-static void driver_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
- driver *d = arg;
- size_t i;
- gpr_log(GPR_ERROR, "driver_cb");
- if (error == GRPC_ERROR_NONE) {
- gpr_log(GPR_ERROR, "GRPC_ERROR_NONE");
- for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
- ares_process_fd(
- *d->channel,
- ARES_GETSOCK_READABLE(d->bitmask, i) ? d->socks[i] : ARES_SOCKET_BAD,
- ARES_GETSOCK_WRITABLE(d->bitmask, i) ? d->socks[i] : ARES_SOCKET_BAD);
- }
- }
- notify_on_event(exec_ctx, d);
- grpc_exec_ctx_flush(exec_ctx);
-}
+// struct grpc_pollset_set {
+// gpr_mu mu;
+//
+// size_t pollset_count;
+// size_t pollset_capacity;
+// grpc_pollset **pollsets;
+//
+// size_t pollset_set_count;
+// size_t pollset_set_capacity;
+// struct grpc_pollset_set **pollset_sets;
+//
+// size_t fd_count;
+// size_t fd_capacity;
+// grpc_fd **fds;
+// };
+
+// static void driver_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error);
+//
+// static fd_pair *get_fd(fd_pair **head, int fd) {
+// fd_pair dummy_head;
+// fd_pair *node;
+// fd_pair *ret;
+// dummy_head.next = *head;
+// node = &dummy_head;
+// while (node->next != NULL) {
+// if (node->next->fd == fd) {
+// ret = node->next;
+// node->next = node->next->next;
+// *head = dummy_head.next;
+// return ret;
+// }
+// }
+// return NULL;
+// }
+//
+// static void notify_on_event(grpc_exec_ctx *exec_ctx, driver *ev_driver) {
+// size_t i;
+// fd_pair *new_list = NULL;
+// ev_driver->bitmask =
+// ares_getsock(*ev_driver->channel, ev_driver->socks, ARES_GETSOCK_MAXNUM);
+// grpc_closure_init(&ev_driver->driver_closure, driver_cb, ev_driver);
+// for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
+// char *final_name;
+// gpr_asprintf(&final_name, "host1%" PRIuPTR, i);
+//
+// if (ARES_GETSOCK_READABLE(ev_driver->bitmask, i) ||
+// ARES_GETSOCK_WRITABLE(ev_driver->bitmask, i)) {
+// gpr_log(GPR_ERROR, "%d", ev_driver->socks[i]);
+// fd_pair *fdp = get_fd(&ev_driver->fds, ev_driver->socks[i]);
+// if (!fdp) {
+// gpr_log(GPR_ERROR, "new fd");
+// fdp = gpr_malloc(sizeof(fd_pair));
+// fdp->grpc_fd = grpc_fd_create(ev_driver->socks[i], final_name);
+// fdp->fd = ev_driver->socks[i];
+// grpc_pollset_set_add_fd(exec_ctx, ev_driver->pollset_set, fdp->grpc_fd);
+// // new_fd_pair->grpc_fd = fd;
+// // new_fd_pair->next = ev_driver->fds;
+// }
+// fdp->next = new_list;
+// new_list = fdp;
+//
+// if (ARES_GETSOCK_READABLE(ev_driver->bitmask, i)) {
+// gpr_log(GPR_ERROR, "READABLE");
+//
+// grpc_fd_notify_on_read(exec_ctx, fdp->grpc_fd,
+// &ev_driver->driver_closure);
+// }
+// if (ARES_GETSOCK_WRITABLE(ev_driver->bitmask, i)) {
+// gpr_log(GPR_ERROR, "writable");
+//
+// grpc_fd_notify_on_write(exec_ctx, fdp->grpc_fd,
+// &ev_driver->driver_closure);
+// }
+// }
+// gpr_free(final_name);
+// }
+//
+// while (ev_driver->fds != NULL) {
+// fd_pair *cur;
+// // int fd;s
+// cur = ev_driver->fds;
+// ev_driver->fds = ev_driver->fds->next;
+// gpr_log(GPR_ERROR, "fd in ev_driver: %d\n", cur->fd);
+// grpc_pollset_set_del_fd(exec_ctx, ev_driver->pollset_set, cur->grpc_fd);
+// gpr_log(GPR_ERROR, "grpc_pollset_set_del_fd");
+// grpc_fd_shutdown(exec_ctx, cur->grpc_fd);
+// gpr_log(GPR_ERROR, "grpc_fd_shutdown");
+// grpc_fd_orphan(exec_ctx, cur->grpc_fd, NULL, NULL, "come on..");
+// gpr_log(GPR_ERROR, "grpc_fd_orphan");
+// gpr_free(cur);
+// }
+//
+// ev_driver->fds = new_list;
+//
+// gpr_log(GPR_ERROR, "eof notify_on_event");
+// }
+//
+// static void driver_cb(grpc_exec_ctx *exec_ctx, void *arg, grpc_error *error) {
+// driver *d = arg;
+// size_t i;
+// gpr_log(GPR_ERROR, "driver_cb");
+// if (error == GRPC_ERROR_NONE) {
+// gpr_log(GPR_ERROR, "GRPC_ERROR_NONE");
+// for (i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
+// ares_process_fd(
+// *d->channel,
+// ARES_GETSOCK_READABLE(d->bitmask, i) ? d->socks[i] : ARES_SOCKET_BAD,
+// ARES_GETSOCK_WRITABLE(d->bitmask, i) ? d->socks[i] : ARES_SOCKET_BAD);
+// }
+// }
+// notify_on_event(exec_ctx, d);
+// grpc_exec_ctx_flush(exec_ctx);
+// }
static void on_done_cb(void *arg, int status, int timeouts,
struct hostent *hostent) {
@@ -277,25 +262,12 @@ static void on_done_cb(void *arg, int status, int timeouts,
static void resolve_address_impl(grpc_exec_ctx *exec_ctx, void *arg,
grpc_error *error) {
- int status;
-
grpc_ares_request *r = (grpc_ares_request *)arg;
- gpr_log(GPR_ERROR, "Really?");
-
- status = ares_init(&r->channel);
- if (status != ARES_SUCCESS) {
- gpr_log(GPR_ERROR, "ares_init failed");
- }
- driver *ev_driver = &r->ev_driver;
- ev_driver->channel = &r->channel;
gpr_log(GPR_ERROR, "before ares_gethostbyname %s", r->host);
ares_gethostbyname(r->channel, r->host, AF_UNSPEC, on_done_cb, r);
gpr_log(GPR_ERROR, "before ares_getsock");
- notify_on_event(exec_ctx, &r->ev_driver);
-
- gpr_log(GPR_ERROR, "before poll");
-
+ grpc_ares_notify_on_event(exec_ctx, r->ev_driver);
gpr_log(GPR_ERROR, "eof resolve_address_impl");
}
@@ -344,14 +316,21 @@ grpc_ares_request *grpc_resolve_address_ares(grpc_exec_ctx *exec_ctx,
char *host;
char *port;
grpc_error *err;
+ int status;
grpc_ares_request *r = gpr_malloc(sizeof(grpc_ares_request));
r->name = gpr_strdup(name);
r->default_port = gpr_strdup(default_port);
r->on_done = on_done;
r->addrs_out = addrs;
- r->ev_driver.pollset_set = pollset_set;
- r->ev_driver.fds = NULL;
+
+ status = ares_init(&r->channel);
+ if (status != ARES_SUCCESS) {
+ grpc_exec_ctx_sched(exec_ctx, on_done, GRPC_ERROR_CREATE("Failed to init ares"), NULL);
+ return r;
+ }
+
+ r->ev_driver = grpc_ares_ev_driver_create(&r->channel, pollset_set);
if (name[0] == 'u' && name[1] == 'n' && name[2] == 'i' && name[3] == 'x' &&
name[4] == ':' && name[5] != 0) {
@@ -385,9 +364,14 @@ grpc_ares_request *grpc_resolve_address_ares(grpc_exec_ctx *exec_ctx,
return r;
}
-void grpc_ares_init(void) {
+grpc_error *grpc_ares_init(void) {
int status = ares_library_init(ARES_LIB_INIT_ALL);
if (status != ARES_SUCCESS) {
- gpr_log(GPR_ERROR, "ares_library_init failed");
+ return GRPC_ERROR_CREATE("ares_library_init failed");
}
+ return GRPC_ERROR_NONE;
+}
+
+void grpc_ares_cleanup(void) {
+ ares_library_cleanup();
}
diff --git a/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h b/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h
index 6d931c7e02..f4468df5a0 100644
--- a/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h
+++ b/src/core/ext/resolver/dns/c_ares/grpc_ares_wrapper.h
@@ -1,6 +1,6 @@
/*
*
- * Copyright 2015, Google Inc.
+ * Copyright 2016, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -47,6 +47,8 @@ grpc_ares_request *grpc_resolve_address_ares(
grpc_pollset_set *pollset_set, grpc_closure *on_done,
grpc_resolved_addresses **addresses);
-void grpc_ares_init(void);
+grpc_error *grpc_ares_init(void);
+
+void grpc_ares_cleanup(void);
#endif /* GRPC_CORE_EXT_RESOLVER_DNS_CARES_RESOLVE_ADDRESS_CARES_H */