Ejemplo n.º 1
0
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """
        if self.POISON_MODE:
            for p_route in self.routing_data.get_poisoned_route_map():
                self.send(basics.RoutePacket(p_route, INFINITY), flood=True)
        for host in self.routing_data.get_host_route_map():
            route = self.routing_data.get_route(host)
            if route:
                time_diff = api.current_time() - route.get_time()
                if route.get_time() == -1 or time_diff <= self.ROUTE_TIMEOUT:
                    if self.POISON_MODE:
                        self.send(basics.RoutePacket(host, INFINITY),
                                  route.get_port())
                    self.send(basics.RoutePacket(host, route.get_distance()),
                              route.get_port(),
                              flood=True)
                else:
                    if self.POISON_MODE:
                        self.send(basics.RoutePacket(host, INFINITY),
                                  route.get_port())
                        self.routing_data.set_poisoned_route(host, route)
                    self.routing_data.null_route(host)
Ejemplo n.º 2
0
    def handle_timer(self):
        """
    Called periodically.

    When called, your router should send tables to neighbors.  It also might
    not be a bad place to check for whether any entries have expired.
    """

        #self.log("handle_timer " + str(len(self.routesToDest)))
        deleteDests = set()
        for dest in self.routesToDest:
            recievedTime = self.routesToDest[dest][2]

            if api.current_time(
            ) - recievedTime <= TimeDiff or self.routesToDest[dest][3]:
                latency = self.routesToDest[dest][1]
                packet = basics.RoutePacket(dest, latency)
                port = self.routesToDest[dest][0]
                self.send(packet, port, True)
            else:
                if dest in self.hosts:
                    packet = basics.RoutePacket(dest, self.hosts[dest][1])
                    self.send(packet, self.hosts[dest][0], True)
                    self.routesToDest[dest] = self.hosts[dest]
                else:
                    deleteDests.add(dest)
                    self.sendPoison(dest)

        for dest in deleteDests:
            del self.routesToDest[dest]
Ejemplo n.º 3
0
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """
        expired = []
        for entity, node in self.table.iteritems():
            if entity in self.connection_entities:
                new_port = self.connection_entities[entity]
                if new_port in self.connection_latencies:
                    new_latency = self.connection_latencies[new_port]
                    if new_latency < node.latency:
                        self.table[entity] = self.NetworkNode(
                            new_latency, entity, new_port)
            node.timeout += self.DEFAULT_TIMER_INTERVAL
            if node.predecessor_port in self.connection_latencies:
                if node.timeout <= self.ROUTE_TIMEOUT and node.latency < INFINITY and self.connection_latencies[
                        node.predecessor_port] < INFINITY:
                    for port in self.connection_latencies.keys():
                        self.send(basics.RoutePacket(entity, node.latency),
                                  port=port)
                    continue
            if self.POISON_MODE:
                for port in self.connection_latencies.keys():
                    self.send(basics.RoutePacket(entity, INFINITY), port=port)
            expired.append(entity)
        for entity in expired:
            del self.table[entity]
Ejemplo n.º 4
0
    def handle_link_down(self, port):
        """
        Called by the framework when a link attached to this Entity does down.

        The port number used by the link is passed in.

        """
        #poison the route then wait for replacement route via RoutePacket
        if port in self.port_table.keys():
            del self.port_table[port]

        to_remove = []
        if self.POISON_MODE:
            for dst in self.distance_table.keys():
                if self.distance_table[dst][0] == port:
                    #send poison with INFINITY cost, delete local table[dst]
                    p = basics.RoutePacket(dst, INFINITY)
                    self.send(p, flood=True)
                    to_remove.append(dst)
            for dst in to_remove:
                del self.distance_table[dst]

        else:
            to_remove = []
            for dst in self.distance_table.keys():
                if self.distance_table[dst][0] == port:
                    to_remove.append(dst)

            for dst in to_remove:
                del self.distance_table[dst]
            for dst in self.distance_table.keys():
                p = basics.RoutePacket(dst, self.distance_table[dst][1])
                self.send(p, flood=True)
