Hi there,
My application works as a client/server architecture where I send commands to a server through sockets which are then initialized and executed to a Server class and finally a CPU class.
The Server class initializes the CPU class and the CPU class initializes the CPU utilization.
When i send the start commando the Server class creates a new thread for the CPU class which start the CPU utilization.
This all works fine. My problem arises when I want to stop the thread by sending a stop command trough the socket. Is doesn't initialize.
The process flows goes like this:
1. Command send from JSP page to Client class
2. Client class initializes a connection and send the command
3. Command is interpreted bij JANEServer class and send to Server class
4. Server class executes the start and stop method of the CPU class
5. CPU class start the method for utilization.
Below I posted the code and I hope someone is able to help me because I'm getting a little frustrated because I can't figure out what I'm doing wrong here.
JANEServer class:
import java.net.*; import java.io.*; /** * JANEServer class for creating the server sockets and * interpreting the commands send by the JANE client * * @author Sander Stad * @version 0.1 * */ public class JANEServer { private static Server iServer = null; // Declaration of the JANEServer class private static Socket socket = null; // Declaration of the socket private static ServerSocket socketServer = null; // Declaration of the server socket private static final int PORT = 20000; // Constant variable for default port /** * Constructor of the JANEServer class with default settings */ public JANEServer(){} private void startServer(){ try { // Open a socket with the default port socketServer = new ServerSocket(PORT); } catch (IOException e) { // Print error if port could not be bound System.out.println("Could not listen on port: " + PORT); // Exit system System.exit(1); } // Server is ready for connections. Print status System.out.println("Waiting for connection.."); try { // Accept connections socket = socketServer.accept(); } catch (IOException e) { // Print error if no connections can be accepted System.err.println("Accept failed."); // Exit system System.exit(1); } // Connection is made with the client. Ready for commands System.out.println ("Connection successful"); System.out.println ("Waiting for input....."); } private void startServer(int p){ try { // Open a socket with port variable socketServer = new ServerSocket(p); } catch (IOException e) { // Print error if port could not be bound System.out.println("Could not listen on port: " + p); // Exit system System.exit(1); } // Server is ready for connections. Print status System.out.println("Waiting for connection.."); try { // Accept connections socket = socketServer.accept(); } catch (IOException e) { // Print error if no connections can be accepted System.err.println("Accept failed."); // Exit system System.exit(1); } // Connection is made with the client. Ready for commands System.out.println ("Connection successful"); System.out.println ("Waiting for input....."); } private void closeServer() throws IOException{ // Clean up sockets and readers socket.close(); socketServer.close(); } /** * Main method for executing the JANEServer class * @param args * @throws IOException */ public static void main(String args[]) throws IOException{ JANEServer jServer = new JANEServer(); jServer.startServer(PORT); // Initialize Server class for performance issues iServer = new Server(); // Create print writer to enable outputs PrintWriter out = new PrintWriter(socket.getOutputStream(), true); // Create buffered reader to read commands send from the client BufferedReader in = new BufferedReader( new InputStreamReader( socket.getInputStream())); // Input line for getting commands from buffered reader String inputLine; // Loop the buffered reader until it is null while ((inputLine = in.readLine()) != null) { // Print commands send by connected client System.out.println ("Server: " + inputLine); out.println(inputLine); if(inputLine.equals("startcpu")){ iServer.startCPU(); } if(inputLine.equals("stopcpu")){ iServer.stopCPU(); } if(inputLine.equals("disconnect")){ jServer.closeServer(); out.close(); in.close(); break; } } } }
Server class:
/** * Server class for executing commands to performance issue * classes i.e. the CPU * * @author Sander * @version 0.1 */ public class Server { private volatile CPU cpu; private Thread threadCPU; /** * Standard constructor for the Server class */ public Server(){ cpu = new CPU(); threadCPU = new Thread(cpu, "CPU"); } /** * Starts a CPU thread which causes CPU utilization */ public void startCPU(){ // Check if no other thread is already started if(!cpu.isRunning()){ // Start the thread threadCPU.start(); // Print status to screen System.out.println("CPU Thread started!"); }else{ // If the thread is already alive print error to screen System.out.println("CPU Thread is already alive!"); } } /** * Starts a CPU thread which causes CPU utilization with time limit * Stops the cpu automatically after x seconds * * @param seconds Amount of time in milliseconds until the thread stops */ public void startCPU(int seconds){ try { // Check if no other thread is already started if(!cpu.isRunning()){ // Start the thread threadCPU.start(); // Print status to screen System.out.println("CPU Thread started!"); }else{ // If the thread is already alive print error to screen System.out.println("CPU Thread is already alive!"); } // Sleep for x seconds Thread.sleep(seconds); stopCPU(); } catch (InterruptedException e) { // Print the stack trace when exception occurred e.printStackTrace(); } } /** * Stops a CPU thread */ public void stopCPU(){ // Request a stop in the CPU class System.out.println("Stop initiated"); cpu.requestStop(); threadCPU.interrupt(); // Check if the thread successfully stopped if(!cpu.isRunning()){ // Print status to screen System.out.println("CPU Thread is stopped"); } } }
CPU class:
/** * CPU class for creating CPU utilization * * @author Sander * @version 0.1 */ public class CPU implements Runnable{ // Initialize variables @SuppressWarnings("unused") private long value = 10L; private volatile boolean running = false; /** * Standard run method for the Runnable interface */ public void run() { // Set running variable to true this.running = true; // Loop al long as the running variable is true while(running){ // Increase variable that causes CPU utilization this.value += 1L; } } /** * Stops the while loop which causes the CPU utilization */ public void requestStop(){ if(running){ // Set running variable to false running = false; }else{ System.out.println("CPU isn't running!"); } } /** * Returns if the CPU utilization is running or not * @return boolean */ public boolean isRunning(){ if(running){ // If CPU utilization is running return true return true; } return false; } }