def hover(event): global moves canvas = event.widget ids = canvas.find_overlapping(event.x - 2, event.y - 2, event.x + 2, event.y + 2) for id in ids: host = Host.ovaltohost.get(canvas.items.get(id)) if host: time.sleep(TF * 0.1) text = '%s:%s (%s files)' % ( host.addr, host.port, host.files) if hasattr(host, 'kb'): other = host.counts.get('other', 0) pong = host.counts.get('pong', 0) query = host.counts.get('query', 0) text = '%s:%s (%s files, %s kb)\n' % ( host.addr, host.port, host.files, host.kb) + \ '%d messages (%d queries, %d pongs)' % ( other + pong + query, query, pong) canvas.itemconfig(info, text=text) canvas.tkraise(info) host.flash('pink') moves = 5 break else: if moves > 0: moves = moves - 1 if moves == 0: canvas.itemconfig(info, text='') canvas.tkraise(info)
def click(event): canvas = event.widget ids = canvas.find_overlapping(event.x - 2, event.y - 2, event.x + 2, event.y + 2) for id in ids: host = Host.ovaltohost.get(canvas.items.get(id)) if host: try: host.conn.query(TESTKEY, ttl=2) except (IOError, AttributeError): pass time.sleep(TF * 0.1) host.flash('red') break
def check(): global newcenter, nthreads showthreads() while workqueue and nthreads < MAXTHREADS: (src, host, port) = workqueue.pop(0) conn = gnut.ThrConn(host, port) try: conn.setDaemon(1) conn.start() except: # print 'threading error', sys.exc_info() workqueue.insert(0, (src, host, port)) # print 'think', nthreads # nthreads = len(threading.enumerate()) - 1 # print 'actual', nthreads # print 'threads:', threading.enumerate() break # nthreads = nthreads + 1 src.attempttime = time.time() src.init(conn) # print 'spawn', src canvas.update() for host in hosts.values(): if host.state in (Host.DROPPED, Host.REFUSED): continue if newcenter: break if host.conn and host.conn.messages: for msg in host.conn.messages: if isinstance(msg, gnut.Query) and msg.key == TESTKEY: host.flash('red') canvas.itemconfig(info, text= 'query echo from %s' % host.addr) canvas.tkraise(info) if host.conn.isAlive(): messages = host.conn.get(1) else: messages = filter(lambda msg: isinstance(msg, gnut.Pong), host.conn.get())[:10] host.conn.messages = [] if messages: for msg in messages: if isinstance(msg, gnut.Pong): host.count('pong') if msg.addr in ['127.0.0.1', '0.0.0.0']: continue src = hosts.get(msg.addr) if src: src.resize(msg.files) src.join(host) else: src = Host(canvas, msg.addr, msg.files, host) src.kb = msg.kb src.port = msg.port top.arrange() hosts[msg.addr] = src workqueue.append((src, msg.addr, msg.port)) elif isinstance(msg, gnut.Query): host.count('query') if msg.key == TESTKEY: host.flash('red') canvas.itemconfig(info, text= 'query echo from %s' % host.addr) canvas.tkraise(info) host.gotquery(msg.key) if msg.key and 32 <= ord(msg.key[0]) < 127: key = string.join(string.split(msg.key, '\x00'), '') querylist.append(key) text = string.join(querylist[-60:], '\n') canvas.itemconfig(queries, text=text) else: host.count('other') canvas.update() time.sleep(TF * 0.05) if host.state == Host.CONNECTING: if host.conn.connected: nthreads = nthreads + 1 showthreads() host.connecttime = time.time() host.connect() try: host.conn.ping(ttl=2) except IOError: pass if host.conn and not host.conn.isAlive() and not host.conn.messages: # print 'terminated', host if host.state == Host.CONNECTED: host.drop() elif host.conn.error: errmsg = string.lower(host.conn.error[1].args[-1]) if host.state == Host.CONNECTING: if string.find(errmsg, 'refused') >= 0: host.refuse() else: host.delete() del hosts[host.conn.host] nthreads = nthreads - 1 if nthreads > MAXTHREADS - 5: def cmptime(a, b, now=time.time()): at = a.__dict__.get('attempttime', now) bt = a.__dict__.get('attempttime', now) return cmp(at, bt) h = filter(lambda h: h.conn and h.conn.isAlive() and not h.conn.opening and not h.conn.closing, hosts.values()) h.sort(cmptime) for old in h[:nthreads - (MAXTHREADS - 5)]: print 'killing off', old, old.conn.incollect, old.conn.inselect, \ old.conn.looping, old.conn.loopcount, old.conn.opening old.conn.close() if newcenter: recenter(newcenter) newcenter = None lock = 0 canvas.tk.createtimerhandler(timestep * TF, check)