def control_strategy():
    if len(sys.argv) < 3:
        print "not gonna work"
        sys.exit()
    else:
        global x
        x = UI("test")
        num = int(sys.argv[1])
        #Dest = sys.argv[2].split(":")
        my_host = Network(num, droprate=0.2, corruptrate=0.01)
        forwarding = Forwarding()
        #next = forwarding.reversed_lookup_dict[sys.argv[2]]
        #next = forwarding.next_hop(next)
        Dest = int(sys.argv[2])
        next = forwarding.next_hop(Dest)
        #addr,port = Dest[0],Dest[1]
        n = my_host.add_neighbour('131.231.115.27', 1)

    going = True
    fd = x.getfd()
    send_data = {}
    tries = 0
    global host_num
    host_num = 9
    reliability = Full_reliability()
    checksum = Checksum()
    msg_dict = {}

    while going:
        signal.signal(signal.SIGINT, handler)
        r = my_host.orfd(fd, timeout=0.5)
        if send_data != {} and r == TIMEOUT:  #if packet state exist
            if tries < 5:
                x.addline('Packet send failed... resent')
                for key in send_data.keys():
                    checksum_data = forwarding.encapsulate(
                        Dest, send_data[key])
                    total_data = checksum.encapsulate(checksum_data,
                                                      len(send_data[key]) - 3)
                    my_host.send(n, total_data)
                tries += 1
            else:  #resent more than 5 times
                send_data = {}
                msg_dict = {}

        if r == FD_READY:
            #x.addline('FD')
            line = os.read(fd, 300)

            packet_list, len_of_last_msg = reliability.encapsulate(line)
            for send_pack in packet_list:
                # if len(send_pack) == 97:
                #     len_of_msg = 100
                # else:
                #     len_of_msg = len_of_last_msg
                checksum_data = forwarding.encapsulate(Dest, send_pack)
                total_data = checksum.encapsulate(checksum_data,
                                                  len(send_pack) - 3)
                my_host.send(n, total_data)
                tries = 0
                unique_id = bin(struct.unpack(
                    'B', send_pack[1])[0])[2:].zfill(8) + bin(
                        struct.unpack('B', send_pack[2])[0])[2:].zfill(8)
                send_data.update({unique_id: send_pack})
                if line == '/q':
                    going = False

        if r == NET_READY:
            #x.addline('net')
            line, source = my_host.receive()
            header = checksum.decapsulate(line[:2]) + forwarding.decapsulate(
                line[2]) + reliability.decapsulate(line[3:6])

            ack = header[24]
            end = header[25]
            seq = int(header[40:48], 2)
            len_msg = int(header[8:16], 2)

            source_host = int(header[16:20], 2)
            dest_host = int(header[20:24], 2)
            unique_id = header[32:48]

            if source_host != host_num and dest_host != host_num:
                if ack == '1':
                    next = forwarding.next_hop(source_host)
                else:
                    next = forwarding.next_hop(dest_host)
                if next != False:
                    neighbour_addr = forwarding.lookup(next)
                    addr, port = neighbour_addr.split(':')
                    #n = my_host.add_neighbour(addr,int(port))
                    my_host.send(n, line)
                else:
                    x.addline('Error data!')
                    continue
            else:
                if ack != '1' and unique_id not in reliability.unique_id_list:
                    #compute new sum and compared with checksum
                    if end != '1':  #do checksum first
                        new_sum = checksum.cal_checksum_header(
                            line[2:], len_msg)[0]
                    else:
                        new_sum = checksum.cal_checksum_header(
                            line[2:6 + len_msg], len_msg)[0]

                    if new_sum != line[0]:  #corruption happened
                        x.addline('Corrupted data!')
                        continue
                    else:
                        #x.addline('them:'+line[6:6+len_msg])
                        if end == '1':
                            msg_dict.update({1: line[6:6 + len_msg]})
                            if seq == 0:
                                x.addline('them:' + msg_dict[1])
                            else:
                                if len(msg_dict) == 2:
                                    x.addline('them:' + msg_dict[0] +
                                              msg_dict[1])
                                    msg_dict = {}
                        else:
                            msg_dict.update({0: line[6:6 + len_msg]})

                        reliability.unique_id_list.append(unique_id)

                #x.addline('received ack:'+header[24:32]+'###')
                if ack == '1':
                    if unique_id in send_data.keys():
                        send_data.pop(unique_id)  #drop data
                    tries = 0
                    if end == '1':
                        reliability.unique_id_list = []
                        reliability.msg_id_list = []
                        reliability.total_package_list = []
                else:  #send ack_packet
                    reliability.send_ack(line, n, my_host, source_host)
