1 6 7 package com.quikj.server.framework; 8 9 import java.util.*; 10 import java.io.*; 11 12 16 public class AceCommandService 17 { 18 private static AceCommandService instance = null; 19 private int port = -1; 20 private String commandPrompt = null; 21 private int maxUsers = 0; 22 private ThreadGroup group = new ThreadGroup ("AceCommandGroup"); 23 private AceCommandConnectionListener connectionListener = null; 24 private String greetings = null; 25 private Hashtable commandList = new Hashtable(); 26 27 28 public AceCommandService(String greetings, 29 String prompt, 30 int port, 31 int max_users) 32 throws IOException 33 { 34 commandPrompt = prompt; 35 maxUsers = max_users; 36 this.greetings = greetings; 37 38 connectionListener = new AceCommandConnectionListener(port); 40 connectionListener.start(); 41 42 instance = this; 43 } 44 45 public static AceCommandService getInstance() 46 { 47 return instance; 48 } 49 50 public void dispose() 51 { 52 if (connectionListener != null) 54 { 55 connectionListener.dispose(); 56 connectionListener = null; 57 } 58 59 AceCommandSession[] sessions = new AceCommandSession[group.activeCount()]; 61 if (sessions != null) 62 { 63 int count = group.enumerate(sessions); 64 for (int i = 0; i < count; i++) 65 { 66 if (sessions[i] != null) 67 { 68 sessions[i].dispose(); 69 } 70 } 71 } 72 73 instance = null; 74 } 75 76 public void broadcastMessage(String message) 77 { 78 AceCommandSession[] sessions = new AceCommandSession[group.activeCount()]; 79 if (sessions != null) 80 { 81 int count = group.enumerate(sessions); 82 for (int i = 0; i < count; i++) 83 { 84 if (sessions[i] != null) 85 { 86 sessions[i].sendMessage(message); 87 } 88 } 89 } 90 } 91 92 public boolean registerCommandHandler(String command, 93 AceCommandHandlerInterface handler) 94 { 95 commandList.put(command, handler); 96 return true; 97 } 98 99 public boolean unregisterCommandHandler(String command) 100 { 101 if (commandList.remove(command) == null) 102 { 103 return false; 104 } 105 return true; 106 } 107 108 protected synchronized boolean executeCommand(String command_line, 109 AceCommandSession session) 110 { 111 String line = command_line.trim(); 112 if (line.length() == 0) 113 { 114 return true; 115 } 116 117 StringTokenizer tokens = new StringTokenizer(line); 118 int num_tokens = tokens.countTokens(); 119 String command = tokens.nextToken(); 120 121 AceCommandHandlerInterface handler = null; 122 123 handler = (AceCommandHandlerInterface)commandList.get(command); 124 125 if (handler != null) 126 { 127 handler.actionPerformed(command_line, session); 128 } 129 else { 131 if ((command.equals("quit") == true) || 132 (command.equals("exit") == true)) 133 { 134 session.sendMessage("Goodbye!\n"); 135 return false; 136 } 137 else if (command.equals("help") == true) 138 { 139 if (num_tokens >= 2) 140 { 141 String topic = tokens.nextToken(); 143 144 handler = (AceCommandHandlerInterface)commandList.get(topic); 145 if (handler != null) 146 { 147 session.sendMessage(handler.usage() + "\n\n"); 148 return true; 149 } 150 else 151 { 152 session.sendMessage("Help on " + topic + " not found\n"); 153 return true; 154 } 155 156 } 157 else { 159 Enumeration iter = commandList.keys(); 160 session.sendMessage("List of commands:\n"); 161 while (iter.hasMoreElements() == true) 162 { 163 session.sendMessage((String )iter.nextElement() + "\n"); 164 } 165 166 return true; 167 } 168 } 169 else 170 { 171 session.sendMessage("Bad command!\n"); 172 return true; 173 } 174 } 175 176 return true; 177 } 178 179 protected ThreadGroup getCommandSessionThreadGrup() 180 { 181 return group; 182 } 183 184 187 public int getPort() 188 { 189 return port; 190 } 191 192 195 private void setPort(int port) 196 { 197 this.port = port; 198 } 199 200 203 public java.lang.String getCommandPrompt() 204 { 205 return commandPrompt; 206 } 207 208 211 public void setCommandPrompt(java.lang.String commandPrompt) 212 { 213 this.commandPrompt = commandPrompt; 214 } 215 216 219 public int getMaxUsers() 220 { 221 return maxUsers; 222 } 223 224 227 public void setMaxUsers(int maxUsers) 228 { 229 this.maxUsers = maxUsers; 230 } 231 232 235 public java.lang.String getGreetings() 236 { 237 return greetings; 238 } 239 240 243 public void setGreetings(java.lang.String greetings) 244 { 245 this.greetings = greetings; 246 } 247 248 } 249 | Popular Tags |