KickJava   Java API By Example, From Geeks To Geeks.

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


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.SocketTimeoutException JavaDoc;
19 /**
20  * This interface defines the methods that should be implemented by any
21  * class that wants to handle client events.
22  *
23  * <p>
24  * Recommendations to be followed when implementing ClientEventHandler
25  * <ul>
26  * <li>Should have a default constructor.
27  * <li>Should be thread safe.
28  * <li>It should not store any data that may is associated with a particular client.
29  * <li>If any client data is need to be saved from the client session,
30  * it should be saved to a {@link ClientData} class, which can be retrieved
31  * using handler.getClientData() method.
32  * </ul>
33  * </p>
34  * <p>If not ClientEventHandler is set for QuickServer then a
35  * default implementation {@link org.quickserver.net.server.impl.DefaultClientEventHandler} is used.
36  * </p>
37  * <p>
38  * Ex:
39  * <code><BLOCKQUOTE><pre>
40 package echoserver;
41
42 import java.net.*;
43 import java.io.*;
44 import org.quickserver.net.server.ClientEventHandler;
45 import org.quickserver.net.server.ClientHandler;
46
47 public class EchoEventHandler implements ClientEventHandler {
48
49     public void gotConnected(ClientHandler handler)
50         throws SocketTimeoutException, IOException {
51         handler.sendSystemMsg("Connection opened : "+
52             handler.getSocket().getInetAddress());
53
54         handler.sendClientMsg("Welcome to EchoServer v1.0 ");
55         handler.sendClientMsg("Note: Password = Username");
56         handler.sendClientMsg("Send 'Quit' to exit");
57     }
58
59     public void lostConnection(ClientHandler handler)
60         throws IOException {
61         handler.sendSystemMsg("Connection lost : " +
62             handler.getSocket().getInetAddress());
63     }
64     public void closingConnection(ClientHandler handler)
65         throws IOException {
66         handler.sendSystemMsg("Connection closing : " +
67             handler.getSocket().getInetAddress());
68     }
69 }
70 </pre></BLOCKQUOTE></code></p>
71  * @since 1.4.5
72  * @author Akshathkumar Shetty
73  */

74 public interface ClientEventHandler {
75
76     /**
77      * Method called when there is a new client connects
78      * to the QuickServer.
79      * Can be used to send welcome message to the client and logging.
80      * @exception java.net.SocketTimeoutException if socket times out
81      * @exception java.io.IOException if io error in socket
82      */

83     public void gotConnected(ClientHandler handler)
84         throws SocketTimeoutException JavaDoc, IOException;
85
86     /**
87      * Method called when client connection is lost.
88      * Don't write to the connection in this method.
89      * Its just information, to be used at the Server end.
90      * It can be caused due to network errors.
91      * @exception java.io.IOException if io error in socket
92      */

93     public void lostConnection(ClientHandler handler)
94         throws IOException;
95
96     /**
97      * Method called when client connection is closed.
98      * Don't write to the connection in this method.
99      * Its just information, you can use to log time and ip of client closing connection.
100      * @exception java.io.IOException if io error in socket
101      */

102     public void closingConnection(ClientHandler handler)
103         throws IOException;
104
105 }
106
Popular Tags