def control_strategy():
    if len(sys.argv) < 3:
        print "not gonna work"
        sys.exit()
    else:
        global x
        x = UI("test")
        num = int(sys.argv[1])
        Dest = int(sys.argv[2])
        my_host = Network(num, droprate=0, corruptrate=0)
        forwarding = Forwarding()
        #next = forwarding.reversed_lookup_dict[sys.argv[2]]
        next = forwarding.next_hop(Dest)
        #add all the neighbours
        n1 = my_host.add_neighbour('131.231.114.243', 1)
        n2 = my_host.add_neighbour('131.231.114.87', 1)
        if next != 'BAD_DEST':  #if dest is not this host and dest is an exist neighbour in the dict
            neighbour_addr = forwarding.lookup(next)
            if neighbour_addr == '131.231.114.243:1':
                n = n1
            elif neighbour_addr == '131.231.114.87:1':
                n = n2

    going = True
    fd = x.getfd()
    send_data = {}
    tries = 0
    global host_num
    host_num = 1
    reliability = Full_reliability()
    checksum = Checksum()
    msg_dict = {}
    while going:
        signal.signal(signal.SIGINT, handler)
        r = my_host.orfd(fd, timeout=0.5)
        if send_data != {} and r == TIMEOUT:  #if packet state exist and timeout then resend all the packets left in dict
            if tries < 5:
                x.addline('Packet send failed... resent')
                for key in send_data.keys(
                ):  #lookup all the remaining packet in the dist and resend
                    checksum_data = forwarding.encapsulate(
                        Dest, send_data[key])
                    total_data = checksum.encapsulate(checksum_data,
                                                      len(send_data[key]) - 3)
                    my_host.send(n, total_data)
                tries += 1
            else:  #resent more than 5 times then drop data
                semd_data = {}
                msg_dict = {}

        if r == FD_READY:
            #x.addline('FD')
            line = os.read(fd, 300)

            packet_list, len_of_last_msg = reliability.encapsulate(line)

            for send_pack in packet_list:  #encapsulate all the packet and send then in a loop
                checksum_data = forwarding.encapsulate(Dest, send_pack)
                total_data = checksum.encapsulate(checksum_data,
                                                  len(send_pack) - 3)
                my_host.send(n, total_data)
                tries = 0
                unique_id = bin(struct.unpack(
                    'B', send_pack[1])[0])[2:].zfill(8) + bin(
                        struct.unpack('B', send_pack[2])[0])[2:].zfill(
                            8)  #msg_id + seq_num
                send_data.update({unique_id: send_pack})
                if line == '/q':
                    going = False

        if r == NET_READY:
            #x.addline('NET')
            line, source = my_host.receive()
            header = checksum.decapsulate(line[:2]) + forwarding.decapsulate(
                line[2]) + reliability.decapsulate(
                    line[3:6])  #scrap the header from received msg

            ack = header[24]
            end = header[25]

            len_msg = int(header[8:16], 2)
            seq = int(header[40:48], 2)
            dest_host = int(header[20:24], 2)
            source_host = int(header[16:20], 2)
            unique_id = header[32:48]

            if dest_host != host_num:  #in this case only do forwarding without considering ack and packet loss
                # if ack == '1':
                #when forwarding if it's an ack msg send back to source host otherwise send to dest host
                #     next = forwarding.next_hop(source_host)
                # else:
                next = forwarding.next_hop(dest_host)

                if next != 'BAD_DEST':  #if dest_host isn't itself or a wrong number
                    neighbour_addr = forwarding.lookup(next)
                    if neighbour_addr == '131.231.114.243:1':
                        n = n1
                    elif neighbour_addr == '131.231.114.87:1':
                        n = n2
                    my_host.send(n, line)
                    x.addline('forwarding')
                else:  #may be currupted or wrong dest_host entered
                    x.addline('Error data!')
                    continue

            else:  #this case is as a receiver
                if ack != '1' and unique_id not in reliability.unique_id_list:
                    #compute new sum and compared with checksum
                    if end != '1':  #if it's not the last pack the length of it should be 100
                        new_sum = checksum.cal_checksum_header(
                            line[2:], len_msg)[0]
                    else:  #if it's the last pack the length should be len_msg
                        new_sum = checksum.cal_checksum_header(
                            line[2:6 + len_msg], len_msg)[0]
                    if new_sum != line[0]:  #corruption happened
                        x.addline('Corrupted data!')
                        continue
                    else:  #no curruption, normal state
                        if end == '1':
                            msg_dict.update({1: line[6:6 + len_msg]})
                            if seq == 0:
                                x.addline('them:' + msg_dict[1])
                            else:
                                if len(msg_dict) == 2:
                                    x.addline('them:' + msg_dict[0] +
                                              msg_dict[1])
                                    msg_dict = {}
                        else:
                            msg_dict.update({0: line[6:6 + len_msg]})
                        reliability.unique_id_list.append(unique_id)

                #x.addline('received ack:'+header[24:32]+'###')
                if ack == '1':
                    if unique_id in send_data.keys():
                        send_data.pop(unique_id)  #drop data
                    tries = 0
                    if end == '1':  #refresh the lists
                        reliability.unique_id_list = []
                        reliability.msg_id_list = []
                        reliability.total_package_list = []
                else:  #send ack_packet
                    reliability.send_ack(line, n, my_host, source_host)
