Exemplo n.º 1
0
 def run(self):
     print(threading.currentThread().getName() + ': Starting')
     while True:
         self.process_queues()
         if self.stop:
             print(threading.currentThread().getName() + ': Ending')
             return
Exemplo n.º 2
0
    def run(self):
        while not self.stoprequest.isSet():
            try:
                filename = self.file_q.get()
                print("check " + filename)
                unknown = 0
                known = 0
                doc = load(filename)

                for page in doc:
                    for zone in page:
                        if zone.label.name == "unknown":
                            unknown += 1
                        else:
                            known += 1
                delete = False
                if known + unknown == 0:
                    delete = True
                    factor = 0
                else:
                    factor = known / (known + unknown)
            except Queue.Empty:
                continue
            except SAXParseException:
                factor = 0
            self.write_q.put('%s %4.3f\n' %
                             (os.path.basename(filename), factor))
Exemplo n.º 3
0
 def run(self):
     print(threading.currentThread().getName() + ': Starting')
     while True:
         self.forward()
         if self.stop:
             print(threading.currentThread().getName() + ': Ending')
             return
Exemplo n.º 4
0
    def __init__(self, name, cost_D, max_queue_size):
        self.stop = False  # for thread termination
        self.name = name
        # create a list of interfaces
        self.intf_L = [Interface(max_queue_size) for _ in range(len(cost_D))]
        # save neighbors and interfeces on which we connect to them
        self.cost_D = cost_D  # {neighbor: {interface: cost}}
        self.rt_tbl_D = {}  # {destination: {from-router: cost}}

        self.neighboring_routers = [self.name]
        for neighbor in self.cost_D:
            if Router.is_router(
                    neighbor):  # check to see if neighbor is a router
                self.neighboring_routers.append(neighbor)

        for destination in ["H1", "H2", "RA", "RB", "RC", "RD"]:
            for router in self.neighboring_routers:
                if destination not in self.rt_tbl_D:
                    self.rt_tbl_D.update({destination: {router: "-"}})
                else:
                    self.rt_tbl_D[destination].update({router: "-"})
                if router == self.name and destination in self.cost_D:
                    for interface, cost in self.cost_D[destination].items():
                        self.rt_tbl_D[destination][router] = cost

        self.rt_tbl_D[self.name][self.name] = 0

        print('%s: Initialized routing table' % self)
        self.print_routes()
    def run(self):
        while not self.stoprequest.isSet():
            try:
                filename = self.file_q.get()
                print("check " + filename)
                unknown = 0
                known = 0
                doc = load(filename)
		    
                for page in doc:
                    for zone in page:
                        if zone.label.name == "unknown":
                            unknown += 1
                        else:
                            known += 1
                delete = False
                if known+unknown == 0:
                    delete = True
                    factor = 0
                else:
                    factor = known / (known+unknown)
            except Queue.Empty:
                continue
	    except SAXParseException:
		factor = 0
            self.write_q.put('%s %4.3f\n' % (os.path.basename(filename), factor))
Exemplo n.º 6
0
    def forward_packet(self, p, i):
        last_hop = False  # flag to determine whether this is the last hop before destination
        cost_to_dst = self.rt_tbl_D[p.dst][self.name]
        next_hop_out_intf = None

        # checks if we're at last hop before destination and if so sets appropriate out intf
        if p.dst in self.cost_D and cost_to_dst == self.rt_tbl_D[p.dst][
                self.name]:
            next_hop_out_intf = list(self.cost_D[p.dst])[0]
            last_hop = True

        # if it's not the last hop before the destination, it determines the out intf of the next hop to reach dst
        if not last_hop:
            for neighbor in self.cost_D:
                if neighbor not in self.rt_tbl_D[p.dst]:
                    continue
                if self.rt_tbl_D[p.dst][neighbor] + self.rt_tbl_D[neighbor][
                        self.name] == cost_to_dst:
                    next_hop_out_intf = list(self.cost_D[neighbor])[0]

        if next_hop_out_intf is None:
            return  # something went wrong, there is no hop that matches routing table

        try:
            self.intf_L[next_hop_out_intf].put(p.to_byte_S(), 'out', True)
            print('%s: forwarding packet "%s" from interface %d to %d' % \
                  (self, p, i, next_hop_out_intf))
        except queue.Full:
            print('%s: packet "%s" lost on interface %d' % (self, p, i))
            pass