Ejemplo n.º 5
0
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """
        #will make expire_routes pass.

        #If time has expired, you remove the entry from the routing table.
        for key, val in self.routing_table.items():
            if val[2] != None:
                if api.current_time() - val[2] > self.ROUTE_TIMEOUT:
                    if self.POISON_MODE:
                        self.routing_table[key][1] = INFINITY
                    else:
                        del self.routing_table[key]

        for port in self.link_up:
            for dest, val_list in self.routing_table.items():
                if self.POISON_MODE == True:
                    latency = val_list[1]
                    if val_list[0] == port:
                        latency = INFINITY
                    new_packet = basics.RoutePacket(dest, latency)
                    self.send(new_packet, port, flood=False)
                else:
                    latency = val_list[1]
                    if val_list[0] != port:
                        new_packet = basics.RoutePacket(dest, latency)
                        self.send(new_packet, port, flood=False)
Ejemplo n.º 6
0
  def handle_rx (self, packet, port):
    """
    Called by the framework when this Entity receives a packet.

    packet is a Packet (or subclass).
    port is the port number it arrived on.

    You definitely want to fill this in.
    """
    self.log("RX %s on %s (%s)", packet, port, api.current_time())
    if isinstance(packet, basics.RoutePacket):
      new_latency = packet.latency + self.weights[port]
      
      if new_latency >= INFINITY:
        if packet.destination in self.DP and self.DP[packet.destination][0] == port:
          del self.DP[packet.destination]
          if self.POISON_MODE:
            self.send(basics.RoutePacket(packet.destination, INFINITY), port, flood=True)

      elif packet.destination not in self.DP or self.DP[packet.destination][1] >= new_latency or self.DP[packet.destination][0] == port:
        self.DP[packet.destination] = (port, new_latency)
        self.timer[packet.destination] = api.current_time()
        self.send(basics.RoutePacket(packet.destination, new_latency), port, flood=True)  

    elif isinstance(packet, basics.HostDiscoveryPacket):
      self.DP[packet.src] = (port, self.weights[port])
      self.timer[packet.src] = api.current_time()

    elif packet.dst in self.DP:
      self.send(packet, self.DP[packet.dst][0])
Ejemplo n.º 7
0
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """
        # remove expired routes
        remove_dests = []

        for dest, info in self.router_table.items():
            time = api.current_time() - info[2]
            if time >= self.ROUTE_TIMEOUT:
                remove_dests.append(dest)
        for dest in remove_dests:
            # remove routes
            del self.router_table[dest]
            if self.POISON_MODE:
                self.poison.append(dest)
            self.host_route_default(dest, api.current_time())

        # send to neighbors the router_table
        for port in self.port_latency.keys():
            for dest, info in self.router_table.items():
                if (info[0] != port):
                    self.send(basics.RoutePacket(dest, info[1]), port)
                elif (info[0] == port and self.POISON_MODE):
                    self.send(basics.RoutePacket(dest, INFINITY), port)
            if self.POISON_MODE:
                for dest in self.poison:
                    if dest not in self.host_port:
                        self.send(basics.RoutePacket(dest, INFINITY), port)
        self.poison = []
Ejemplo n.º 8
0
    def handle_timer(self):
        """
    Called periodically.

    When called, your router should send tables to neighbors.  It also might
    not be a bad place to check for whether any entries have expired.
    """
        to_delete = []
        for dest in self.routeTableLatency:
            if api.current_time() - self.routeTableTime[dest] <= 15:
                routePacket = basics.RoutePacket(dest,
                                                 self.routeTableLatency[dest])
                self.send(routePacket, self.routeTablePort[dest], True)
            else:
                if dest in self.neighborHosts and self.neighborHosts[dest][
                        1] == self.routeTablePort[dest]:
                    self.routeTableLatency[dest] = self.neighborHosts[dest][0]
                    self.routeTablePort[dest] = self.neighborHosts[dest][1]
                    self.routeTableTime[dest] = api.current_time()
                    routePacket = basics.RoutePacket(
                        dest, self.routeTableLatency[dest])
                    self.send(routePacket, self.routeTablePort[dest], True)
                else:
                    to_delete.append(dest)
                    routePacket = basics.RoutePacket(dest, INFINITY)
                    self.send(routePacket, self.routeTablePort[dest], True)

        self.delete(to_delete)
