001: /*
002: * This file is part of the QuickServer library
003: * Copyright (C) 2003-2005 QuickServer.org
004: *
005: * Use, modification, copying and distribution of this software is subject to
006: * the terms and conditions of the GNU Lesser General Public License.
007: * You should have received a copy of the GNU LGP License along with this
008: * library; if not, you can download a copy from <http://www.quickserver.org/>.
009: *
010: * For questions, suggestions, bug-reports, enhancement-requests etc.
011: * visit http://www.quickserver.org
012: *
013: */
014:
015: package org.quickserver.net.server;
016:
017: import java.io.*;
018: import java.net.SocketTimeoutException;
019:
020: /**
021: * This interface defines the methods that should be implemented by any
022: * class that wants to handle client events.
023: *
024: * <p>
025: * Recommendations to be followed when implementing ClientEventHandler
026: * <ul>
027: * <li>Should have a default constructor.
028: * <li>Should be thread safe.
029: * <li>It should not store any data that may is associated with a particular client.
030: * <li>If any client data is need to be saved from the client session,
031: * it should be saved to a {@link ClientData} class, which can be retrieved
032: * using handler.getClientData() method.
033: * </ul>
034: * </p>
035: * <p>If not ClientEventHandler is set for QuickServer then a
036: * default implementation {@link org.quickserver.net.server.impl.DefaultClientEventHandler} is used.
037: * </p>
038: * <p>
039: * Ex:
040: * <code><BLOCKQUOTE><pre>
041: package echoserver;
042:
043: import java.net.*;
044: import java.io.*;
045: import org.quickserver.net.server.ClientEventHandler;
046: import org.quickserver.net.server.ClientHandler;
047:
048: public class EchoEventHandler implements ClientEventHandler {
049:
050: public void gotConnected(ClientHandler handler)
051: throws SocketTimeoutException, IOException {
052: handler.sendSystemMsg("Connection opened : "+
053: handler.getSocket().getInetAddress());
054:
055: handler.sendClientMsg("Welcome to EchoServer v1.0 ");
056: handler.sendClientMsg("Note: Password = Username");
057: handler.sendClientMsg("Send 'Quit' to exit");
058: }
059:
060: public void lostConnection(ClientHandler handler)
061: throws IOException {
062: handler.sendSystemMsg("Connection lost : " +
063: handler.getSocket().getInetAddress());
064: }
065: public void closingConnection(ClientHandler handler)
066: throws IOException {
067: handler.sendSystemMsg("Connection closing : " +
068: handler.getSocket().getInetAddress());
069: }
070: }
071: </pre></BLOCKQUOTE></code></p>
072: * @since 1.4.5
073: * @author Akshathkumar Shetty
074: */
075: public interface ClientEventHandler {
076:
077: /**
078: * Method called when there is a new client connects
079: * to the QuickServer.
080: * Can be used to send welcome message to the client and logging.
081: * @exception java.net.SocketTimeoutException if socket times out
082: * @exception java.io.IOException if io error in socket
083: */
084: public void gotConnected(ClientHandler handler)
085: throws SocketTimeoutException, IOException;
086:
087: /**
088: * Method called when client connection is lost.
089: * Don't write to the connection in this method.
090: * Its just information, to be used at the Server end.
091: * It can be caused due to network errors.
092: * @exception java.io.IOException if io error in socket
093: */
094: public void lostConnection(ClientHandler handler)
095: throws IOException;
096:
097: /**
098: * Method called when client connection is closed.
099: * Don't write to the connection in this method.
100: * Its just information, you can use to log time and ip of client closing connection.
101: * @exception java.io.IOException if io error in socket
102: */
103: public void closingConnection(ClientHandler handler)
104: throws IOException;
105:
106: }
|