-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBLEDiscSimulator.java
More file actions
680 lines (600 loc) · 25.5 KB
/
BLEDiscSimulator.java
File metadata and controls
680 lines (600 loc) · 25.5 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.Vector;
import java.util.HashMap;
import java.util.Iterator;
import java.io.IOException;
import java.io.OutputStream;
import java.io.ObjectOutput;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.ObjectOutputStream;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.FileInputStream;
import java.io.BufferedInputStream;
import java.io.ObjectInputStream;
//import org.jfree.ui.RefineryUtilities;
import java.util.stream.Stream;
public class BLEDiscSimulator{
private ArrayList<BLESchedule> nodeSchedules = new ArrayList<BLESchedule>();
// listeners do discovery
private ArrayList<ListenEventRecord> currentListeners = new ArrayList<ListenEventRecord>();
// advertisers are discovered
private ArrayList<AdvertisingEventRecord> currentAdvertisers = new ArrayList<AdvertisingEventRecord>();
private ArrayList<CompletedDiscovery> successfulDiscoveries = new ArrayList<CompletedDiscovery>();
private ArrayList<Collision> collisions = new ArrayList<Collision>();
private BLEDiscSimulatorOptions options;
private BLEDiscLogger logger;
private BLEDiscLogger dc;
//private List<BLEScheduleEvent> events = null;
private PriorityQueue<BLEScheduleEvent> eventQueue =
new PriorityQueue<BLEScheduleEvent>(1000, new BLEScheduleEventComparator());
private int numNodes;
private double simulationTime;
private double missingRate;
private double contactTime;
private int logStyle;
//simulation time is specified in number of epochs
public BLEDiscSimulator(String propertiesFile, String logfile, String dcfile){
this.options = new BLEDiscSimulatorOptions(propertiesFile);
this.logger = new BLEDiscLogger(logfile);
this.dc = new BLEDiscLogger(dcfile);
this.logStyle = options.getLogStyle();
if(logStyle == BLEDiscSimulatorOptions.LOG_STYLE_VERBOSE){
logger.log(options.toString());
}
this.numNodes = options.getNumNodes();
this.simulationTime = options.getSimulationTime();
if(options.loadSchedulesFromFile()){
loadSchedules(options.getScheduleLoadFile());
}
else{
createSchedules();
}
if(options.saveSchedulesToFile()){
writeSchedules(options.getScheduleSaveFile());
}
/*for(BLESchedule schedule : nodeSchedules){
schedule.printSchedule();
}*/
/*if(options.showSchedules()){
// TODO: how much should we display, and how is it dependent on the protocol?
ShowSchedules display = new ShowSchedules("BLEnd Node Schedules", nodeSchedules, 4000);
display.pack();
RefineryUtilities.centerFrameOnScreen(display);
display.setVisible(true);
}*/
}
private void createSchedules(){
// first, I need to create the schedules for each node in the simulation
if(!options.controlStartOffset()){
for(int idCounter = 0; idCounter<numNodes; idCounter++){
BLESchedule schedule = null;
if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_BLEND){
schedule = new BLEndSchedule(idCounter, options, simulationTime, null);
}
else if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_SEARCHLIGHT){
schedule = new SearchLightSchedule(idCounter, options, simulationTime, null);
}
else if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_NIHAO){
schedule = new NihaoSchedule(idCounter, options, simulationTime, null);
}
if(schedule != null){
nodeSchedules.add(schedule);
}
}
}
// otherwise, we need to create a data structure to store the selected start offsets and make
// sure that each node selects a compatible one
else{
ArrayList<Double> selectedStartOffsets = new ArrayList<Double>();
for(int idCounter = 0; idCounter<numNodes; idCounter++){
BLESchedule schedule = null;
if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_BLEND){
double[] selectedOffsets = selectedStartOffsets.stream().mapToDouble(d -> d).toArray();
schedule = new BLEndSchedule(idCounter, options, simulationTime, selectedOffsets);
if(schedule != null){
selectedStartOffsets.add(new Double(schedule.getStartOffset()));
}
}
else if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_SEARCHLIGHT){
double[] selectedOffsets = selectedStartOffsets.stream().mapToDouble(i -> i).toArray();
schedule = new SearchLightSchedule(idCounter, options, simulationTime, selectedOffsets);
if(schedule != null){
selectedStartOffsets.add(new Double(schedule.getStartOffset()));
}
}
else if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_NIHAO){
double[] selectedOffsets = selectedStartOffsets.stream().mapToDouble(i -> i).toArray();
schedule = new NihaoSchedule(idCounter, options, simulationTime, selectedOffsets);
if(schedule != null){
selectedStartOffsets.add(new Double(schedule.getStartOffset()));
}
}
if(schedule != null){
nodeSchedules.add(schedule);
}
}
}
makeEventQueue();
}
private void makeEventQueue(){
// then I need to add each event in each schedule to the event queue
// EVENTS DELETED events = new ArrayList<BLEScheduleEvent>();
for(BLESchedule nodeSchedule : nodeSchedules){
ArrayList<BLEScheduleEvent> toAdd = nodeSchedule.getSchedule();
// EVENTS DELETED toAdd.forEach(event -> events.add(event));
toAdd.forEach(event -> eventQueue.add(event));
}
// then, I want them in order, so sort them.
// EVENTS DELETED (but it's ok... it's a priority queue now) events.sort((e1, e2) -> BLEScheduleEvent.compareEvents(e1,e2));
}
private void simulate(){
// events contains the sequential list of events to simulate
// I'm not using an iterator because eventually I want to be able to insert into this list
// while I traverse it.
// EVENTS DELETED events.forEach(event -> processEvent(event));
BLEScheduleEvent nextEvent = eventQueue.poll();
while(nextEvent != null){
processEvent(nextEvent);
nextEvent = eventQueue.poll();
}
// we can only safely log discoveries once the simulation has completed (because of the funky
// way I'm "undoing" discoveries in the case of collisions.
// because of the way the list is implemented, the discovery events should be in order by time
// however, we all know what happens when you assume...
// so let's sort them, just to be sure.
successfulDiscoveries.sort((d1, d2) -> Double.compare(d1.timestamp, d2.timestamp));
computeCDFData();
if(logStyle == BLEDiscSimulatorOptions.LOG_STYLE_BRIEF){
logger.log("A : B : time\n");
}
Vector<String> discoveryStrings = new Vector<String>();
for(CompletedDiscovery cd : successfulDiscoveries){
if(logStyle == BLEDiscSimulatorOptions.LOG_STYLE_BRIEF){
logger.log(cd.discovererID + " : " + cd.discoveredID + " : " + cd.timestamp + "\n");
}
else if(logStyle == BLEDiscSimulatorOptions.LOG_STYLE_VERBOSE){
logger.log("N: " + cd.timestamp + " : " + cd.discovererID + " : " + cd.discoveredID + "\n");
}
}
logger.close();
if(options.printStatistics()){
nodeSchedules.forEach(nodeSchedule -> printStatistics(nodeSchedule));
}
dc.close();
}
private void printStatistics(BLESchedule nodeSchedule){
int nodeID = nodeSchedule.getNodeID();
String stats = nodeSchedule.getDutyCycle() + ","
+ nodeSchedule.getConsumption() + ","
+ nodeSchedule.getConsumptionWithIdleCost(0.001) + ","
+ nodeSchedule.getConsumptionWithIdleCost(0.339)+"\n";
//+ nodeSchedule.getConsumptionWithIdleCost(0.080)+"\n";
// System.out.println(stats);
dc.log(stats);
/*System.out.println("Duty cycle of node " + nodeID + ": " +
nodeSchedule.getDutyCycle());
System.out.println("Node " + nodeID + "'s discovery rate: " +
computeDiscoveryRate(nodeID, simulationTime/2));
System.out.println("Node " + nodeID + "'s average discovery latency: " +
computeAverageDiscoveryLatency(nodeID, simulationTime/2));*/
}
// if we're just printing the data for the CDF generation in R, we just need to, for each node,
// find the *first* time it discovered each other time (starting at some randomly selected "contact"
// time. We randomly select the contact time as being between T (or t) (for warmup) and simulationTime/2
private void computeCDFData(){
double warmupPeriod = 0;
if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_BLEND){
warmupPeriod = options.getT();
}
else if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_SEARCHLIGHT){
warmupPeriod = options.getT() * options.getSlotLength();
}
else if(options.getProtocol() == BLEDiscSimulatorOptions.PROTOCOL_NIHAO){
warmupPeriod = options.getN() * options.getN() * options.getSlotLength();
}
double intervalSize = (simulationTime/2) - options.getT();
double contactTime = (Math.random() * intervalSize) + options.getT()+260; //TODO: Changes with +260
//System.out.println("SETTING CONTACT TIME EXPLICITLY!");
//int contactTime = 8638;
// buono. the discoveries are in order, so we scroll through them to find the first event that happens
// at or after the contact time
Iterator<CompletedDiscovery> it = successfulDiscoveries.iterator();
CompletedDiscovery next = null;
if(it.hasNext()){
next = it.next();
}
while(next != null && next.timestamp < contactTime){
if(it.hasNext()){
next = it.next();
}
else{
next = null;
}
}
// if(next != null){
double[][] discoveryLatencies = new double[numNodes][numNodes];
for(int i = 0; i<numNodes; i++){
for(int j = 0; j<numNodes; j++){
discoveryLatencies[i][j] = Double.MAX_VALUE;
}
}
// this will count how many things are in the 2D array. When this counter gets to
// n*n, we're done (TODO: not true for unidirectional discovery...)
int count = 0;
for(int i = 0; i<numNodes; i++){
// a node always discovers itself in 0 time
discoveryLatencies[i][i] = 0;
count++;
}
if(next != null){
// next references the first discovery event after the selected contactTime
// now we'll just scroll through the discovery events, using the info to fill up our 2D array
// we can quit when either (a) the array is full (i.e., count = n*n) or we run out of events
while(next != null && (count < (numNodes*numNodes))){
int discovererID = next.discovererID;
int discoveredID = next.discoveredID;
double discoveryTime = next.timestamp - contactTime;
if(discoveryLatencies[discovererID][discoveredID] > simulationTime){
discoveryLatencies[discovererID][discoveredID] = discoveryTime;
count++;
}
if(it.hasNext()){
next = it.next();
}
else{
next = null;
}
}
/*if(count < (numNodes * numNodes)){
System.out.println("OOPS!");
}*/
}
// if we run out of events without getting to n*n,
// anything that is still MAX_INT indicates a discovery that never happened
if(logStyle == BLEDiscSimulatorOptions.LOG_STYLE_CDF){
for(int i = 0; i<numNodes; i++){
for(int j = 0; j<numNodes; j++){
if(i != j){
// logger.log(discoveryLatencies[i][j] + "," + i + "," + j + "\n");
logger.log(discoveryLatencies[i][j] + "\n");
}
}
}
}
// logger.log("----------\n");
// }
}
// this is providing me some polymorphic static binding. It's ok. I read about it on the Internet.
private void processEvent(BLEScheduleEvent bse){
// this calls to the base class, but the method is abstract, implemented in only the derived classes
// each derived class calls back to the below "process" method with the correct derived type
// is this easier than using instanceof? Meh.
if(!bse.isInWPScan() && !bse.isInWPAdv() && !bse.isPkLoss()) {
bse.process(this);
}
}
public void process(BLEListenStartEvent blse){
// create a record for the listener in case he discovers someone
ListenEventRecord myListenEventRecord = new ListenEventRecord(blse.getNodeID(), blse.getChannel(), blse.getTime());
// only add to currentListeners when it is not in Warmup interval
currentListeners.add(myListenEventRecord);
if(logStyle == BLEDiscSimulatorOptions.LOG_STYLE_VERBOSE){
logger.log("L" + blse.getChannel() + ": " + blse.getTime() + " : " + blse.getNodeID() + "\n");
}
}
public void process(BLEListenEndEvent blee){
// only process this when it is not in Warmup interval
// remove the record for the listener
ListenEventRecord myListenEventRecord = getListenEventRecordForNodeID(blee.getNodeID());
currentListeners.remove(myListenEventRecord);
// if myDiscoveries.discoveryEvents.size() != 0, I successfully discovered someone!
for(ListenEventRecord.DiscoveryEvent de : myListenEventRecord.discoveryEvents){
successfulDiscoveries.add(new CompletedDiscovery(blee.getNodeID(), de.discoveredNode, de.timestamp));
}
}
public void process(BLEAdvertiseStartEvent base){
// if we're not modeling the three channels, we do what we originally did, which is to
// just assume that the beacon lasts for entire time (i.e., 3ms), that any listener listening on any channel
// will hear it, as long as they are listening for the whole beacon time
double time = base.getTime();
if (logStyle == BLEDiscSimulatorOptions.LOG_STYLE_VERBOSE) {
logger.log("A: " + base.getTime() + " : " + base.getNodeID() + "\n");
}
if (!options.modelChannels()) {
processSingleBeacon(base, 0); // just use channel 0; no one's going to check
} else {
// we need to create three beacons on the three different beacon channels.
// then we need to insert the second two into the event queue and immediately process the first
// TODO: instead of doing +1, +2, and +3, we should really do b, 2b, and 3b
BLEAdvertiseOneChannelStartEvent beacon1 = new BLEAdvertiseOneChannelStartEvent(base.getNodeID(), base.getTime(),
BLEScheduleEvent.ADVERTISEMENT_CHANNEL_ONE);
BLEAdvertiseOneChannelEndEvent endbeacon1 = new BLEAdvertiseOneChannelEndEvent(base.getNodeID(), base.getTime() + 1,
BLEScheduleEvent.ADVERTISEMENT_CHANNEL_ONE);
BLEAdvertiseOneChannelStartEvent beacon2 = new BLEAdvertiseOneChannelStartEvent(base.getNodeID(), base.getTime() + 1,
BLEScheduleEvent.ADVERTISEMENT_CHANNEL_TWO);
BLEAdvertiseOneChannelEndEvent endbeacon2 = new BLEAdvertiseOneChannelEndEvent(base.getNodeID(), base.getTime() + 2,
BLEScheduleEvent.ADVERTISEMENT_CHANNEL_TWO);
BLEAdvertiseOneChannelStartEvent beacon3 = new BLEAdvertiseOneChannelStartEvent(base.getNodeID(), base.getTime() + 2,
BLEScheduleEvent.ADVERTISEMENT_CHANNEL_THREE);
BLEAdvertiseOneChannelEndEvent endbeacon3 = new BLEAdvertiseOneChannelEndEvent(base.getNodeID(), base.getTime() + 3,
BLEScheduleEvent.ADVERTISEMENT_CHANNEL_THREE);
// all I need to do with the priority queue is add these new events!
eventQueue.add(beacon1);
eventQueue.add(endbeacon1);
eventQueue.add(beacon2);
eventQueue.add(endbeacon2);
eventQueue.add(beacon3);
eventQueue.add(endbeacon3);
}
}
public void process(BLEAdvertiseOneChannelStartEvent baocse){
if (logStyle == BLEDiscSimulatorOptions.LOG_STYLE_VERBOSE) {
logger.log("A" + baocse.getChannel() + ": " + baocse.getTime() + " : " + baocse.getNodeID() + "\n");
}
processSingleBeacon(baocse, baocse.getChannel());
}
// depending on whether we're modeling the three channels or not, this could be a BLEAdvertiseStartEvent or it could actually be
// a BLEAdvertiseOneChannelStartEvent. This will matter for determining whether or not we check the channel against the listener's channel.
private void processSingleBeacon(BLEAdvertiseStartEvent base, int channel){
// create a record to store the other devices that have discovered me
// this is actually so we can correct for conflicts
AdvertisingEventRecord myAdvertisingEventRecord = new AdvertisingEventRecord(base.getNodeID(), channel, base.getTime());
currentAdvertisers.add(myAdvertisingEventRecord);
// when I start advertising, I assume that every listener discovers me. If they're not still
// listening when I stop, I'll remove them. Also, if we detect a collision, we'll remove them
// let's do collisions first. If there's a collision, I'm just gonna assume that no listener
// hears this advertisement
if (options.modelCollisions() && currentAdvertisers.size() > 1) { // it should be 1... that's me!
// collision happened
// cycle through all of the current advertisers
for (AdvertisingEventRecord otherAdvertiser : currentAdvertisers) {
// if we're modeling the three BLE channels, we need to ensure that the two advertisers' channels match. If they don't,
// then we don't actually have a collision
// therefore, we only proceed for this advertiser if either (a) we're not modeling channels OR the two advertsisers are on
// the same channel
// because the parameter for this method is only a BLEAdvertiseOneChannelStart event if we're modeling channels,
// we have to check and cast...
boolean proceed = true;
if (options.modelChannels()) {
BLEAdvertiseOneChannelStartEvent baocse = (BLEAdvertiseOneChannelStartEvent) base;
if (otherAdvertiser.channel != baocse.getChannel()) {
proceed = false;
}
}
if (proceed) {
// all I have to do is mark the advertiser as a collider. Then when we end advertising, we can handle discovery.
otherAdvertiser.collided = true;
// for bookkeeping, we keep track of the collisions. So log it.
for (Integer discovererNode : otherAdvertiser.discovererNodes) {
int discovererNodeID = discovererNode.intValue();
ListenEventRecord listenerRecord = getListenEventRecordForNodeID(discovererNodeID);
if (listenerRecord != null) {
if (logStyle == BLEDiscSimulatorOptions.LOG_STYLE_VERBOSE) {
logger.log("C: " + base.getTime() + " : " + base.getNodeID() + " : "
+ otherAdvertiser.nodeID + " : (" + discovererNodeID + ")\n");
}
}
// we want to keep track of the collision, just for completeness
Collision c = new Collision(base.getNodeID(), otherAdvertiser.nodeID, discovererNodeID, base.getTime());
collisions.add(c);
}
}
}
}
}
public void process(BLEAdvertiseEndEvent baee){
// if I didn't collide, then the current listeners (who were also listening when I started) discovered me
AdvertisingEventRecord aer = getAdvertisingEventRecordForNodeID(baee.getNodeID());
// OK, so this is maybe terrible, but the one time this aer might be null is if we're modeling the three channels and this is the
// leftover end event that the schedule originally had, but was replaced by the three end events for the individual beacons.
// in those cases, we can just ignore those beacons, so we just skip the rest of this method...
if (aer != null) {
if (!aer.collided) {
for (ListenEventRecord listenerRecord : currentListeners) {
// add each discoverer
if (listenerRecord.startTime <= aer.startTime) {
// if we're modeling channels, we have to make sure the channels match, too. Therefore, we log a discovery event if:
// (a) we're not modeling channels or (b) the channels match
if (!options.modelChannels() || aer.channel == listenerRecord.channel) {
aer.addDiscovererNode(listenerRecord.nodeID);
// create the discovery event for the listener
listenerRecord.addDiscoveryEvent(baee.getNodeID(), aer.startTime);
// some protocols (e.g., BLEnd with bidirectional discovery) need to trigger some behavior
// upon a successful discovery. So we need to grab the BLESchedule associatd with the discoverer
// and call the onDiscoveryEvent callback
BLESchedule discoverersSchedule = getScheduleForNodeID(listenerRecord.nodeID);
discoverersSchedule.onDiscovery(baee, this);
}
}
}
}
// to completely stop advertising, I remove the aer
currentAdvertisers.remove(aer);
}
}
// this will grab the DiscoveryEvent object associated with a given node id
// it's a helper method to assist the collision detection algorithm
private ListenEventRecord getListenEventRecordForNodeID(int nodeID){
for(ListenEventRecord listenRecord : currentListeners){
if(listenRecord.nodeID == nodeID){
return listenRecord;
}
}
return null;
}
// this is similarly a helper method for me to find the right advertising event record so I can remove it
private AdvertisingEventRecord getAdvertisingEventRecordForNodeID(int nodeID){
for(AdvertisingEventRecord record : currentAdvertisers){
if(record.nodeID == nodeID){
return record;
}
}
return null;
}
public BLESchedule getScheduleForNodeID(int nodeID){
for(BLESchedule schedule : nodeSchedules){
if(schedule.getNodeID() == nodeID){
return schedule;
}
}
return null;
}
@SuppressWarnings("unchecked")
private void loadSchedules(String scheduleLoadFile){
System.out.println("Loading schedules");
try{
InputStream file = new FileInputStream(scheduleLoadFile);
InputStream buffer = new BufferedInputStream(file);
ObjectInput ois = new ObjectInputStream(buffer);
try{
nodeSchedules = (ArrayList<BLESchedule>)ois.readObject();
} catch(ClassNotFoundException cnfe){
cnfe.printStackTrace();
} finally{
ois.close();
}
} catch(IOException ioe){
ioe.printStackTrace();
}
makeEventQueue();
}
private void writeSchedules(String scheduleSaveFile){
try{
OutputStream file = new FileOutputStream(scheduleSaveFile);
OutputStream buffer = new BufferedOutputStream(file);
ObjectOutput oos = new ObjectOutputStream(buffer);
try{
oos.writeObject(nodeSchedules);
} finally {
oos.flush();
oos.close();
}
} catch(IOException ioe){
ioe.printStackTrace();
}
}
private double computeDiscoveryRate(int nodeID, double fromTime){
ArrayList<Integer> discoveredNodeIDs = new ArrayList<Integer>();
for(CompletedDiscovery discoveryEvent : successfulDiscoveries){
if(discoveryEvent.discovererID == nodeID){
Integer discoveredIDInteger = new Integer(discoveryEvent.discoveredID);
if(!discoveredNodeIDs.contains(discoveredIDInteger)){
if(discoveryEvent.timestamp > fromTime){
discoveredNodeIDs.add(discoveredIDInteger);
}
}
}
}
return discoveredNodeIDs.size() / (double) (nodeSchedules.size() - 1);
}
private double computeAverageDiscoveryLatency(int nodeID, double fromTime){
HashMap<Integer, Double> nodesDiscoveryEvents = new HashMap<Integer, Double>();
for(CompletedDiscovery discoveryEvent : successfulDiscoveries){
if(discoveryEvent.discovererID == nodeID){
Integer discoveredIDInteger = new Integer(discoveryEvent.discoveredID);
if(!nodesDiscoveryEvents.containsKey(discoveredIDInteger)){
if(discoveryEvent.timestamp > fromTime){
nodesDiscoveryEvents.put(discoveredIDInteger,
new Double(discoveryEvent.timestamp - fromTime));
}
}
}
}
double sumOfLatencies = nodesDiscoveryEvents.values().stream().mapToDouble(d -> d).sum();
return sumOfLatencies / (double) nodesDiscoveryEvents.size();
}
public static void main(String[] args){
//System.out.println("BLEDiscSimulator Starting...");
if(args.length != 4){
System.out.println("Usage: java BLEDiscSimulator <propertiesfile> <logfile> <numberofruns>");
}
else{
int numRuns = Integer.parseInt(args[3]);
for(int i = 0; i<numRuns; i++){
BLEDiscSimulator simulator = new BLEDiscSimulator(args[0], args[1], args[2]);
simulator.simulate();
}
}
}
class CompletedDiscovery{
int discovererID;
int discoveredID;
double timestamp;
CompletedDiscovery(int discovererID, int discoveredID, double timestamp){
this.discovererID = discovererID;
this.discoveredID = discoveredID;
this.timestamp = timestamp;
}
public int compareTo(CompletedDiscovery d2){
return Double.compare(timestamp, d2.timestamp);
}
}
class Collision{
int node1;
int node2;
int listener;
double timestamp;
Collision(int node1, int node2, int listener, double timestamp){
this.node1 = node1;
this.node2 = node2;
this.timestamp = timestamp;
}
}
// this class is used by the listening task to keep track of nodes that are discovered
// (and timestamps for that discovery)
class ListenEventRecord{
int nodeID;
int channel;
double startTime;
ArrayList<DiscoveryEvent> discoveryEvents = new ArrayList<DiscoveryEvent>();
ListenEventRecord(int nodeID, int channel, double startTime){
this.nodeID = nodeID;
this.channel = channel;
this.startTime = startTime;
}
void addDiscoveryEvent(int nodeID, double timestamp){
discoveryEvents.add(new DiscoveryEvent(nodeID, timestamp));
}
void removeDiscoveryEvent(int nodeID){
for(int i = 0; i<discoveryEvents.size(); i++){
if(discoveryEvents.get(i).discoveredNode == nodeID){
discoveryEvents.remove(i);
}
}
}
class DiscoveryEvent{
int discoveredNode;
double timestamp;
DiscoveryEvent(int discoveredNode, double timestamp){
this.discoveredNode = discoveredNode;
this.timestamp = timestamp;
}
}
}
// this class is used in the advertising task to keep track of the nodes that have discovered
// the advertiser. Note that this is not really something that an advertiser can KNOW, but we
// use it simply to keep track of collisions and to undo discovery events after the fact
class AdvertisingEventRecord{
int nodeID;
boolean collided;
int channel;
double startTime;
ArrayList<Integer> discovererNodes = new ArrayList<Integer>();
AdvertisingEventRecord(int nodeID, int channel, double startTime){
this.nodeID = nodeID;
this.channel = channel;
this.startTime = startTime;
}
void addDiscovererNode(int nodeID){
discovererNodes.add(new Integer(nodeID));
}
void removeDiscovererNode(int nodeID){
discovererNodes.remove(new Integer(nodeID));
}
}
}