| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.http.transport; |
| 34 | | | |
| 35 | | | import java.net.ServerSocket; |
| 36 | | | import java.net.Socket; |
| 37 | | | import java.net.SocketTimeoutException; |
| 38 | | | import java.util.HashMap; |
| 39 | | | import java.util.Iterator; |
| 40 | | | import java.util.Map; |
| 41 | | | import java.util.logging.Level; |
| 42 | | | import java.util.logging.Logger; |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | (1) | public class SimpleServer |
| 49 | | | extends Thread |
| 50 | | | { |
| 51 | | | |
| 52 | 75 | | private static final String CLASSNAME |
| 53 | | | = SimpleServer.class.getName(); |
| 54 | | | |
| 55 | 100 | | private static final Logger logger |
| 56 | | | = Logger.getLogger(CLASSNAME); |
| 57 | | | private static final int SOCKET_TIMEOUT = 10000; |
| 58 | | | private static final int JOIN_TIMEFRAME = 5000; |
| 59 | | | private static final int WAITING_LOOPS = 10; |
| 60 | | | |
| 61 | | | private final Terminator mTerminator; |
| 62 | | | |
| 63 | 100 | | private boolean mDoStop = false; |
| 64 | | | |
| 65 | 100 | | private final Object mMonitor = new Object(); |
| 66 | | | |
| 67 | 100 | | private final Map mHandler = new HashMap(); |
| 68 | | | |
| 69 | | | private final int mPort; |
| 70 | | | |
| 71 | 100 | | private volatile boolean mServerStarted = false; |
| 72 | | | |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | @param |
| 77 | | | |
| 78 | | | |
| 79 | | | public SimpleServer (int port) |
| 80 | 100 | | { |
| 81 | 100 | | mPort = port; |
| 82 | 100 | | mTerminator = new Terminator(this); |
| 83 | 100 | | } |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | @param |
| 89 | | | |
| 90 | | | |
| 91 | | | public static void main (String[] args) |
| 92 | | | { |
| 93 | 0 | | if (args.length != 1) |
| 94 | | | { |
| 95 | 0 | | logger.info("Usage: SimpleServer <port>"); |
| 96 | 0 | | return; |
| 97 | | | } |
| 98 | 0 | | final SimpleServer server = new SimpleServer(Integer.parseInt(args[0])); |
| 99 | 0 | | server.startServer(); |
| 100 | 0 | | server.stopServer(); |
| 101 | 0 | | } |
| 102 | | | |
| 103 | | | |
| 104 | | | |
| 105 | | | |
| 106 | | | public void run () |
| 107 | | | { |
| 108 | 0 | | startServer(); |
| 109 | 0 | | stopServer(); |
| 110 | 0 | | } |
| 111 | | | |
| 112 | | | |
| 113 | | | |
| 114 | | | |
| 115 | | | |
| 116 | | | |
| 117 | | | public void startServer () |
| 118 | | | { |
| 119 | 100 | | logger.info("Starting server.."); |
| 120 | | | try |
| 121 | | | { |
| 122 | 100 | | final ServerSocket s = new ServerSocket(mPort); |
| 123 | 100 | | s.setSoTimeout(SOCKET_TIMEOUT); |
| 124 | 100 | | int clientCounter = 0; |
| 125 | 100 | | int waitingCounter = 0; |
| 126 | 100 | | ClientHandler client = null; |
| 127 | 100 | | logger.info("Waiting for connection on " + s); |
| 128 | 100 | | while (!haveToStop() && waitingCounter < WAITING_LOOPS) |
| 129 | | | { |
| 130 | 100 | | Socket incoming = null; |
| 131 | | | try |
| 132 | | | { |
| 133 | 100 | | mServerStarted = true; |
| 134 | 100 | | incoming = s.accept(); |
| 135 | | | } |
| 136 | 0 | | catch (SocketTimeoutException ste) |
| 137 | | | { |
| 138 | 0 | | waitingCounter++; |
| 139 | 100 | | } |
| 140 | | | |
| 141 | 100 | | if (incoming != null) |
| 142 | | | { |
| 143 | 100 | | clientCounter++; |
| 144 | 100 | | client = new ClientHandler(incoming, clientCounter); |
| 145 | 100 | | mHandler.put(Integer.toString(clientCounter), client); |
| 146 | 100 | | client.start(); |
| 147 | | | } |
| 148 | 100 | | } |
| 149 | | | } |
| 150 | 0 | | catch (Exception e) |
| 151 | | | { |
| 152 | 0 | | logger.log(Level.WARNING, "Failed to start SimpleServer: " + e, e); |
| 153 | 0 | (2) | e.printStackTrace(); |
| 154 | 0 | | } |
| 155 | | | |
| 156 | 0 | | mServerStarted = true; |
| 157 | 0 | | } |
| 158 | | | |
| 159 | | | |
| 160 | | | |
| 161 | | | |
| 162 | | | public void doStop () |
| 163 | | | { |
| 164 | 100 | | synchronized (mMonitor) |
| 165 | | | { |
| 166 | 100 | | mDoStop = true; |
| 167 | 50 | | } |
| 168 | 100 | | } |
| 169 | | | |
| 170 | | | |
| 171 | | | |
| 172 | | | @return |
| 173 | | | |
| 174 | | | |
| 175 | | | public boolean haveToStop () |
| 176 | | | { |
| 177 | 100 | | synchronized (mMonitor) |
| 178 | | | { |
| 179 | 100 | | return mDoStop; |
| 180 | 0 | | } |
| 181 | | | } |
| 182 | | | |
| 183 | | | |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | public void stopServer () |
| 188 | | | { |
| 189 | 0 | | logger.info("\n---Stopping Server with " + mHandler.size() |
| 190 | | | + " client handler---\n"); |
| 191 | 0 | | final Iterator it = mHandler.keySet().iterator(); |
| 192 | 0 | | while (it.hasNext()) |
| 193 | | | { |
| 194 | | | try |
| 195 | | | { |
| 196 | 0 | | final String clientId = (String) it.next(); |
| 197 | 0 | | final ClientHandler client = (ClientHandler) mHandler.get(clientId); |
| 198 | 0 | | if (client != null) |
| 199 | | | { |
| 200 | 0 | | client.haveToStop(); |
| 201 | 0 | | client.join(JOIN_TIMEFRAME); |
| 202 | 0 | | if (client.isAlive()) |
| 203 | | | { |
| 204 | 0 | | interruptClient(client); |
| 205 | | | } |
| 206 | | | } |
| 207 | | | } |
| 208 | 0 | | catch (Exception ex) |
| 209 | | | { |
| 210 | | | |
| 211 | 0 | | } |
| 212 | | | } |
| 213 | 0 | | if (mTerminator != null && mTerminator.isAlive()) |
| 214 | | | { |
| 215 | 0 | | mTerminator.interrupt(); |
| 216 | | | } |
| 217 | | | |
| 218 | 0 | | logger.warning("---Stopping DONE---"); |
| 219 | 0 | | } |
| 220 | | | |
| 221 | | | |
| 222 | | | <tt></tt> |
| 223 | | | @return<tt></tt> |
| 224 | | | <tt></tt> |
| 225 | | | |
| 226 | | | public boolean isServerStarted () |
| 227 | | | { |
| 228 | 100 | | return mServerStarted; |
| 229 | | | } |
| 230 | | | |
| 231 | | | |
| 232 | | | @param |
| 233 | | | |
| 234 | | | @throws |
| 235 | | | |
| 236 | | | |
| 237 | | | private void interruptClient (ClientHandler client) |
| 238 | | | throws InterruptedException |
| 239 | | | { |
| 240 | | | try |
| 241 | | | { |
| 242 | 0 | | client.interrupt(); |
| 243 | 0 | | logger.info("Needed to interrupt client handler thread."); |
| 244 | | | } |
| 245 | 0 | | catch (Exception ex) |
| 246 | | | { |
| 247 | 0 | | logger.log(Level.WARNING, |
| 248 | | | "Exception while interrupting client handler thread: " |
| 249 | | | + ex.getMessage(), ex); |
| 250 | 0 | | } |
| 251 | 0 | | client.join(JOIN_TIMEFRAME); |
| 252 | 0 | | if (client.isAlive()) |
| 253 | | | { |
| 254 | 0 | | logger.warning("unable to interrupt client thread"); |
| 255 | | | } |
| 256 | 0 | | } |
| 257 | | | } |