我没有在此队列中丢失数据包

时间:2018-06-04 10:15:56

标签: python networking simulation

我写这个程序,当我使用类生成id随机选择队列中的客户时,我没有丢包。否则,当我没有这个课程时,该程序运作良好,我丢失了很多数据包......任何人都可以帮助我,哪里是错误???

class Packets(object):
    #constructor
    def __init__(self, environ, arrival_time,buffer_capacity):
        #hold samples of inter-arrival time
        self.inter_arrival = []

        self.arrival_time = arrival_time
        self.env = environ

    #execute the process
    def arrival_process(self, client_departure):
        #while True:
            #sample the time to next arrival
       inter_arrival = random.expovariate(lambd = self.arrival_time) #lambda



            #yield an event to the simulator
       yield self.env.timeout(inter_arrival)

            #the packet has arrived,save samples to calculate statistics later on
       self.inter_arrival.append(inter_arrival)

            # a client has arrived - request buffer to do its job
       yield self.env.process(client_departure.packet_processing())



class Server(object):
 #constructor
    def __init__(self, environ, num_servers, server_id, service_rate,buffer):

        self.arriving_time = 0
        self.molt = []
        # the service time
        self.service_rate = service_rate
        # server
        self.servers = num_servers
        self.server_id = server_id
        # the environment
        self.env = environ
        # number of packets in the buffer
        self.queue_size = self.servers.queue
        self.loss = 0
        self.buffer=buffer

        self.queue_size=0.0

    def packet_processing(self):

        response_time = self.env.now
        # request memory to put the new coming packet in the buffer
        with self.servers.request() as request:
            if (len(self.servers.queue)< self.buffer):

                yield request
                service_rate = random.expovariate(lambd=self.service_rate)  #mu

                self.molt.append((self.env.now - self.arriving_time))
                self.queue_size -= 1
                self.arriving_time = self.env.now

                # yield an event to the simulator
                yield self.env.timeout(service_rate)

                response_time = self.env.now - response_time
                response_time_set[self.server_id].append(response_time)

            else:
                self.loss+=1


    class generate_id(object):
        def __init__(self, env, mu):
            self.env = env
            self.mu = mu
        def run(self):
            while True:
                j = random.randint(0,3)
                if j==0:
                    #j = 0
                    client_arrival = Packets(env, LAMBD, buffer_capacity)
                    self.client_departure= Server(env, machine[j], j, 10*self.mu, buffer_capacity)       
                else:
                    client_arrival = Packets(env, LAMBD, buffer_capacity)
                    self.client_departure= Server(env, machine[j], j, self.mu, buffer_capacity)       
                yield env.process(client_arrival.arrival_process(self.client_departure))

    if __name__ == '__main__':
        for i in MU:
            env = simpy.Environment()

            machine=[simpy.Resource(env,NUM_SERVERS),
            simpy.Resource(env,NUM_SERVERS),
            simpy.Resource(env,NUM_SERVERS),
            simpy.Resource(env,NUM_SERVERS)]
            # arrival process
            client = generate_id(env, i)
            env.process(client.run())
            # simulate until sim_time
            env.run(until=SIM_TIME)
            ro.append(LAMBD /i)
            for k in range(4):
                avrage_resonse_time[k].append(
                              (numpy.mean(response_time_set[k])))
                lost_packets.append(client.client_departure.loss)     

0 个答案:

没有答案