Example #1
0
def protocol4(ps):
    next_frame_to_send = protocol.SequenceNumber(0, MAX_SEQ)
    frame_expected = protocol.SequenceNumber(0, MAX_SEQ)
    r = protocol.Frame()
    s = protocol.Frame()
    buffer = protocol.Packet()
    ps.from_network_layer(buffer)
    s.info = buffer
    s.seq = next_frame_to_send.copy()
    s.ack = frame_expected.opposite()
    ps.to_physical_layer(s)
    ps.start_timer(s.seq.value)
    while True:
        event = ps.wait_for_event()
        if event == protocol.frame_arrival:
            r = ps.from_physical_layer()
            if r.seq == frame_expected:
                ps.to_network_layer(r.info)
                frame_expected.inc()
            if r.ack == next_frame_to_send:
                ps.stop_timer(r.ack.value)
                ps.from_network_layer(buffer)
                next_frame_to_send.inc()
        s.info = buffer
        s.seq = next_frame_to_send.copy()
        s.ack = frame_expected.opposite()
        ps.to_physical_layer(s)
        ps.start_timer(s.seq.value)
Example #2
0
def sender1(ps):
    while True:
        buffer = protocol.Packet()
        ps.from_network_layer(buffer)
        s = protocol.Frame()
        s.info = buffer
        ps.to_physical_layer(s)
Example #3
0
def protocol5(ps):
	ack_expected = protocol.SequenceNumber(0,MAX_SEQ)
	next_frame_to_send = protocol.SequenceNumber(0,MAX_SEQ)
	frame_expected = protocol.SequenceNumber(0,MAX_SEQ)
	nbuffered = 0

	buffer = map(lambda x:protocol.Packet(),range(MAX_SEQ+1))

	ps.enable_network_layer()

	while True:
		event = ps.wait_for_event()
		if event == protocol.network_layer_ready:
			ps.from_network_layer(buffer[next_frame_to_send.value])
			nbuffered = nbuffered + 1
			send_data(next_frame_to_send,frame_expected,buffer,ps)
			next_frame_to_send.inc()
		elif event == protocol.frame_arrival:
			r = ps.from_physical_layer()
			if r.seq == frame_expected:
				ps.to_network_layer(r.info)
				frame_expected.inc()
			while (r.ack.between(ack_expected,next_frame_to_send)):
				nbuffered = nbuffered-1
				ps.stop_timer(ack_expected.value)
				ack_expected.inc()
		elif event == protocol.timeout:
			next_frame_to_send = ack_expected.copy()
			for i in range(1,nbuffered+1):
				send_data(next_frame_to_send,frame_expected,buffer,ps)
				next_frame_to_send.inc()
		if nbuffered < MAX_SEQ:
			ps.enable_network_layer()
		else:
			ps.disable_network_layer()
Example #4
0
    def process_triggered_updates(self, routes):
        """
            Processes the triggered updates.
        """
        sock = self.input_ports[0]
        for output_port in self.config.output_ports:

            packet_routes = [{
                    "destination": route.destination,
                    "cost": 16,
                    "next-hop": self.config.router_id
                } for route in routes]

            p = protocol.Packet(output_port.cost, packet_routes)
            sock.sendto(p.to_data(), ('localhost', output_port.port))
Example #5
0
def sender3(ps):
    buffer = protocol.Packet()
    ps.from_network_layer(buffer)
    next_frame_to_send = 0
    while True:
        s = protocol.Frame()
        s.info = buffer
        s.seq = next_frame_to_send
        print("sending", s.seq)
        ps.to_physical_layer(s)
        print("starting", s.seq)
        ps.start_timer(s.seq)
        event = ps.wait_for_event()
        if event == protocol.frame_arrival:
            s = ps.from_physical_layer()
            print("got ack", s.ack)
            if s.ack == next_frame_to_send:
                print("ack was desired")
                print("stopping", s.ack)
                ps.stop_timer(s.ack)
                ps.from_network_layer(buffer)
                # in stop-and-wait PAR, seq changes as
                next_frame_to_send = 1 - next_frame_to_send
