1 24 25 package org.objectweb.dream.channel; 26 27 import java.io.IOException ; 28 import java.net.InetAddress ; 29 import java.net.Socket ; 30 import java.net.SocketException ; 31 32 import org.objectweb.dream.AbstractComponent; 33 import org.objectweb.dream.pool.ObjectPool; 34 import org.objectweb.fractal.api.NoSuchInterfaceException; 35 import org.objectweb.fractal.api.control.IllegalBindingException; 36 import org.objectweb.fractal.api.control.IllegalLifeCycleException; 37 import org.objectweb.util.monolog.api.BasicLevel; 38 39 46 public class IPSocketManagerTCPImpl extends AbstractComponent 47 implements 48 IPSocketManager, 49 IPSocketManagerTCPAttributeController 50 { 51 52 56 protected int cnxRetry = CNX_RETRY_DEFAULT; 57 protected boolean tcpNoDelay = true; 58 protected int soTimeout = 0; 59 protected int soLinger = 60; 60 61 65 66 public static final String SOCKET_STATE_POOL_ITF_NAME = "socket-state-pool"; 67 protected ObjectPool socketStatePoolItf = null; 68 69 73 76 public SocketState getSocket(InetAddress address, int port) 77 throws IOException 78 { 79 for (int i = 0;; i++) 80 { 81 try 82 { 83 logger.log(BasicLevel.DEBUG, "try to connect"); 84 Socket sock = new Socket (address, port); 85 setSocketOption(sock); 86 logger.log(BasicLevel.DEBUG, "connected"); 87 SocketState socketState = (SocketState) socketStatePoolItf 88 .newInstance(); 89 socketState.setSocket(sock); 90 return socketState; 91 } 92 catch (IOException exc) 93 { 94 if (i > cnxRetry) 95 { 96 throw exc; 97 } 98 logger.log(BasicLevel.WARN, "connection failed on address " + address 99 + " will retry"); 100 try 101 { 102 Thread.sleep(i * 250); 103 } 104 catch (InterruptedException ignored) 105 { 106 } 108 } 109 } 110 } 111 112 115 public void releaseSocket(SocketState socketState, boolean error) 116 { 117 socketState.close(); 118 socketStatePoolItf.recycleInstance(socketState); 119 } 120 121 protected void setSocketOption(Socket sock) throws SocketException 122 { 123 sock.setTcpNoDelay(tcpNoDelay); 124 sock.setSoTimeout(soTimeout); 125 if (soLinger >= 0) 126 { 127 sock.setSoLinger(true, soLinger); 128 } 129 else 130 { 131 sock.setSoLinger(false, 0); 132 } 133 } 134 135 139 142 public int getCnxRetry() 143 { 144 return cnxRetry; 145 } 146 147 150 public void setCnxRetry(int cnxRetry) 151 { 152 this.cnxRetry = cnxRetry; 153 } 154 155 158 public boolean getTcpNoDelay() 159 { 160 return tcpNoDelay; 161 } 162 163 166 public void setTcpNoDelay(boolean tcpNoDelay) 167 { 168 this.tcpNoDelay = tcpNoDelay; 169 } 170 171 174 public int getSoTimeout() 175 { 176 return soTimeout; 177 } 178 179 182 public void setSoTimeout(int timeout) 183 { 184 soTimeout = timeout; 185 } 186 187 190 public int getSoLinger() 191 { 192 return soLinger; 193 } 194 195 198 public void setSoLinger(int timeout) 199 { 200 soLinger = timeout; 201 } 202 203 207 210 public String [] listFc() 211 { 212 return new String []{SOCKET_STATE_POOL_ITF_NAME}; 213 } 214 215 219 public void bindFc(String clientItfName, Object serverItf) 220 throws NoSuchInterfaceException, IllegalBindingException, 221 IllegalLifeCycleException 222 { 223 super.bindFc(clientItfName, serverItf); 224 if (clientItfName.equals(SOCKET_STATE_POOL_ITF_NAME)) 225 { 226 socketStatePoolItf = (ObjectPool) serverItf; 227 } 228 } 229 } | Popular Tags |