/** Handles operation of the error simulator server. */ public void start() { DatagramPacket receivePacket = null; try { errorSimulatorSock = new DatagramSocket(Configurations.ERROR_SIM_LISTEN_PORT); // errorSimulatorSock.setSoTimeout(30000); } catch (SocketException e) { e.printStackTrace(); System.exit(1); } /* * - Receive packets until the admin console gives the shutdown signal. * - Since receiving a packet is a blocking operation, timeouts have been set to loop back * and check if the close signal has been given. */ while (active.get()) { try { // Create the packet for receiving. byte[] buffer = new byte[Configurations.MAX_BUFFER]; this.mErrorOptionSettings = this.mErrorUI.getErrorCodeFromUser(testInstance); if (this.mErrorOptionSettings.first == ErrorType.EXIT) { active.set(false); break; } receivePacket = new DatagramPacket(buffer, buffer.length); logger.print( Logger.VERBOSE, String.format(Strings.ES_INITIALIZED, Configurations.ERROR_SIM_LISTEN_PORT)); logger.print(Logger.VERBOSE, Strings.ES_START_LISTENING); errorSimulatorSock.receive(receivePacket); } catch (SocketTimeoutException e) { continue; } catch (SocketException e) { continue; } catch (IOException e) { System.out.println(Strings.SERVER_RECEIVE_ERROR); e.printStackTrace(); } System.out.println( BufferPrinter.acceptConnectionMessage( Strings.SERVER_ACCEPT_CONNECTION, receivePacket.getSocketAddress().toString())); Thread service = new Thread( new ErrorSimulatorService( receivePacket, this, this.mErrorOptionSettings, testInstance), CLASS_TAG); threads.addElement(service); service.start(); } this.errorSimulatorSock.close(); // Wait for all service threads to close before completely exiting. for (Thread t : threads) { try { t.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }
/** Constructor for ErrorSimulatorServer that initializes the thread container 'threads'. */ public ErrorSimulatorServer() { threads = new Vector<Thread>(); this.mErrorUI = new TFTPUserInterface(); testInstance = this.mErrorUI.printTestableProcess(); logger.setClassTag(CLASS_TAG); }