KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > net > server > MaxClientHandler


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.net.server;
16
17 import java.io.*;
18 import java.net.*;
19 import org.quickserver.net.AppException;
20 //v1.2
21
import java.util.logging.*;
22
23
24 /**
25  * Class that handles excess clients of QuickServer.
26  * <p>
27  * This class is used by {@link QuickServer} to create a new thread for
28  * each new client connected after maximum client connection has reached.
29  *
30  * This class is responsible to handle excess client sockets.
31  * Will log client ip if Logging level is FINEST.
32  * </p>
33  * @since 1.1
34  * @author Akshathkumar Shetty
35  */

36 public class MaxClientHandler extends Thread {
37
38     /** Client socket */
39     private Socket socket;
40     /** reason message */
41     private String message;
42
43     private OutputStream out;
44     
45     private static Logger logger = Logger.getLogger(
46         MaxClientHandler.class.getName());
47
48     /**
49      * Created new MaxClientHandler thread that handles that
50      * client. It sends the given message and closes the connection.
51      * @param client socket to handle
52      * @param message reason to be sent to client
53      * @deprecated use {@link #MaxClientHandler(Socket, String, QuickServer)}
54      */

55     public MaxClientHandler(Socket client, String message) {
56         super("MaxClientHandler");
57         socket = client;
58         this.message = message;
59     }
60
61     /**
62      * Created new MaxClientHandler thread that handles that
63      * client. It sends the given message and closes the connection.
64      * @param client socket to handle
65      * @param message reason to be sent to client
66      * @param quickServer parent QuickServer
67      */

68     public MaxClientHandler(Socket client, String message,
69             QuickServer quickServer) {
70         this(client, message);
71         logger = quickServer.getAppLogger();
72     }
73
74     public void run() {
75         String temp = message;
76         logger.fine("Handled client " +getSocket().getInetAddress());
77         try {
78             out = socket.getOutputStream();
79             out.write(temp.getBytes());
80             out.flush();
81             out.close();
82         } catch(javax.net.ssl.SSLException e) {
83             logger.finest("IGNORE: SSLException: " + e);
84         } catch(IOException ie) {
85             logger.finest("IOError: " + ie);
86         } catch(Exception e) {
87             logger.finest("Error: " + e);
88         } finally {
89             try {
90                 if(socket!=null) {
91                     socket.close();
92                     socket=null;
93                 }
94             } catch (Exception re) {
95                 socket=null;
96             }
97         }
98     }
99
100     /**
101      * Returns the Clients InetAddress if socket not null else will
102      * return null
103      */

104     public String toString() {
105         if(socket!=null)
106             return socket.getInetAddress().getHostName();
107         return null;
108     }
109
110     /** Sets client socket associated. */
111     public void setSocket(Socket socket) {
112         this.socket = socket;
113     }
114     /** Returns client socket associated. */
115     public Socket getSocket() {
116         return socket;
117     }
118 }
119
Popular Tags