Ejemplo n.º 9
0
 def handle_timer(self):
     for i in self.routeTable.keys():
         if (((api.current_time() - self.routeTable[i][2]) <=
              self.ROUTE_TIMEOUT)
                 or (self.routeTable[i][2]
                     == -1)):  #Checks if a route is expired or not
             self.send(
                 basics.RoutePacket(i, self.routeTable[i][0]),
                 flood=True)  #updates neighbours with distance vector table
             if self.POISON_MODE == True:
                 self.send(basics.RoutePacket(i, INFINITY),
                           self.routeTable[i][1],
                           flood=False)
         else:
             if self.POISON_MODE == True:
                 self.p_reverse[i] = self.routeTable[
                     i]  #populates poisoned reverse dictionary
                 self.send(basics.RoutePacket(i, INFINITY),
                           self.routeTable[i][1],
                           flood=False)
             del self.routeTable[
                 i]  #deletes route from dictionary if it is expired.
     if self.POISON_MODE == True:
         for i in self.p_reverse.keys():
             self.send(basics.RoutePacket(i, INFINITY),
                       port=None,
                       flood=True)
Ejemplo n.º 10
0
 def valid_packet(self, packet, port):
     new_dist = packet.latency + self.port_to_latency[port];
     if self.POISON_MODE:
         if packet.destination in self.poison_state:
             self.poison_state.pop(packet.destination)
         self.send(basics.RoutePacket(packet.destination, INFINITY), port)
     if packet.destination not in self.host_to_route.keys() \
             or self.host_to_route[packet.destination] is None \
             or self.host_to_route[packet.destination][0] > new_dist:
         self.host_to_route[packet.destination] = \
             [self.port_to_latency[port] + packet.latency, port, api.current_time()]
         self.send(basics.RoutePacket(packet.destination, new_dist), port,
                   flood=True)
     else:
         if new_dist == self.host_to_route[packet.destination][0]:
             if self.host_to_route[packet.destination][2] < api.current_time():
                 self.host_to_route[packet.destination] = \
                     [new_dist, port, api.current_time()]
                 self.send(basics.RoutePacket(
                     packet.destination, new_dist), port, flood=True)
         if self.host_to_route[packet.destination][1] == port:  # ???
             if new_dist > self.host_to_route[packet.destination][0]:
                 self.host_to_route[packet.destination][0] = new_dist
                 self.send(basics.RoutePacket(
                     packet.destination, new_dist), port, flood=True)
             self.host_to_route[packet.destination][2] = api.current_time()
