1 6 7 package com.quikj.server.framework; 8 9 import java.net.*; 10 import java.io.*; 11 12 16 public class AceCommandSession extends Thread 17 { 18 private String commandPrompt; 19 private Socket socket; 20 private BufferedReader reader; 21 private OutputStreamWriter writer; 22 23 private static int count = 0; 24 25 26 public AceCommandSession(ThreadGroup group, Socket sock, String prompt) 27 throws IOException, SocketException 28 { 29 super(group, "CommandSession"); 30 setName("CommandSession_" + count++); 31 32 commandPrompt = prompt; 33 socket = sock; 34 35 socket.setSoTimeout(5000); 36 37 reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); 38 writer = new OutputStreamWriter(socket.getOutputStream()); 39 } 40 41 public void dispose() 42 { 43 try 44 { 45 socket.close(); 46 } 47 catch (IOException ex) 48 { 49 ; 50 } 51 52 interrupt(); 53 } 54 55 public void run() 56 { 57 sendMessage (AceCommandService.getInstance().getGreetings() + "\n\n"); 58 59 try 60 { 61 boolean no_prompt = false; 62 while (isInterrupted() == false) 63 { 64 if (no_prompt == false) 65 { 66 if (sendMessage(commandPrompt) == false) 67 { 68 break; 69 } 70 } 71 no_prompt = false; 72 73 String line = null; 74 try 75 { 76 line = reader.readLine(); 77 } 78 catch (InterruptedIOException ex) 79 { 80 if (isInterrupted() == true) 81 { 82 break; 83 } 84 else 85 { 86 no_prompt = true; 87 continue; 88 } 89 } 90 91 if (isInterrupted() == true) 92 { 93 break; 94 } 95 96 if (line == null) 97 { 98 break; 99 } 100 101 if (AceCommandService.getInstance().executeCommand(line, this) 102 == false) 103 { 104 break; 105 } 106 } } 108 catch (IOException ex) 109 { 110 ; 111 } 112 113 dispose(); 114 } 115 116 public boolean sendMessage(String message) 117 { 118 try 119 { 120 writer.write(message); 121 writer.flush(); 122 return true; 123 } 124 catch (IOException ex) 125 { 126 return false; 127 } 128 } 129 130 133 public java.net.Socket getSocket() 134 { 135 return socket; 136 } 137 138 141 public void setSocket(java.net.Socket socket) 142 { 143 this.socket = socket; 144 } 145 146 } 147 | Popular Tags |