KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > chatserver > ChatCommandHandler


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 chatserver;
16
17 import java.net.*;
18 import java.io.*;
19 import java.util.*;
20 import org.quickserver.net.server.ClientCommandHandler;
21 import org.quickserver.net.server.ClientEventHandler;
22 import org.quickserver.net.server.ClientHandler;
23 import java.util.logging.*;
24
25 /**
26  * IMPORTANT NOTE: This example just demonstrates how to use some
27  * of the ClientIdentifiable features. This is not the good
28  * way to write a chat application. It will be overkill on the server to
29  * ask it to find a client from over 1000 clients every time you need
30  * to send a message.
31  *
32  * @author Akshathkumar Shetty
33  */

34 public class ChatCommandHandler
35         implements ClientEventHandler, ClientCommandHandler {
36     private static final Logger logger =
37         Logger.getLogger(ChatCommandHandler.class.getName());
38
39     //--ClientEventHandler
40
public void gotConnected(ClientHandler handler)
41         throws SocketTimeoutException, IOException {
42         handler.sendSystemMsg("Connection opened : "+handler.getHostAddress());
43         
44         ChatData cd = (ChatData)handler.getClientData();
45         cd.setClientInfo("IP: "+handler.getHostAddress());
46
47         handler.sendClientMsg("{system.msg} Welcome to ChatServer v "+ChatServer.VER);
48         handler.sendClientMsg("{system.help} Send username=password ");
49         handler.sendClientMsg("{system.help} Send 'quit' to exit ");
50     }
51
52     public void lostConnection(ClientHandler handler) throws IOException {
53         handler.sendSystemMsg("Connection lost : "+handler.getHostAddress());
54         tellOthersInRoom(handler);
55     }
56     public void closingConnection(ClientHandler handler) throws IOException {
57         handler.sendSystemMsg("Connection closed: "+handler.getHostAddress());
58         tellOthersInRoom(handler);
59     }
60     //--ClientEventHandler
61

62     private void tellOthersInRoom(ClientHandler handler) {
63         try {
64             ChatData cd = (ChatData)handler.getClientData();
65             ChatMessaging.sendInfoMessage2Room(handler, cd.getRoom(), "LoggedOut");
66         } catch(Exception JavaDoc e) {
67             logger.fine("IGNORE Error: "+e);
68             e.printStackTrace();
69         }
70     }
71
72     public void handleCommand(ClientHandler handler, String JavaDoc command)
73             throws SocketTimeoutException, IOException {
74         if(command.toLowerCase().equals("quit")) {
75             handler.sendClientMsg("{system.msg} Bye");
76             handler.closeConnection();
77             return;
78         }
79
80         if(command.startsWith("changeRoom")) {
81             changeRoom(handler, command);
82             return;
83         }
84
85         if(command.startsWith("sendMsgToRoom")) {
86             ChatMessaging.sendMsgToRoom(handler, command);
87             return;
88         }
89         
90         if(command.startsWith("sendMsg")) {
91             ChatMessaging.sendMsg(handler, command);
92             return;
93         }
94
95         if(command.startsWith("userList")) {
96             ChatMessaging.sendRoomList(handler);
97             return;
98         }
99
100         ChatMessaging.printHelp(handler, command);
101     }
102
103     public static void changeRoom(ClientHandler handler, String JavaDoc command)
104             throws SocketTimeoutException, IOException {
105         String JavaDoc room = null;
106         int i = command.indexOf(" ");
107         if(i==-1) {
108             handler.sendClientMsg("{system.error} BadParam no room name sent");
109             return;
110         }
111         room = command.substring(i+1);
112         ChatData cd = (ChatData)handler.getClientData();
113         String JavaDoc oldRoom = cd.getRoom();
114         ChatMessaging.sendInfoMessage2Room(handler, oldRoom, "LoggedOut");
115         cd.setRoom(room);
116         ChatMessaging.sendInfoMessage2Room(handler, room, "LoggedIn");
117         handler.sendClientMsg("{system.msg} Chat Room changed to: "+room);
118     }
119
120 }
121
Popular Tags