Skip to content

Commit 27351bf

Browse files
author
Sandy W
committed
Added awesome comments!
1 parent e85b7f2 commit 27351bf

File tree

5 files changed

+112
-52
lines changed

5 files changed

+112
-52
lines changed

client/Client.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class Client {
1919
// multiple threads. Here it is used for benchmarking, to store the sum
2020
// of the command completion times for all threads
2121
private static AtomicLong totalTime = new AtomicLong(0);
22-
22+
2323
// this AtomicLong is used to keep track of the current # of running threads
2424
private static AtomicLong runningThreads = new AtomicLong(0);
2525

@@ -77,7 +77,8 @@ else while (menuSelection != 8) {
7777
e.printStackTrace();
7878
}
7979
}
80-
80+
// while runningThreads is not 0, there are still clients waiting for the server
81+
// to send a response, so keep looping (waiting) until they are finished
8182
while (runningThreads.get() != 0) {}
8283

8384
System.out.println("Average response time: " + (totalTime.get() / numProcesses) + " ms\n");
@@ -86,22 +87,32 @@ else while (menuSelection != 8) {
8687

8788
}
8889
//----------------------------------------------------------------------------
90+
/**
91+
* Function to prompt the user for a command to run
92+
* @return command number 1-8
93+
*/
8994
public static int mainMenu() {
9095
int menuSelection = 0;
96+
// loop (and prompt again) until the user's input is an integer between 1 and 8
9197
while ((menuSelection <= 0) || (menuSelection > 8)) {
9298
System.out.println("The menu provides the following choices to the user: ");
9399
System.out.println("1. Host current Date and Time \n2. Host uptime\n"
94100
+ "3. Host memory use \n4. Host Netstat \n5. Host current users "
95-
+ "\n6. Host running processes \n7. Benchmark\n8. Quit ");
101+
+ "\n6. Host running processes \n7. Benchmark (measure mean response time)\n8. Quit ");
96102
System.out.print("Please provide number corresponding to the action you want to be performed: ");
97103
Scanner sc = new Scanner(System.in);
98104
if (sc.hasNextInt()) menuSelection = sc.nextInt();
99105
}
100106
return menuSelection;
101107
}
102108

