From 4eee5236f66f1ac3f3ae559e5c6db30633088996 Mon Sep 17 00:00:00 2001 From: Sonny Sasaka Date: Mon, 25 Jan 2010 03:12:07 +0800 Subject: [PATCH] added notify children --- p2prfd.py | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 65 insertions(+), 5 deletions(-) diff --git a/p2prfd.py b/p2prfd.py index 6ae9187..74b7697 100644 --- a/p2prfd.py +++ b/p2prfd.py @@ -24,7 +24,8 @@ class P2prfdProxy(twisted.web.resource.Resource): ip_addr = {} child_left = {} child_right = {} - update_interval = 10 + update_interval = 49 + root_update_interval = 5 def generate_id(self): sh = hashlib.sha1() sh.update(str(random.random())) @@ -72,6 +73,43 @@ class P2prfdProxy(twisted.web.resource.Resource): dht_network = self.dht_network root_key = 'root:' + feed_url dht_network.get_value(root_key, root_gotten) + pass + def notify_node(self, node, feed_url, content): + http_con = httplib.HTTPConnection(node[0], node[1]) + queries = {} + queries['action'] = 'notify_update' + queries['content'] = content + queries['feed_url'] = feed_url + body = urllib.urlencode(queries) + headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"} + http_con.request("POST", '/', body, headers) + http_con.close() + pass + def notify_children(self, feed_url): + print 'notify children %s' % (feed_url) + child_left = None + child_right = None + if feed_url in self.child_left: child_left = self.child_left[feed_url] + if feed_url in self.child_right: child_right = self.child_right[feed_url] + me = (self.ip_addr[feed_url], self.p2prfd_http_port) + content = self.cache_data[feed_url] + if child_left: + try: + self.notify_node(child_left, feed_url, content) + except: + print 'left child %s error' % (repr(child_left)) + child_left = None + del self.child_left[feed_url] + pass + if child_right: + try: + self.notify_node(child_right, feed_url, content) + except: + print 'right child %s error' % (repr(child_right)) + child_right = None + del self.child_right[feed_url] + pass + pass def update_feed(self, feed_url): if feed_url in self.parent_of: parent = self.parent_of[feed_url] @@ -87,6 +125,7 @@ class P2prfdProxy(twisted.web.resource.Resource): data = self.get_data_from_server(feed_url) self.cache_data[feed_url] = data pass + self.notify_children(feed_url) pass def ping_node(self, node): http_con = httplib.HTTPConnection(node[0], node[1]) @@ -121,14 +160,20 @@ class P2prfdProxy(twisted.web.resource.Resource): print 'left child is %s' % (repr(child_left)) print 'right child is %s' % (repr(child_right)) pass + def root_update(self): + print 'root updating' + for feed_url in self.cache_data: + if not (feed_url in self.parent_of): + print 'updating %s' % (feed_url) + self.update_feed(feed_url) + twisted.internet.reactor.callLater(self.root_update_interval, self.root_update) def update(self): - print 'updating' + print 'non-root updating' for feed_url in self.cache_data: print 'updating %s' % (feed_url) self.update_feed(feed_url) self.check_children(feed_url) twisted.internet.reactor.callLater(self.update_interval, self.update) - #self.update_timer.reset(self.update_interval) def get_data_from_parent(self, parent, key): #return 'url %s gotten from %s' % (key, repr(parent)) """get data from parent""" @@ -169,7 +214,7 @@ class P2prfdProxy(twisted.web.resource.Resource): parent = result['parent'] return parent def insert_to_parent(self, parent, me, feed_url): - print 'insert_to_parent' + print 'insert_to_parent %s' % (repr(parent)) http_con = httplib.HTTPConnection(parent[0], parent[1]) queries = {} queries['action'] = 'insert_child' @@ -203,10 +248,24 @@ class P2prfdResource(twisted.web.resource.Resource): resp_str = response.read() result = eval(resp_str) return result['info'] + def render_POST(self, request): + queries = request.args + action = '' + if 'action' in queries: action = queries['action'][0] + result = {} + result['status'] = False + result['data'] = 'no data found' + if action == 'notify_update': + feed_url = queries['feed_url'][0] + print 'received update for %s' % feed_url + content = queries['content'][0] + self.rsrc.cache_data[feed_url] = content + result['status'] = True + return repr(result) def render_GET(self, request): parsed = urlparse.urlparse(request.uri) queries = cgi.parse_qs(parsed.query) - print 'queries =', queries + print 'received queries =', queries action = '' if 'action' in queries: action = queries['action'][0] result = {} @@ -316,6 +375,7 @@ p2prfd_rsrc = P2prfdResource() p2prfd_rsrc.rsrc = rsrc p2prfd_site = twisted.web.server.Site(p2prfd_rsrc) update_timer = twisted.internet.reactor.callLater(rsrc.update_interval, rsrc.update) +root_update_timer = twisted.internet.reactor.callLater(rsrc.root_update_interval, rsrc.root_update) rsrc.update_timer = update_timer twisted.internet.reactor.listenTCP(int(sys.argv[2]), site) twisted.internet.reactor.listenTCP(rsrc.p2prfd_http_port, p2prfd_site) -- 2.11.4.GIT