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.
27 def do_force_reconnect(arg):
28 r.force_reconnect(now)
30 def error_from_string(s):
33 elif s == "ECONNREFUSED":
34 return errno.ECONNREFUSED
38 sys.stderr.write("unknown error '%s'\n" % s)
41 def do_disconnected(arg):
42 r.disconnected(now, error_from_string(arg))
44 def do_connecting(arg):
47 def do_connect_failed(arg):
48 r.connect_failed(now, error_from_string(arg))
50 def do_connected(arg):
64 elif action == ovs.reconnect.CONNECT:
65 print " should connect"
66 elif action == ovs.reconnect.DISCONNECT:
67 print " should disconnect"
68 elif action == ovs.reconnect.PROBE:
69 print " should send probe"
79 timeout = r.timeout(now)
81 print " advance %d ms" % timeout
86 def do_set_max_tries(arg):
87 r.set_max_tries(int(arg))
89 def diff_stats(old, new, delta):
90 if (old.state != new.state or
91 old.state_elapsed != new.state_elapsed or
92 old.backoff != new.backoff):
93 print(" in %s for %d ms (%d ms backoff)"
94 % (new.state, new.state_elapsed, new.backoff))
96 if (old.creation_time != new.creation_time or
97 old.last_received != new.last_received or
98 old.last_connected != new.last_connected):
99 print(" created %d, last received %d, last connected %d"
100 % (new.creation_time, new.last_received, new.last_connected))
102 if (old.n_successful_connections != new.n_successful_connections or
103 old.n_attempted_connections != new.n_attempted_connections or
104 old.seqno != new.seqno):
105 print(" %d successful connections out of %d attempts, seqno %d"
106 % (new.n_successful_connections, new.n_attempted_connections,
109 if (old.is_connected != new.is_connected):
114 print(" %sconnected" % negate)
116 if (old.last_connected != new.last_connected or
117 (new.msec_since_connect != None and
118 old.msec_since_connect != new.msec_since_connect - delta) or
119 (old.total_connected_duration != new.total_connected_duration - delta and
120 not (old.total_connected_duration == 0 and
121 new.total_connected_duration == 0))):
122 print(" last connected %d ms ago, connected %d ms total"
123 % (new.msec_since_connect, new.total_connected_duration))
125 if (old.last_disconnected != new.last_disconnected or
126 (new.msec_since_disconnect != None and
127 old.msec_since_disconnect != new.msec_since_disconnect - delta)):
128 print(" disconnected at %d ms (%d ms ago)"
129 % (new.last_disconnected, new.msec_since_disconnect))
131 def do_set_passive(arg):
132 r.set_passive(True, now)
134 def do_listening(arg):
137 def do_listen_error(arg):
138 r.listen_error(now, int(arg))
143 "disable": do_disable,
144 "force-reconnect": do_force_reconnect,
145 "disconnected": do_disconnected,
146 "connecting": do_connecting,
147 "connect-failed": do_connect_failed,
148 "connected": do_connected,
149 "received": do_received,
151 "advance": do_advance,
152 "timeout": do_timeout,
153 "set-max-tries": do_set_max_tries,
154 "passive": do_set_passive,
155 "listening": do_listening,
156 "listen-error": do_listen_error
159 logging.basicConfig(level=logging.CRITICAL)
165 r = ovs.reconnect.Reconnect(now)
167 prev = r.get_stats(now)
168 print "### t=%d ###" % now
170 old_max_tries = r.get_max_tries()
172 line = sys.stdin.readline()
189 commands[command](op)
193 print "### t=%d ###" % now
195 cur = r.get_stats(now)
196 diff_stats(prev, cur, now - old_time)
198 if r.get_max_tries() != old_max_tries:
199 old_max_tries = r.get_max_tries()
200 print " %d tries left" % old_max_tries
204 if __name__ == '__main__':