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):
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 or
110 old.current_connection_duration != new.current_connection_duration or
111 old.total_connected_duration != new.total_connected_duration):
116 print(" %sconnected (%d ms), total %d ms connected"
117 % (negate, new.current_connection_duration,
118 new.total_connected_duration))
120 if (old.last_disconnected != new.last_disconnected):
121 print(" disconnected at %d ms (%d ms ago)"
122 % (new.last_disconnected, new.current_disconnect_duration))
124 if (old.current_disconnect_duration != new.current_disconnect_duration):
125 print(" disconnected for %d ms" % (new.current_disconnect_duration))
127 def do_set_passive(arg):
128 r.set_passive(True, now)
130 def do_listening(arg):
133 def do_listen_error(arg):
134 r.listen_error(now, int(arg))
139 "disable": do_disable,
140 "force-reconnect": do_force_reconnect,
141 "disconnected": do_disconnected,
142 "connecting": do_connecting,
143 "connect-failed": do_connect_failed,
144 "connected": do_connected,
145 "received": do_received,
147 "advance": do_advance,
148 "timeout": do_timeout,
149 "set-max-tries": do_set_max_tries,
150 "passive": do_set_passive,
151 "listening": do_listening,
152 "listen-error": do_listen_error
155 logging.basicConfig(level=logging.CRITICAL)
161 r = ovs.reconnect.Reconnect(now)
163 prev = r.get_stats(now)
164 print "### t=%d ###" % now
166 old_max_tries = r.get_max_tries()
168 line = sys.stdin.readline()
185 commands[command](op)
189 print "### t=%d ###" % now
192 cur = r.get_stats(now)
193 diff_stats(prev, cur)
195 if r.get_max_tries() != old_max_tries:
196 old_max_tries = r.get_max_tries()
197 print " %d tries left" % old_max_tries
199 if __name__ == '__main__':