Ejemplo n.º 11
0
  def handle_timer (self):
    """
    Called periodically.

    When called, your router should send tables to neighbors.  It also might
    not be a bad place to check for whether any entries have expired.
    """
    for x in self.DV.keys():
      #print x, self.DV[x][2], self.DV[x][0], self
      if (api.current_time() - self.DV[x][1] ) <= 15 or self.DV[x][1] == -1:
        route = basics.RoutePacket(x, self.DV[x][0])
        self.send(route, self.DV[x][2], flood=True)
        if self.POISON_MODE:
          routePoison = basics.RoutePacket(x, INFINITY)
          self.send(routePoison, self.DV[x][2], flood=False)
      else:
        if self.POISON_MODE:
          routePoison = basics.RoutePacket(x, INFINITY)
          self.send(routePoison, self.DV[x][2], flood=False)
          self.poisoned[x] = self.DV[x]
        del self.DV[x]
    if self.POISON_MODE:
      for x in self.poisoned.keys():
        poison = basics.RoutePacket(x, INFINITY)
        self.send(poison, port=None, flood=True)
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """

        # check through the routes and see if 15 seconds have passed -> remove route
        for dest in self.destination_map.keys():
            if dest == self:
                self.destination_map.get(dest)[2] = api.current_time()

            if self.destination_map.get(dest)[3] == True:
                self.destination_map.get(dest)[2] = api.current_time()

            if api.current_time() - self.destination_map.get(
                    dest)[2] >= self.ROUTE_TIMEOUT:
                del self.destination_map[dest]

        #send updated routes to neighbors/ poison stuff
        for port in self.port_latency:
            for dest in self.destination_map:
                #if dest is itself, send with latency 0
                if self.destination_map.get(dest)[0] == 0:
                    latency = 0
                    destination = dest
                    rPacket = basics.RoutePacket(destination, latency)
                    self.send(rPacket, port)
                    continue

                #poison mode
                if self.POISON_MODE:
                    #same port that the route uses, advertise dist of INFINITY
                    if port == self.destination_map.get(dest)[1]:
                        destination = dest
                        latency = INFINITY
                        rPacket = basics.RoutePacket(destination, latency)
                        self.send(rPacket, port)
                    #different port than the one route uses
                    elif port != self.destination_map.get(dest)[1]:
                        latency = self.destination_map.get(dest)[0]
                        destination = dest
                        rPacket = basics.RoutePacket(destination, latency)
                        self.send(rPacket, port)

                #not poison mode
                else:
                    #same port that the route uses, don't advertise route
                    if port == dest[1]:
                        continue
                    #different port than the one route uses
                    elif port != dest[1]:
                        latency = self.destination_map.get(dest)[0]
                        destination = dest
                        rPacket = basics.RoutePacket(destination, latency)
                        self.send(rPacket, port)
Ejemplo n.º 13
0
    def handle_rx(self, packet, port):
        """
        Called by the framework when this Entity receives a packet.

        packet is a Packet (or subclass).
        port is the port number it arrived on.

        You definitely want to fill this in.

        """
        #self.log("RX %s on %s (%s)", packet, port, api.current_time())
        if isinstance(packet, basics.RoutePacket):
            #if host is not in table
            if packet.destination not in self.table:
                #add host to table with (link latency + this latency) and current time
                newLatency = packet.latency + self.portLatency[port]
                self.table[packet.destination] = (newLatency, port, api.current_time(), port)
                #flood new host to other switches/routers
                self.send(basics.RoutePacket(packet.destination, newLatency), port, flood=True)
            #else try to update vector
            else:
                hostVector = self.table[packet.destination]
                originalLatency = hostVector[0]
                newLatency = packet.latency + self.portLatency[port]
                if self.directRoute.has_key(packet.destination) and self.directRoute[packet.destination][0] < originalLatency:
                    #update to direct route if possible
                    self.table[packet.destination] = self.directRoute[packet.destination]
                elif hostVector[1] == port:
                    #update time
                    if newLatency > originalLatency:
                        #update latency
                        self.table[packet.destination] = (newLatency, hostVector[1], api.current_time(), hostVector[3])
                        self.send(basics.RoutePacket(packet.destination, newLatency), port, flood=True)
                    else:
                        self.table[packet.destination] = (originalLatency, hostVector[1], api.current_time(), hostVector[3])
                elif newLatency <= originalLatency:
                    #update vector with lower latency
                    self.table[packet.destination] = (newLatency, port, api.current_time(), originalLatency)

        elif isinstance(packet, basics.HostDiscoveryPacket):
            #add host to table
            hostVector = (self.portLatency[port], port, None, port)
            self.table[packet.src] = hostVector
            self.directRoute[packet.src] = hostVector
            #send host to other links
            self.send(basics.RoutePacket(packet.src, hostVector[0]), port, flood=True)

        else:
            #forward regular packet
            isHairpin = packet.src != packet.dst #need to better implement
            isIntable = packet.dst in self.table
            if isinstance(packet.dst, api.HostEntity) and isHairpin and isIntable:
                hostPort = self.table[packet.dst][1]
                hostLatency = self.table[packet.dst][0]
                if not hostLatency >= INFINITY:
                    self.send(packet, hostPort)
Ejemplo n.º 14
0
  def handle_rx (self, packet, port):
    """
    Called by the framework when this Entity receives a packet.

    packet is a Packet (or subclass).
    port is the port number it arrived on.

    You definitely want to fill this in.
    """
    
    #self.log("RX %s on %s (%s)", packet, port, api.current_time())
    if isinstance(packet, basics.RoutePacket):

      dest = packet.destination
      newLatency = self.neighbours[port] + packet.latency

      oldLatency = INFINITY
      if dest in self.routesToDest:
        oldLatency = self.routesToDest[dest][1]


      if dest not in self.routesToDest or newLatency <= oldLatency or (dest in self.routesToDest and self.routesToDest[dest][0] == port):
        self.routesToDest[dest] = (port, newLatency, api.current_time(), False)
        
        
      if dest in self.hosts and self.hosts[dest][1] <= self.routesToDest[dest][1]:
        self.routesToDest[dest] = self.hosts[dest]
      
      if oldLatency != self.routesToDest[dest][1]:
        newPacket = basics.RoutePacket(dest, self.routesToDest[dest][1])
        self.send(newPacket, port, True)

    elif isinstance(packet, basics.HostDiscoveryPacket):
      self.routesToDest[packet.src] = (port, self.neighbours[port], api.current_time(), True)
      self.send(basics.RoutePacket(packet.src, self.neighbours[port]), port, True)
      self.hosts[packet.src] = (port, self.neighbours[port], api.current_time(), True)
    else:
      # Totally wrong behavior for the sake of demonstration only: send
      # the packet back to where it came from!

      
      reciever = packet.dst
      if reciever in self.routesToDest:
        latency = self.routesToDest[reciever][1]

        if latency < INFINITY and self.routesToDest[reciever][0] != port:
          self.send(packet, self.routesToDest[reciever][0], False)
          return

      if reciever in self.hosts:
        self.routesToDest[reciever] = self.hosts[reciever]
        latency = self.routesToDest[reciever][1]

        if latency < INFINITY and self.routesToDest[reciever][0] != port:
          self.send(packet, self.hosts[reciever][0], False)
Ejemplo n.º 15
0
    def calc_send(self, cost, port, host):

        self.routing_tb[host] = self.b_ford(cost, port, host)

        if cost == INFINITY:
            if self.POISON_MODE:
                self.routing_tb[host] = [INFINITY, port, api.current_time()]
                self.send(basics.RoutePacket(host, cost), port, flood=True)
            elif host in self.routing_tb:
                del self.routing_tb[host]
        else:
            self.routing_tb[host] = [cost, port, api.current_time()]
            self.send(basics.RoutePacket(host, cost), port, flood=True)
Ejemplo n.º 16
0
 def send_all_vectors_to(self, port):
     """
 Sends all current distance vector information to the specified port
 """
     for destination in self.distanceVectors:
         minDistance = self.min_distance_to(destination)
         # print("Attempting to send destination: " + str(destination) + "to port: " + str(port))
         if (self.POISON_MODE or not self.is_infinity(minDistance)):
             if self.passes_split_horizon(destination, port):
                 # print("Sent!")
                 self.send(basics.RoutePacket(destination, minDistance),
                           port)
             elif self.POISON_MODE:
                 self.send(basics.RoutePacket(destination, INFINITY), port)
Ejemplo n.º 17
0
    def handle_link_up(self, port, latency):
        """
        Called by the framework when a link attached to this Entity goes up.

        The port attached to the link and the link latency are passed
        in.

        """
        self.port_latency[port] = latency
        for dest, info in self.router_table.items():
            if (info[0] != port):
                self.send(basics.RoutePacket(dest, info[1]), port)
            elif (info[0] == port and self.POISON_MODE):
                self.send(basics.RoutePacket(dest, INFINITY), port)
Ejemplo n.º 18
0
    def handle_link_up(self, port, latency):
        """
        Called by the framework when a link attached to this Entity goes up.

        The port attached to the link and the link latency are passed
        in.

        """
        self.link[port] = latency
        self.port_dst_lookup[port] = []
        for dst in self.dst_latency_lookup:
            pack = basics.RoutePacket(dst, self.dst_latency_lookup[dst])
            self.send(pack, port)
        pack = basics.RoutePacket(self, 0)
        self.send(pack, port)
Ejemplo n.º 19
0
    def handle_link_down(self, port):
        """
        Called by the framework when a link attached to this Entity does down.

        The port number used by the link is passed in.

        """
        del self.neighbors[port]
        del self.ports_latency[port]
        for dst in self.paths.keys():
            if self.paths[dst][0] == port:
                del self.paths[dst]
                if not self.POISON_MODE:
                    del self.dv[dst]
                else:
                    self.dv[dst] = INFINITY
        for dst in self.direct_paths.keys():
            if self.direct_paths[dst] == port:
                del self.direct_paths[dst]
                if not self.POISON_MODE:
                    del self.dv[dst]
                else:
                    self.dv[dst] = INFINITY
        for neighbor in self.neighbors.keys():
            for dst in self.dv.keys():
                if self.paths.has_key(dst):
                    if self.paths[dst][0] == neighbor:
                        continue
                self.send(basics.RoutePacket(dst, self.dv[dst]), neighbor)
Ejemplo n.º 20
0
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """
        for route in self.routes:
            if (api.current_time() -
                    self.route_time[route]) > self.ROUTE_TIMEOUT:
                self.log("%s (%s)",
                         api.current_time() - self.route_time[route],
                         api.current_time())
                self.delete_route(route)
                self.update_state(route[0])

        for destination in self.dst_port_lookup:
            self.update_neighbors(destination,
                                  self.dst_port_lookup[destination],
                                  self.dst_latency_lookup[destination])
        for port in self.link:
            pack = basics.RoutePacket(self, 0)
            self.send(pack, port)
