def main(): # start local server to respond to pings and whatnot thrd = threading.Thread(target=server.loop, args=(protocol.node.handler,)) thrd.daemon = True # to ensure program exits even if thrd still runs thrd.start() while 1: # look for things the server might want to know local_changes = statistics.gather() print 'Local changes:',local_changes node_changes = watcher.gather() print 'Node changes:',node_changes # and send them to the server try: s = client.connect(MASTER_ADDR) # 1: data push request = protocol.master.push_request(local_changes, node_changes) stamp,version,do_clear_cache = protocol.master.push_response( client.ask(s, request)) print 'Data pushed to server.' if do_clear_cache: print 'Cache cleared due to server request.' statistics.cache = {} watcher.cache = {} # check if we're out of date if version>conf.VERSION: print 'Client of out date!' print 'Version %d, latest is %d.' % (conf.VERSION, version) print 'Exiting.' import sys sys.exit(1) # 2: node list request print 'Node timestamps, server:',stamp,'mine:',neighbours.stamp if stamp!=neighbours.stamp: # request update! request = protocol.master.nodes_request() stamp,nodes = protocol.master.nodes_response( client.ask(s, request)) print 'Received',len(nodes),' new nodes with stamp:',stamp neighbours.set(stamp,nodes) client.close(s) except: import traceback traceback.print_exc() print 'Failed to connect to server. Aborting communication.' # since the server didn't get our date (and is probably down!) # we really should clear our cache, to ensure the server gets a # full picture statistics.cache = {} watcher.cache = {} # then wait, to not waste resourses print 'Sleeping...' try: time.sleep(POLLING_INTERVAL) except KeyboardInterrupt: print 'Exiting.' return 0
import neighbours import time DO_GET_NEW_NODES = False if __name__=='__main__': try: # cache path override hack neighbours.CACHE_FILE = 'neighbours.cache' neighbours.load('neighbours.cache') if DO_GET_NEW_NODES: # check for node updates print 'Getting node list...' nodes = master.nodes.get_hostnames() print 'Retrieved',len(nodes),'nodes.' # are these nodes 'new'? for n in nodes: if n not in neighbours.nodes: # node list is different! # save it, make new timestamp print 'Node list is new. Updating local copy.' neighbours.set(int(time.time()), nodes) break except: print 'Failed to get node list. Going with cache.' try: server.loop(master.handler.handler) #yuck! except KeyboardInterrupt: print 'Exiting!'