Exemplo n.º 7
0
 def run(self):
     print (threading.currentThread().getName() + ': Starting')
     while True:
         #transfer one packet on all the links
         self.transfer()
         #terminate
         if self.stop:
             print (threading.currentThread().getName() + ': Ending')
             return
Exemplo n.º 8
0
 def run(self):
     print(threading.currentThread().getName() + ': Starting')
     while True:
         # receive data arriving to the in interface
         self.udt_receive()
         # terminate
         if (self.stop):
             print(threading.currentThread().getName() + ': Ending')
             return
Exemplo n.º 9
0
 def tic(self):
     tic = 0
     print("tic " + str(tic))
     sleep(1)
     while tic < 13:
         for arm in self.arms:
             arm.nextTic()
         print("tic " + str(tic))
         tic += 1
         sleep(self.tic_time)
Exemplo n.º 10
0
 def forward_packet(self, p, i):
     try:
         # TODO: Here you will need to implement a lookup into the
         # forwarding table to find the appropriate outgoing interface
         # for now we assume the outgoing interface is 1
         self.intf_L[1].put(p.to_byte_S(), 'out', True)
         print('%s: forwarding packet "%s" from interface %d to %d' % \
               (self, p, i, 1))
     except queue.Full:
         print('%s: packet "%s" lost on interface %d' % (self, p, i))
         pass
Exemplo n.º 11
0
 def udt_receive(self):
     pkt_S = self.in_intf_L[0].get()
     if pkt_S is not None:
         if pkt_S[5] == '0':
             self.out += pkt_S[5:]
         else:
             dst = pkt_S[:5]
             self.out = dst + self.out + pkt_S[6:]
             print('%s: received packet "%s" on the in interface' %
                   (self, self.out))
             self.out = ''
Exemplo n.º 12
0
 def send_routes(self, i):
     to_send = {"from": self.name, "rt_tbl": self.rt_tbl_D}
     json_to_send = json.dumps(to_send)
     p = NetworkPacket(0, 'control',
                       json_to_send)  # create a routing table update packet
     try:
         print('%s: sending routing update "%s" from interface %d' %
               (self, p, i))
         self.intf_L[i].put(p.to_byte_S(), 'out', True)
     except queue.Full:
         print('%s: packet "%s" lost on interface %d' % (self, p, i))
         pass
Exemplo n.º 13
0
 def udt_send(self, dst_addr, data_S):
     # splits up packet into two pieces to be able to forward them through the host's out interface
     packet_len = len(data_S) // 2
     p = NetworkPacket(dst_addr, data_S[:packet_len])
     print('%s: sending packet "%s" on the out interface with mtu=%d' %
           (self, p, self.out_intf_L[0].mtu))
     self.out_intf_L[0].put(
         p.to_byte_S())  # send packets always enqueued successfully
     p = NetworkPacket(dst_addr, data_S[packet_len:])
     print('%s: sending packet "%s" on the out interface with mtu=%d' %
           (self, p, self.out_intf_L[0].mtu))
     self.out_intf_L[0].put(
         p.to_byte_S())  # send packets always enqueued successfully
Exemplo n.º 14
0
    def update_routes(self, p, i):

        print('%s: Received routing update %s from interface %d' %
              (self, p, i))

        update = False  # flag to check if there has been an update to current router's cost to destinations
        src_router = json.loads(
            p.data_S)["from"]  # src of router who sent updated routing table
        rcvd_rt_tbl = json.loads(p.data_S)["rt_tbl"]  # received routing table

        # iterates over every destination in received routing table
        for destination in rcvd_rt_tbl:

            # if there isn't a cost for that destination move onto next destination
            if rcvd_rt_tbl[destination][src_router] == "-":
                continue

            # update the destination costs for the src router in the routing table
            self.rt_tbl_D[destination][src_router] = rcvd_rt_tbl[destination][
                src_router]

            # calculate the cost to the destination taking into account the new received cost from src router
            rcvd_cost_to_dest = int(
                self.rt_tbl_D[src_router][self.name]) + int(
                    rcvd_rt_tbl[destination][src_router])

            curr_cost_to_dest = float(
                'inf')  # sets default current cost to infinity

            # updates current cost if there is an actual cost saved
            if destination in self.rt_tbl_D and self.rt_tbl_D[destination][
                    self.name] != "-":
                curr_cost_to_dest = int(self.rt_tbl_D[destination][self.name])

            # calcualtes new cost to destination by taking the min value of the received cost to destination and the
            # current cost to destination
            new_cost_to_dest = min(curr_cost_to_dest, rcvd_cost_to_dest)

            # if the new cost is different, it updates the routing table and sets the update flag to True
            if new_cost_to_dest != curr_cost_to_dest:
                self.rt_tbl_D[destination][self.name] = new_cost_to_dest
                update = True

        # checks if there were any updates, if so it sends new routes to all neighboring routers
        if update:
            for neighbor in self.cost_D:
                if Router.is_router(neighbor):
                    out_intf = list(self.cost_D[neighbor])[0]
                    self.send_routes(out_intf)
