-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientThread.java
More file actions
110 lines (94 loc) · 3.86 KB
/
ClientThread.java
File metadata and controls
110 lines (94 loc) · 3.86 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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.net.ConnectException;
import java.util.concurrent.atomic.AtomicLong;
public class ClientThread extends Thread {
// every clientThread is passed which command to send to the server
int menuSelection;
// every clientThread is passed the hostname of the server to connect to
String hostName;
Socket socket = null;
// totalTime is used to keep the sum of response times for all threads. after all threads
// have completed it is divided by the total number of threads to get an
// average response time
AtomicLong totalTime;
// runningThreads is the total number of running threads. it is set to numThreads (the number
// of threads that are started) before any threads are started by the Client class. Every time
// a ClientThread finishes it will decrement runningThreads by one, so runningThreads == 0 when
// all threads have finished
AtomicLong runningThreads;
// each class is passed false for printOutput if the number of threads started is > 1. When running more
// than one client thread the clientThreads should not print output, input order to not clutter the screen
boolean printOutput;
// startTime and endTime are used to keep track of the current time when the thread conects to the
// server and when the thread gets a response from the server. The difference between the two
// (endTime - startTime) is the response time
long startTime;
long endTime;
ClientThread(String hostName, int menuSelection, AtomicLong totalTime, boolean printOutput, AtomicLong runningThreads) {
this.menuSelection = menuSelection;
this.hostName = hostName;
this.totalTime = totalTime;
this.printOutput = printOutput;
this.runningThreads = runningThreads;
}
public void run() {
PrintWriter out = null;
BufferedReader input = null;
try {
//creates a new Socket object and names it socket.
//Establishes the socket connection between the client & server
//name of the machine & the port number to which we want to connect
socket = new Socket(hostName, 15432);
if (printOutput) {
System.out.print("Establishing connection.");
}
//opens a PrintWriter on the socket input autoflush mode
out = new PrintWriter(socket.getOutputStream(), true);
//opens a BufferedReader on the socket
input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if (printOutput) System.out.println("\nRequesting output for the '" + menuSelection + "' command from " + hostName);
// get the current time (before sending the request to the server)
startTime = System.currentTimeMillis();
// send the command to the server
out.println(Integer.toString(menuSelection));
if (printOutput) System.out.println("Sent output");
// read the output from the server
String outputString;
while (((outputString = input.readLine()) != null) && (!outputString.equals("END_MESSAGE"))) {
if (printOutput) System.out.println(outputString);
}
// get the current time (after connecting to the server)
endTime = System.currentTimeMillis();
// endTime - startTime = the time it took to get the response from the sever
totalTime.addAndGet(endTime - startTime);
}
catch (UnknownHostException e) {
System.err.println("Unknown host: " + hostName);
System.exit(1);
}
catch (ConnectException e) {
System.err.println("Connection refused by host: " + hostName);
System.exit(1);
}
catch (IOException e) {
e.printStackTrace();
}
// finally, close the socket and decrement runningThreads
finally {
if (printOutput) System.out.println("closing");
try {
socket.close();
runningThreads.decrementAndGet();
System.out.flush();
}
catch (IOException e ) {
System.out.println("Couldn't close socket");
}
}
}
}