Example #6
0
    def process_periodic_update(self, dt):
        """
            Called when the periodic timer is triggered.
        """
        # send destination, next hop and total cost of each routing entry to each input port
        sock = self.input_ports[0]

        for output_port in self.config.output_ports:

            # add self to the routes
            routes = [{
                    "destination": self.config.router_id,
                    "cost": 0,
                    "next-hop": self.config.router_id
                }]
            
            # if len(self.rt) == 0:
            #     self.log("advertising self to " + str(output_port.router_id))

            for route in self.rt:
                
                cost = route.cost
                destination = route.destination
                
                # poison reverse by setting cost to 16 when announcing routes back from where they were learned
                if self.rt[destination].nextHop == output_port.router_id:
                    cost = 16

                routes.append({
                    "destination": destination,
                    "cost": cost,
                    "next-hop": self.config.router_id
                })

            packet = protocol.Packet(output_port.cost, routes)
            sock.sendto(packet.to_data(), ('localhost', output_port.port))
Example #7
0
    def process_incoming_data(self, addr, data):
        """
            Called when incoming data is received. The data returned from this function is sent back through the socket. If None is returned, nothing will be sent.
        """

        triggered_updates = []
        packet = protocol.Packet()
        
        if not packet.from_data(data):
            self.log("invalid packet hash")
            return
        
        if packet.link_cost < 0:
            return

        for route in packet.routes:

            route_destination = route["destination"]
            route_cost = route["cost"]
            route_next_hop = route["next-hop"]

            destination_entry = self.rt[route_destination]

            # Check route is valid before any processing is done

            # route lists ourself as the destination (useless) or as the next hop (invalid) and should be dropped
            if route_destination == self.config.router_id or route_next_hop == self.config.router_id:
                continue
            
            # don't process if the route is invalid
            if not self.is_valid_route(route_destination, route_cost, route_next_hop):
                continue

            # route is valid and should be processed
    
            # total cost is the link cost added to the cost contained in the packet
            total_destination_cost = route_cost + packet.link_cost

            is_destination_unreachable = (total_destination_cost >= 16)

            # clamp cost to maximum of 16
            if is_destination_unreachable:
                total_destination_cost = 16

            # is the destination routerID known
            is_destination_in_table = destination_entry is not None
            
            # New valid route
            if not is_destination_in_table and not is_destination_unreachable:

                # put the destination in the table
                self.rt.add_entry(route_destination, route_next_hop, total_destination_cost)
                self.log("added new router " + str(route_destination) + " via " + str(route_next_hop) + " with a cost of " + str(total_destination_cost))
            
            # Route already exists in table
            elif is_destination_in_table:

                is_destination_garbage = destination_entry.garbage

                # Check for a better route.
                if total_destination_cost < destination_entry.cost:
                    self.rt.set_cost(route_destination, total_destination_cost)
                    self.rt.set_garbage(route_destination, False)
                    self.rt.set_next_hop(route_destination, route_next_hop)

                    self.log("found new route to " + str(route_destination) + " via " + str(route_next_hop) + " with a cost of " + str(total_destination_cost))

                # Check for worse route from the same hop
                elif route_next_hop == destination_entry.nextHop and total_destination_cost > destination_entry.cost:
                    if is_destination_unreachable:
                        # garbage it if we haven't seen it before, otherwise ignore it
                        if not is_destination_garbage:
                            self.rt.set_garbage(route_destination, True)
                            triggered_updates.append(destination_entry)
                            self.log("processed a triggered update from " + str(packet.routes[0]["next-hop"]) + " marked " + str(route_destination) + " as garbage")
                    
                    # We got a worse route from the samehop but its not infinite. As a neighbour we MUST update to the higher cost.
                    else:
                        self.rt.set_cost(route_destination, total_destination_cost)
                        self.rt.reset_age(route_destination)

                # Check for worse route from a different hop and ignore it
                elif total_destination_cost > destination_entry.cost:
                    #self.log("Worse route to " + str(route_destination) + " ignoring it")
                    continue

                # Check for same route and keep it alive
                elif route_next_hop == destination_entry.nextHop and total_destination_cost == destination_entry.cost:
                    # We dont want to keep alive infinite weight routes
                    if not is_destination_garbage:
                        self.rt.reset_age(route_destination)

        if len(triggered_updates) > 0:
            self.log("sending triggered updates")
            self.process_triggered_updates(triggered_updates)

        return None
Example #8
0
HEAD = protocol.HEAD
TAIL = protocol.TAIL
DISCONNECT_MESSAGE = '!DISCONNECT'

client = protocol.Client()
client.connect()

try:
    PKT_BUFFER = int(input("Enter packet buffer size: "))