Exemplo n.º 15
0
 def forward(self):
     for i in range(len(self.in_intf_L)):
         pkt_S = None
         try:
             # get packet from interface i
             pkt_S = self.in_intf_L[i].get()
             # if packet exists make a forwarding decision
             if pkt_S is not None:
                 p = NetworkPacket.from_byte_S(pkt_S)  # parse a packet out
                 print('%s: forwarding packet "%s" from interface %d to %d with mtu %d' \
                       % (self, p, i, i, self.out_intf_L[i].mtu))
                 self.out_intf_L[i].put(p.to_byte_S())
         except queue.Full:
             print('%s: packet "%s" lost on interface %d' % (self, p, i))
             pass
Exemplo n.º 16
0
    def udt_send(self, dst_addr, data_S):
        data = data_S
        while len(data) > 44:
            p = NetworkPacket(dst_addr, '0', data[:44])
            data = data[44:]
            print('%s: sending packet "%s" on the out interface with mtu=%d' %
                  (self, p, self.out_intf_L[0].mtu))
            self.out_intf_L[0].put(
                p.to_byte_S())  # send packets always enqueued successfully

        p = NetworkPacket(dst_addr, '1', data)
        print('%s: sending packet "%s" on the out interface with mtu=%d' %
              (self, p, self.out_intf_L[0].mtu))
        self.out_intf_L[0].put(
            p.to_byte_S())  # send packets always enqueued successfully
Exemplo n.º 17
0
    def forward_packet(self, p, i):
        try:
            # TODO: Here you will need to implement a lookup into the
            # forwarding table to find the appropriate outgoing interface
            # for now we assume the outgoing interface is 1

            interface = self.fwd_tbl_D[p.dst]
            self.intf_L[interface].put(p.to_byte_S(), 'out', True)
            # I removed the 'to interface %d' portion of the below print because I could find no way to
            # determine the interface it would be arriving on at the next router/host
            print('%s: forwarding packet "%s" on interface %d' % \
                (self, p, interface))
        except queue.Full:
            print('%s: packet "%s" lost on interface %d' % (self, p, i))
            pass
Exemplo n.º 18
0
 def udt_receive(self):
     pkt_S = self.in_intf_L[0].get()
     # if there's an incoming packet start building up fragmented packets into a buffer until all fragments have
     # been received, then print packet that and clear said buffer
     if pkt_S is not None:
         frag_pkt = NetworkPacket.from_byte_S(pkt_S)
         pkt_id = int(frag_pkt.pkt_id)
         if pkt_id in self.frag_pkt_buffer.keys():
             self.frag_pkt_buffer[pkt_id].append(frag_pkt.data_S)
         else:
             self.frag_pkt_buffer[pkt_id] = [frag_pkt.data_S]
         if frag_pkt.frag_flag == "0":
             frag_list = self.frag_pkt_buffer[pkt_id]
             del self.frag_pkt_buffer[pkt_id]
             print('%s: received packet "%s" on the in interface' %
                   (self, ''.join(frag_list)))
Exemplo n.º 19
0
    def print_routes(self):
        table = "╒══════╤══════╤══════╤══════╤══════╤══════╤══════╕\n│ "
        table += self.name
        table += "   │"
        for destination in ["H1", "H2", "RA", "RB", "RC", "RD"]:
            table += "   " + destination + " │"
        table += "\n╞══════╪══════╪══════╪══════╪══════╪══════╪══════╡\n"

        for router in self.neighboring_routers:
            table += "│ " + router + "   │"
            for destination in ["H1", "H2", "RA", "RB", "RC", "RD"]:
                table += "    "
                table += str(self.rt_tbl_D[destination][router])
                table += " │"
            table += "\n├──────┼──────┼──────┼──────┼──────┼──────┼──────┤\n"
        print(table)
