1 14 15 package chatserver; 16 17 18 import java.io.*; 19 import java.net.*; 20 import org.quickserver.net.server.ClientHandler; 21 import java.util.*; 22 import java.util.logging.*; 23 24 28 public class ChatMessaging { 29 private static final Logger logger = 30 Logger.getLogger(ChatMessaging.class.getName()); 31 32 public static void sendInfoMessage2Room(ClientHandler handler, 33 String room, String msg) throws SocketTimeoutException, 34 IOException { 35 if(room==null || msg==null) return; 36 Iterator iterator = handler.getServer().findAllClientByKey( 37 ".+@"+room); 38 ClientHandler toHandler = null; 39 if(iterator.hasNext()) { 40 while(iterator.hasNext()) { 41 if(msg.equals("LoggedOut")==false) 42 handler.isConnected(); toHandler = (ClientHandler) iterator.next(); 44 if(toHandler==handler) continue; 45 sendInfoMessage(handler, toHandler, msg); 46 } 47 } 50 } 51 52 public static void sendInfoMessage(ClientHandler handler, 53 ClientHandler toHandler, String message) throws IOException { 54 if(message==null) return; 55 logger.fine("From: "+handler+"To: "+toHandler+", "+message); 56 if(toHandler==null) return; 57 58 ChatData data = (ChatData)handler.getClientData(); 59 try { 60 toHandler.sendClientMsg("{user.info:"+data.getClientKey()+"} "+message); 61 } catch(Exception e) { 62 logger.fine("Error sending msg: "+e); 63 } 64 } 65 66 public static void sendMessageBwUsers(ClientHandler handler, 67 ClientHandler toHandler, String message) throws IOException { 68 logger.fine("From: "+handler+"To: "+toHandler+", "+message); 69 if(toHandler==null) return; 70 71 ChatData data = (ChatData)handler.getClientData(); 72 try { 73 ChatData toCd = (ChatData)toHandler.getClientData(); 74 toHandler.sendClientMsg("{user.msg:"+data.getClientKey()+":"+ 75 toCd.getClientKey()+"} "+message); 76 handler.sendClientMsg("{system.debug} SentMessageTo "+toCd.getClientKey()); 77 } catch(Exception e) { 78 logger.warning("Error sending msg: "+e); 79 handler.sendClientMsg("{system.error} MessageNotSent "+e.getMessage()); 80 } 81 } 82 83 public static void sendMessage2Room(ClientHandler handler, 84 ClientHandler toHandler, String message) throws IOException { 85 logger.fine("From: "+handler+"To: "+toHandler+", "+message); 86 if(toHandler==null) return; 87 88 ChatData data = (ChatData)handler.getClientData(); 89 try { 90 ChatData toCd = (ChatData)toHandler.getClientData(); 91 toHandler.sendClientMsg("{user.msg:"+data.getClientKey()+":@"+ 92 toCd.getRoom()+"} "+message); 93 handler.sendClientMsg("{system.debug} SentMessageTo "+toCd.getClientKey()); 94 } catch(Exception e) { 95 logger.warning("Error sending msg: "+e); 96 handler.sendClientMsg("{system.error} MessageNotSent "+e.getMessage()); 97 } 98 } 99 100 public static void printHelp(ClientHandler handler, String command) 101 throws IOException { 102 if(command!=null) 103 handler.sendClientMsg("{system.error} BadCommand "+command); 104 handler.sendClientMsg("{system.help} Sending Message Format:"); 105 handler.sendClientMsg("{system.help} sendMsg <<username>> <<message>>"); 106 handler.sendClientMsg("{system.help} sendMsg <<username@room>> <<message>>"); 107 handler.sendClientMsg("{system.help} sendMsgToRoom <<room_name>> <<message>>"); 108 handler.sendClientMsg("{system.help} userList"); 109 handler.sendClientMsg("{system.help} changeRoom <<room_name>>"); 110 } 111 112 113 public static void sendMsgToRoom(ClientHandler handler, String command) 114 throws SocketTimeoutException, IOException { 115 String room = null; 116 String message = null; 117 int i = command.indexOf(" "); 118 if(i==-1) { 119 handler.sendClientMsg("{system.error} BadCommand "+command); 120 return; 121 } 122 int j = command.indexOf(" ",i+1); 123 if(j==-1) j = command.length(); 124 room = command.substring(i+1, j); 125 126 if(j!=command.length()) 127 message = command.substring(j+1); 128 else 129 message = ""; 130 131 Iterator iterator = handler.getServer().findAllClientByKey(".+@"+room); 132 if(iterator.hasNext()) { 133 while(iterator.hasNext()) { 134 handler.isConnected(); ChatMessaging.sendMessage2Room(handler, (ClientHandler) 136 iterator.next(), message); 137 } 138 } else { 139 handler.sendClientMsg("{system.error} MessageNotSent No Clients in that room"); 140 } 141 } 142 143 public static void sendMsg(ClientHandler handler, String command) 144 throws SocketTimeoutException, IOException { 145 String id = null; 146 String message = null; 147 int i = command.indexOf(" "); 148 if(i==-1) { 149 handler.sendClientMsg("{system.error} BadCommand "+command); 150 return; 151 } 152 int j = command.indexOf(" ",i+1); 153 if(j==-1) j = command.length(); 154 id = command.substring(i+1, j); 155 if(j!=command.length()) 156 message = command.substring(j+1); 157 else 158 message = ""; 159 160 ClientHandler toHandler = null; 161 Iterator iterator = null; 162 if(id.indexOf("@")!=-1) { 163 toHandler = handler.getServer().findClientByKey(id); 164 List list = new ArrayList(); 165 if(toHandler!=null) { 166 logger.finest("Found to by Key: "+toHandler); 167 list.add(toHandler); 168 } 169 iterator = list.iterator(); 170 } else { 171 logger.finest("Finding to by Id"); 172 iterator = handler.getServer().findAllClientById(id); 173 } 174 175 if(iterator.hasNext()) { 176 while(iterator.hasNext()) { 177 handler.isConnected(); toHandler = (ClientHandler) iterator.next(); 179 ChatMessaging.sendMessageBwUsers(handler, toHandler, message); 180 } } else { 182 handler.sendClientMsg("{system.error} MessageNotSent No Client by that id"); 183 } 184 } 185 186 public static void sendRoomList(ClientHandler handler) 187 throws SocketTimeoutException, IOException { 188 ChatData data = (ChatData)handler.getClientData(); 189 Iterator iterator = 190 handler.getServer().findAllClientByKey(".+@"+data.getRoom()); 191 if(iterator.hasNext()) { 192 while(iterator.hasNext()) { 193 handler.isConnected(); sendList(handler, (ClientHandler) iterator.next()); 195 } 196 } 197 } 198 199 public static void sendList(ClientHandler toHandler, 200 ClientHandler handler) throws IOException { 201 logger.finest("From: "+handler+"To: "+toHandler); 202 if(handler==null) return; 203 204 ChatData data = (ChatData)handler.getClientData(); 205 try { 206 toHandler.sendClientMsg("{user.list} "+data.getClientKey()); 207 } catch(Exception e) { 208 logger.finest("Error sending msg: "+e); 209 } 210 } 211 } 212 | Popular Tags |