1 package test; 2 import java.io.*; 3 import java.net.*; 4 5 8 public class TestService implements Runnable { 9 static final String chargenSequence = " !\"#$%&'()*+,-./0123456789:;<=>?@"+ 10 "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefg"; 11 12 static final String serviceNames[] = {"echo","discard","chargen","connect"}; 13 static final int servicePorts[] = {5678,5679,5680,5681}; 14 15 static final int ECHO = 0; 16 static final int DISCARD = 1; 17 static final int CHARGEN = 2; 18 static final int CONNECT = 3; 19 20 static final int BUF_SIZE = 1024; 21 22 static final int CHARGEN_WAIT = 1000; static final int MAX_WAIT = 60000; 25 static PrintStream log = null; 26 27 Socket s; 29 int service; 30 31 38 public TestService(Socket s, int service){ 39 this.s = s; 40 this.service = service; 41 } 42 43 46 public TestService(){ 47 this.s = null; 48 this.service = -1; 49 } 50 51 public void run(){ 52 try{ 53 serve(s,service); 54 }catch(IOException ioe){ 55 log("Exception:"+ioe); 56 ioe.printStackTrace(); 57 } 58 try{ s.close();}catch(IOException ioe){} 59 } 60 61 64 71 static public int getServiceId(String serviceName){ 72 serviceName = serviceName.toLowerCase(); 73 for(int i = 0;i < serviceNames.length;++i) 74 if(serviceName.equals(serviceNames[i])) 75 return i; 76 77 return -1; 79 } 80 81 89 static public boolean serve(Socket s, int service) throws IOException{ 90 switch(service){ 91 case ECHO: 92 echo(s); 93 break; 94 case DISCARD: 95 discard(s); 96 break; 97 case CHARGEN: 98 chargen(s,CHARGEN_WAIT,MAX_WAIT); 99 break; 100 case CONNECT: 101 connect(s); 102 break; 103 default: 104 log("Unknown service:"+service); 105 return false; 106 } 107 return true; 108 } 109 114 static public void echo(Socket s) throws IOException{ 115 BufferedReader in = new BufferedReader(new InputStreamReader( 116 s.getInputStream())); 117 OutputStream out = s.getOutputStream(); 118 119 log("Starting \"echo\" on "+s); 120 121 String line = in.readLine(); 122 while(line != null){ 123 out.write((line+"\n").getBytes()); 124 log(line); 125 line = in.readLine(); 126 } 127 128 log("Echo done."); 129 } 130 131 136 static public void discard(Socket s) throws IOException{ 137 BufferedReader in = new BufferedReader(new InputStreamReader( 138 s.getInputStream())); 139 log("Starting discard on "+s); 140 141 String line = in.readLine(); 142 while(line != null){ 143 log(line); 144 line = in.readLine(); 145 } 146 147 log("Discard finished."); 148 } 149 150 167 static public void chargen(Socket s,long wait_time,long max_wait) 168 throws IOException{ 169 byte[] buf = chargenSequence.getBytes(); 170 int pos = 0; 171 OutputStream out = s.getOutputStream(); 172 InputStream in = s.getInputStream(); 173 s.setSoTimeout(100); 175 log("Starting \"chargen\" on "+s); 176 while(true){ 177 log("Sending message."); 178 out.write(buf,pos,buf.length - pos); 179 out.write(buf,0,pos); 180 out.write("\n".getBytes()); 181 pos++; 182 try{ 183 if(wait_time > max_wait) break; 184 185 log("Going to sleep for "+wait_time+" ms."); 186 Thread.currentThread().sleep(wait_time); 187 wait_time *= 2; 188 if(in.read() < 0) break; }catch(InterruptedException ie){ 190 }catch(InterruptedIOException ioe){ 191 } 192 } 193 log("Chargen finished."); 194 } 195 196 208 static public void connect(Socket s)throws IOException{ 209 String line = null; 210 Socket sock; 211 int port; 212 int service_id; 213 214 BufferedReader in = new BufferedReader(new InputStreamReader( 215 s.getInputStream())); 216 OutputStream out = s.getOutputStream(); 217 218 log("Starting \"connect\" on "+s); 219 line = in.readLine(); 220 if(line == null) return; 222 java.util.StringTokenizer st = new java.util.StringTokenizer (line); 223 if(st.countTokens() < 2){ out.write("Expect: port serviceId.\n".getBytes()); 225 log("Invalid arguments."); 226 return; 227 } 228 try{ 229 port = Integer.parseInt(st.nextToken()); 230 service_id = Integer.parseInt(st.nextToken()); 231 }catch(NumberFormatException nfe){ 232 out.write("Expect: port serviceId.\n".getBytes()); 233 log("Invalid arguments."); 234 return; 235 } 236 try{ 237 log("Connecting to "+s.getInetAddress()+":"+port); 238 sock = new Socket(s.getInetAddress(),port); 239 }catch(IOException ioe){ 240 out.write(("Connect to "+s.getInetAddress()+ 241 ":"+port+" failed").getBytes()); 242 log("Connect failed."); 243 return; 244 } 245 s.close(); 246 log("About to serve "+service_id); 247 serve(sock,service_id); 248 } 249 250 255 static public void pipe(InputStream in, OutputStream out) 256 throws IOException{ 257 byte[] buf = new byte[BUF_SIZE]; 258 int bread = 0; 259 while(bread >= 0){ 260 bread = in.read(buf); 261 out.write(buf,0,bread); 262 } 263 } 264 265 268 static synchronized void log(String s){ 269 if(log != null) log.println(s); 270 } 271 272 } 273 | Popular Tags |