Exemplo n.º 20
0
 def udt_receive(self):
         pkt_S = self.in_intf_L[0].get()
         if pkt_S is not None:
                 pkt = NetworkPacket.from_byte_S(pkt_S)
                 if(pkt.fragment == 1):
                         print('%s: received packet segment on the in interface' % (self))
                         if(pkt.offset == 0):
                                 self.pkt = NetworkPacket(pkt.dst_addr, pkt.data_S, pkt.pktID, 0, 1, pkt.src_addr)
                         else:
                                 self.pkt = NetworkPacket(pkt.dst_addr, (self.pkt.data_S + pkt.data_S), pkt.pktID, 0, 1, pkt.src_addr)
                 else:
                         if(self.pkt is not None):
                                 self.pkt = NetworkPacket(pkt.dst_addr, (self.pkt.data_S + pkt.data_S), pkt.pktID, 0, 0, pkt.src_addr)
                         else:      
                                 self.pkt = pkt                                
                         print('%s: received packet "%s" on the in interface' % (self, self.pkt.to_byte_S()))
                         self.pkt = None
Exemplo n.º 21
0
    def update_routes(self, p, i):
        #TODO: add logic to update the routing tables and
        # possibly send out routing updates

        print('%s: Received routing update %s from interface %d' %
              (self, p, i))
        routing_table_msg = pickle.loads(p.data_S.encode('latin1'))
        # examine incoming routing table
        for destination in routing_table_msg:
            # find unknown destinations
            if destination not in self.rt_tbl_D.keys():
                self.rt_tbl_D[destination] = {self.name: 0}
                self.fwd_tbl_D[
                    destination] = 99  # placeholder value until next-hop is known
            # examine known routers in incoming routing table
            for router in routing_table_msg[destination]:
                if router in self.cost_D.keys():
                    # router is a neighbor of self
                    self.rt_tbl_D[destination][router] = routing_table_msg[
                        destination][router]
                    for rt_tbl_destination in self.rt_tbl_D:
                        if rt_tbl_destination not in routing_table_msg.keys():
                            self.rt_tbl_D[rt_tbl_destination][router] = 0
        # Bellman-Ford equation
        for destination in self.rt_tbl_D:
            if destination != self.name:
                prev_value = self.rt_tbl_D[destination][self.name]
                min_cost = self.rt_tbl_D[destination][self.name]
                min_cost_next_hop = None
                for router in self.rt_tbl_D[destination]:
                    if router != self.name and self.rt_tbl_D[destination][
                            router] != 0:
                        # look at every other known router in network
                        cost_to_dest = self.rt_tbl_D[router][
                            self.name] + self.rt_tbl_D[destination][router]
                        if min_cost == 0 or cost_to_dest < min_cost:
                            min_cost = cost_to_dest
                            min_cost_next_hop = router
                if min_cost != prev_value:
                    # cost value has changed
                    self.rt_tbl_D[destination][self.name] = min_cost
                    for interface in self.cost_D[min_cost_next_hop]:
                        self.fwd_tbl_D[destination] = interface
                        break
                    self.send_routes()
Exemplo n.º 22
0
 def forward(self):
     for i in range(len(self.in_intf_L)):
         pkt_S = None
         try:
             # get packet from interface i
             pkt_S = self.in_intf_L[i].get()
             # if packet exists make a forwarding decision
             if pkt_S is not None:
                 p = NetworkPacket.from_byte_S(pkt_S)  # parse a packet out
                 # HERE you will need to implement a lookup into the
                 # forwarding table to find the appropriate outgoing interface
                 # for now we assume the outgoing interface is also i
                 print('%s: forwarding packet "%s" from interface %d to %d with mtu %d' \
                       % (self, p, i, i, self.out_intf_L[i].mtu))
                 self.out_intf_L[i].put(p.to_byte_S())
         except queue.Full:
             print('%s: packet "%s" lost on interface %d' % (self, p, i))
             pass
