Beispiel #1
0
 def __repr__(self):
     d = self.data
     if d is not None:
         d = ': ' + str(d)
     else:
         d = ''
     return "<%s %s->%s ttl:%i%s>" % (type(self).__name__,
                                      api.get_name(self.src),
                                      api.get_name(self.dst), self.ttl, d)
Beispiel #2
0
    def getOrigins(self, packet):
        src = api.get_name(packet.src)

        # RoutePacket uses "destination" instead of "dst"
        if isinstance(packet, basics.RoutePacket):
            dest = api.get_name(packet.destination)
        else:
            dest = api.get_name(packet.dst)

        return src, dest
Beispiel #3
0
 def __repr__ (self):
   d = self.data
   if d is not None:
     d = ': ' + str(d)
   else:
     d = ''
   return "<%s %s->%s ttl:%i%s>" % (type(self).__name__,
                                    api.get_name(self.src),
                                    api.get_name(self.dst),
                                    self.ttl, d)
Beispiel #4
0
    def handle_rx(self, packet, in_port):
        """
        Called when a packet is received.

        You most certainly want to process packets here, learning where
        they're from, and either forwarding them toward the destination
        or flooding them.

        """

        print("Entity:", api.get_name(self), "PACKET RECEIVED: ", packet,
              "ON PORT:", in_port)

        src, dest = self.getOrigins(packet)

        print("PACKET SRC:", src, "DEST:", dest, "TRACE:", packet.trace)

        # The source of the packet can obviously be reached via the input port, so
        # we should "learn" that the source host is out that port.  If we later see
        # a packet with that host as the *destination*, we know where to send it!
        # But it's up to you to implement that.  For now, we just implement a
        # simple hub.

        if isinstance(packet, basics.HostDiscoveryPacket):
            # Don't forward discovery messages
            if src not in self.rtable:
                print("UNKNOWN SOURCE")
                self.rtable[src] = in_port
            return

        # Sender is not known to switch
        if src not in self.rtable:
            print("UNKNOWN SOURCE")
            self.rtable[src] = in_port

        # destination properly defined and is not intended for this switch
        if dest and dest != api.get_name(self):
            if dest in self.rtable:
                # destination is a known route
                print("KNOWN ROUTE")
                self.send(packet, self.rtable[dest])
            else:
                # destination is an unknown route
                print("FLOOD")
                self.send(packet, in_port, flood=True)
        else:
            print("packet has no destination or is... FOR ME!!")
Beispiel #5
0
 def handle_rx(self, packet, in_port):
     if isinstance(packet, basics.RoutePacket):
         if self.block_route_packets:
             src = api.get_name(packet.src)
             api.userlog.debug('%s dropped a route packet from %s' %
                               (self.name, src))
             return
     return super(BlockingHub, self).handle_rx(packet, in_port)
Beispiel #6
0
 def get_cell(self, x, y):
     """ return latency (cell value) in matrix given src(x) and dest(y) """
     vector = self.get(x)
     if vector:
         try:
             return vector[api.get_name(y)]
         except KeyError as e:
             return None
     return None
Beispiel #7
0
    def handle_rx(self, packet, in_port):
        """
        Called when a packet is received.

        You most certainly want to process packets here, learning where
        they're from, and either forwarding them toward the destination
        or flooding them.

        """

        # The source of the packet can obviously be reached via the input port, so
        # we should "learn" that the source host is out that port.  If we later see
        # a packet with that host as the *destination*, we know where to send it!
        # But it's up to you to implement that.  For now, we just implement a
        # simple hub.

        if isinstance(packet, basics.HostDiscoveryPacket):
            # Don't forward discovery messages
            return

        sender = packet.src
        sender_name = api.get_name(sender)
        if sender_name not in self.table:
            # Add sender to table
            self.table[sender_name] = in_port

        reciever = packet.dst
        reciever_name = api.get_name(reciever)
        if reciever_name in self.table:
            # Send packet to reciever's port
            reciever_port = self.table[reciever_name]
            packet.ttl -= 1
            packet.trace.append(self)
            if packet.ttl != 0:
                self.send(packet, reciever_port)
        else:
            # Flood out all ports except the input port
            packet.ttl -= 1
            packet.trace.append(self)
            self.send(packet, in_port, flood=True)
Beispiel #8
0
 def get(self, x):
     try:
         vector = self.vectors[api.get_name(x)]
         return vector
     except KeyError as e:
         return None
Beispiel #9
0
 def is_for_me(self, dest):
     return dest and dest == api.get_name(self)
Beispiel #10
0
 def speak(self, msg):
     print ("\n%s says: %s\n" % (api.get_name(self.name), msg)) 
Beispiel #11
0
 def __repr__(self):
     return "ForwardingTableEntry(dst={}, port={}, latency={})".format(
         get_name(self.dst), self.port, self.latency)
Beispiel #12
0
 def __repr__(self):
     return "PeerTableEntry(dst={}, latency={}, expire_time={})".format(
         get_name(self.dst), self.latency, self.expire_time)