/** * This Class implements basic network server functionality * @version 1.0 * @author Tim Stefanini */ import java.io.*; import java.net.*; public abstract class GenericService implements Runnable, Cloneable, ServiceInterface { //++ Public Methods public GenericService () { System.out.println("sTARTING GenericService Constructor"); mfIsWorking = false; mfContinueToListen = true; System.out.println("Called GenericService Constructor"); } public void close() { try { if (clientSocket != null) clientSocket.close(); clientSocket = null; clientInput = null; clientOutput = null; }//end try catch (Exception e) { }//end catch }//end close() public synchronized void ShutDown() { mfContinueToListen = false; try { InetAddress aAddress = InetAddress.getLocalHost(); Socket s = new Socket(aAddress, miPort); OutputStream out = s.getOutputStream(); Thread.currentThread().sleep (100); serverInstance = null; }//end try catch (Exception e) { System.out.println("ShutDown failure" + e); e.printStackTrace(); }//end catch }//end Shutdown() /** Return client connection status */ public boolean clientIsOpen() { return clientSocket != null; }//end clientIsOpen() final public void run() { if (serverSocket != null) { Thread.currentThread().setPriority(Thread.MAX_PRIORITY); System.out.print("Server starts " + serverSocket + "\n"); while (true) { if (mfIsActive){ try { //Create new Thread to handle request Note: This may be too much overhead //for simple requests Socket ns = serverSocket.accept(); if (!mfContinueToListen) break; GenericService n = (GenericService)clone(); n.serverSocket = null; n.clientSocket = ns; //set the new GenericServer's client socket to the //one returned by accept() Thread oThread = new Thread(n); oThread.setDaemon(true); oThread.start(); }//end try catch(Exception e) { System.out.print("Server failure\n"); e.printStackTrace(); try { serverSocket.close(); }//end try catch(IOException e2) { e.printStackTrace(); }//end catch System.out.print("cs="+serverSocket+"\n"); break; }//end catch }//end if else {System.out.println("Server not active on port" + miPort);} }//end while mfIsWorking = false; }//end if serverSocket != null else { try { clientOutput = new PrintStream( new BufferedOutputStream(clientSocket.getOutputStream()), true); //true = autoflush on newline char+clientInput = new DataInputStream(clientSocket.getInputStream()); serviceRequest(); }//end try catch(Exception e) { e.printStackTrace(); }//end catch }//end else close(); }//end run() public void Activate() { this.mfIsActive = true; }//end Activate() public void Deactivate() { this.mfIsActive = false; }//end Deactivate() public boolean IsActive() { return mfIsActive; }//end IsActive() public void StartUp() { try { serverSocket = new ServerSocket(0); miPort = serverSocket.getLocalPort(); serverInstance = new Thread(this); serverInstance.setDaemon(true); serverInstance.start(); System.out.println("Service started"); } catch (IOException e) { e.printStackTrace(); }//end catch }//end startServer() public void serviceRequest() throws IOException { System.out.println("in serviceRequest for server " + getClass().getName() + "\n"); String foo = null; while ( (foo = clientInput.readLine()) != null) clientOutput.print("server: " + foo + " :server\r\n"); clientOutput.flush(); }//end serviceRequest() public Object clone() { try { return super.clone(); }//end try catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable return null; }//end catch }//end clone() //++ Protected Methods protected int miPort = -1; //++ Private Methods //++ Data Members /** Socket for communicating with client. */ private Socket clientSocket = null; private Thread serverInstance; private ServerSocket serverSocket; private boolean mfIsWorking; private boolean mfContinueToListen; private boolean mfIsActive = false; /** Stream for printing to the client. */ public PrintStream clientOutput; /** Buffered stream for reading replies from client. */ public DataInputStream clientInput; }//end class GenericService