Exemplo n.º 23
0
 def tx_pkt(self):
     for (node_a, node_a_intf, node_b, node_b_intf) in \
     [(self.node_1, self.node_1_intf, self.node_2, self.node_2_intf),
      (self.node_2, self.node_2_intf, self.node_1, self.node_1_intf)]:
         intf_a = node_a.intf_L[node_a_intf]
         intf_b = node_b.intf_L[node_b_intf]
         pkt_S = intf_a.get('out')
         if pkt_S is None:
             continue  #continue if no packet to transfer
         #otherwise transmit the packet
         try:
             intf_b.put(pkt_S, 'in')
             print('%s: direction %s-%s -> %s-%s: transmitting packet "%s"' % \
                 (self, node_a, node_a_intf, node_b, node_b_intf, pkt_S))
         except queue.Full:
             print('%s: direction %s-%s -> %s-%s: packet lost' % \
                 (self, node_a, node_a_intf, node_b, node_b_intf))
             pass
Exemplo n.º 24
0
 def forward(self):
         for i in range(len(self.in_intf_L)):
                 pkt_S = None
                 try:
                         # get packet from interface i
                         pkt_S = self.in_intf_L[i].get()
                         # if packet exists make a forwarding decision
                         if pkt_S is not None:
                                 p = NetworkPacket.from_byte_S(pkt_S)  # parse a packet out
                                 output_intf_num =  self.table.getOutIntfNum(p.dst_addr, self.name, p.src_addr, i)
                                 mtu = self.out_intf_L[output_intf_num].mtu
                                 #print("output intf: " + str(output_intf_num))
                                 if(output_intf_num == -1):
                                         print('%s: packet "%s" lost on interface %d' % (self, p, i))
                                         return
                                 if(len(p) > mtu):
                                         if(mtu <= NetworkPacket.header_S_length):
                                                  print('%s: MTU on interface %d too small to transmit (mtu=%d)' % (self, i, mtu))
                                                  continue
                                         pktID = p.pktID
                                         offset = p.offset
                                         frag = 1
                                         dst_addr = p.dst_addr
                                         self.pktData = p.data_S
                                         div = mtu - (NetworkPacket.header_S_length)
                                         numPkt = math.ceil(len(self.pktData)/div)
                                         for j in range(0, numPkt):
                                                 if((p.fragment == 0) and (j == (numPkt-1))):
                                                         frag = 0
                                                 fwdP = NetworkPacket(dst_addr, self.pktData[(div*(j)):(div*(j+1))], pktID, offset, frag)
                                                 print('%s: forwarding packet segment "%s" from interface %d to %d with mtu %d' \
                                                       % (self, fwdP, i, output_intf_num, mtu))
                                                 self.out_intf_L[output_intf_num].put(fwdP.to_byte_S())
                                                 offset += div
                                                 
                                 else:
                                         print('%s: forwarding packet "%s" from interface %d to %d with mtu %d' \
                                               % (self, p, i, output_intf_num, mtu))
                                         self.out_intf_L[output_intf_num].put(p.to_byte_S())
                                                         
                                 
                 except queue.Full:
                         print('%s: packet "%s" lost on interface %d' % (self, p, i))
                         pass
Exemplo n.º 25
0
    def forward(self):
        for i in range(len(self.in_intf_L)):
            pkt_S = None
            try:
                # get packet from interface i
                pkt_S = self.in_intf_L[i].get()
                # if packet exists make a forwarding decision
                if pkt_S is not None:
                    p = NetworkPacket.from_byte_S(pkt_S)  # parse a packet out
                    fwd_out_intf = self.routing_table.get(int(
                        p.dst_addr))  # lookup forwarding out interface num
                    if fwd_out_intf is None:
                        print(
                            "There is no forwarding information for such destination."
                        )
                        continue
                    # calculate max load of data interface can handle
                    max_load = self.out_intf_L[
                        fwd_out_intf].mtu - NetworkPacket.header_length
                    buffer = p.data_S
                    # begin fragmentation if current packet's data length exceeds max load
                    if len(buffer) > max_load:
                        frag_flag = 1
                        frag_offset = 0
                        buffer = p.data_S

                        # iterates over buffer sending packet fragments until end of packet
                        while len(buffer) > 0:
                            if len(
                                    buffer
                            ) <= max_load:  # checks if last fragment in packet
                                frag_flag = 0
                            # creates fragment packet and forwards fragment
                            frag_pkt = NetworkPacket(p.dst_addr,
                                                     buffer[:max_load],
                                                     p.pkt_id, frag_flag,
                                                     frag_offset)
                            print('%s: forwarding packet "%s" from interface %d to %d with mtu %d' \
                                  % (self, frag_pkt, i, fwd_out_intf, self.out_intf_L[fwd_out_intf].mtu))
                            self.out_intf_L[fwd_out_intf].put(
                                frag_pkt.to_byte_S())

                            buffer = buffer[max_load:]
                            frag_offset += len(buffer[:max_load])

                    # otherwise just forward packet
                    else:
                        print('%s: forwarding packet "%s" from interface %d to %d with mtu %d' \
                              % (self, p, i, fwd_out_intf, self.out_intf_L[fwd_out_intf].mtu))
                        self.out_intf_L[fwd_out_intf].put(p.to_byte_S())

            except queue.Full:
                print('%s: packet "%s" lost on interface %d' % (self, p, i))
                pass
