blob: dc6623c46c313aff1f72fbc4fba801e5a7b90c6c [file] [log] [blame]
Craig Tillerc7b5f762015-06-27 11:48:42 -07001/*
2 *
David Garcia Quintas3598d442016-03-15 14:53:05 -07003 * Copyright 2015-2016, Google Inc.
Craig Tillerc7b5f762015-06-27 11:48:42 -07004 * 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
Craig Tiller9a4dddd2016-03-25 17:08:13 -070034#ifndef GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H
35#define GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H
Craig Tillerc7b5f762015-06-27 11:48:42 -070036
37#include <grpc/grpc.h>
Craig Tiller298751c2015-09-22 09:41:05 -070038#include "src/core/iomgr/exec_ctx.h"
Craig Tillerc7b5f762015-06-27 11:48:42 -070039
Craig Tillera82950e2015-09-22 12:33:20 -070040typedef struct grpc_connectivity_state_watcher {
Craig Tiller08a1cf82015-06-29 09:37:52 -070041 /** we keep watchers in a linked list */
Craig Tillerc7b5f762015-06-27 11:48:42 -070042 struct grpc_connectivity_state_watcher *next;
43 /** closure to notify on change */
Craig Tiller33825112015-09-18 07:44:19 -070044 grpc_closure *notify;
Craig Tillerc7b5f762015-06-27 11:48:42 -070045 /** the current state as believed by the watcher */
46 grpc_connectivity_state *current;
47} grpc_connectivity_state_watcher;
48
Craig Tillera82950e2015-09-22 12:33:20 -070049typedef struct {
Craig Tiller08a1cf82015-06-29 09:37:52 -070050 /** current connectivity state */
Craig Tillerc7b5f762015-06-27 11:48:42 -070051 grpc_connectivity_state current_state;
52 /** all our watchers */
53 grpc_connectivity_state_watcher *watchers;
Craig Tiller1ada6ad2015-07-16 16:19:14 -070054 /** a name to help debugging */
55 char *name;
Craig Tillerc7b5f762015-06-27 11:48:42 -070056} grpc_connectivity_state_tracker;
57
Craig Tiller1ada6ad2015-07-16 16:19:14 -070058extern int grpc_connectivity_state_trace;
59
Craig Tiller179e6fe2015-12-09 11:09:47 -080060const char *grpc_connectivity_state_name(grpc_connectivity_state state);
61
Craig Tillera82950e2015-09-22 12:33:20 -070062void grpc_connectivity_state_init(grpc_connectivity_state_tracker *tracker,
63 grpc_connectivity_state init_state,
64 const char *name);
65void grpc_connectivity_state_destroy(grpc_exec_ctx *exec_ctx,
66 grpc_connectivity_state_tracker *tracker);
Craig Tillerc7b5f762015-06-27 11:48:42 -070067
Craig Tiller5795da72015-09-17 15:27:13 -070068/** Set connectivity state; not thread safe; access must be serialized with an
69 * external lock */
Craig Tillera82950e2015-09-22 12:33:20 -070070void grpc_connectivity_state_set(grpc_exec_ctx *exec_ctx,
71 grpc_connectivity_state_tracker *tracker,
72 grpc_connectivity_state state,
73 const char *reason);
Craig Tillerc7b5f762015-06-27 11:48:42 -070074
Craig Tillera82950e2015-09-22 12:33:20 -070075grpc_connectivity_state grpc_connectivity_state_check(
76 grpc_connectivity_state_tracker *tracker);
Craig Tillerc7b5f762015-06-27 11:48:42 -070077
Craig Tiller48613042015-11-29 14:45:11 -080078/** Return 1 if the channel should start connecting, 0 otherwise.
Craig Tiller1d881fb2015-12-01 07:39:04 -080079 If current==NULL cancel notify if it is already queued (success==0 in that
Craig Tiller48613042015-11-29 14:45:11 -080080 case) */
Craig Tillera82950e2015-09-22 12:33:20 -070081int grpc_connectivity_state_notify_on_state_change(
82 grpc_exec_ctx *exec_ctx, grpc_connectivity_state_tracker *tracker,
83 grpc_connectivity_state *current, grpc_closure *notify);
Craig Tillerc7b5f762015-06-27 11:48:42 -070084
Craig Tiller9a4dddd2016-03-25 17:08:13 -070085#endif /* GRPC_CORE_LIB_TRANSPORT_CONNECTIVITY_STATE_H */