-
Notifications
You must be signed in to change notification settings - Fork 1
/
NetworkSimulator.java
122 lines (109 loc) · 4.78 KB
/
NetworkSimulator.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
import java.util.*;
import java.io.FileReader;
public class NetworkSimulator {
public static int DEBUG;
/**
* Main method with follwing variables
* @param args[0] file with messages
* @param args[1] time between messages
* @param args[2] loss probability
* @param args[3] corruption probability
* @param args[4] window Size
* @param args[5] mss
* @param args[6] Protocol (buffering or nno buffering of out of order packets)
* @param args[7] Debugging flag
* @param args[8] max receiver buffer size in bytes
* @param args[9] timeout length
*/
public static void main(String[] args) {
//current event to process
Event currentEvent;
//checking to see if enough arguments have been sent
if(args.length < 9) {
System.out.println("need at least 9 arguments");
System.exit(1);
}
//reading in file line by line. Each line will be one message
ArrayList<String> messageArray = readFile(args[0]);
//creating a new timeline with an average time between packets.
Timeline tl = new Timeline(Integer.parseInt(args[1]), messageArray.size());
//creating a new network layer with specific loss and corruption probability.
NetworkLayer nl = new NetworkLayer(Float.parseFloat(args[2]), Float.parseFloat(args[3]), tl);
SenderApplication sa = new SenderApplication(messageArray, nl);
SenderTransport st = sa.getSenderTransport();
//sender and receiver transport needs access to timeline to set timer.
st.setTimeLine(tl);
ReceiverTransport rt = new ReceiverTransport(nl);
//setting window size
st.setWindowSize(Integer.parseInt(args[4]));
st.setMSS(Integer.parseInt(args[5]));
st.setN(messageArray.size());
//setting protocol type
st.setProtocol(Integer.parseInt(args[6]));
rt.setProtocol(Integer.parseInt(args[6]));
DEBUG = Integer.parseInt(args[7]);
//set buffer size and timeout
rt.setBufferSize(Integer.parseInt(args[8]));
st.setTimeout(Integer.parseInt(args[9]));
//this loop will run while there are events in the priority queue
ArrayList<Integer> timestamps = new ArrayList<Integer>();
while(!st.finished()) {
//get next event
currentEvent = tl.returnNextEvent();
//if no event present, break out
if(currentEvent == null) break;
// Print out time
if(DEBUG > 0 && (timestamps.size() == 0 || timestamps.get(timestamps.size() - 1) != currentEvent.getTime())) {
System.out.println(" ----------------------------------------------------------------------- ");
System.out.println("|\t\t\t\033[0;36mTIME:\t\t" + currentEvent.getTime() + "\033[0m\t\t\t\t|");
System.out.println(" ----------------------------------------------------------------------- \n");
timestamps.add(currentEvent.getTime());
}
if(currentEvent.getType() == Event.MESSAGESEND) {
//if event is time to send a message, call the send message function of the sender application.
sa.sendMessage();
} else if (currentEvent.getType() == Event.MESSAGEARRIVE) {
//if event is a message arrival
if(currentEvent.getHost() == Event.SENDER) {
//if it arrives at the sender, call the get packet from the sender
st.receiveMessage(currentEvent.getPacket());
}
else {
//if it arrives at the receiver, call the get packet from the receiver
rt.receiveMessage(currentEvent.getPacket());
}
} else if (currentEvent.getType()==Event.TIMER) {
//If event is an expired timer, call the timerExpired method in the sender transport.
if(DEBUG > 0) {
System.out.println(" ----------------------------------------------------------------------- ");
System.out.println("|\t\t\033[0;36mTIMER:\t\tEXPIRED / TIME = " + currentEvent.getTime() + "\033[0m\t\t\t|");
System.out.println(" ----------------------------------------------------------------------- \n");
}
tl.stopTimer();
st.timerExpired();
} else if (currentEvent.getType() == Event.KILLEDTIMER) {
//do nothing if it is just a turned off timer.
} else if (currentEvent.getType() == Event.RECVREQ) {
//Pass data from reciver transport to receiver application
rt.popBuffer();
} else {
//this should not happen.
System.out.println("Unidentified event type!");
System.exit(1);
}
}
}
//reading in file line by line.
public static ArrayList<String> readFile(String fileName) {
ArrayList<String> messageArray = new ArrayList<String>();
Scanner sc = null;
try{
sc = new Scanner(new FileReader(fileName));
}catch(Exception e) {
System.out.println("Could not open file " + e);
}
while(sc.hasNextLine())
messageArray.add(sc.nextLine());
return messageArray;
}
}