1 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 34 public class ChatCommandHandler 35 implements ClientEventHandler, ClientCommandHandler { 36 private static final Logger logger = 37 Logger.getLogger(ChatCommandHandler.class.getName()); 38 39 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 62 private void tellOthersInRoom(ClientHandler handler) { 63 try { 64 ChatData cd = (ChatData)handler.getClientData(); 65 ChatMessaging.sendInfoMessage2Room(handler, cd.getRoom(), "LoggedOut"); 66 } catch(Exception e) { 67 logger.fine("IGNORE Error: "+e); 68 e.printStackTrace(); 69 } 70 } 71 72 public void handleCommand(ClientHandler handler, String 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 command) 104 throws SocketTimeoutException, IOException { 105 String 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 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 |