1 25 package fr.dyade.aaa.agent; 26 27 import java.io.*; 28 import java.util.Vector ; 29 import java.util.Enumeration ; 30 import java.net.Socket ; 31 import java.net.ServerSocket ; 32 import java.net.InetAddress ; 33 import java.net.ConnectException ; 34 import java.net.BindException ; 35 import java.net.UnknownHostException ; 36 import java.net.SocketException ; 37 38 import org.objectweb.util.monolog.api.BasicLevel; 39 import org.objectweb.util.monolog.api.Logger; 40 41 import fr.dyade.aaa.util.SocketAddress; 42 43 47 public abstract class StreamNetwork extends Network { 48 58 int CnxRetry = 3; 59 69 int backlog = 5; 70 80 boolean TcpNoDelay = false; 81 91 int SoLinger = -1; 92 103 int SoTimeout = 0; 104 105 106 public StreamNetwork() { 107 super(); 108 } 109 110 120 public void init(String name, int port, short[] servers) throws Exception { 121 super.init(name, port, servers); 122 123 CnxRetry = Integer.getInteger("CnxRetry", CnxRetry).intValue(); 124 CnxRetry = Integer.getInteger(name + ".CnxRetry", CnxRetry).intValue(); 125 126 backlog = Integer.getInteger("backlog", backlog).intValue(); 127 backlog = Integer.getInteger(name + ".backlog", backlog).intValue(); 128 129 TcpNoDelay = Boolean.getBoolean(name + ".TcpNoDelay"); 130 if (! TcpNoDelay) TcpNoDelay = Boolean.getBoolean("TcpNoDelay"); 131 132 SoLinger = Integer.getInteger("SoLinger", SoLinger).intValue(); 133 SoLinger = Integer.getInteger(name + ".SoLinger", SoLinger).intValue(); 134 135 SoTimeout = Integer.getInteger("SoTimeout", SoTimeout).intValue(); 136 SoTimeout = Integer.getInteger(name + ".SoTimeout", SoTimeout).intValue(); 137 } 138 139 150 final Socket createSocket(ServerDesc server) throws IOException { 151 for (Enumeration e = server.getSockAddrs(); e.hasMoreElements();) { 152 SocketAddress sa = (SocketAddress) e.nextElement(); 153 154 if (this.logmon.isLoggable(BasicLevel.DEBUG)) 155 this.logmon.log(BasicLevel.DEBUG, 156 this.getName() + ", try to connect server#" + 157 server.getServerId() + 158 ", addr=" + sa.getHostname() + 159 ", port=" + sa.getPort()); 160 161 try { 162 Socket socket = createSocket(sa); 163 164 if (this.logmon.isLoggable(BasicLevel.DEBUG)) 165 this.logmon.log(BasicLevel.DEBUG, this.getName() + ", connected"); 166 167 server.moveToFirst(sa); 169 return socket; 170 } catch (IOException exc) { 171 this.logmon.log(BasicLevel.DEBUG, 172 this.getName() + ", connection refused, try next element"); 173 continue; 174 } 175 } 176 177 throw new ConnectException ("Cannot connect to server#" + server.getServerId()); 178 } 179 180 192 final Socket createSocket(SocketAddress addr) throws IOException { 193 try { 194 return createSocket(addr.getAddress(), addr.getPort()); 195 } catch (IOException exc) { 196 this.logmon.log(BasicLevel.DEBUG, 197 this.getName() + ", connection refused, reset addr"); 198 addr.resetAddr(); 199 return createSocket(addr.getAddress(), addr.getPort()); 200 } 201 } 202 203 221 Socket createSocket(InetAddress host, int port) throws IOException { 222 if (host == null) 223 throw new UnknownHostException (); 224 return new Socket (host, port); 225 } 226 227 235 final ServerSocket createServerSocket() throws IOException { 236 for (int i=0; ; i++) { 237 try { 238 return createServerSocket(port); 239 } catch (BindException exc) { 240 if (i > CnxRetry) throw exc; 241 try { 242 Thread.sleep(i * 200); 243 } catch (InterruptedException e) {} 244 } 245 } 246 } 247 248 259 ServerSocket createServerSocket(int port) throws IOException { 260 return new ServerSocket (port, backlog); 261 } 262 263 272 void setSocketOption(Socket sock) throws SocketException { 273 sock.setTcpNoDelay(TcpNoDelay); 275 sock.setSoTimeout(SoTimeout); 277 if (SoLinger >= 0) 279 sock.setSoLinger(true, SoLinger); 280 else 281 sock.setSoLinger(false, 0); 282 } 283 } 284 | Popular Tags |