def on_contact(self, messages): for message in messages: bz2log("walktest.log", "in-contact", source_address=message.candidate.sock_addr, identifier=message.payload.identifier, **self._default_log())
def dispersy_take_step(self): now = time() addresses = [(candidate.lan_address, candidate.get_category(self, now), "-".join(self._get_merged_candidate_category(candidate, self, now))) for candidate in self._dispersy._candidates.itervalues() if candidate.in_community(self, now)] bz2log("walktest.log", "candidates", candidates=addresses, **self._default_log()) return super(WalktestCommunity, self).dispersy_take_step()
def on_puncture_request(self, meta, messages): for message in messages: bz2log("walktest.log", "in-puncture-request", source_address=message.candidate.sock_addr, lan_walker_address=message.payload.lan_walker_address, wan_walker_address=message.payload.wan_walker_address, identifier=message.payload.identifier, **self._default_log()) return meta.__origional_handle(messages)
def create_contact(self, destination, identifier): meta = self._meta_messages[u"contact"] message = meta.impl(distribution=(self.global_time,), destination=(destination,), payload=(identifier,)) self._dispersy.store_update_forward([message], False, False, True) bz2log("walktest.log", "out-contact", destination_address=destination.sock_addr, identifier=identifier, **self._default_log())
def on_puncture(self, meta, messages): for message in messages: bz2log("walktest.log", "in-puncture", member=message.authentication.member.mid, source_address=message.candidate.sock_addr, source_lan_address=message.payload.source_lan_address, source_wan_address=message.payload.source_wan_address, identifier=message.payload.identifier, **self._default_log()) return meta.__origional_handle(messages)
def _replacement__introduction_response_timeout(self, identifier): has_response, into_resp_candidate, punct_candidate, community, helper_candidate, req_timestamp = self._dispersy._walk_identifiers.get(identifier) if not has_response and self == community: bz2log("walktest.log", "timeout", identifier=identifier, intermediary_sock_address=helper_candidate.sock_addr, intermediary_lan_address=helper_candidate.lan_address, intermediary_wan_address=helper_candidate.wan_address, **self._default_log()) return self._origional__introduction_response_timeout(identifier)
def impl_introduction_response(self, meta, *args, **kargs): message = meta.__origional_impl(*args, **kargs) assert len(message.destination.candidates) == 1 bz2log("walktest.log", "out-introduction-response", destination_address=message.destination.candidates[0].sock_addr, lan_introduction_address=message.payload.lan_introduction_address, wan_introduction_address=message.payload.wan_introduction_address, identifier=message.payload.identifier, **self._default_log()) return message
def impl_puncture_request(self, meta, *args, **kargs): message = meta.__origional_impl(*args, **kargs) assert len(message.destination.candidates) == 1 bz2log("walktest.log", "out-puncture-request", destination=message.destination.candidates[0].sock_addr, lan_walker_address=message.payload.lan_walker_address, wan_walker_address=message.payload.wan_walker_address, identifier=message.payload.identifier, **self._default_log()) return message
def impl_puncture(self, meta, *args, **kargs): message = meta.__origional_impl(*args, **kargs) assert len(message.destination.candidates) == 1 bz2log("walktest.log", "out-puncture", destination_address=message.destination.candidates[0].sock_addr, source_lan_address=message.payload.source_lan_address, source_wan_address=message.payload.source_wan_address, identifier=message.payload.identifier, **self._default_log()) return message
def impl_introduction_request(self, meta, *args, **kargs): message = meta.__origional_impl(*args, **kargs) bz2log("walktest.log", "out-introduction-request", destination_address=message.destination.candidates[0].sock_addr, destination_lan_address=message.destination.candidates[0].lan_address, destination_wan_address=message.destination.candidates[0].wan_address, advice=message.payload.advice, identifier=message.payload.identifier, **self._default_log()) return message
def _watchdog(self): while True: try: yield 60.0 # flush changes to disk every 1 minutes self._database.commit() except GeneratorExit: bz2log("effort.log", "unload") if __debug__: dprint("shutdown") self._database.commit() break
def _replacement__introduction_response_timeout(self, identifier): has_response, into_resp_candidate, punct_candidate, community, helper_candidate, req_timestamp = self._dispersy._walk_identifiers.get( identifier) if not has_response and self == community: bz2log("walktest.log", "timeout", identifier=identifier, intermediary_sock_address=helper_candidate.sock_addr, intermediary_lan_address=helper_candidate.lan_address, intermediary_wan_address=helper_candidate.wan_address, **self._default_log()) return self._origional__introduction_response_timeout(identifier)
def create_contact(self, destination, identifier): meta = self._meta_messages[u"contact"] message = meta.impl(distribution=(self.global_time, ), destination=(destination, ), payload=(identifier, )) self._dispersy.store_update_forward([message], False, False, True) bz2log("walktest.log", "out-contact", destination_address=destination.sock_addr, identifier=identifier, **self._default_log())
def on_introduction_request(self, meta, messages): for message in messages: bz2log("walktest.log", "in-introduction-request", source_address=message.candidate.sock_addr, mid=message.authentication.member.mid, destination_address=message.payload.destination_address, source_lan_address=message.payload.source_lan_address, source_wan_address=message.payload.source_wan_address, advice=message.payload.advice, identifier=message.payload.identifier, **self._default_log()) return meta.__origional_handle(messages)
def impl_introduction_request(self, meta, *args, **kargs): message = meta.__origional_impl(*args, **kargs) bz2log("walktest.log", "out-introduction-request", destination_address=message.destination.candidates[0]. sock_addr, destination_lan_address=message.destination.candidates[0]. lan_address, destination_wan_address=message.destination.candidates[0]. wan_address, advice=message.payload.advice, identifier=message.payload.identifier, **self._default_log()) return message
def impl_introduction_response(self, meta, *args, **kargs): message = meta.__origional_impl(*args, **kargs) assert len(message.destination.candidates) == 1 bz2log("walktest.log", "out-introduction-response", destination_address=message.destination.candidates[0]. sock_addr, lan_introduction_address=message.payload. lan_introduction_address, wan_introduction_address=message.payload. wan_introduction_address, identifier=message.payload.identifier, **self._default_log()) return message
def dispersy_take_step(self): now = time() addresses = [ (candidate.lan_address, candidate.get_category(self, now), "-".join( self._get_merged_candidate_category(candidate, self, now))) for candidate in self._dispersy._candidates.itervalues() if candidate.in_community(self, now) ] bz2log("walktest.log", "candidates", candidates=addresses, **self._default_log()) return super(WalktestCommunity, self).dispersy_take_step()
def __init__(self, master, class_): # original walker callbacks (will be set during super(...).__init__) self._original_on_introduction_request = None self._original_on_introduction_response = None super(EffortCommunity, self).__init__(master) # storage self._database = EffortDatabase.get_instance(self._dispersy.working_directory) # cache self._histories = dict() # periodic tasks self._pending_callbacks.append(self._dispersy.callback.register(self._watchdog)) # log bz2log("effort.log", "load", my_member=self._my_member.mid, class_=class_, cycle_size=CYCLE_SIZE)
def on_introduction_response(self, meta, messages): for message in messages: bz2log("walktest.log", "in-introduction-response", member=message.authentication.member.public_key, source_address=message.candidate.sock_addr, destination_address=message.payload.destination_address, source_lan_address=message.payload.source_lan_address, source_wan_address=message.payload.source_wan_address, lan_introduction_address=message.payload.lan_introduction_address, wan_introduction_address=message.payload.wan_introduction_address, identifier=message.payload.identifier, **self._default_log()) # schedule the 'contact' message after one second. this should give time for the # puncture to complete # self._dispersy.callback.register(self.create_contact, (message.candidate, message.payload.identifier), delay=1.0) return meta.__origional_handle(messages)
def __init__(self, *args, **kargs): super(WalktestCommunity, self).__init__(*args, **kargs) if __debug__: dprint("cid: ", self.cid.encode("HEX"), force=1) dprint("mid: ", self.my_member.mid.encode("HEX"), force=1) try: hostname = open("/etc/hostname", "r").readline().strip() except: hostname = "unknown" bz2log("walktest.log", "load", mid=self.my_member.mid, hostname=hostname, **self._default_log()) # redirect introduction-response timeout self._origional__introduction_response_timeout = self._dispersy.introduction_response_timeout self._dispersy.introduction_response_timeout = self._replacement__introduction_response_timeout
def on_introduction_response(self, meta, messages): for message in messages: bz2log("walktest.log", "in-introduction-response", member=message.authentication.member.public_key, source_address=message.candidate.sock_addr, destination_address=message.payload.destination_address, source_lan_address=message.payload.source_lan_address, source_wan_address=message.payload.source_wan_address, lan_introduction_address=message.payload. lan_introduction_address, wan_introduction_address=message.payload. wan_introduction_address, identifier=message.payload.identifier, **self._default_log()) # schedule the 'contact' message after one second. this should give time for the # puncture to complete # self._dispersy.callback.register(self.create_contact, (message.candidate, message.payload.identifier), delay=1.0) return meta.__origional_handle(messages)
def allow_signature_request(self, message): """ A dispersy-signature-request has been received. Return None or a Message.Implementation. """ assert message.name == u"effort-record" assert not message.authentication.is_signed if __debug__: dprint(message) _, first_member = message.authentication.signed_members[0] _, second_member = message.authentication.signed_members[1] global_time = message.distribution.global_time if message.distribution.global_time <= self.global_time else self.claim_global_time() if first_member == self._my_member: local_history = self._get_or_create_history(second_member, time()) first_timestamp = local_history.origin second_timestamp = message.payload.second_timestamp else: assert second_member == self._my_member local_history = self._get_or_create_history(first_member, time()) first_timestamp = message.payload.first_timestamp second_timestamp = local_history.origin if __debug__: dprint("time diff:", abs(first_timestamp - second_timestamp), "; bits diff:", local_history.long ^ message.payload.history.long) # TODO shift history and origin for a match history = EffortHistory(local_history.long & message.payload.history.long, local_history.size, local_history.origin) bz2log("effort.log", "diff", local=bin(history.long), remote=bin(message.payload.history.long), propose=bin(history.long), time_diff=int(abs(first_timestamp - second_timestamp))) # return the modified effort-record we propose meta = self.get_meta_message(u"effort-record") return meta.impl(authentication=([first_member, second_member],), distribution=(global_time,), payload=(first_timestamp, second_timestamp, history))
def log(self, _message, **kargs): bz2log("walktest.log", _message, **kargs)
def unload_community(self): bz2log("walktest.log", "unload", **self._default_log()) return super(WalktestCommunity, self).unload_community()
def scenario_end(self): bz2log("walktest.log", "scenario-end") close("walktest.log") return "END"
def on_effort_record(self, messages): if __debug__: dprint("storing ", len(messages), " effort records") for message in messages: bz2log("effort.log", "effort-record", global_time=message.distribution.global_time, first_member=message.authentication.members[0].mid, second_member=message.authentication.members[1].mid, first_timestamp=int(message.payload.first_timestamp), second_timestamp=int(message.payload.second_timestamp), bits=message.payload.history.long) self._database.executemany(u"INSERT OR REPLACE INTO record (community, global_time, first_member, second_member, first_timestamp, second_timestamp, effort) VALUES (?, ?, ?, ?, ?, ?, ?)", ((self._database_id, message.distribution.global_time, message.authentication.members[0].database_id, message.authentication.members[1].database_id, int(message.payload.first_timestamp), int(message.payload.second_timestamp), buffer(message.payload.history.bytes)) for message in messages))