-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClient.java
More file actions
149 lines (134 loc) · 5.55 KB
/
Client.java
File metadata and controls
149 lines (134 loc) · 5.55 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
import java.util.*;
import java.io.*;
import java.util.concurrent.atomic.AtomicLong;
/**
* @author Ikonija Bogojevic
* @author Sandra Weber
* @author Miguel Perez
*
* This is the main Client class, which spawns ClientThreads which connect to the server. This class
* includes methods for obtaining the user's commands and validating them.
*/
public class Client {
private static String hostName;
// create a variable to initialize new threads with
private static Thread thrd = null;
// the threads are kept track of with a linked list
private static LinkedList<Thread> list = new LinkedList<Thread>();
// AtomicLong is a class that is synchronized, and can be used across
// multiple threads. Here it is used for benchmarking, to store the sum
// of the command completion times for all threads
private static AtomicLong totalTime = new AtomicLong(0);
// this AtomicLong is used to keep track of the current # of running threads
private static AtomicLong runningThreads = new AtomicLong(0);
private static boolean printOutput = true;
public static void main(String[] args) {
int menuSelection = 0;
int numProcesses = 1;
// if no hostname is provided, quit
if (args.length == 0) {
System.out.println("User did not enter a host name. Client program exiting.");
System.exit(1);
}
// until the user selects 8, the Exit option, keep looping and
// offering the menu again after running the queries to the server
else while (menuSelection != 8) {
// display the menu and get the user's choice
menuSelection = mainMenu();
// if 8, exit program
if (menuSelection == 8) {
System.out.println("Quitting.");
System.exit(0);
}
// if 7, ask which command should be run in the benchmark mode
// and how many connections to create
if (menuSelection == 7) {
printOutput = false;
menuSelection = benchmarkMenu();
numProcesses = numProcessesMenu();
}
// create threads. since numProcesses is initialized to 1 and gets reset
// at the end of this loop, if the user has not selected to benchmark
// a command, this loop will only create one process
totalTime.set(0);
runningThreads.set(numProcesses);
for (int i = 0; i < numProcesses; i++) {
// make a new thread, tell it the hostname to connect to
// and the command to run. It is also passed the totalTime object,
// so it can record how much time its command took to complete
thrd = new Thread(new ClientThread(args[0], menuSelection, totalTime, printOutput, runningThreads));
thrd.start(); // start the thread
list.add(thrd); // add the thread to the end of the linked list
}
// wait for all of the threads to complete before going to the top
// of the loop again. This ensures that all threads complete before the
// menu is shown again
for (int i = 0; i < numProcesses; i++) {
try {
// wait for the thread to finish
list.get(i).join();
} catch (InterruptedException e) {
// if the join interrupts the thread, print an error
e.printStackTrace();
}
}
// while runningThreads is not 0, there are still clients waiting for the server
// to send a response, so keep looping (waiting) until they are finished
while (runningThreads.get() != 0) {}
System.out.println("Average response time: " + (totalTime.get() / numProcesses) + " ms\n");
numProcesses = 1;
printOutput = true;
}
}
//----------------------------------------------------------------------------
/**
* Function to prompt the user for a command to run
* @return command number 1-8
*/
public static int mainMenu() {
int menuSelection = 0;
// loop (and prompt again) until the user's input is an integer between 1 and 8
while ((menuSelection <= 0) || (menuSelection > 8)) {
System.out.println("The menu provides the following choices to the user: ");
System.out.println("1. Host current Date and Time \n2. Host uptime\n"
+ "3. Host memory use \n4. Host Netstat \n5. Host current users "
+ "\n6. Host running processes \n7. Benchmark (measure mean response time)\n8. Quit ");
System.out.print("Please provide number corresponding to the action you want to be performed: ");
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) menuSelection = sc.nextInt();
}
return menuSelection;
}
/**
* Function to prompt the user for a benchmark command to run
* @return command number 1-6
*/
public static int benchmarkMenu() {
int menuSelection = 0;
// loop (and prompt again) until the user's input is an integer between 1 and 6
while ((menuSelection <= 0) || (menuSelection > 6)) {
System.out.println("Which command would you like to benchmark? ");
System.out.println("1. Host current Date and Time \n2. Host uptime\n"
+ "3. Host memory use \n4. Host Netstat \n5. Host current users "
+ "\n6. Host running processes");
System.out.print("Please provide number corresponding to the action you want to be performed: ");
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) menuSelection = sc.nextInt();
}
return menuSelection;
}
/**
* Function to prompt the user for how many connections to make (for measuring the mean response time)
* @return number 1-100
*/
public static int numProcessesMenu() {
int menuSelection = 0;
// loop (and prompt again) until the user's input is an integer between 1 and 100
while ((menuSelection <= 0) || (menuSelection > 100)) {
System.out.print("How many connections to the server would you like to open? [1-100]: ");
Scanner sc = new Scanner(System.in);
if (sc.hasNextInt()) menuSelection = sc.nextInt();
}
return menuSelection;
}
}