def hop_count_handler(self, event): """Hop count handler Initialize only of the last hop count event is 4 clocks old. Otherwise update the hop count and resend the new value only if its larger than the previous hop count value. Arguments: event {HopCount} -- Hop count event instance """ # initialize if (self.clock - self.last_hop_count_clock) > 4: self.hop_count_initiator = False self.hop_distance = event.hops + 1 self.hop_count = event.hops + 1 self.messages.append( (self, HopCount(self.id, self.info_clock, self.hop_count))) else: # propagate value if self.hop_count < event.hops: self.hop_count = event.hops if not self.hop_count_initiator: self.messages.append((self, HopCount(self.id, self.info_clock, self.hop_count))) self.last_hop_count_clock = self.clock if self.verbose: print('Fish #{} counts hops {} from #{}'.format( self.id, event.hop_count, event.source_id))
def start_hop_count_handler(self, event): """Hop count start handler Always accept a new start event for a hop count Arguments: event {StartHopCount} -- Hop count start event """ self.last_hop_count_clock = self.clock self.hop_distance = 0 self.hop_count = 0 self.hop_count_initiator = True self.initial_hop_count_clock = self.clock self.messages.append( (self, HopCount(self.id, self.info_clock, self.hop_count))) if self.verbose: print('Fish #{} counts hops {} from #{}'.format( self.id, event.hop_count, event.source_id))