diff options
Diffstat (limited to 'bandwidthmon.py')
-rwxr-xr-x | bandwidthmon.py | 25 |
1 files changed, 16 insertions, 9 deletions
diff --git a/bandwidthmon.py b/bandwidthmon.py index 4e88c16..c99aaac 100755 --- a/bandwidthmon.py +++ b/bandwidthmon.py @@ -6,6 +6,11 @@ import glib import gtk def main(): + debug = True + def dprint(x): + if not debug: + return + print x p = argparse.ArgumentParser(description="proxy multiple socket connections") p.add_argument('--listen-port', required=True, type=int) p.add_argument('--remote-port', required=True, type=int) @@ -14,10 +19,12 @@ def main(): args = p.parse_args(sys.argv[1:]) iterate_packets, handle_input, select_based_iterator, get_fds = proxy.make_proxy( args.listen_port, (args.remote_host, args.remote_port), - args.listen_host, debug=True) + args.listen_host, debug=debug) + assert(len(get_fds()) == 1) # only the accepting socket + accepter = get_fds()[0] added_fds = set() def on_new_fd(fd): - print "on_new_fd %s" % fd.fileno() + dprint("on_new_fd %s" % fd.fileno()) # Don't add glib.IO_OUT unless you mean it. # corollary: when I want to really implement this (to implement non # blocking writes) I'll have to io_add_watch(OUT) and return False and @@ -30,26 +37,26 @@ def main(): try: fd.fileno() except socket.error: - print "removing socket %s" % fd + dprint("removing socket %s" % fd) added_fds.remove(fd) return False - print "called back %s, condition %s" % (fd.fileno(), condition) + dprint("called back %s, condition %s" % (fd.fileno(), condition)) if condition != glib.IO_IN: - print "not reading from %d" % fd.fileno() + dprint("not reading from %d" % fd.fileno()) return True result = handle_input(fd) - print "%s: result %s" % (fd.fileno(), repr(result)) + dprint("%s: result %s" % (fd.fileno(), repr(result))) fds, new_fds = update_fds() if not result: - # closed port - return False + # accepter port returns nothing, don't close it. + return fd == accepter (src, dst, data, completer) = result completer() print repr((src, dst, data)) return True def update_fds(): fds = get_fds() - print "update_fds: %s" % (','.join(str(f.fileno()) for f in fds)) + dprint("update_fds: %s" % (','.join(str(f.fileno()) for f in fds))) new_fds = [] for fd in set(fds) - added_fds: on_new_fd(fd) |