Ejemplo n.º 21
0
    def handle_link_down(self, port):
        """
    Called by the framework when a link attached to this Entity does down.

    The port number used by the link is passed in.
    """
        # self.log(" %s (%s)", port, api.current_time())
        to_delete = []

        del self.portLatency[port]

        for d in self.neighborHosts.keys():
            if self.neighborHosts[d][1] == port:
                del self.neighborHosts[d]

        for d in self.routeTablePort:
            if self.routeTablePort[d] == port:
                if d in self.neighborHosts:
                    self.routeTableLatency[d] = self.neighborHosts[d][0]
                    self.routeTablePort[d] = self.neighborHosts[d][1]
                    self.routeTableTime[d] = api.current_time()
                    self.send(basics.RoutePacket(d, self.routeTableLatency[d]),
                              port, True)
                else:
                    to_delete.append(d)
                    self.sendPoison(port, d)
        self.delete(to_delete)
Ejemplo n.º 22
0
 def handle_link_up(self, port, latency):
     self.neighbours[port] = latency  #populates neighbours dictionary
     for i in self.routeTable.keys():
         self.send(basics.RoutePacket(i, self.routeTable[i][0]),
                   port,
                   flood=False)
     """
Ejemplo n.º 23
0
  def handle_link_down (self, port):
    """
    Called by the framework when a link attached to this Entity does down.

    The port number used by the link is passed in.
    """
    for dest in self.hosts.keys():
      currPort = self.hosts[dest][0]
      if currPort == port:
        del self.hosts[dest]
    
    deleteDests = set()
    for dest in self.routesToDest:
      currPort = self.routesToDest[dest][0]
      
      if currPort == port:

        if dest in self.hosts:
          self.routesToDest[dest] = self.hosts[dest]
          packet = basics.RoutePacket(dest, self.routesToDest[dest][1])
          self.send(packet, self.routesToDest[dest][0], True)
        else:
          self.sendPoison(dest)
          deleteDests.add(dest)


    for dest in deleteDests:
      del self.routesToDest[dest]

    del self.neighbours[port]
Ejemplo n.º 24
0
    def handle_rx(self, packet, port):
        """
        Called by the framework when this Entity receives a packet.

        packet is a Packet (or subclass).
        port is the port number it arrived on.

        You definitely want to fill this in.
        """
        if isinstance(packet, basics.RoutePacket):
            self.handle_route_packet(packet, port)
            return
        elif isinstance(packet, basics.HostDiscoveryPacket):
            self.handle_host_discover_packet(packet, port)
            return
        else:
            if not self.NO_LOG:
                self.log("RX UNKNOWN %s on %s (%s)", packet, port,
                         api.current_time())

            host = packet.dst
            mapping = self.route_map.next_hop(host)
            if mapping == DESTINATION_NOT_FOUND:
                self.send(packet, port, flood=True)
                return
            next_hop = m_next_hop(mapping)
            if next_hop != port:
                self.send(packet, next_hop)
            elif self.POISON_MODE:
                # The correct route is sending the packet back to the port
                # we received it on. We need to poison this route
                poison_packet = basics.RoutePacket(host, INFINITY)
                self.send(poison_packet, port)
Ejemplo n.º 25
0
    def handle_link_down(self, port):
        """
        Called by the framework when a link attached to this Entity does down.

        The port number used by the link is passed in.
        """
        # remove port from our remembered ports
        if port in self.ports:
            self.ports.pop(port)

        to_be_removed = []  # to not mutate collection while iterating
        for entity in self.table:  # for entity in entities
            if port == self.table[entity][
                    0]:  # if I could reach that entity through this port
                to_be_removed.append(entity)  # remove that route from my table
                if entity in self.neighboring_hosts:  # if it is neighboring host
                    self.neighboring_hosts.remove(
                        entity)  # remove from remembered neighboring hosts
                if self.POISON_MODE:
                    # send poisoned packets to tell other routers that I can't reach that entity anymore
                    info = basics.RoutePacket(entity, INFINITY)
                    self.send(
                        info, port, flood=True
                    )  # with flood=True packets are sent from all ports except listed

        for entity in to_be_removed:
            self.table.pop(entity)
Ejemplo n.º 26
0
    def handle_timer(self):
        """
        Called periodically.

        When called, your router should send tables to neighbors.  It
        also might not be a bad place to check for whether any entries
        have expired.

        """
        # check through the routes and see if 15 seconds have passed if so remove route
        expire = []
        for dst in self.distance_table.keys():
            self.distance_table[dst][2] += self.DEFAULT_TIMER_INTERVAL
            if self.distance_table[dst][2] >= 15:
                expire.append(dst)

        for dst in expire:
            if self.distance_table[dst][0] in self.port_table.keys(
            ) and self.port_table[self.distance_table[dst]
                                  [0]] == self.distance_table[dst][1]:
                self.distance_table[dst] = [
                    self.distance_table[dst][0], self.distance_table[dst][1], 0
                ]
            else:
                del self.distance_table[dst]

        # send table to all neighbour except that using
        for dst in self.distance_table.keys():
            p = basics.RoutePacket(dst, self.distance_table[dst][1])
            self.send(p, self.distance_table[dst][0], flood=True)
Ejemplo n.º 27
0
    def handle_link_down(self, port):
        """
    Called by the framework when a link attached to this Entity does down.

    The port number used by the link is passed in.
    """

        del self.ports_info[port]

        nodes_to_delete = []
        for dst in self.table.keys():
            dst_info = self.table[dst]
            if (port == dst_info[0]):
                #change my cost to infinity

                nodes_to_delete.append(dst)
                if (self.POISON_MODE):
                    #iterate over all ports except given port and send infinitu to neighbours
                    for my_port in self.ports_info:
                        if (port != my_port):
                            r_packet = basics.RoutePacket(dst, INFINITY)
                            self.send(r_packet, my_port)

        for node in nodes_to_delete:
            del self.table[node]
Ejemplo n.º 28
0
    def handle_timer(self):
        """
    Called periodically.

    When called, your router should send tables to neighbors.  It also might
    not be a bad place to check for whether any entries have expired.
    """
        # if(self.counter <=1):
        # print("in handle_timer router",self.counter)
        # print("table ",self.table)
        nodes_to_delete_for_timeout = []
        for dest in self.table.keys():
            for port in self.ports_info.keys():
                time_now = time.time()
                dest_info = self.table[dest]
                if ((time_now - dest_info[2]) > 15 and dest_info[3] == True):

                    if (dest not in nodes_to_delete_for_timeout):
                        nodes_to_delete_for_timeout.append(dest)
                else:
                    r_packet = basics.RoutePacket(dest, dest_info[1])
                    self.send(r_packet, port)

        #not delete nodes for timeout
        for node in nodes_to_delete_for_timeout:
            # print("expired", node)

            del self.table[node]
Ejemplo n.º 29
0
    def handle_timer(self):
        """
    Called periodically.

    When called, your router should send tables to neighbors.  It also might
    not be a bad place to check for whether any entries have expired.
    """
        dead_table = True
        current_time = api.current_time()
        for destination in self.table:
            to_expire = []
            for port in self.table[destination]:
                if current_time - self.table[destination][port][1] > 15:
                    to_expire.append(port)
            if destination not in self.hosts:
                for port in to_expire:
                    del (self.table[destination][port])
                    self.poison(destination, port)
        for destination in self.table:
            port = self.best_path(destination)
            if not (port == None):
                dead_table = False
                update_packet = basics.RoutePacket(
                    destination, self.table[destination][port][0])
                self.send(update_packet, [port] + self.hosts.values(),
                          flood=True)
Ejemplo n.º 30
0
 def send_updated_route_to_neighbours(self, destination, latency,
                                      input_port):
     #should i send update to neighbour from where it came from?
     for port in self.ports_info.keys():
         if (port != input_port):
             r_packet = basics.RoutePacket(destination, latency)
             self.send(r_packet, port)