Beispiel #3
0
def control_strategy():
    if len(sys.argv) < 3:
        print "not gonna work"
        sys.exit()
    else:
        global x
        x = UI("test")
        num = int(sys.argv[1])
        Dest = sys.argv[2].split(":")
        my_host = Network(num, droprate=0.2, corruptrate=0)
        forwarding = Forwarding()
        next = forwarding.reversed_lookup_dict[sys.argv[2]]
        next = forwarding.next_hop(next)
        addr, port = Dest[0], Dest[1]
        n = my_host.add_neighbour(addr, int(port))

    going = True
    fd = x.getfd()
    send_data = {}
    tries = 0
    global host_num
    host_num = 2
    reliability = Full_reliability()

    while going:
        signal.signal(signal.SIGINT, handler)
        r = my_host.orfd(fd, timeout=0.2)
        if send_data != {} and r == TIMEOUT:  #if packet state exist
            if tries < 5:
                x.addline('Packet send failed... resent')
                for key in send_data.keys():
                    checksum_data = forwarding.encapsulate(
                        next, send_data[key])
                    my_host.send(n, checksum_data)
                tries += 1

        if r == FD_READY:
            x.addline('FD')
            line = os.read(fd, 300)

            packet_list = reliability.encapsulate(line)

            for send_pack in packet_list:
                checksum_data = forwarding.encapsulate(next, send_pack)
                my_host.send(n, checksum_data)
                tries = 0
                unique_id = bin(struct.unpack(
                    'B', send_pack[2])[0])[2:].zfill(8) + bin(
                        struct.unpack('B', send_pack[3])[0])[2:].zfill(8)
                send_data.update({unique_id: send_pack})
                if line == '/q':
                    going = False

        if r == NET_READY:
            x.addline('net')
            line, source = n1.receive()
            header = reliability.decapsulate(line)

            ack = header[8]
            end = header[9]

            dest_host = int(header[4:8], 2)
            unique_id = header[16:32]

            if ack != '1' and unique_id not in reliability.unique_id_list:
                x.addline('them:' + line[4:])
                reliability.unique_id_list.append(unique_id)

            x.addline('received ack:' + header[8:16] + '###')
            if ack == '1':
                send_data.pop(unique_id)  #drop data
                tries = 0
                if end == '1':
                    reliability.unique_id_list = []
                    reliability.msg_id_list = []
                    reliability.total_package_list = []
            else:  #send ack_packet
                reliability.send_ack(line, n, my_host)
                x.addline('sent ack')
            if dest_host != host_num:
                next = forwarding.next_hop(dest_host)
                if next != False:
                    neighbour_addr = forwarding.lookup(next)
                    addr, port = neighbour_addr.split()
                    n = my_host.add_neighbour(addr, int(port))
                my_host.send(n, line)
