diff options
author | Alon Levy <alevy@redhat.com> | 2011-08-13 06:25:29 +0300 |
---|---|---|
committer | Alon Levy <alevy@redhat.com> | 2011-08-13 06:25:29 +0300 |
commit | a7bae6e58a53efad6cf55ae1f234bf719ebae266 (patch) | |
tree | c9d4efd0158d8e72178a90fa27d2aa9d08b22271 | |
parent | aab587d6f9c9692d9662b460dcc3068d5a61db9a (diff) |
bandwidthmon: glib based event loop, works, no graphics
-rwxr-xr-x | bandwidthmon.py | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/bandwidthmon.py b/bandwidthmon.py new file mode 100755 index 0000000..4e88c16 --- /dev/null +++ b/bandwidthmon.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +import proxy +import argparse +import sys +import glib +import gtk + +def main(): + 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) + p.add_argument('--remote-host', default='127.0.0.1') + p.add_argument('--listen-host', default='127.0.0.1') + 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) + added_fds = set() + def on_new_fd(fd): + print "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 + # io_add_watch(~OUT) + glib.io_add_watch(fd.fileno(), + glib.IO_IN | glib.IO_HUP | glib.IO_ERR, + on_read, fd) + def on_read(glib_fd, condition, fd): + # lame check to find out the fd is closed + try: + fd.fileno() + except socket.error: + print "removing socket %s" % fd + added_fds.remove(fd) + return False + print "called back %s, condition %s" % (fd.fileno(), condition) + if condition != glib.IO_IN: + print "not reading from %d" % fd.fileno() + return True + result = handle_input(fd) + print "%s: result %s" % (fd.fileno(), repr(result)) + fds, new_fds = update_fds() + if not result: + # closed port + return False + (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)) + new_fds = [] + for fd in set(fds) - added_fds: + on_new_fd(fd) + added_fds.add(fd) + new_fds.append(fd) + return fds, new_fds + update_fds() + gtk.main() + +if __name__ == '__main__': + main() |