-
Notifications
You must be signed in to change notification settings - Fork 1
/
Timeline.java
113 lines (103 loc) · 4.3 KB
/
Timeline.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
import java.util.*;
/**
* This class represents the timeline of events in a priority queue
*/
public class Timeline {
private PriorityQueue<Event> events; //timeline of events.
private int timeBetweenSends; //Avg. time between two packets being sent
private int totalMessagesToSend; //total number of messages to send
private int sentSoFar; //number fo messages sent so far
private int timeSoFar; // time which has passed so far
private Random ran; //random number generator
private int lastArrivalTime; //last arrival time so far
private Event timerPointer; //pointer to currently running timer
private int nextRecvAppReq; //time at which receiver application will next request data from receiver transport
/**
* A constructor to initialize variables.
*/
public Timeline(int time, int numOfMessages) {
events = new PriorityQueue<Event>();
timeBetweenSends = time;
totalMessagesToSend = numOfMessages;
ran = new Random();
timeSoFar = 0;
sentSoFar = 1; //set to one because we send the fisrt packet right away
lastArrivalTime = 0;
nextRecvAppReq = 0;
createSendEvent();//sengin first packet
timerPointer=null;
}
/**
* Getting next event. It it is a send event, and there are still messages that need to be sent, sending the next one.
*/
public Event returnNextEvent() {
Event tmp = events.poll();
if(tmp == null)
return tmp;
timeSoFar = tmp.getTime();
if(tmp.getType() == Event.MESSAGESEND && sentSoFar < totalMessagesToSend) {
createSendEvent();
sentSoFar++;
}
// Creates periodic polling from receiver application layer
// to draw in order packets from receive buffer
if(timeSoFar >= nextRecvAppReq) {
int newRecvReq = timeSoFar + (int)(ran.nextGaussian() * 2 + 10);
nextRecvAppReq = Math.max(newRecvReq, nextRecvAppReq + 1);
events.add(new Event(nextRecvAppReq, Event.RECVREQ, Event.RECEIVER));
}
return tmp;
}
/**
* Creating a send event.First generating a random enumber using the exponential distribution with average timeBetweenSends and then adding the event.
*/
public void createSendEvent() {
double tmp = ran.nextFloat();
tmp = (tmp == 0) ? 0.00001 : tmp;
int time = (int)(timeBetweenSends * (-Math.log(tmp)) + timeSoFar);
if(NetworkSimulator.DEBUG > 2)
System.out.println("inserting fututre send event at " + timeSoFar + " with time: " + time );
events.add(new Event(time, Event.MESSAGESEND, Event.SENDER));
}
/**
* Creating an arrive event. This first checks for the last arrival time (since packets cannot be reordered), and the adds a random number uniformly distributed from 1-9
* to calculate the time of the arrival event. It then adds the event to the queue.
* @param pkt packet that will arrive
* @param to who are we sending the packet to
*/
public void createArriveEvent(Packet pkt, int to) {
int newArrivalTime = timeSoFar + (int)(ran.nextGaussian() * 2 + 10);
lastArrivalTime = Math.max(newArrivalTime,lastArrivalTime + 1);
if(NetworkSimulator.DEBUG > 2) {
String tmp = (to == Event.SENDER) ? "sender" : "receiver";
System.out.println("inserting futurre arrive event at " + timeSoFar + " with time: " + lastArrivalTime + "to :" +tmp);
}
events.add(new Event(lastArrivalTime,Event.MESSAGEARRIVE,to,pkt));
}
/**
* Starting timer.If it si already started it prints out an error message. setting timerPointer to point at timer event.
* @ param increment timeout for timer
*/
public void startTimer(int increment) {
if(timerPointer != null) {
if (NetworkSimulator.DEBUG >= 1) System.out.println("|\t\033[0;32mTIMER:\t\tON\033[0m\t\t\t\t\t\t|");
return;
}
timerPointer = new Event(timeSoFar+increment,Event.TIMER,Event.SENDER);
if (NetworkSimulator.DEBUG >= 1) System.out.println("|\t\033[0;32mTIMER:\t\tSTARTING\033[0m\t\t\t\t\t|");
events.add(timerPointer);
if(NetworkSimulator.DEBUG > 2)
System.out.println("inserting future timer event at time: " + timeSoFar + " for " + increment);
}
/**
* Kills timer and sets timerPointer to null
*/
public void stopTimer() {
if(timerPointer == null) {
if (NetworkSimulator.DEBUG >= 1) System.out.println("Timer is not on!");
return;
}
timerPointer.killTimer();
timerPointer = null;
}
}