-
Notifications
You must be signed in to change notification settings - Fork 2
/
Customer.java
187 lines (162 loc) · 9.96 KB
/
Customer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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); //闁跨喖鎽▎锛勵暜閹风兘鏁撻弬銈嗗閺冨爼鏁撻弬銈嗗
//缂佺喖鏁撻弬銈嗗delay
private double totalDelay = 0;
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 { //闁跨喖鎽潏鐐arrival閺冨爼鏁撻弫娆戭劜閹风兘鏁撻弬銈嗗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);
}
}