Beispiel #4
0
def control_strategy():
    if len(sys.argv) < 3:
        print "not gonna work"
        sys.exit()
    else:
        global x
        x = UI("test")
        num = int(sys.argv[1])
        Dest = int(sys.argv[2])
        my_host = Network(num, droprate=0.2, corruptrate=0)
        forwarding = Forwarding()
        #next = forwarding.reversed_lookup_dict[sys.argv[2]]
        next = forwarding.next_hop(Dest)
        #add all the neighbours
        n1 = my_host.add_neighbour('131.231.115.148',1)
        n2 = my_host.add_neighbour('131.231.114.82',1)
        if next: #if dest is not this host
            neighbour_addr = forwarding.lookup(next)
            if neighbour_addr == '131.231.114.82:1':
                n = n2
            elif neighbour_addr == '131.231.115.148:1':
                n = n1

    going = True
    fd = x.getfd()
    send_data = {}
    tries = 0
    global host_num
    host_num = 2
    reliability = Full_reliability()


    while going:
        signal.signal(signal.SIGINT,handler)
        r= my_host.orfd(fd,timeout=0.2)
        if send_data!={}  and r == TIMEOUT: #if packet state exist and timeout then resend all the packets left in dict
            if tries<5:
                x.addline('Packet send failed... resent')
                for key in send_data.keys(): #lookup all the remaining packet in the dist and resend
                    checksum_data = forwarding.encapsulate(Dest,send_data[key])
                    my_host.send(n,checksum_data)
                tries += 1

        if r == FD_READY:
            x.addline('FD')
            line = os.read(fd,300)

            packet_list = reliability.encapsulate(line)


            for send_pack in packet_list:
                checksum_data = forwarding.encapsulate(Dest,send_pack)

                my_host.send(n,checksum_data)
                tries = 0
                unique_id = bin(struct.unpack('B',send_pack[1])[0])[2:].zfill(8) + bin(struct.unpack('B',send_pack[2])[0])[2:].zfill(8) #msg_id + seq_num
                send_data.update({unique_id:send_pack})
                if line == '/q':
	            	going = False

        if r == NET_READY:
            #x.addline('net')
            line,source = my_host.receive()
            header = forwarding.decapsulate(line[0]) + reliability.decapsulate(line[1:4])

            ack = header[8]
            end = header[9]

            dest_host = int(header[4:8],2)
            source_host = int(header[:4],2)
            unique_id = header[16:32]

            if source_host != host_num and dest_host != host_num: #in this case only do forwarding without considering ack and packet loss
                next = forwarding.next_hop(dest_host)
                if next != False:
                    neighbour_addr = forwarding.lookup(next)
                    if neighbour_addr == '131.231.114.82:1':
                        n = n2
                    elif neighbour_addr == '131.231.115.148:1':
                        n = n1
                    my_host.send(n,line)
                    x.addline('forwarding')

            else: #this case is as a receiver
                if ack!='1' and unique_id not in reliability.unique_id_list:
                    x.addline('them:'+line[4:])
                    reliability.unique_id_list.append(unique_id)

                x.addline('received ack:'+header[8:16]+'###')
                if ack == '1':
                    send_data.pop(unique_id) #drop data
                    tries = 0
                    if end == '1': #refresh the lists
                        reliability.unique_id_list = []
                        reliability.msg_id_list = []
                        reliability.total_package_list = []
                else: #send ack_packet
                    if source_host == 3:
                        n = n2
                    elif source_host == 9:
                        n = n1
                    reliability.send_ack(line,n,my_host,source_host)
                    x.addline('sent ack')
Beispiel #5
0
    if len(a) % 792 == 0:
        num_packets = len(a) / 792
    else:
        num_packets = len(a) / 792 + 1
    return a, num_packets, chinese


#def control_strategy():
if len(sys.argv) < 3:
    print "not gonna work"
    sys.exit()
else:
    x = UI("test")
    num = int(sys.argv[1])
    neighbour = sys.argv[2].split(":")
    n1 = Network(num, droprate=0.5, corruptrate=0)
    n = n1.add_neighbour(neighbour[0], int(neighbour[1]))

going = True
fd = x.getfd()
send_data = False
tries = 0

reliability = simple_reliability()

while going:

    signal.signal(signal.SIGINT, handler)
    r = n1.orfd(fd, timeout=0.5)
    if send_data != False and r == TIMEOUT:  #if packet state exist
        if tries < 5:
Beispiel #6
0
def control_strategy():
    if len(sys.argv) < 3:
        print "not gonna work"
        sys.exit()
    else:
        global x
        x = UI("test")
        num = int(sys.argv[1])
        neighbour = sys.argv[2].split(":")
        n1 = Network(num, droprate=0.5, corruptrate=0)
        n = n1.add_neighbour(neighbour[0],int(neighbour[1]))

    going = True
    fd = x.getfd()
    send_data = False
    tries = 0
    global host_num

    # This machine's host number is 2
    host_num = 2
    reliability = Full_reliability()

    while going:
        signal.signal(signal.SIGINT,handler)
        r= n1.orfd(fd,timeout=0.2)
        if send_data!=False  and r == TIMEOUT: #if packet state exist
            if tries<5:
                x.addline('Packet send failed... resent')
                n1.send(n,send_data)
                tries += 1

        if r == FD_READY:
            x.addline('FD')
            line = os.read(fd,100)

            packet_list = reliability.encapsulate(line)

            for send_pack in packet_list:
                n1.send(n,send_pack)
                tries = 0

                if line == '/q':
	            	going = False

            send_data = send_pack

        if r == NET_READY:
            x.addline('net')
            line,source = n1.receive()
            header = reliability.decapsulate(line)

            newid = header[8:16]

            if newid not in reliability.msg_id_list:
                x.addline('them:'+line[4:])
                reliability.msg_id_list.append(newid)

            ack = header[0]
            end = header[1]
            x.addline(header[:8]+'###')
            if ack == '1':
                send_data = False #drop data
                tries = 0
                if end == '1':
                    reliability.msg_id_list = []
                    reliability.total_package_list = []
            else: #send ack_packet
                reliability.send_ack(line,n,n1)
                x.addline('sent ack')