109+
/**
110+
* Function to prompt the user for a benchmark command to run
111+
* @return command number 1-6
112+
*/
103113
public static int benchmarkMenu() {
104114
int menuSelection = 0;
115+
// loop (and prompt again) until the user's input is an integer between 1 and 6
105116
while ((menuSelection <= 0) || (menuSelection > 6)) {
106117
System.out.println("Which command would you like to benchmark? ");
107118
System.out.println("1. Host current Date and Time \n2. Host uptime\n"
@@ -114,8 +125,13 @@ public static int benchmarkMenu() {
114125
return menuSelection;
115126
}
116127

128+
/**
129+
* Function to prompt the user for how many connections to make (for measuring the mean response time)
130+
* @return number 1-100
131+
*/
117132
public static int numProcessesMenu() {
118133
int menuSelection = 0;
134+
// loop (and prompt again) until the user's input is an integer between 1 and 100
119135
while ((menuSelection <= 0) || (menuSelection > 100)) {
120136
System.out.print("How many connections to the server would you like to open? [1-100]: ");
121137
Scanner sc = new Scanner(System.in);

client/ClientThread.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,30 @@
88

99

1010
public class ClientThread extends Thread {
11+
// every clientThread is passed which command to send to the server
1112
int menuSelection;
13+
// every clientThread is passed the hostname of the server to connect to
1214
String hostName;
13-
Socket socket = null;
15+
Socket socket = null;
16+
17+
// totalTime is used to keep the sum of response times for all threads. after all threads
18+
// have completed it is divided by the total number of threads to get an
19+
// average response time
1420
AtomicLong totalTime;
21+
22+
// runningThreads is the total number of running threads. it is set to numThreads (the number
23+
// of threads that are started) before any threads are started by the Client class. Every time
24+
// a ClientThread finishes it will decrement runningThreads by one, so runningThreads == 0 when
25+
// all threads have finished
1526
AtomicLong runningThreads;
27+
28+
// each class is passed false for printOutput if the number of threads started is > 1. When running more
29+
// than one client thread the clientThreads should not print output, in order to not clutter the screen
1630
boolean printOutput;
1731

32+
// startTime and endTime are used to keep track of the current time when the thread conects to the
33+
// server and when the thread gets a response from the server. The difference between the two
34+
// (endTime - startTime) is the response time
1835
long startTime;
1936
long endTime;
2037

@@ -30,6 +47,7 @@ public void run() {
3047
PrintWriter out = null;
3148
BufferedReader in = null;
3249
try {
50+
// get the current time (before connecting to the server)
3351
startTime = System.currentTimeMillis();
3452

3553
//creates a new Socket object and names it socket.
@@ -54,7 +72,9 @@ public void run() {
5472
if (printOutput) System.out.println(outputString);
5573
}
5674

75+
// get the current time (after connecting to the server)
5776
endTime = System.currentTimeMillis();
77+
// endTime - startTime = the time it took to get the response from the sever
5878
totalTime.addAndGet(endTime - startTime);
5979

6080
}
@@ -65,6 +85,7 @@ public void run() {
6585
catch (IOException e) {
6686
e.printStackTrace();
6787
}
88+
// finally, close the socket and decrement runningThreads
6889
finally {
6990
if (printOutput) System.out.println("closing");
7091
try {

server/CommandExecutor.java

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,83 @@
88
* then executing it and returning the results.
99
*/
1010
public class CommandExecutor {
11-
11+
1212
/**
1313
* Runs the shell command after first using parseCommand() to determine which
1414
* command to run.
1515
*
1616
* @param commandString A string containing a single digit, 1-6;
17-
* @return A string containing the results of the shell command.
17+
* @return A string containing the results of the shell command.
1818
*/
1919
static String run(String commandString) {
2020
String result = "";
2121
String line;
2222
try {
23-
Process child = Runtime.getRuntime().exec(parseCommand(commandString));
23+
// start the shell command running as a child processes
24+
Process child = Runtime.getRuntime().exec(parseCommand(commandString));
25+
26+
// open a BufferedReader to read the output of the child process
27+
BufferedReader output = new BufferedReader(new InputStreamReader(child.getInputStream()));
28+
// while the child process is still outputting, add the output to the result string
29+
while ((line = output.readLine()) != null) {
30+
result = result.concat(line);
31+
result = result.concat("\n");
32+
}
2433

25-
BufferedReader output = new BufferedReader(new InputStreamReader(child.getInputStream()));
26-
while ((line = output.readLine()) != null) {
27-
result = result.concat(line);
2834
result = result.concat("\n");
29-
}
30-
31-
result = result.concat("\n");
32-
result = result.concat("END_MESSAGE");
33-
output.close();
34-
35+
// add "END_MESSAGE" to the result string. When the client sees END_MESSAGE it
36+
// will know that the server is done sending
37+
result = result.concat("END_MESSAGE");
38+
output.close();
39+
3540
} catch (IOException e) {
3641
e.printStackTrace();
3742
}
38-
43+
3944
return result;
4045
}
41-
46+
4247
/**
4348
* Converts the digit string into its respective shell command.
4449
*
4550
* @param inputString A string containing a single digit, 1-6;
46-
* @return
51+
* @return A string containing the shell command to run
4752
*/
4853
static String parseCommand(String inputString) {
4954
int inputInt = Integer.parseInt(inputString);
5055
String commandString = "";
5156
switch (inputInt) {
52-
// Date
53-
case 1:
54-
commandString = "date";
55-
break;
56-
57-
// Uptime
58-
case 2:
59-
commandString = "uptime";
60-
break;
61-
62-
// Memory use
63-
case 3:
64-
commandString = "free";
65-
break;
66-
67-
// netstat
68-
case 4:
69-
commandString = "netstat";
70-
break;
71-
72-
// current users
73-
case 5:
74-
commandString = "who";
75-
break;
76-
77-
// running processes
78-
case 6:
79-
commandString = "ps -e";
80-
break;
57+
// Date
58+
case 1:
59+
commandString = "date";
60+
break;
61+
62+
// Uptime
63+
case 2:
64+
commandString = "uptime";
65+
break;
66+
67+
// Memory use
68+
case 3:
69+
commandString = "free";
70+
break;
71+
72+
// netstat
73+
case 4:
74+
commandString = "netstat";
75+
break;
76+
77+
// current users
78+
case 5:
79+
commandString = "who";
80+
break;
81+
82+
// running processes
83+
case 6:
84+
commandString = "ps -e";
85+
break;
8186
}
82-
87+
8388
return commandString;
8489
}
8590
}

server/Server.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
import java.util.ArrayList;
55
import java.util.concurrent.atomic.AtomicInteger;
66

7-
7+
/*
8+
* Main server class. This class includes main(), and is the class that listens
9+
* for incoming connections and starts ServerThreads to handle those connections
10+
*
11+
*/
812
public class Server {
913

1014
/**
@@ -16,11 +20,16 @@ public static void main(String[] args) {
1620
ArrayList<Thread> list = new ArrayList<Thread>();
1721

1822
try {
23+
// listen for incoming connections on port 4000
1924
ServerSocket socket = new ServerSocket(4000);
2025
System.out.println("Server listening on port 4000");
2126

27+
// loop (forever) until program is stopped
2228
while(true) {
29+
// accept a new connection
2330
Socket client = socket.accept();
31+
// start a new ServerThread to handle the connection and send
32+
// output to the client
2433
Thread thrd = new Thread(new ServerThread(client));
2534
list.add(thrd);
2635
thrd.start();
@@ -33,4 +42,4 @@ public static void main(String[] args) {
3342
ioe.printStackTrace();
3443
}
3544
}
36-
}
45+
}

server/ServerThread.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
import java.net.Socket;
66
import java.util.concurrent.atomic.AtomicInteger;
77

8-
8+
/*
9+
* Individual ServerThread listens for the client to tell it what command to run, then
10+
* runs that command and sends the output of that command to the client
11+
*
12+
*/
913
public class ServerThread extends Thread {
1014
Socket client = null;
1115

@@ -17,22 +21,27 @@ public void run() {
1721
System.out.print("Accepted connection. ");
1822

1923
try {
24+
// open a new PrintWriter and BufferedReader on the socket
2025
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
2126
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
2227
System.out.print("Reader and writer created. ");
2328

2429
String inString;
30+
// read the command from the client
2531
while ((inString = in.readLine()) == null);
2632
System.out.println("Read command " + inString);
2733

34+
// run the command using CommandExecutor and get its output
2835
String outString = CommandExecutor.run(inString);
2936
System.out.println("Server sending: " + outString);
37+
// send the result of the command to the client
3038
out.print(outString);
3139
}
3240
catch (IOException e) {
3341
e.printStackTrace();
3442
}
3543
finally {
44+
// close the connection to the client
3645
out.close();
3746
in.close();
3847
client.close();

0 commit comments

Comments
 (0)