package net.sourceforge.jsrvany.test; import java.io.*; import java.net.*; import java.util.*; import net.sourceforge.jsrvany.*; import net.sourceforge.jsrvany.util.*; /** * This class implements an SimpleServer * *

* *

Copyright (c) 1999 Damian Fauth

* *

* *

 * 
* * @version 0.9 * @author Damian Fauth */ public class SimpleServerImplementation implements SimpleServer, ServiceControlListener { private ServerSocket serverSocket; private Vector v; private int soTimeout=0; private boolean isRunning = true; /** * main entry point * * @returns void */ public static void main(String[] args) { // local variables int port=23; int soTimeout=0; // Process commandLine Arguments CommandLineParser cmdLineArgs = new CommandLineParser(args); try{ port = Integer.parseInt(cmdLineArgs.get("-port")); } catch(NoSuchElementException e){} try{ soTimeout = Integer.parseInt(cmdLineArgs.get("-timeout")); } catch(NoSuchElementException e){} try{ // get the ServiceConrolManager ServiceControlManager manager = ServiceControlManager.getInstance(); SimpleServerImplementation main = new SimpleServerImplementation(); // register this class as a listener manager.addServiceControlListener(main); main.listen(port); } catch(IOException ioe){ System.err.println(""+ioe); } } /** * default constructor * */ public SimpleServerImplementation() { this.v = new Vector(); } /** * listen on a port * * @param port is the number of the port to listen on as an int * @returns void * @exception IOException may be thrown if there is connectivity problems */ public void listen(int port) throws IOException { // Listen for connections try { this.serverSocket = new ServerSocket(port); this.serverSocket.setSoTimeout(this.soTimeout); } catch (IOException ioe) { System.err.println("Could not listen on port: "+port+" reason was "+ioe.getMessage()); throw ioe; } while (isRunning){ try { SimpleServerImplementationThread thread = new SimpleServerImplementationThread(serverSocket.accept()); thread.start(); v.addElement(thread); } catch (IOException ioe) { serverSocket.close(); throw ioe; } } } /** * set the readtimeout on the socket * * @param msecs is the timeout value in milliseconds as an int * @returns void */ public void setSoTimeout(int msecs) { this.soTimeout = msecs; } /** * handle a StartServiceControlEvent This method is called by the ServiceControlManager * when the appropriate event occurs * * @param e is the event as an instance of StartServiceControlEvent * @return void * @exception ServiceControlException may be thrown if the service cannot respond correctly to the event */ public void handleServiceControlEvent(StartServiceControlEvent e) throws ServiceControlException { try { for(int i=v.size()-1;i>=0;i--) { ((SimpleServerImplementationThread) v.elementAt(i)).write("ServiceControlEvent occurred: "+e.toString()+" thread index "+i+" of "+v.size()); } } catch(IOException ioe) { throw new ServiceControlException("Error writing to client thread",ioe); } } /** * handle a StopServiceControlEvent This method is called by the ServiceControlManager * when the appropriate event occurs * * @param e is the event as an instance of StopServiceControlEvent * @return void * @exception ServiceControlException may be thrown if the service cannot respond correctly to the event */ public void handleServiceControlEvent(StopServiceControlEvent e) throws ServiceControlException { try { for(int i=v.size()-1;i>=0;i--) { ((SimpleServerImplementationThread) v.elementAt(i)).write("ServiceControlEvent occurred: "+e.toString()+" thread index "+i+" of "+v.size()); ((SimpleServerImplementationThread) v.elementAt(i)).stopRunning(); v.remove(i); } stop(); } catch(IOException ioe) { throw new ServiceControlException("Error writing to client thread",ioe); } } /** * handle a PauseServiceControlEvent This method is called by the ServiceControlManager * when the appropriate event occurs * * @param e is the event as an instance of PauseServiceControlEvent * @return void * @exception ServiceControlException may be thrown if the service cannot respond correctly to the event */ public void handleServiceControlEvent(PauseServiceControlEvent e) throws ServiceControlException { try { for(int i=v.size()-1;i>=0;i--) { ((SimpleServerImplementationThread) v.elementAt(i)).write("ServiceControlEvent occurred: "+e.toString()+" thread index "+i+" of "+v.size()); } } catch(IOException ioe) { throw new ServiceControlException("Error writing to client thread",ioe); } } /** * handle a ContinueServiceControlEvent This method is called by the ServiceControlManager * when the appropriate event occurs * * @param e is the event as an instance of ContinueServiceControlEvent * @return void * @exception ServiceControlException may be thrown if the service cannot respond correctly to the event */ public void handleServiceControlEvent(ContinueServiceControlEvent e) throws ServiceControlException { try { for(int i=v.size()-1;i>=0;i--) { ((SimpleServerImplementationThread) v.elementAt(i)).write("ServiceControlEvent occurred: "+e.toString()+" thread index "+i+" of "+v.size()); } } catch(IOException ioe) { throw new ServiceControlException("Error writing to client thread",ioe); } } /** * handle a TerminateServiceControlEvent This method is called by the ServiceControlManager * when the appropriate event occurs * * @param e is the event as an instance of TerminateServiceControlEvent * @return void * @exception ServiceControlException may be thrown if the service cannot respond correctly to the event */ public void handleServiceControlEvent(TerminateServiceControlEvent e) throws ServiceControlException { try { for(int i=v.size()-1;i>=0;i--) { ((SimpleServerImplementationThread) v.elementAt(i)).write("ServiceControlEvent occurred: "+e.toString()+" thread index "+i+" of "+v.size()); ((SimpleServerImplementationThread) v.elementAt(i)).stopRunning(); v.remove(i); } stop(); } catch(IOException ioe) { throw new ServiceControlException("Error writing to client thread",ioe); } } /** * handle a InterrogateServiceControlEvent This method is called by the ServiceControlManager * when the appropriate event occurs * * @param e is the event as an instance of InterrogateServiceControlEvent * @return void * @exception ServiceControlException may be thrown if the service cannot respond correctly to the event */ public void handleServiceControlEvent(InterrogateServiceControlEvent e) throws ServiceControlException { try { for(int i=v.size()-1;i>=0;i--) { ((SimpleServerImplementationThread) v.elementAt(i)).write("ServiceControlEvent occurred: "+e.toString()+" thread index "+i+" of "+v.size()); } } catch(IOException ioe) { throw new ServiceControlException("Error writing to client thread",ioe); } } /** * a private method to group methods required when shutting down the server * * @return void * @exception IOException may be thrown if there are connectivity or other networking problems */ private void stop() throws IOException { this.isRunning = false; this.serverSocket.close(); } }