-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbfclient.java
More file actions
219 lines (179 loc) · 6.91 KB
/
bfclient.java
File metadata and controls
219 lines (179 loc) · 6.91 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
import java.net.*;
import java.io.*;
public class bfclient {
private static RoutingTable myTable;
private static int localPort;
private static int timeout;
private static boolean isRunning = true;
private static int MAXSIZE = 512;
private static DatagramSocket listenSocket;
public static void main(String[] args) {
readCommandLine(args);
listenToSocket();
manageTimeouts();
listenToUser();
}
public static void readCommandLine(String[] args) {
//used at startup, reads commands and neighbor data into routing table
if(args.length < 3 || ((args.length-2)%3 != 0)) {
System.out.println("Usage: java bfclient <localport> <timeout> <neighbor1 ip> <neighbor1 port> <neighbor1 link cost>");
System.exit(1);
}
localPort = Integer.parseInt(args[0]);
timeout = Integer.parseInt(args[1]) * 1000;
try {
myTable = new RoutingTable(localPort, timeout);
//int num_neighbors = Integer.parseInt(args[2]);
//if((args.length -3) != num_neighbors*3 ) {
// System.exit(1);
//}
int argIndex = 2;
for(int i = 0; i<(args.length-2)/3; i++){
myTable.addNeighbor(args[argIndex], args[argIndex+1], args[argIndex + 2]);
argIndex += 3;
}
}
catch (Exception e) {
System.out.println("Error allocating RT");
e.printStackTrace();
System.exit(1);
}
//initialize sending socket
try {
listenSocket = new DatagramSocket(localPort);
}
catch (SocketException e){
System.out.println("SocketError couldn't bind to port "+e );
System.exit(1);
}
}
//listen for updates from other sockets - modifying your dv as a result
//only listening
//send out your dv on a scheduled basis so that your neighbors know you exist
//only sending
//listen for user commands on command line, and execute as such
//only sending
public static void handleUserCommands(String input){
try {
//Handles logic behind reading user argumeants
String[] parts = input.split(" |\\:");
if(parts[0].equalsIgnoreCase("SHOWRT")) {
myTable.printRoutingTable();
return;
}
if(parts[0].equalsIgnoreCase("CLOSE")){
//do close
isRunning = false;
System.exit(1);
return;
}
if(parts.length != 3) {
//invalid format
return;
}
if(parts[0].equalsIgnoreCase("LINKDOWN")){
myTable.userLinkCommand(parts[1], parts[2], false);
return;
}
if(parts[0].equalsIgnoreCase("LINKUP")){
myTable.userLinkCommand(parts[1], parts[2], true);
return;
}
}
catch (Exception e) {
System.out.println("Error allocating handling user commanding");
e.printStackTrace();
System.exit(1);
}
}
public static void listenToUser() {
BufferedReader input = new BufferedReader (new InputStreamReader(System.in));
//affirm execution
isRunning = true;
//print command list to user
while(isRunning) {
try {
handleUserCommands(input.readLine());
}
catch (Exception e){
System.out.println("Error occured whilst waiting on your directive master "+e);
}
}
//stopped executing
System.out.println("Closing time - you don't have to go home but you can't stay here");
try {
input.close();
}
catch (Exception e){
System.out.println("Time for you to go out to the places you will be from " +e);
}
System.exit(0);
}
public static void listenToSocket() {
Thread t = new Thread(new Runnable() {
public void run() {
while(isRunning) {
byte[] data = new byte[MAXSIZE];
InetAddress senderAddress = null;
int inputSize = 0;
DatagramPacket packet = new DatagramPacket(data, data.length);
//System.out.println("Waiting in receive loop");
try {
listenSocket.receive(packet);
senderAddress = packet.getAddress();
inputSize = packet.getLength();
//System.out.println("Received from "+ packet.getSocketAddress());
}
catch (IOException e) {
System.out.println("IO Error "+e);
}
byte[] recvData = new byte[inputSize];
System.arraycopy(packet.getData(), 0, recvData, 0, inputSize);
//System.out.println("Processing input now");
try {
myTable.handleReceivedInputs(recvData, senderAddress);
}
catch (Exception e) {
System.out.println("Error handling received inputs");
e.printStackTrace();
System.exit(1);
}
}
}
});
t.start();
}
public static void manageTimeouts(){
Thread t = new Thread(new Runnable() {
public void run() {
long time = System.currentTimeMillis();
long check = System.currentTimeMillis();
long i = 0;
try{
myTable.initialLinkUp();
}
catch (Exception e) {
e.printStackTrace();
}
while (isRunning) {
if((check - time) > (timeout - 200) ){
try{
myTable.sendRouteUpdate();
}
catch (Exception e) {
System.out.println("Error sending route update");
e.printStackTrace();
System.exit(1);
}
myTable.scanForNeighborTimeout(check);
time = System.currentTimeMillis();
i = 0;
}
check = System.currentTimeMillis();
i++;
}
}
});
t.start();
}
}