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 chatserver;
016:
017: import java.net.*;
018: import java.io.*;
019: import java.util.*;
020: import org.quickserver.net.server.ClientCommandHandler;
021: import org.quickserver.net.server.ClientEventHandler;
022: import org.quickserver.net.server.ClientHandler;
023: import java.util.logging.*;
024:
025: /**
026: * IMPORTANT NOTE: This example just demonstrates how to use some
027: * of the ClientIdentifiable features. This is not the good
028: * way to write a chat application. It will be overkill on the server to
029: * ask it to find a client from over 1000 clients every time you need
030: * to send a message.
031: *
032: * @author Akshathkumar Shetty
033: */
034: public class ChatCommandHandler implements ClientEventHandler,
035: ClientCommandHandler {
036: private static final Logger logger = Logger
037: .getLogger(ChatCommandHandler.class.getName());
038:
039: //--ClientEventHandler
040: public void gotConnected(ClientHandler handler)
041: throws SocketTimeoutException, IOException {
042: handler.sendSystemMsg("Connection opened : "
043: + handler.getHostAddress());
044:
045: ChatData cd = (ChatData) handler.getClientData();
046: cd.setClientInfo("IP: " + handler.getHostAddress());
047:
048: handler.sendClientMsg("{system.msg} Welcome to ChatServer v "
049: + ChatServer.VER);
050: handler.sendClientMsg("{system.help} Send username=password ");
051: handler.sendClientMsg("{system.help} Send 'quit' to exit ");
052: }
053:
054: public void lostConnection(ClientHandler handler)
055: throws IOException {
056: handler.sendSystemMsg("Connection lost : "
057: + handler.getHostAddress());
058: tellOthersInRoom(handler);
059: }
060:
061: public void closingConnection(ClientHandler handler)
062: throws IOException {
063: handler.sendSystemMsg("Connection closed: "
064: + handler.getHostAddress());
065: tellOthersInRoom(handler);
066: }
067:
068: //--ClientEventHandler
069:
070: private void tellOthersInRoom(ClientHandler handler) {
071: try {
072: ChatData cd = (ChatData) handler.getClientData();
073: ChatMessaging.sendInfoMessage2Room(handler, cd.getRoom(),
074: "LoggedOut");
075: } catch (Exception e) {
076: logger.fine("IGNORE Error: " + e);
077: e.printStackTrace();
078: }
079: }
080:
081: public void handleCommand(ClientHandler handler, String command)
082: throws SocketTimeoutException, IOException {
083: if (command.toLowerCase().equals("quit")) {
084: handler.sendClientMsg("{system.msg} Bye");
085: handler.closeConnection();
086: return;
087: }
088:
089: if (command.startsWith("changeRoom")) {
090: changeRoom(handler, command);
091: return;
092: }
093:
094: if (command.startsWith("sendMsgToRoom")) {
095: ChatMessaging.sendMsgToRoom(handler, command);
096: return;
097: }
098:
099: if (command.startsWith("sendMsg")) {
100: ChatMessaging.sendMsg(handler, command);
101: return;
102: }
103:
104: if (command.startsWith("userList")) {
105: ChatMessaging.sendRoomList(handler);
106: return;
107: }
108:
109: ChatMessaging.printHelp(handler, command);
110: }
111:
112: public static void changeRoom(ClientHandler handler, String command)
113: throws SocketTimeoutException, IOException {
114: String room = null;
115: int i = command.indexOf(" ");
116: if (i == -1) {
117: handler
118: .sendClientMsg("{system.error} BadParam no room name sent");
119: return;
120: }
121: room = command.substring(i + 1);
122: ChatData cd = (ChatData) handler.getClientData();
123: String oldRoom = cd.getRoom();
124: ChatMessaging.sendInfoMessage2Room(handler, oldRoom,
125: "LoggedOut");
126: cd.setRoom(room);
127: ChatMessaging.sendInfoMessage2Room(handler, room, "LoggedIn");
128: handler.sendClientMsg("{system.msg} Chat Room changed to: "
129: + room);
130: }
131:
132: }
|