Main Page   Class Hierarchy   Compound List   File List   Compound Members  

RemoteServer.java

00001 /*  Package Web Test Tools 
00002  *  Copyright (C) 2001 "Artur Hefczyc" <kobit@users.sourceforge.net>
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU Lesser General Public License as published
00006  *  by the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00012  *  GNU Lesser General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU Lesser General Public License
00015  *  along with this program; if not, write to the Free Software Foundation,
00016  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  *
00018  * $Id: RemoteServer.java,v 1.1 2002/11/29 15:18:34 kobit Exp $
00019  * $Author: kobit $
00020  * $Date: 2002/11/29 15:18:34 $
00021  */
00022 
00023 package wttools.remotecons;
00024 
00025 import java.util.List;
00026 import java.util.LinkedList;
00027 import java.io.IOException;
00028 import wttools.remotecons.ifc.CommandHandlerIfc;
00029 
00184 public class RemoteServer {
00185 
00194   protected int server_port = Constants.DEFAULT_SERVER_PORT;
00202   protected boolean as_daemon = true;
00209   protected SocketListener s_listener = null;
00210   
00223   public void initialize(List command_handlers) throws IOException {
00224     initialize(server_port, command_handlers);
00225   }
00226 
00242   public void initialize(int port_no, List command_handlers)
00243     throws IOException
00244   {
00245     s_listener = new SocketListener();
00246     s_listener.setConn_handler(new ConnectionHandlerImpl());
00247     s_listener.setCommand_handlers(command_handlers);
00248     s_listener.startListening(port_no, as_daemon);
00249   }
00250 
00265   public void asyncMessage(String msg) {
00266     synchronized(ConnectionServer.active_connections) {
00267       List l = ConnectionServer.active_connections;
00268       for (int i = 0; i < l.size(); i++) {
00269         ((ConnectionServer)l.get(i)).asyncMessage(msg);
00270       } // end of for (int i = 0; i < ConnectionServer.active_connections.size(); i++)
00271     }
00272   }
00273 
00282   public void disable() {
00283     s_listener.close();
00284   }
00285   
00293   public static void main (String[] args) {
00294 
00295     //     System.getProperties().list(new java.io.PrintStream(System.out));
00296     //     byte[] line_sep = System.getProperty("line.separator").getBytes();
00297     //     for (int i = 0; i < line_sep.length; i++) {
00298     //       System.out.print(line_sep[i]+" ");
00299     //     } // end of for (int i = 0; i < line_sep.length(); i++)
00300     //     line_sep = new String("\r\n").getBytes();
00301     //     for (int i = 0; i < line_sep.length; i++) {
00302     //       System.out.print(line_sep[i]+" ");
00303     //     } // end of for (int i = 0; i < line_sep.length(); i++)
00304     RemoteServer rs = new RemoteServer();
00305     rs.setServer_port(2702);
00306     rs.setAs_daemon(false);
00307     LinkedList l = new LinkedList();
00308     l.add(rs.new SampleCommandHandler());
00309     try {
00310       rs.initialize(l);
00311     } catch (IOException e) {
00312       e.printStackTrace();
00313       System.exit(1);
00314     } // end of try-catch
00315     //    rs.disable();
00316     System.out.println("Remote server started in test mode.");
00317     System.out.println("Waiting for incoming connections on port "+rs.server_port+"...");
00318   } // end of main ()
00319   
00320   
00326   public int getServer_port() {
00327     return this.server_port;
00328   }
00329 
00335   public void setServer_port(int argServer_port){
00336     this.server_port = argServer_port;
00337   }
00338   
00339 
00345   public boolean isAs_daemon() {
00346     return this.as_daemon;
00347   }
00348 
00354   public void setAs_daemon(boolean argAs_daemon){
00355     this.as_daemon = argAs_daemon;
00356   }
00357 
00362   public class SampleCommandHandler implements CommandHandlerIfc {
00363 
00371     public String handleCommand(String comm) {
00372       if (comm.trim().equals("hello")) {
00373         return "Hello world!\r\n";
00374       } // end of if (comm.trim().equals("hello"))
00375       if (comm.trim().equals("isdaemon")) {
00376         return ""+as_daemon+"\r\n";
00377       } // end of if (comm.trim().equals("hello"))
00378       return null;
00379     }
00380 
00387     public String help() {
00388       return
00389         "\r\n"+
00390         "  hello    - Returns 'Hello world!' string as a result.\r\n"+
00391         "  isdaemon - returns info if console started as daemon.\r\n";
00392     }
00393 
00403     public String help(String comm) {
00404       return "";
00405     }
00406 
00413     public CommandHandlerIfc getInstance() {
00414       return new SampleCommandHandler();
00415     }
00416     
00417   }
00418 
00419 }// RemoteServer
00420 /*
00421  * Changes in file:
00422  *
00423  * $Log: RemoteServer.java,v $
00424  * Revision 1.1  2002/11/29 15:18:34  kobit
00425  * Refactoring packages, thanks to RefactorIT
00426  *
00427  * Revision 1.11  2002/03/21 09:57:27  kobit
00428  * 1. Small but important redesign:
00429  *    - package 'remotecons.wttools' was removed and all classes
00430  *      were moved to 'remotecons' package
00431  * 2. Added 'asyncMessage' to provide sending messages to remote clients
00432  *    in asynchronous mode
00433  *
00434  * Revision 1.10  2002/01/23 15:49:24  kobit
00435  * Java doc for all classes finishedant clean dist
00436  *
00437  * Revision 1.9  2002/01/20 18:39:39  kobit
00438  * Some inprovments added in executing external commands
00439  *
00440  * Revision 1.8  2002/01/17 22:52:11  kobit
00441  * Updating documentation is in progress now...
00442  *
00443  * Revision 1.7  2002/01/17 22:37:44  kobit
00444  * Javadoc comments for classes in progress...
00445  *
00446  * Revision 1.6  2002/01/15 19:20:01  kobit
00447  * Corrected bug in external command runner
00448  *
00449  * Revision 1.5  2002/01/13 18:09:29  kobit
00450  * Added methods for closing remote console
00451  *
00452  * Revision 1.4  2002/01/13 17:27:13  kobit
00453  * Corrected interface for external command handlers
00454  *
00455  * Revision 1.3  2002/01/13 17:14:45  kobit
00456  * Added commands: ls, show file, echo and support for windows telnet
00457  *
00458  * Revision 1.2  2002/01/12 12:31:26  kobit
00459  * Updated CVS keyword in all files
00460  *
00461  *
00462  */

Generated on Thu Dec 19 21:00:47 2002 for WTTools - Remote Console by doxygen1.3-rc2