1 # Copyright (c) 2009, 2010 Nicira Networks.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at:
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
32 def do_force_reconnect(_):
33 r.force_reconnect(now)
36 def error_from_string(s):
39 elif s == "ECONNREFUSED":
40 return errno.ECONNREFUSED
42 return ovs.reconnect.EOF
44 sys.stderr.write("unknown error '%s'\n" % s)
48 def do_disconnected(arg):
49 r.disconnected(now, error_from_string(arg))
56 def do_connect_failed(arg):
57 r.connect_failed(now, error_from_string(arg))
76 elif action == ovs.reconnect.CONNECT:
77 print " should connect"
78 elif action == ovs.reconnect.DISCONNECT:
79 print " should disconnect"
80 elif action == ovs.reconnect.PROBE:
81 print " should send probe"
93 timeout = r.timeout(now)
95 print " advance %d ms" % timeout
101 def do_set_max_tries(arg):
102 r.set_max_tries(int(arg))
105 def diff_stats(old, new, delta):
106 if (old.state != new.state or
107 old.state_elapsed != new.state_elapsed or
108 old.backoff != new.backoff):
109 print(" in %s for %d ms (%d ms backoff)"
110 % (new.state, new.state_elapsed, new.backoff))
112 if (old.creation_time != new.creation_time or
113 old.last_received != new.last_received or
114 old.last_connected != new.last_connected):
115 print(" created %d, last received %d, last connected %d"
116 % (new.creation_time, new.last_received, new.last_connected))
118 if (old.n_successful_connections != new.n_successful_connections or
119 old.n_attempted_connections != new.n_attempted_connections or
120 old.seqno != new.seqno):
121 print(" %d successful connections out of %d attempts, seqno %d"
122 % (new.n_successful_connections, new.n_attempted_connections,
125 if (old.is_connected != new.is_connected):
130 print(" %sconnected" % negate)
132 if (old.last_connected != new.last_connected or
133 (new.msec_since_connect != None and
134 old.msec_since_connect != new.msec_since_connect - delta) or
135 (old.total_connected_duration != new.total_connected_duration - delta
136 and not (old.total_connected_duration == 0 and
137 new.total_connected_duration == 0))):
138 print(" last connected %d ms ago, connected %d ms total"
139 % (new.msec_since_connect, new.total_connected_duration))
141 if (old.last_disconnected != new.last_disconnected or
142 (new.msec_since_disconnect != None and
143 old.msec_since_disconnect != new.msec_since_disconnect - delta)):
144 print(" disconnected at %d ms (%d ms ago)"
145 % (new.last_disconnected, new.msec_since_disconnect))
148 def do_set_passive(_):
149 r.set_passive(True, now)
156 def do_listen_error(arg):
157 r.listen_error(now, int(arg))
163 "disable": do_disable,
164 "force-reconnect": do_force_reconnect,
165 "disconnected": do_disconnected,
166 "connecting": do_connecting,
167 "connect-failed": do_connect_failed,
168 "connected": do_connected,
169 "received": do_received,
171 "advance": do_advance,
172 "timeout": do_timeout,
173 "set-max-tries": do_set_max_tries,
174 "passive": do_set_passive,
175 "listening": do_listening,
176 "listen-error": do_listen_error
183 r = ovs.reconnect.Reconnect(now)
185 prev = r.get_stats(now)
186 print "### t=%d ###" % now
188 old_max_tries = r.get_max_tries()
190 line = sys.stdin.readline()
207 commands[command](op)
211 print "### t=%d ###" % now
213 cur = r.get_stats(now)
214 diff_stats(prev, cur, now - old_time)
216 if r.get_max_tries() != old_max_tries:
217 old_max_tries = r.get_max_tries()
218 print " %d tries left" % old_max_tries
223 if __name__ == '__main__':