/
WirelessDevice.java
248 lines (198 loc) · 6.66 KB
/
WirelessDevice.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
public class WirelessDevice implements Runnable{
public String Name;
private AccessPoint AP;
private final String MAC;
private String localIPaddress;
public String publicIPaddress;
private int signalStrength; // from 1 to 100
public int obstacles = 0;
private int metersAwayFromAP;
private double throughput;
Stopwatch stopwatch;
long runtime;
boolean running;
///////////////////////////////////////////////////////////////////////////
public WirelessDevice(String name){
this.Name = name;
this.AP = null;
this.MAC = Integer.toHexString(AccessPoint.randInt(0,255)) + "-" + Integer.toHexString(AccessPoint.randInt(0,255)) + "-" +
Integer.toHexString(AccessPoint.randInt(0,255)) + "-" + Integer.toHexString(AccessPoint.randInt(0,255)) + "-" +
Integer.toHexString(AccessPoint.randInt(0,255)) + "-" + Integer.toHexString(AccessPoint.randInt(0,255));
this.localIPaddress = null;
this.publicIPaddress = null;
this.stopwatch = new Stopwatch();
this.throughput = 0.0;
this.runtime = 0;
running = false;
}
///////////////////////////////////////////////////////////////////////////
public boolean isMAC(String mac){
if(mac == this.MAC){
return true;
}
else{
return false;
}
}
///////////////////////////////////////////////////////////////////////////
public void connectToAP(AccessPoint ap){
sendAssociationReq(ap);
}
///////////////////////////////////////////////////////////////////////////
public void sendAssociationReq(AccessPoint ap){
//System.out.println(Name + " sending association request frame to " + AP.getSSID());
ap.requestConnection(this);
}
///////////////////////////////////////////////////////////////////////////
public void receiveAssociationReply(AccessPoint ap, int distance){
this.AP = ap;
this.metersAwayFromAP = distance;
obstacles = AccessPoint.randInt(0, 2);
getIPFromDHCP();
calculateSignalStrength();
}
///////////////////////////////////////////////////////////////////////////
public void getIPFromDHCP(){
// variables
String localIP = AP.getLocalIP();
int num;
String tokens[] = localIP.split("\\.");
// look for last number
num = Integer.parseInt(tokens[3]);
num++;
num+=AP.getLocationInAP(this)+1;
tokens[3] = "" + num;
// set new local ip
this.localIPaddress = tokens[0] + '.' + tokens[1] + '.' + tokens[2] + '.' + tokens[3];
// set public ip
this.publicIPaddress = AP.getPublicIP();
}
///////////////////////////////////////////////////////////////////////////
public void calculateSignalStrength(){
double freq = AP.channel_2G.GHz * Math.pow(10, 9);
double wavelength = MainSimulation.lightspeed / freq;
double signalLoss = 4 * Math.PI * metersAwayFromAP;
signalLoss = signalLoss / wavelength;
signalLoss = Math.pow(signalLoss, 2);
signalLoss = signalLoss / 1000000;
signalStrength = (int)(100 - (signalLoss * obstacles * 2));
}
public void updateThroughput(int size, double time){
if(throughput == 0.0){
throughput = size / time;
}
else{
double newThpt = size / time;
throughput = (throughput + newThpt) / 2;
}
}
///////////////////////////////////////////////////////////////////////////
public void RequestFromInternet(){
// load frame and header with information
Frame frame = new Frame();
frame.loadNetworkData("request for webpage from " + Websites.getRandom() + ".com");
frame.macHeader.sourceMAC = this.MAC;
frame.macHeader.destinationMAC = this.AP.getMAC();
frame.setAssociatedDevice(this);
frame.size = 20 + frame.networkData.length();
// "sense" the channel to see if it's in use
while(AP.channel_2G.inUse){
// there was a collision so increase counter & wait random amnt of time
MainSimulation.numCollisions++;
try {
Thread.sleep(AccessPoint.randInt(50,150));
} catch (InterruptedException e) {}
}
// send request frame to the AP
AP.channel_2G.inUse = true;
stopwatch.start();
AP.channel_2G.sendFrameToAP(frame);
try {
Thread.sleep((100-signalStrength));
} catch (InterruptedException e) {}
AP.channel_2G.inUse = false;
AP.getRequestedFromInternet();
}
///////////////////////////////////////////////////////////////////////////
public void ReceiveReplyFromInternet(){
// for parsing content in frame
Frame IPdatagram[] = new Frame[4];
int i = 0;
// "sense" the channel to see if it's in use
while(AP.channel_2G.inUse){
// there was a collision so increase counter & wait random amnt of time
MainSimulation.numCollisions++;
try {
Thread.sleep(AccessPoint.randInt(50,200));
} catch (InterruptedException e) {}
}
// receive frames from the access point
AP.channel_2G.inUse = true;
// package frames into IP datagram and send to network layer in wireless device
while(!AP.channel_2G.isEmpty()){
IPdatagram[i] = AP.channel_2G.getFrameFromAP();
System.out.println(Name + " received " + IPdatagram[i].networkData);
i++;
}
try {
Thread.sleep((100-signalStrength));
} catch (InterruptedException e) {}
AP.channel_2G.inUse = false;
updateThroughput(IPdatagram[0].size, stopwatch.elapsedTime());
}
/**
* This thread runs a new wireless device on the wifi LAN. It sends
* requests to the access point randomly, trying as accurately as possible
* to simulate a normal user on their smart phone or tablet.
* @pre the wireless device should already be connected to an access point.
* if it is not, this algorithm will not work correctly.
*/
///////////////////////////////////////////////////////////////////////////
public void run(){
// variables
int randomTime,i;
Stopwatch timer = new Stopwatch();
// start simulation
running = true;
timer.start();
while((timer.elapsedTime()*1000) < this.runtime){
// request packets from the internet at random times (1-30 sec)
randomTime = AccessPoint.randInt(1000,3200);
// request a series of packets from random webpage
for(i = 0; i < AccessPoint.randInt(1,10); i++){
RequestFromInternet();
}
// delay
try {
Thread.sleep(randomTime);
} catch (InterruptedException e) {}
}
running = false;
}
///////////////////////////////////////////////////////////////////////////
public void setRunTime(long val){
this.runtime = val;
}
// getters ////////////////////////////////////////////////////////////////
public AccessPoint getAP() {
return AP;
}
public String getMAC() {
return MAC;
}
public String getLocalIP() {
return localIPaddress;
}
public String getPublicIP(){
return publicIPaddress;
}
public double getThroughput(){
return throughput;
}
public int getSignalStrength(){
return signalStrength;
}
public int getDistance(){
return metersAwayFromAP;
}
}