1 24 25 package org.objectweb.dream.channel; 26 27 import java.io.IOException ; 28 import java.net.BindException ; 29 import java.net.ServerSocket ; 30 import java.net.Socket ; 31 import java.net.SocketException ; 32 33 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 39 public class TCPAcceptSocketImpl extends AbstractAcceptSocketImpl 40 implements 41 TCPAcceptSocketAttributeController 42 { 43 44 45 protected ServerSocket listen; 46 47 51 protected int listeningPort = -1; 52 protected int openRetry = OPEN_RETRY_DEFAULT; 53 protected boolean tcpNoDelay = true; 54 protected int soTimeout = 0; 55 protected int soLinger = 60; 56 57 61 64 protected SocketState acceptSocket() throws IOException 65 { 66 if (listen == null || listen.isClosed()) 67 { 68 logger.log(BasicLevel.DEBUG, "ServerSocket closed, create new one"); 69 listen = createServerSocket(); 70 } 71 logger.log(BasicLevel.DEBUG, "waiting connection"); 72 Socket socket = listen.accept(); 73 setSocketOption(socket); 74 SocketState socketState = new SocketStateImpl(); 75 socketState.setSocket(socket); 76 return socketState; 77 } 78 79 83 86 public int getListeningPort() 87 { 88 return listeningPort; 89 } 90 91 94 public void setListeningPort(int port) throws IllegalLifeCycleException 95 { 96 if (getFcState() == STARTED) 97 { 98 throw new IllegalLifeCycleException( 99 "The component is started, can't change the listeningPort attribute"); 100 } 101 listeningPort = port; 102 } 103 104 107 public int getOpenRetry() 108 { 109 return openRetry; 110 } 111 112 115 public void setOpenRetry(int retry) 116 { 117 openRetry = retry; 118 } 119 120 123 public int getSoLinger() 124 { 125 return soLinger; 126 } 127 128 131 public void setSoLinger(int timeout) 132 { 133 soLinger = timeout; 134 } 135 136 139 public int getSoTimeout() 140 { 141 return soTimeout; 142 } 143 144 147 public void setSoTimeout(int timeout) 148 { 149 soTimeout = timeout; 150 } 151 152 155 public boolean getTcpNoDelay() 156 { 157 return tcpNoDelay; 158 } 159 160 163 public void setTcpNoDelay(boolean tcpNoDelay) 164 { 165 this.tcpNoDelay = tcpNoDelay; 166 } 167 168 172 175 public void startFc() throws IllegalLifeCycleException 176 { 177 if (listeningPort < 0) 178 { 179 throw new IllegalLifeCycleException("Unset attribute : listeningPort"); 180 } 181 super.startFc(); 182 } 183 184 187 public void stopFc() throws IllegalLifeCycleException 188 { 189 if (listen != null && !listen.isClosed()) 190 { 191 try 192 { 193 listen.close(); 194 } 195 catch (IOException e) 196 { 197 logger.log(BasicLevel.WARN, 198 "An error occured while closing server socket", e); 199 } 200 listen = null; 201 } 202 super.stopFc(); 203 } 204 205 209 protected ServerSocket createServerSocket() throws IOException 210 { 211 for (int i = 0;; i++) 212 { 213 try 214 { 215 return new ServerSocket (listeningPort); 216 } 217 catch (BindException exc) 218 { 219 if (i > openRetry) 220 { 221 throw exc; 222 } 223 try 224 { 225 Thread.sleep(i * 250); 226 } 227 catch (InterruptedException ignored) 228 { 229 } 231 } 232 } 233 } 234 235 protected void setSocketOption(Socket sock) throws SocketException 236 { 237 sock.setTcpNoDelay(tcpNoDelay); 238 sock.setSoTimeout(soTimeout); 239 if (soLinger >= 0) 240 { 241 sock.setSoLinger(true, soLinger); 242 } 243 else 244 { 245 sock.setSoLinger(false, 0); 246 } 247 } 248 249 } | Popular Tags |