首页 > 代码库 > Customer.java

Customer.java

package EPON;import java.util.LinkedList;import java.util.Queue;import java.util.Random;import java.io.File;import java.io.FileNotFoundException;import java.io.PrintWriter;public class Customer implements Runnable {        private static final int Tmax = 10000000;    private int identifier;    private final int SLEEP = 0, DOZE = 1;    private int currentState = 0; //闂佽法鍠愰弸濠氬箯瀹勯偊娼楅梺璺ㄥ枑閺嬪骞忔搴㈣拫闂佽法鍠愰弸濠氬箯閿燂拷    private double systemTime = 0;    private double lastSystemTime = 0; //闂佽法鍠曠欢婵嬫閳哄倸顏剁紒顖濆吹缁椽寮崼鏇熸櫢闁哄倶鍊栫?氾拷    private double nextArrivalTime = exponential(Simulation.ARRIVALRATE); //闂佽法鍠栭幗顏勨枎閿涘嫷鏆滈柟椋庡厴閺佹捇寮妶鍡楊伓闁哄啫鐖奸弫鎾诲棘閵堝棗顏?    //缂備胶鍠栭弫鎾诲棘閵堝棗顏禿elay    private double totalDelay = 0;    @SuppressWarnings("unused")    private double totalvar = 0;    private int totalPacket = 0;    private int m = 0;    //缂備胶鍠栭弫鎾绘偑閿熺姵浜ら柟椋庡厴閺佹捇寮妶鍡楊伓闁绘鍩栭敓鎴掔劍濡炲倿鏌ㄩ悢鍛婄伄闁瑰嚖鎷?    private double activeTime = 0, sleepTime = 0, dozeTime = 0;    int[]  vacation= new int[1000];    private int wakeUpTimes;    private Queue<Packet> buffer = new LinkedList<Packet>();    public Customer(int identifier) {this.identifier = identifier;}    @Override    public void run() {                    while (true) {                        try {                                Simulation.customer_lock[identifier].acquire();                if(Simulation.grant[identifier] < -0.5) break;                totalvar+=Math.pow((systemTime-lastSystemTime-(32e-6/(1-Simulation.OFFERLOAD))), 2);                lastSystemTime = systemTime;                                           double grant = Simulation.grant[identifier] + Simulation.RTT / 2; //grant闁哄啫鐖奸弫鎾诲棘閵堝棗顏?                                if (grant < systemTime) Simulation.report[identifier] = 0;                else {                               switch(currentState) {                        case SLEEP: //闁活偓绻濋弫鎾诲棘閵堝棗顏?                        {                            boolean isWakeUp = false;                            double wakeUpPoint = 0;                            if (buffer.size() >= Simulation.WAKEUPTHRESHOLD) currentState = DOZE; //閻犲洦鎸抽弫鎾诲棘閵堝棗顏堕梺璺ㄥ枑閺嬪骞忛摎鍌滎伇闂佽法鍠愰弸濠氬箯缁屽紣ant闂佽法鍠愰弸濠氬箯缁嬬keUp闂佽法鍠曟俊顓犳兜闁垮顏?                                                        while (systemTime < grant ) { //闂佽法鍠撶?氭盯寮妶鍡楊伓缂侇垵宕电划娲籍閸洘鏅搁柡鍌樺?栫?氾拷                                if (nextArrivalTime < grant) {                                    addPacket();                                     if (buffer.size() == Simulation.WAKEUPTHRESHOLD) { //闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鏌ㄩ悢鍛婄伄闁瑰嘲鍢查敓鏂ゆ嫹                                        isWakeUp = true;                                        wakeUpPoint = systemTime;                                        wakeUpTimes ++;                                        if (grant > (systemTime + Simulation.WAKEUPTIME)) currentState = DOZE; //grant闁哄啫鐖奸弫鎾诲棘閵堝棗顏堕梺璺ㄥ枔閻涖倝骞橀崨顔碱伓闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氱OZE                                                                                else { //grant闁哄啫鐖奸弫鎾诲极濞嗘帗褰ч柟椋庡厴閺佹捇寮妶鍡楊伓WAKEUP                                            grant = systemTime + Simulation.WAKEUPTIME; //闂佽法鍠撶?氭盯寮妶鍡楊伓闂佽法鍠愰弸濠氬箯缁嬬瘓KEUP闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊寮崼鏇熸櫢闁哄倶鍊栫?氾拷                                            currentState = SLEEP;                                        }                                    }                                } else { //闂佽法鍠栭幗顏呮綇閻愵剙顏禷rrival闁哄啫鐖奸弫鎾诲极濞嗘埈鍔滈柟椋庡厴閺佹捇寮妶鍡楊伓grant                                                                 if (isWakeUp) {                                                                      sleepTime += wakeUpPoint - lastSystemTime;                                        if (grant > (wakeUpPoint + Simulation.WAKEUPTIME)) dozeTime += grant - (wakeUpPoint + Simulation.WAKEUPTIME);                                    }                                    else { //婵炲矉绻濋弫鎾诲棘閵堝棗顏堕梺璺ㄥ枑閺嬪骞忛悜鑺ユ櫢闁哄倶鍊栫?氾拷                                        if (currentState == SLEEP) sleepTime += grant - lastSystemTime;                                        if (currentState == DOZE) dozeTime += grant - lastSystemTime;                                    }                                                                  systemTime = grant;                                    if (currentState == DOZE) Simulation.report[identifier] = buffer.size();                                          if (currentState == SLEEP) Simulation.report[identifier] = 0;                                    break;                                }                              }                            break;                        }                        case DOZE: //闂佽法鍠庢竟娆撴晸閿燂拷                        {                            while (systemTime < grant) {                                if (nextArrivalTime < grant) {                                    addPacket();                                } else {                                    systemTime = grant;                                    break;                                } //闂佽法鍠撶?氭盯寮妶鍡楊伓闂佽法鍠愰弸濠氬箯缁屽紣ant,闂佽法鍠愰弸濠氬箯瀹勯偊娼楅梺璺ㄥ枑閺嬪骞忓畡鏉跨                            }                            dozeTime += grant - lastSystemTime;                                                        //System.out.println("A: " + (grant - lastSystemTime));                             //  vacation[(int)((grant - lastSystemTime)/(1e-7))]++;                                                        for (int i = 0; i < Simulation.report[identifier]; i++) { //闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鏌ㄩ悢鑽ょ獩婵炲棌鍓濋崬濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鏌ㄩ悢鍛婄伄闁归鍏橀弫鎾绘晸閿燂拷                                Packet tmp = buffer.remove();                                tmp.setDepartureTime(systemTime + (i+1) * Simulation.PACKETBIT / Simulation.RATE);                                totalDelay += tmp.getDepartureTime() - tmp.getArriveTime();                                totalPacket++;                            }                            grant += Simulation.report[identifier] * Simulation.PACKETBIT / Simulation.RATE;                            activeTime += Simulation.report[identifier] * Simulation.PACKETBIT / Simulation.RATE;                                                        while (systemTime < grant) {//闂佽法鍠撶?氭盯寮妶鍡楊伓闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鏌ㄩ悢鍛婄伄闁归鍏橀弫鎾诲棘閵堝棗顏堕梺璺ㄥ枑閺嬪骞忛悜鑺ユ櫢闁汇劌妫涚亸銊╁箯閿燂拷                                if (nextArrivalTime < grant) addPacket();                                else {                                    systemTime = grant;                                    break;                                }                            }                            if (buffer.size() >= Simulation.SLEEPTHRESHOLD) {                                 if (buffer.size()<Tmax){Simulation.report[identifier] = buffer.size();                                                                 }                                else {Simulation.report[identifier] = Tmax;                                //System.out.println("/闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鏌ㄩ悢鍛婄伄闁瑰嘲鍢查敓鏂ゆ嫹,闂佽法鍠栭幗顏勨枎" );                                }//闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鏌ㄩ悢鍛婄伄闁瑰嘲鍢查敓鏂ゆ嫹,闂佽法鍠栭幗顏勨枎閿涘嫨锟藉骞忛悜鑺ユ櫢闁哄倶鍊栫?氬綊鏌ㄩ悢鍛婄伄闁归鍏橀弫鎾诲棘閵堝棗顏?                                                                currentState = DOZE;                            } else {                                currentState = SLEEP; //闂佽法鍠愰弸濠氬箯閻戣姤鏅搁柡鍌樺?栫?氬綊鎯堥敓鐘虫櫢闁哄倶鍊栫?氾拷                                Simulation.report[identifier] = 0;                            }                            break;                        }                    }                }            }catch (InterruptedException e){                e.printStackTrace();            }            Simulation.server_lock.release();        }            Simulation.totalpacket[identifier]=totalPacket;        if (identifier == 0){         //  System.out.println("OfferLoad:" + offerLoad);           // Simulation.out.println("Ad\t"+(totalDelay / totalPacket)+"\tpp\t"+((sleepTime*0.1484 + activeTime + (dozeTime + wakeUpTimes * Simulation.WAKEUPTIME)*0.76207)/systemTime)+"\tsp\t" +(sleepTime /systemTime)+"\tW-T\t" +(Simulation.WAKEUPTIME)+"\tdp\t" +((dozeTime)/systemTime)+"\tap\t" +((activeTime)/systemTime )+"\tsc\t" +((sleepTime + (wakeUpTimes) * Simulation.WAKEUPTIME)/wakeUpTimes)+"\tdc\t" +(dozeTime/wakeUpTimes )+"\ttc\t" +(systemTime/wakeUpTimes)+"\n");           System.out.print("Average delay:");            System.out.println(totalDelay / totalPacket);           // System.out.println(totalPacket);                      //System.out.println(totalDelay-(totalPacket*(512/1e9)) / totalPacket);            //System.out.println( systemTime/Simulation.CYCLENUMBER+"\tpp\t"+totalvar/Simulation.CYCLENUMBER);            //System.out.println( systemTime/Simulation.CYCLENUMBER);         System.out.println("Total system time:" + systemTime);            System.out.println("Sleep time:" + sleepTime);            System.out.println("Active time:" + activeTime);            System.out.println("wakeUpTimes:" + wakeUpTimes);            System.out.println("WakeUp time:" + wakeUpTimes * Simulation.WAKEUPTIME);            System.out.println("Doze time:" + dozeTime);            System.out.println("SleepTime + ActiveTime + DozeTime + WakeUpTimes: " + (sleepTime + activeTime + dozeTime + (wakeUpTimes) * Simulation.WAKEUPTIME));             System.out.println();          //  for (  m = 0;m<1000;m++){System.out.println(vacation[m]);}        }            }        private double exponential(double mean) {return (-(1 / mean) * Math.log(Math.random()));}    private double pareto(double mean) {         Random rng ;        double alpha;        double arrival_time = 0;        alpha = 2.5;        rng = new Random();                double xm = (alpha - 1) / alpha / mean;        double rand = rng.nextDouble();        arrival_time += xm / Math.pow(rand, 1 / alpha);            return arrival_time;        }        private void addPacket(){        systemTime = nextArrivalTime;        Packet tmp = new Packet();        tmp.setArriveTime(systemTime);        tmp.setArrivenumb(totalPacket+buffer.size());        buffer.add(tmp);        nextArrivalTime = systemTime + exponential(Simulation.ARRIVALRATE);            }}

 

Customer.java