KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > echoserver > EchoCommandHandler


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 echoserver;
16
17 import java.net.*;
18 import java.io.*;
19 import org.quickserver.net.server.ClientCommandHandler;
20 import org.quickserver.net.server.ClientHandler;
21 import org.quickserver.net.server.ClientEventHandler;
22 import java.util.logging.*;
23
24 public class EchoCommandHandler implements ClientCommandHandler, ClientEventHandler {
25     private static Logger logger =
26             Logger.getLogger(EchoCommandHandler.class.getName());
27
28     //--ClientEventHandler
29
public void gotConnected(ClientHandler handler)
30         throws SocketTimeoutException, IOException {
31         logger.fine("Connection opened : "+handler.getHostAddress());
32
33         handler.sendClientMsg("+++++++++++++++++++++++++++++++");
34         handler.sendClientMsg("| Welcome to EchoServer v " + EchoServer.version+" |");
35         handler.sendClientMsg("| Note: Password = Username |");
36         handler.sendClientMsg("| Send 'Quit' to exit |");
37         handler.sendClientMsg("+++++++++++++++++++++++++++++++");
38     }
39
40     public void lostConnection(ClientHandler handler)
41         throws IOException {
42         handler.sendSystemMsg("Connection lost : "+
43             handler.getSocket().getInetAddress(), Level.FINE);
44     }
45     public void closingConnection(ClientHandler handler)
46         throws IOException {
47         logger.fine("Connection closed : "+
48             handler.getSocket().getInetAddress());
49     }
50     //--ClientEventHandler
51

52     public void handleCommand(ClientHandler handler, String JavaDoc command)
53             throws SocketTimeoutException, IOException {
54         if(command.toLowerCase().equals("quit")) {
55             handler.sendClientMsg("Bye ;-)");
56             handler.closeConnection();
57         } else {
58             handler.sendClientMsg("Echo : "+command);
59         }
60     }
61 }
62
Popular Tags