예제 #1
0
 def broadcast_costs(self):
     costs = {node_addr_key: node_info['cost'] for node_addr_key, node_info \
                                           in self.node_dict.iteritems()}
     packet  = {'cmd': RTUPDATE}
     for neighbor_key, neighbor in self.get_neighbors().iteritems():
         updated_costs = copy.deepcopy(costs)
         for dest_addr, cost in costs.iteritems():
             if dest_addr not in [self.me_key, neighbor_key] and            \
                 self.node_dict[dest_addr]['link'] == neighbor_key:
                     updated_costs[dest_addr] = INF # poisoned the cost!
         packet['payload'] = {'costs': updated_costs,
                              'neighbor': {'direct_dist':
                                             neighbor['direct_dist']}}
         send_packet = json.dumps(packet)
         self.sock.sendto(send_packet, key_to_addr(neighbor_key))
예제 #2
0
 def node_generator(self, cost, is_neighbor,
                                 costs={}, direct_dist=INF, addr_key=""):
     node = self.init_node()
     node["cost"] = cost
     node["is_neighbor"] = is_neighbor
     node["costs"] = costs if costs else defaultdict(lambda: INF)
     node["direct_dist"] = direct_dist
     if is_neighbor:
         node['link'] = addr_key
         monitor = ResetTimer(
                     interval=self.timeout,
                     func_ptr=self.link_down,
                     args=list(key_to_addr(addr_key)))
         monitor.start()
         node['watch_dog'] = monitor
     return node