2 * Copyright (c) 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 /* This library implements a finite-state machine for connecting and
21 * reconnecting to a network resource with exponential backoff. It also
22 * provides optional support for detecting a connection on which the peer is no
25 * The library does not implement anything networking related, only an FSM for
26 * networking code to use.
28 * Many "reconnect" functions take a "now" argument. This makes testing easier
29 * since there is no hidden state. When not testing, just pass the return
30 * value of time_msec() from timeval.h. (Perhaps this design should be
31 * revisited later.) */
35 struct reconnect *reconnect_create(long long int now);
36 void reconnect_destroy(struct reconnect *);
38 const char *reconnect_get_name(const struct reconnect *);
39 void reconnect_set_name(struct reconnect *, const char *name);
41 int reconnect_get_min_backoff(const struct reconnect *);
42 int reconnect_get_max_backoff(const struct reconnect *);
43 int reconnect_get_probe_interval(const struct reconnect *);
45 void reconnect_set_backoff(struct reconnect *,
46 int min_backoff, int max_backoff);
47 void reconnect_set_probe_interval(struct reconnect *, int probe_interval);
49 bool reconnect_is_enabled(const struct reconnect *);
50 void reconnect_enable(struct reconnect *, long long int now);
51 void reconnect_disable(struct reconnect *, long long int now);
53 void reconnect_force_reconnect(struct reconnect *, long long int now);
55 bool reconnect_is_connected(const struct reconnect *);
56 unsigned int reconnect_get_connection_duration(const struct reconnect *,
59 void reconnect_disconnected(struct reconnect *, long long int now, int error);
60 void reconnect_connecting(struct reconnect *, long long int now);
61 void reconnect_connected(struct reconnect *, long long int now);
62 void reconnect_connect_failed(struct reconnect *, long long int now,
64 void reconnect_received(struct reconnect *, long long int now);
66 enum reconnect_action {
67 RECONNECT_CONNECT = 1,
71 enum reconnect_action reconnect_run(struct reconnect *, long long int now);
72 void reconnect_wait(struct reconnect *, long long int now);
73 int reconnect_timeout(struct reconnect *, long long int now);
75 struct reconnect_stats {
76 /* All times and durations in this structure are in milliseconds. */
77 long long int creation_time; /* Time reconnect_create() called. */
78 long long int last_received; /* Last call to reconnect_received(). */
79 long long int last_connected; /* Last call to reconnect_connected(). */
80 int backoff; /* Current backoff duration. */
82 unsigned int seqno; /* # of connections + # of disconnections. */
84 bool is_connected; /* Currently connected? */
85 unsigned int current_connection_duration; /* Time of current connection. */
86 unsigned int total_connected_duration; /* Sum of all connections. */
87 unsigned int n_attempted_connections;
88 unsigned int n_successful_connections;
90 /* These should only be provided to a human user for debugging purposes.
91 * The client should not attempt to interpret them. */
92 const char *state; /* FSM state. */
93 unsigned int state_elapsed; /* Time since FSM state entered. */
96 void reconnect_get_stats(const struct reconnect *, long long int now,
97 struct reconnect_stats *);
99 #endif /* reconnect.h */