Main Page   Class Hierarchy   Compound List   File List   Compound Members  

SocketListener.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: SocketListener.java,v 1.1 2002/11/29 15:18:35 kobit Exp $
00019  * $Author: kobit $
00020  * $Date: 2002/11/29 15:18:35 $
00021  */
00022 
00023 package wttools.remotecons;
00024 
00025 import wttools.remotecons.ifc.ConnectionHandlerIfc;
00026 import wttools.remotecons.ifc.LogIfc;
00027 import wttools.remotecons.ifc.CommandHandlerIfc;
00028 import java.net.Socket;
00029 import java.net.ServerSocket;
00030 import java.io.IOException;
00031 import java.util.List;
00032 
00046 public class SocketListener extends Thread {
00047 
00052   protected int port_no = Constants.DEFAULT_SERVER_PORT;
00059   protected ConnectionHandlerIfc conn_handler = null;
00064   protected List command_handlers = null;
00070   protected ServerSocket server_socket = null;
00077   protected LogIfc log_impl = null;
00082   protected boolean may_run = true;
00083   
00087   public SocketListener() {}
00088 
00095   public void startListening() throws IOException {
00096     startListening(Constants.DEFAULT_SERVER_PORT, true);
00097   }
00098   
00107   public void startListening(int port_no, boolean daemon)
00108     throws IOException
00109   {
00110     may_run = true;
00111     this.port_no = port_no;
00112     server_socket = new ServerSocket(port_no);
00113     setDaemon(daemon);
00114     start();
00115   }
00116 
00122   public void close() {
00123     may_run = false;
00124     try {
00125       server_socket.close();
00126     } catch (IOException e) {
00127       
00128     } // end of try-catch
00129   }
00130 
00135   public void run() {
00136     while (may_run) {
00137       try {
00138         Socket socket = server_socket.accept();
00139         log("Connection request received.");
00140         log("Remote host is: "+socket.getInetAddress().getHostAddress());
00141         if (conn_handler != null) {
00142           conn_handler.handleNewConnection(socket, command_handlers);
00143         } // end of if (conn_handler != null)
00144         else {
00145           log("Cant handle received connection request!");
00146           log("No connection handler defined.");
00147         } // end of else
00148       } catch (IOException e) {
00149         if (conn_handler != null) {
00150           conn_handler.handleException(e);
00151         } // end of if (conn_handler != null)
00152         else {
00153           e.printStackTrace();
00154         } // end of if (conn_handler != null)else
00155         break;
00156       } // end of try-catch
00157     } // end of while (true)
00158   }
00159 
00168   protected void log(String msg) {
00169     if (log_impl != null) {
00170       log_impl.log(msg);
00171     } // end of if (log_impl != null)
00172   }
00173   
00179   public int getPort_no() {
00180     return this.port_no;
00181   }
00182 
00188   public void setPort_no(int argPort_no){
00189     this.port_no = argPort_no;
00190   }
00191 
00198   public ConnectionHandlerIfc getConn_handler() {
00199     return this.conn_handler;
00200   }
00201 
00208   public void setConn_handler(ConnectionHandlerIfc argConn_handler){
00209     this.conn_handler = argConn_handler;
00210   }
00211   
00217   public List getCommand_handlers() {
00218     return this.command_handlers;
00219   }
00220 
00226   public void setCommand_handlers(List argCommand_handlers){
00227     this.command_handlers = argCommand_handlers;
00228   }
00229 
00230 }// SocketListener
00231 /*
00232  * Changes in file:
00233  *
00234  * $Log: SocketListener.java,v $
00235  * Revision 1.1  2002/11/29 15:18:35  kobit
00236  * Refactoring packages, thanks to RefactorIT
00237  *
00238  * Revision 1.6  2002/01/23 15:49:24  kobit
00239  * Java doc for all classes finishedant clean dist
00240  *
00241  * Revision 1.5  2002/01/17 22:52:11  kobit
00242  * Updating documentation is in progress now...
00243  *
00244  * Revision 1.4  2002/01/13 18:09:29  kobit
00245  * Added methods for closing remote console
00246  *
00247  * Revision 1.3  2002/01/13 17:27:13  kobit
00248  * Corrected interface for external command handlers
00249  *
00250  * Revision 1.2  2002/01/12 12:31:26  kobit
00251  * Updated CVS keyword in all files
00252  *
00253  *
00254  */

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