Repository navigation
Expand file tree
/
Copy pathCSCv1.java
More file actions
executable file
·110 lines (93 loc) · 4.18 KB
/
Copy pathCSCv1.java
File metadata and controls
executable file
·110 lines (93 loc) · 4.18 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
/**
* The CSCv1 class is a threaded socket server client that sends commands to a server,
* tracks the request time, then prints the result.
*
*/
import java.net.*;
import java.io.*;
import java.util.Scanner;
import java.util.ArrayList;
class ClientThread extends Thread {
String hostname;
int port;
String command;
long start = 0;
long end = 0;
ClientThread( String name, String hname, int portNumber, String c, long time ) {
super( name );
hostname = hname;
port = portNumber;
command = c;
start = time; // Record task's start time on thread creation
this.start(); // Start thread immediately upon creation
}
public long getRuntime() {
if( end != 0 ) // Double-check thread has finished
return end - start; // Report task duration in ns
else
return -1;
}
public void run() {
try( Socket socket = new Socket( hostname, port ) ) {
// Open input stream and reader
InputStream input = socket.getInputStream();
BufferedReader reader = new BufferedReader( new InputStreamReader( input ) );
// Open output stream and writer
OutputStream output = socket.getOutputStream();
PrintWriter writer = new PrintWriter( output, true );
writer.println( command ); // send the command
// Record output
String s = reader.readLine();
String result = "";
while( s != null ) {
result = result + "\n" + s;
s = reader.readLine();
}
// Record task's ending time
end = System.nanoTime();
} catch(UnknownHostException ex ){
System.out.println( "Server not found:" + ex.getMessage());
} catch( IOException ex ) {
System.out.println( "IOException in thread " + this.getName() + ": " + ex.getMessage() );
}
}
}
public class CSCv1 {
public static void main( String[] args ) {
Scanner keyboard = new Scanner( System.in );
System.out.print( "Hostname: " );
String hostname = keyboard.nextLine();
System.out.print( "Port: " );
int port = Integer.parseInt( keyboard.nextLine() );
// Solicit command from user
String command = "";
while( !command.equals( "exit" ) ) {
System.out.println( "Available commands: date uptime memory netstat users ps exit" );
System.out.print( "Command to perform: " );
command = keyboard.nextLine();
if( ! command.equals( "exit" ) ) {
// Solicit num clients
System.out.print( "Number of simulated clients: " );
int clients = Integer.parseInt( keyboard.nextLine() );
ArrayList<ClientThread> client = new ArrayList<>();
long runtime = -1 * System.nanoTime(); // take the inverse of the time before starting
long ttime = 0;
for(int i = 0; i < clients; i++){
// Create a new client
client.add( new ClientThread( "Client " + (i+1), hostname, port, command, System.nanoTime() ) );
}
for( int i = 0; i < client.size(); i++ ){
// wait until this client thread is finished
while( client.get(i).isAlive() ) {}
// get runtime of this client
long duration = client.get(i).getRuntime();
ttime += duration; // add this client's duration to the total thread time
}
runtime += System.nanoTime(); // add the end time to the inverse of the start time
// Print data
System.out.println( command + " " + clients + " " + runtime/1000000 + " " + (ttime/1000000)/clients );
System.out.println();
}
}
}
}