2012-08-08 13:32:57 -07:00
|
|
|
# Copyright (c) 2009, 2010, 2012 Nicira, Inc.
|
2011-09-23 23:43:12 -07:00
|
|
|
#
|
2010-08-25 10:26:40 -07:00
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at:
|
2011-09-23 23:43:12 -07:00
|
|
|
#
|
2010-08-25 10:26:40 -07:00
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
2011-09-23 23:43:12 -07:00
|
|
|
#
|
2010-08-25 10:26:40 -07:00
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import errno
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import ovs.reconnect
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
now = 0
|
|
|
|
r = None
|
|
|
|
|
|
|
|
|
|
|
|
def do_enable(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.enable(now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
def do_disable(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.disable(now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
def do_force_reconnect(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.force_reconnect(now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def error_from_string(s):
|
|
|
|
if not s:
|
|
|
|
return 0
|
|
|
|
elif s == "ECONNREFUSED":
|
|
|
|
return errno.ECONNREFUSED
|
|
|
|
elif s == "EOF":
|
2011-09-24 16:19:27 -07:00
|
|
|
return ovs.reconnect.EOF
|
2010-08-25 10:26:40 -07:00
|
|
|
else:
|
|
|
|
sys.stderr.write("unknown error '%s'\n" % s)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def do_disconnected(arg):
|
|
|
|
r.disconnected(now, error_from_string(arg))
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
def do_connecting(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.connecting(now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def do_connect_failed(arg):
|
|
|
|
r.connect_failed(now, error_from_string(arg))
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
def do_connected(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.connected(now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2012-08-08 13:32:57 -07:00
|
|
|
def do_activity(_):
|
|
|
|
r.activity(now)
|
2010-08-25 10:26:40 -07:00
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def do_run(arg):
|
|
|
|
global now
|
|
|
|
if arg is not None:
|
|
|
|
now += int(arg)
|
|
|
|
|
|
|
|
action = r.run(now)
|
|
|
|
if action is None:
|
|
|
|
pass
|
|
|
|
elif action == ovs.reconnect.CONNECT:
|
|
|
|
print " should connect"
|
|
|
|
elif action == ovs.reconnect.DISCONNECT:
|
|
|
|
print " should disconnect"
|
|
|
|
elif action == ovs.reconnect.PROBE:
|
|
|
|
print " should send probe"
|
|
|
|
else:
|
|
|
|
assert False
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def do_advance(arg):
|
|
|
|
global now
|
|
|
|
now += int(arg)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
def do_timeout(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
global now
|
|
|
|
timeout = r.timeout(now)
|
|
|
|
if timeout >= 0:
|
|
|
|
print " advance %d ms" % timeout
|
|
|
|
now += timeout
|
|
|
|
else:
|
|
|
|
print " no timeout"
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def do_set_max_tries(arg):
|
|
|
|
r.set_max_tries(int(arg))
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2011-03-14 13:10:02 -07:00
|
|
|
def diff_stats(old, new, delta):
|
2010-08-25 10:26:40 -07:00
|
|
|
if (old.state != new.state or
|
|
|
|
old.state_elapsed != new.state_elapsed or
|
|
|
|
old.backoff != new.backoff):
|
|
|
|
print(" in %s for %d ms (%d ms backoff)"
|
|
|
|
% (new.state, new.state_elapsed, new.backoff))
|
|
|
|
|
|
|
|
if (old.creation_time != new.creation_time or
|
2012-08-08 13:32:57 -07:00
|
|
|
old.last_activity != new.last_activity or
|
2010-08-25 10:26:40 -07:00
|
|
|
old.last_connected != new.last_connected):
|
2012-08-08 13:32:57 -07:00
|
|
|
print(" created %d, last activity %d, last connected %d"
|
|
|
|
% (new.creation_time, new.last_activity, new.last_connected))
|
2010-08-25 10:26:40 -07:00
|
|
|
|
|
|
|
if (old.n_successful_connections != new.n_successful_connections or
|
|
|
|
old.n_attempted_connections != new.n_attempted_connections or
|
|
|
|
old.seqno != new.seqno):
|
|
|
|
print(" %d successful connections out of %d attempts, seqno %d"
|
|
|
|
% (new.n_successful_connections, new.n_attempted_connections,
|
|
|
|
new.seqno))
|
|
|
|
|
2011-03-14 13:10:02 -07:00
|
|
|
if (old.is_connected != new.is_connected):
|
2010-08-25 10:26:40 -07:00
|
|
|
if new.is_connected:
|
|
|
|
negate = ""
|
|
|
|
else:
|
2011-03-14 13:10:02 -07:00
|
|
|
negate = "dis"
|
|
|
|
print(" %sconnected" % negate)
|
|
|
|
|
|
|
|
if (old.last_connected != new.last_connected or
|
|
|
|
(new.msec_since_connect != None and
|
|
|
|
old.msec_since_connect != new.msec_since_connect - delta) or
|
2011-09-23 23:43:12 -07:00
|
|
|
(old.total_connected_duration != new.total_connected_duration - delta
|
|
|
|
and not (old.total_connected_duration == 0 and
|
|
|
|
new.total_connected_duration == 0))):
|
2011-03-14 13:10:02 -07:00
|
|
|
print(" last connected %d ms ago, connected %d ms total"
|
|
|
|
% (new.msec_since_connect, new.total_connected_duration))
|
|
|
|
|
|
|
|
if (old.last_disconnected != new.last_disconnected or
|
|
|
|
(new.msec_since_disconnect != None and
|
|
|
|
old.msec_since_disconnect != new.msec_since_disconnect - delta)):
|
2011-03-09 18:36:26 -08:00
|
|
|
print(" disconnected at %d ms (%d ms ago)"
|
2011-03-14 13:10:02 -07:00
|
|
|
% (new.last_disconnected, new.msec_since_disconnect))
|
2011-03-09 18:36:26 -08:00
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
def do_set_passive(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.set_passive(True, now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
|
|
|
def do_listening(_):
|
2010-08-25 10:26:40 -07:00
|
|
|
r.listening(now)
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def do_listen_error(arg):
|
|
|
|
r.listen_error(now, int(arg))
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
def main():
|
|
|
|
commands = {
|
|
|
|
"enable": do_enable,
|
|
|
|
"disable": do_disable,
|
|
|
|
"force-reconnect": do_force_reconnect,
|
|
|
|
"disconnected": do_disconnected,
|
|
|
|
"connecting": do_connecting,
|
|
|
|
"connect-failed": do_connect_failed,
|
|
|
|
"connected": do_connected,
|
2012-08-08 13:32:57 -07:00
|
|
|
"activity": do_activity,
|
2010-08-25 10:26:40 -07:00
|
|
|
"run": do_run,
|
|
|
|
"advance": do_advance,
|
|
|
|
"timeout": do_timeout,
|
|
|
|
"set-max-tries": do_set_max_tries,
|
|
|
|
"passive": do_set_passive,
|
|
|
|
"listening": do_listening,
|
|
|
|
"listen-error": do_listen_error
|
|
|
|
}
|
|
|
|
|
|
|
|
global now
|
|
|
|
global r
|
|
|
|
|
|
|
|
now = 1000
|
|
|
|
r = ovs.reconnect.Reconnect(now)
|
|
|
|
r.set_name("remote")
|
|
|
|
prev = r.get_stats(now)
|
|
|
|
print "### t=%d ###" % now
|
|
|
|
old_time = now
|
|
|
|
old_max_tries = r.get_max_tries()
|
|
|
|
while True:
|
|
|
|
line = sys.stdin.readline()
|
|
|
|
if line == "":
|
|
|
|
break
|
|
|
|
|
|
|
|
print line[:-1]
|
|
|
|
if line[0] == "#":
|
|
|
|
continue
|
|
|
|
|
|
|
|
args = line.split()
|
|
|
|
if len(args) == 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
command = args[0]
|
|
|
|
if len(args) > 1:
|
|
|
|
op = args[1]
|
|
|
|
else:
|
|
|
|
op = None
|
|
|
|
commands[command](op)
|
|
|
|
|
|
|
|
if old_time != now:
|
|
|
|
print
|
|
|
|
print "### t=%d ###" % now
|
|
|
|
|
|
|
|
cur = r.get_stats(now)
|
2011-03-14 13:10:02 -07:00
|
|
|
diff_stats(prev, cur, now - old_time)
|
2010-08-25 10:26:40 -07:00
|
|
|
prev = cur
|
|
|
|
if r.get_max_tries() != old_max_tries:
|
|
|
|
old_max_tries = r.get_max_tries()
|
|
|
|
print " %d tries left" % old_max_tries
|
|
|
|
|
2011-03-14 13:10:02 -07:00
|
|
|
old_time = now
|
|
|
|
|
2011-09-23 23:43:12 -07:00
|
|
|
|
2010-08-25 10:26:40 -07:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|