Exemplo n.º 26
0
    def send_routes(self):
        # TODO: Send out a routing table update

        #create a routing table update packet
        routing_table_bytes = pickle.dumps(self.rt_tbl_D)
        p = NetworkPacket(0, 'control', routing_table_bytes.decode('latin1'))

        for neighbor in self.cost_D:
            if (neighbor.startswith('R')):
                # sends updates to all neighbors that are routers
                for interface in self.cost_D[neighbor]:
                    try:
                        print(
                            '%s: sending routing update "%s" from interface %d'
                            % (self, p, int(interface)))
                        self.intf_L[int(interface)].put(
                            p.to_byte_S(), 'out', True)
                    except queue.Full:
                        print('%s: packet "%s" lost on interface %d' %
                              (self, p, int(interface)))
                        pass
Exemplo n.º 27
0
    def udt_send(self, dst_addr, data_S):
        mtu = self.out_intf_L[0].mtu
        if (len(data_S) > mtu):
            offset = 0
            frag = 1
            div = mtu - (NetworkPacket.header_S_length)
            numPkt = math.ceil(len(data_S) / div)
            print(
                '%s: segmenting packet "%s" into %s packets for the out interface with mtu=%d'
                % (self, data_S, numPkt, mtu))
            for i in range(0, numPkt):
                if (i == (numPkt - 1)):
                    frag = 0
                p = NetworkPacket(dst_addr, data_S[offset:(offset + div)],
                                  self.pktID, offset, frag)
                print(
                    '%s: sending packet "%s" on the out interface with mtu=%d'
                    % (self, p, mtu))
                self.out_intf_L[0].put(p.to_byte_S())
                offset += div
            #print('%s: packet dropped; too large: length of %s on the out interface with mtu=%d' % (self, str(len(data_S)), mtu))

        else:
            p = NetworkPacket(dst_addr, data_S)
            print('%s: sending packet "%s" on the out interface with mtu=%d' %
                  (self, p, mtu))
            self.out_intf_L[0].put(
                p.to_byte_S())  # send packets always enqueued successfully
        self.pktID += 1
Exemplo n.º 28
0
 def getLinks(self, table):
         l = ''
         self.links = []
         for i in range(0, len(self.hops)-1):
                 link = -1
                 if(i == 0):
                         link = table.getLink(int(self.hops[i]), str(self.hops[i+1]))
                         self.links.append(link)
                         l += str(link.from_node) + "->"
                 elif(i == (len(self.hops)-2)):
                         link = table.getLink(str(self.hops[i]), int(self.hops[i+1]))
                         self.links.append(link)
                         l += str(self.links[-1].to_node)
                 else:
                         link = table.getLink(str(self.hops[i]), str(self.hops[i+1]))
                         if(link != -1):
                                 self.links.append(link)
                                 l += str(link.to_node) + "->"
                 if(link != -1):
                         mtu = link.in_intf.mtu
                         self.mtu = mtu if mtu < self.mtu else (self.mtu if self.mtu != 0 else mtu)
         print("Route: " + str(self) + " initialized with links: " + l + " with MTU=" + str(self.mtu))