except:
    PKT_BUFFER = 10
first_acknowledgement = str(PKT_BUFFER)
client.send(first_acknowledgement)

packet = protocol.Packet(PKT_BUFFER)

message = "Hello, I am a client. I am sending this message. Happy coding!"
packet.append_msg(message)

active = True
while active:
    # input()
    buffer = protocol.from_network_layer(packet)
    if buffer == None:
        print(f"[NOTE] No more message left.")
        more_msg = input(
            "Enter new message to send OR press ENTER to DISCONNECT: ")
        if more_msg:
            packet.append_msg(more_msg)
            continue
Example #9
0
 def send_ack(self, pid, error = False):
     self.log("Sending ACT to: %d" % pid, log.DEBUG)        
     p = pr.Packet(pr.PACKET_ACK, {"error": error, "id": pid})
     self.ack_bin_data += self.packet_to_bin(p)
Example #10
0
def protocol6(ps):

    global no_nak

    r = protocol.Frame()
    out_buf = mod_list(map(lambda x: protocol.Packet(), range(NR_BUFS)))
    in_buf = mod_list(map(lambda x: protocol.Packet(), range(NR_BUFS)))
    arrived = mod_list(map(lambda x: False, range(NR_BUFS)))

    def dump():
        for i in range(NR_BUFS):
            ps.println(str(("dump", i, "arrived", arrived[i])))
        for i in range(NR_BUFS):
            ps.println(str(("dump", i, "in_buf", in_buf[i].data)))
        for i in range(NR_BUFS):
            ps.println(str(("dump", i, "out_buf", out_buf[i].data)))

    ps.enable_network_layer()
    ack_expected = protocol.SequenceNumber(0, MAX_SEQ)
    next_frame_to_send = protocol.SequenceNumber(0, MAX_SEQ)
    frame_expected = protocol.SequenceNumber(0, MAX_SEQ)
    too_far = protocol.SequenceNumber(NR_BUFS, MAX_SEQ)
    nbuffered = 0

    while True:
        event = ps.wait_for_event()
        if event == protocol.network_layer_ready:
            nbuffered = nbuffered + 1
            ps.from_network_layer(out_buf[next_frame_to_send.value])
            send_frame(protocol.data, next_frame_to_send, frame_expected,
                       out_buf, ps)
            next_frame_to_send.inc()
        elif event == protocol.frame_arrival:
            r = ps.from_physical_layer()
            if r.kind == protocol.data:
                if r.seq != frame_expected and no_nak:
                    send_frame(protocol.nak,
                               protocol.SequenceNumber(0, MAX_SEQ),
                               frame_expected, out_buf, ps)
                else:
                    ps.start_ack_timer()
                if r.seq.between(frame_expected,
                                 too_far) and arrived[r.seq.value] == False:
                    arrived[r.seq.value] = True
                    in_buf[r.seq.value] = r.info
                    while (arrived[frame_expected.value]):
                        ps.to_network_layer(in_buf[frame_expected.value])
                        no_nak = False
                        arrived[frame_expected.value] = False
                        frame_expected.inc()
                        too_far.inc()
                        ps.start_ack_timer()
                else:
                    if r.seq.between(frame_expected, too_far):
                        pass
                    else:
                        pass
                    if arrived[r.seq.value] == False:
                        pass
                    else:
                        pass
            if r.kind == protocol.nak and r.ack.copy().inc().between(
                    ack_expected, next_frame_to_send):
                send_frame(protocol.data,
                           r.ack.copy().inc(), frame_expected, out_buf, ps)
            while r.ack.between(ack_expected, next_frame_to_send):
                nbuffered = nbuffered - 1
                ps.stop_timer(ack_expected.value)
                ack_expected.inc()
        elif event == protocol.cksum_err:
            if no_nak:
                send_frame(protocol.nak, protocol.SequenceNumber(0, MAX_SEQ),
                           frame_expected, out_buf, ps)
        elif event == protocol.timeout:
            send_frame(protocol.data,
                       protocol.SequenceNumber(event.value, MAX_SEQ),
                       frame_expected, out_buf, ps)
        elif event == protocol.ack_timeout:
            send_frame(protocol.ack, protocol.SequenceNumber(0, MAX_SEQ),
                       frame_expected, out_buf, ps)
        if nbuffered < NR_BUFS:
            ps.enable_network_layer()
        else:
            ps.disable_network_layer()