1 19 20 package org.netbeans.test.cvsmodule; 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 (!isRunnuing()) { 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 setRunning(true); 179 180 while (true) { 181 try { 182 clientSocket = serverSocket.accept(); 183 if (ignoreProbe == false) { 184 break; 185 } 186 ignoreProbe = false; 187 } catch (IOException e) { 188 throwable = e; 189 return; 190 } 191 } 192 193 try { 194 socketOut = clientSocket.getOutputStream(); 195 socketIn = clientSocket.getInputStream(); 196 if (consumeInput()) { 197 return; 198 } 199 int nextByte = fakeDataStream.read(); 200 while (nextByte != -1) { 201 if (outputCounter-- == 0) { 202 if ((simulationMode & SIMULATE_DROP) != 0) { 203 socketOut.flush(); 204 socketOut.close(); 205 } 206 if ((simulationMode & SIMULATE_OVERLOAD) != 0) { 207 clientSocket.shutdownOutput(); 208 } 209 if ((simulationMode & SIMULATE_SLOWNESS) != 0) { 210 try { 211 Thread.sleep(5000); 212 } catch (InterruptedException e) { 213 throwable = e; 214 } 215 } 216 if ((simulationMode & (SIMULATE_OVERLOAD | SIMULATE_DROP)) != 0) { 217 consumeInputUntilStopped(); 218 return; 219 } 220 } 221 socketOut.write(nextByte); 222 if (consumeInput()) { 223 return; 224 } 225 nextByte = fakeDataStream.read(); 226 } 227 socketOut.flush(); 228 232 consumeInputUntilStopped(); 234 } catch (IOException e) { 235 throwable = e; 236 return; 237 } 238 } finally { 239 try { 240 fakeDataStream.close(); 241 } catch (IOException alreadyClosed) { 242 } 243 try { 244 if (socketIn != null) socketIn.close(); 245 } catch (IOException alreadyClosed) { 246 } 247 try { 248 if (socketOut != null) socketOut.close(); 249 } catch (IOException alreadyClosed) { 250 } 251 try { 252 if (requestsStream != null) { 253 requestsStream.flush(); 254 requestsStream.close(); 255 } 256 } catch (IOException alreadyClosed) { 257 } 258 } 259 } 260 261 264 public synchronized void stop() throws Exception { 265 stopped = true; 266 notifyAll(); 267 if (throwable != null) { 268 throw throwable; 269 } 270 } 271 272 273 public String toString() { 274 StringWriter sw = new StringWriter(); 275 PrintWriter ps = new PrintWriter(sw); 276 ps.write("PseudoCvsServer on " + serverSocket + "\n"); 277 if (throwable != null) { 278 throwable.fillInStackTrace(); 279 throwable.printStackTrace(ps); 280 } 281 ps.flush(); 282 ps.close(); 283 return sw.getBuffer().toString(); 284 } 285 286 289 private boolean consumeInput() throws IOException { 290 int available = socketIn.available(); 291 for (int i = 0; i<available; i++) { 292 if (inputCounter-- == 0) { 293 if ((simulationMode & SIMULATE_DROP) != 0) { 294 socketIn.close(); 295 if (requestsStream != null) { 296 requestsStream.write("[PseudoCvsServer abort]".getBytes("utf8")); 297 } 298 } 299 if ((simulationMode & SIMULATE_OVERLOAD) != 0) { 300 clientSocket.shutdownInput(); 301 if (requestsStream != null) { 302 requestsStream.write("[PseudoCvsServer abort]".getBytes("utf8")); 303 } 304 } 305 return true; 306 } 307 int octet = socketIn.read(); 308 if (requestsStream != null) { 309 requestsStream.write(octet); 310 requestsStream.flush(); 311 } 312 } 313 return false; 314 } 315 316 private synchronized void consumeInputUntilStopped() throws IOException { 317 while (stopped == false) { 318 try { 319 wait(100); 320 consumeInput(); 321 } catch (InterruptedException e) { 322 throwable = e; 323 } 324 } 325 } 326 327 public synchronized boolean isRunnuing() { 328 return running; 329 } 330 331 public synchronized void setRunning(boolean value) { 332 running = value; 333 if (value) { 334 notifyAll(); 335 } 336 } 337 } 338 | Popular Tags |