1 /* Reattore HTTP Server 2 3 Copyright (C) 2002 Michael Hope <michaelh@juju.net.nz> 4 5 This program is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 2 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program; if not, write to the Free Software 17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 18 19 $Id: MockServer.java,v 1.4 2003/01/21 22:43:12 michaelh Exp $ 20 */ 21 22 package test.juju.reattore.loadtest.controller.impl; 23 24 import java.io.IOException; 25 import java.io.InputStream; 26 import java.net.ServerSocket; 27 import java.net.Socket; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 32 public class MockServer 33 extends SafeThread { 34 35 private static Log log = LogFactory.getLog(MockServer.class); 36 37 private ServerSocket ss; 38 private int numHits; 39 private int numRunning; 40 41 private static final byte[] RESPONSE = 42 "200 HTTP/1.0 OK\r\n\r\nThis is the body.".getBytes(); 43 private static final byte[] EOH = "\r\n\r\n".getBytes(); 44 45 private class ClientThread 46 extends Thread { 47 48 private Socket client; 49 private int eohPos; 50 51 private ClientThread(Socket client) { 52 this.client = client; 53 } 54 55 public void run() { 56 synchronized (log) { 57 numRunning++; 58 } 59 60 try { 61 InputStream in = client.getInputStream(); 62 byte[] ab = new byte[1024]; 63 64 headerDone: 65 do { 66 int got = in.read(ab); 67 68 log.debug("Read " + got + " bytes: '" + (new String(ab, 0, got)) + "'"); 69 70 for (int i = 0; i < got; i++) { 71 if (ab[i] == EOH[eohPos]) { 72 eohPos++; 73 if (eohPos == EOH.length) { 74 log.debug("Found end of HTTP header"); 75 break headerDone; 76 } 77 } 78 else { 79 eohPos = 0; 80 } 81 } 82 } while (true); 83 84 client.getOutputStream().write(RESPONSE); 85 client.getOutputStream().flush(); 86 client.close(); 87 } 88 catch (Exception ex) { 89 log.debug("Unexpected exception in client thread", ex); 90 } 91 92 synchronized (log) { 93 numRunning--; 94 } 95 } 96 } 97 98 99 public MockServer(int port) 100 throws IOException { 101 102 ss = new ServerSocket(port); 103 } 104 105 public void run() { 106 running(); 107 108 while (shouldStop() == false) { 109 try { 110 Socket client = ss.accept(); 111 112 new ClientThread(client).start(); 113 numHits++; 114 } 115 catch (Exception ex) { 116 log.debug("Got an exception", ex); 117 break; 118 } 119 } 120 121 try { 122 ss.close(); 123 } 124 catch (IOException ex) { 125 log.debug("Exception while closing, ignoring", ex); 126 } 127 128 synchronized (log) { 129 do { 130 if (numRunning != 0) { 131 try { 132 Thread.sleep(10); 133 } 134 catch (Exception ex) { 135 /* Ignore */ 136 } 137 } 138 } while (numRunning != 0); 139 } 140 } 141 142 protected void notifyEnd() { 143 try { 144 ss.close(); 145 } 146 catch (IOException ex) { 147 /* Ignore */ 148 } 149 } 150 151 public int getNumHits() { 152 return numHits; 153 } 154 155 public static void main(String[] args) 156 throws IOException { 157 158 new MockServer(8081).start(); 159 } 160 }

This page was automatically generated by Maven