1 19 20 package org.netbeans.lib.cvsclient; 21 22 import java.io.*; 23 import java.net.ServerSocket ; 24 import java.net.Socket ; 25 26 53 public final class PseudoCvsServer implements Runnable { 54 55 private final int SIMULATE_SLOWNESS = 1; 56 private final int SIMULATE_OVERLOAD = 2; 57 private final int SIMULATE_DROP = 4; 58 59 private final InputStream fakeDataStream; 60 private OutputStream requestsStream; 61 private final ServerSocket serverSocket; 62 63 private Socket clientSocket; 64 private OutputStream socketOut; 65 private InputStream socketIn; 66 67 private int outputCounter = -1; 68 private int inputCounter = -1; 69 private int simulationMode; 70 71 private Exception throwable; 72 private boolean stopped; 73 private boolean running; 74 private boolean ignoreProbe; 75 76 83 public PseudoCvsServer(InputStream in) throws IOException { 84 try { 85 this.fakeDataStream = in; 86 serverSocket = new ServerSocket (); 87 serverSocket.bind(null, 2); 88 } catch (IOException ex) { 89 in.close(); 90 throw ex; 91 } 92 } 93 94 97 public int getPort() { 98 return serverSocket.getLocalPort(); 99 } 100 101 107 public synchronized String getCvsRoot() { 108 try { 109 while (running == false) { 110 this.wait(); 111 } 112 } catch (InterruptedException e) { 113 } 114 return ":pserver:anoncvs@127.0.0.1:" + getPort() + "/cvs"; 115 } 116 117 126 public void simulateNetworkFailure(int write, int read) { 127 simulationMode |= SIMULATE_DROP; 128 outputCounter = write; 129 inputCounter = read; 130 } 131 132 141 public void simulateServerOverload(int write, int read) { 142 simulationMode |= SIMULATE_OVERLOAD; 143 outputCounter = write; 144 inputCounter = read; 145 } 146 147 148 public void simulateSlowNetwork(int write, int read) { 149 simulationMode |= SIMULATE_SLOWNESS; 150 outputCounter = write; 151 inputCounter = read; 152 } 153 154 158 public void ignoreProbe() { 159 ignoreProbe = true; 160 } 161 162 167 public void logRequests(OutputStream out) { 168 requestsStream = out; 169 } 170 171 175 public void run() { 176 177 try { 178 synchronized (this) { 179 running = true; 180 notifyAll(); 181 } 182 183 while (true) { 184 try { 185 clientSocket = serverSocket.accept(); 186 if (ignoreProbe == false) { 187 break; 188 } 189 ignoreProbe = false; 190 } catch (IOException e) { 191 throwable = e; 192 return; 193 } 194 } 195 196 try { 197 socketOut = clientSocket.getOutputStream(); 198 socketIn = clientSocket.getInputStream(); 199 if (consumeInput()) { 200 return; 201 } 202 int nextByte = fakeDataStream.read(); 203 while (nextByte != -1) { 204 if (outputCounter-- == 0) { 205 if ((simulationMode & SIMULATE_DROP) != 0) { 206 socketOut.flush(); 207 socketOut.close(); 208 } 209 if ((simulationMode & SIMULATE_OVERLOAD) != 0) { 210 clientSocket.shutdownOutput(); 211 } 212 if ((simulationMode & SIMULATE_SLOWNESS) != 0) { 213 try { 214 Thread.sleep(5000); 215 } catch (InterruptedException e) { 216 throwable = e; 217 } 218 } 219 if ((simulationMode & (SIMULATE_OVERLOAD | SIMULATE_DROP)) != 0) { 220 consumeInputUntilStopped(); 221 return; 222 } 223 } 224 socketOut.write(nextByte); 225 if (consumeInput()) { 226 return; 227 } 228 nextByte = fakeDataStream.read(); 229 } 230 socketOut.flush(); 231 235 consumeInputUntilStopped(); 237 } catch (IOException e) { 238 throwable = e; 239 return; 240 } 241 } finally { 242 try { 243 fakeDataStream.close(); 244 } catch (IOException alreadyClosed) { 245 } 246 try { 247 if (socketIn != null) socketIn.close(); 248 } catch (IOException alreadyClosed) { 249 } 250 try { 251 if (socketOut != null) socketOut.close(); 252 } catch (IOException alreadyClosed) { 253 } 254 try { 255 if (requestsStream != null) { 256 requestsStream.flush(); 257 requestsStream.close(); 258 } 259 } catch (IOException alreadyClosed) { 260 } 261 } 262 } 263 264 267 public synchronized void stop() throws Exception { 268 stopped = true; 269 notifyAll(); 270 if (throwable != null) { 271 throw throwable; 272 } 273 } 274 275 276 public String toString() { 277 StringWriter sw = new StringWriter(); 278 PrintWriter ps = new PrintWriter(sw); 279 ps.write("PseudoCvsServer on " + serverSocket + "\n"); 280 if (throwable != null) { 281 throwable.fillInStackTrace(); 282 throwable.printStackTrace(ps); 283 } 284 ps.flush(); 285 ps.close(); 286 return sw.getBuffer().toString(); 287 } 288 289 292 private boolean consumeInput() throws IOException { 293 int available = socketIn.available(); 294 for (int i = 0; i<available; i++) { 295 if (inputCounter-- == 0) { 296 if ((simulationMode & SIMULATE_DROP) != 0) { 297 socketIn.close(); 298 if (requestsStream != null) { 299 requestsStream.write("[PseudoCvsServer abort]".getBytes("utf8")); 300 } 301 } 302 if ((simulationMode & SIMULATE_OVERLOAD) != 0) { 303 clientSocket.shutdownInput(); 304 if (requestsStream != null) { 305 requestsStream.write("[PseudoCvsServer abort]".getBytes("utf8")); 306 } 307 } 308 return true; 309 } 310 int octet = socketIn.read(); 311 if (requestsStream != null) { 312 requestsStream.write(octet); 313 requestsStream.flush(); 314 } 315 } 316 return false; 317 } 318 319 private synchronized void consumeInputUntilStopped() throws IOException { 320 while (stopped == false) { 321 try { 322 wait(100); 323 consumeInput(); 324 } catch (InterruptedException e) { 325 throwable = e; 326 } 327 } 328 } 329 } 330 | Popular Tags |