1 package org.sapia.ubik.net; 2 3 import java.io.IOException ; 4 5 import java.net.InetAddress ; 6 import java.net.ServerSocket ; 7 import java.net.Socket ; 8 import java.net.SocketException ; 9 import java.net.SocketTimeoutException ; 10 import java.net.UnknownHostException ; 11 12 13 23 public abstract class SocketServer implements Runnable { 24 private ServerSocket _server; 25 protected SocketConnectionFactory _fac; 26 private String _address; 27 private ThreadPool _tp; 28 private boolean _started; 29 private SocketException _startExc; 30 31 protected SocketServer(int port, ThreadPool tp, 32 UbikServerSocketFactory socketFactory) throws java.io.IOException { 33 this(port, new SocketConnectionFactory(), tp, socketFactory); 34 } 35 36 protected SocketServer(String bindAddr, int port, ThreadPool tp, 37 UbikServerSocketFactory socketFactory) throws java.io.IOException { 38 this(bindAddr, port, new SocketConnectionFactory(), tp, socketFactory); 39 } 40 41 protected SocketServer(int port, SocketConnectionFactory fac, ThreadPool tp, 42 UbikServerSocketFactory socketFactory) throws java.io.IOException { 43 _server = socketFactory.createServerSocket(port); 44 _fac = fac; 45 _tp = tp; 46 } 47 48 protected SocketServer(String bindAddr, int port, 49 SocketConnectionFactory fac, ThreadPool tp, 50 UbikServerSocketFactory socketFactory) throws java.io.IOException { 51 _server = socketFactory.createServerSocket(port, bindAddr); 52 _fac = fac; 53 _tp = tp; 54 } 55 56 63 protected SocketServer(ThreadPool tp, ServerSocket server) 64 throws IOException { 65 this(new SocketConnectionFactory(), tp, server); 66 } 67 68 76 protected SocketServer(SocketConnectionFactory fac, ThreadPool tp, 77 ServerSocket server) throws IOException { 78 _server = server; 79 _fac = fac; 80 _tp = tp; 81 } 82 83 public String getAddress() { 84 if (_address == null) { 85 _address = _server.getInetAddress().getHostAddress(); 86 87 if (_address.equals("0.0.0.0")) { 88 try { 89 _address = InetAddress.getLocalHost().getHostAddress(); 90 } catch (UnknownHostException e) { 91 throw new IllegalStateException (e.getClass().getName() + 92 " caught - msg " + e.getMessage()); 93 } 94 } 95 } 96 97 return _address; 98 } 99 100 public int getPort() { 101 return _server.getLocalPort(); 102 } 103 104 public void close() { 105 try { 106 _tp.shutdown(); 108 _server.close(); 109 } catch (Throwable t) { 110 t.printStackTrace(); 111 } 112 } 113 114 public void run() { 115 try { 116 _tp.fill(1); 117 } catch (Throwable t) { 118 t.printStackTrace(); 119 120 return; 121 } 122 123 while (true) { 124 try { 125 Socket client = null; 126 127 try { 128 if (!_started) { 129 try { 130 _server.setSoTimeout(1); 131 client = _server.accept(); 132 } catch (SocketTimeoutException e) { 133 } 135 136 _started = true; 137 _server.setSoTimeout(0); 138 notifyStarted(); 139 140 if (client == null) { 141 continue; 142 } 143 } else { 144 client = _server.accept(); 145 } 146 } catch (SocketTimeoutException ste) { 147 continue; 149 } catch (SocketException e) { 150 if (!_started) { 151 _startExc = e; 152 _started = true; 153 notifyStarted(); 154 } 155 156 break; 157 } 158 159 final Connection conn = _fac.newConnection(client); 160 161 Request req = new Request(conn, 162 new TCPAddress(getAddress(), getPort())); 163 ((PooledThread) _tp.acquire()).exec(req); 164 } catch (Throwable t) { 165 if (handleError(t)) { 166 close(); 167 168 break; 169 } 170 } 171 } 172 } 173 174 181 public synchronized void waitStarted() 182 throws InterruptedException , SocketException { 183 while (!_started) { 184 wait(); 185 } 186 187 if (_startExc != null) { 188 throw _startExc; 189 } 190 } 191 192 protected synchronized void notifyStarted() { 193 notifyAll(); 194 } 195 196 protected boolean handleError(Throwable t) { 197 t.printStackTrace(); 198 199 return true; 200 } 201 } 202 | Popular Tags |