Exemplo n.º 29
0
    def __init__(self, name, cost_D, max_queue_size):
        self.stop = False  #for thread termination
        self.name = name
        #create a list of interfaces
        self.intf_L = [Interface(max_queue_size) for _ in range(len(cost_D))]
        #save neighbors and interfaces on which we connect to them
        self.cost_D = cost_D  # {neighbor: {interface: cost}}
        #TODO: set up the routing table for connected hosts

        self.rt_tbl_D = {}  # {destination: {router: cost}}
        self.fwd_tbl_D = {}  # {destination: interface}
        for neighbor in cost_D:
            for interface in cost_D[neighbor]:
                self.rt_tbl_D[neighbor] = {
                    self.name: cost_D[neighbor][interface]
                }
                # initialize forwarding table with neighbors
                self.fwd_tbl_D[neighbor] = interface
        # add self to routing table
        self.rt_tbl_D[self.name] = {self.name: 0}
        print('%s: Initialized routing table' % self)
        self.print_routes()
Exemplo n.º 30
0
    def forward(self):
        for i in range(len(self.in_intf_L)):
            mtu = self.out_intf_L[i].mtu
            pkt_S = None
            try:
                # get packet from interface i
                pkt_S = self.in_intf_L[i].get()
                # if packet exists make a forwarding decision
                if pkt_S is not None:
                    p = NetworkPacket.from_byte_S(pkt_S)  # parse a packet out
                    if (len(p) > mtu):
                        if (mtu <= NetworkPacket.header_S_length):
                            print(
                                '%s: MTU on interface %d too small to transmit (mtu=%d)'
                                % (self, i, mtu))
                            continue
                        pktID = p.pktID
                        offset = p.offset
                        frag = 1
                        dst_addr = p.dst_addr
                        self.pktData = p.data_S
                        div = mtu - (NetworkPacket.header_S_length)
                        numPkt = math.ceil(len(self.pktData) / div)
                        for j in range(0, numPkt):
                            if ((p.fragment == 0) and (j == (numPkt - 1))):
                                frag = 0
                            fwdP = NetworkPacket(
                                dst_addr,
                                self.pktData[(div * (j)):(div * (j + 1))],
                                pktID, offset, frag)
                            print('%s: forwarding packet segment "%s" from interface %d to %d with mtu %d' \
                                  % (self, fwdP, i, i, self.out_intf_L[i].mtu))
                            self.out_intf_L[i].put(fwdP.to_byte_S())
                            offset += div

                    else:
                        print('%s: forwarding packet "%s" from interface %d to %d with mtu %d' \
                              % (self, p, i, i, self.out_intf_L[i].mtu))
                        self.out_intf_L[i].put(p.to_byte_S())

                    # HERE you will need to implement a lookup into the
                    # forwarding table to find the appropriate outgoing interface
                    # for now we assume the outgoing interface is also i

            except queue.Full:
                print('%s: packet "%s" lost on interface %d' % (self, p, i))
                pass
Exemplo n.º 31
0
 def tx_pkt(self):
     pkt_S = self.in_intf.get()
     if pkt_S is None:
         return # return if no packet to transfer
     if len(pkt_S) > self.in_intf.mtu:
         print('%s: packet "%s" length greater than the from interface MTU (%d)' % (self, pkt_S, self.in_intf.mtu))
         return  # return without transmitting if packet too big
     if len(pkt_S) > self.out_intf.mtu:
         print('%s: packet "%s" length greater than the to interface MTU (%d)' % (self, pkt_S, self.out_intf.mtu))
         return # return without transmitting if packet too big
     # otherwise transmit the packet
     try:
         self.out_intf.put(pkt_S)
         print('%s: transmitting packet "%s"' % (self, pkt_S))
     except queue.Full:
         print('%s: packet lost' % (self))
         pass
                    factor = 0
                else:
                    factor = known / (known+unknown)
            except Queue.Empty:
                continue
	    except SAXParseException:
		factor = 0
            self.write_q.put('%s %4.3f\n' % (os.path.basename(filename), factor))

    def join(self):
        self.stoprequest.set()
        super(CheckThread, self).join()


if len(sys.argv) != 3:
    print("Usage: remove_empty.py /path/to/xml/directory")
    exit(1)
    
dir_path = sys.argv[2]
all_files = []

for root, _, files in os.walk(dir_path):
    for filename in files:
        all_files.append(root + filename)
print("number of all files: %d" % len(all_files))

for file in all_files:
    file_q.put(file)

check_threads_pool = [CheckThread(write_q, file_q) for _ in xrange(0,10)] 
for thread in pool: