1 25 package org.objectweb.joram.mom.proxies.tcp; 26 27 import java.net.*; 28 import java.util.*; 29 30 import fr.dyade.aaa.agent.*; 31 import fr.dyade.aaa.util.*; 32 33 import org.objectweb.joram.shared.JoramTracing; 34 import org.objectweb.util.monolog.api.BasicLevel; 35 36 39 public class TcpProxyService { 40 44 public static final String SO_TIMEOUT_PROP = 45 "org.objectweb.joram.mom.proxies.tcp.soTimeout"; 46 47 50 public static final int DEFAULT_SO_TIMEOUT = 10000; 51 52 56 public static final String POOL_SIZE_PROP = 57 "org.objectweb.joram.mom.proxies.tcp.poolSize"; 58 59 62 public static final int DEFAULT_POOL_SIZE = 1; 63 64 68 public static final String BACKLOG_PROP = 69 "org.objectweb.joram.mom.proxies.tcp.backlog"; 70 71 74 public static final int DEFAULT_BACKLOG = 10; 75 76 79 public static final int DEFAULT_PORT = 16010; 80 81 public static final String DEFAULT_BINDADDRESS = "0.0.0.0"; 83 84 87 protected static TcpProxyService proxyService; 88 89 private static int port; 90 91 public static final int getListenPort() { 92 return port; 93 } 94 95 private static String address; 96 97 public static final String getListenAddress() { 98 return address; 99 } 100 101 108 public static void init(String args, boolean firstTime) throws Exception { 109 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 110 JoramTracing.dbgProxy.log(BasicLevel.DEBUG, 111 "TcpProxyService.init(" + args + ',' + firstTime + ')'); 112 113 port = DEFAULT_PORT;; 114 address = DEFAULT_BINDADDRESS; 115 if (args != null) { 116 StringTokenizer st = new StringTokenizer(args); 117 port = Integer.parseInt(st.nextToken()); 118 if (st.hasMoreTokens()) { 119 address = st.nextToken(); 120 } 121 } 122 123 int backlog = Integer.getInteger(BACKLOG_PROP, DEFAULT_BACKLOG).intValue(); 124 125 ServerSocket serverSocket; 128 129 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 130 JoramTracing.dbgProxy.log(BasicLevel.DEBUG, 131 "SSLTcpProxyService.init() - binding to " + 132 address + ", port " + port); 133 134 if (address.equals("0.0.0.0")) { 135 serverSocket = new ServerSocket(port, backlog); 136 } else { 137 serverSocket = new ServerSocket(port, backlog, InetAddress.getByName(address)); 138 } 139 140 int poolSize = Integer.getInteger(POOL_SIZE_PROP, DEFAULT_POOL_SIZE).intValue(); 141 142 int timeout = Integer.getInteger(SO_TIMEOUT_PROP, DEFAULT_SO_TIMEOUT).intValue(); 143 144 proxyService = new TcpProxyService(serverSocket, poolSize, timeout); 145 proxyService.start(); 146 } 147 148 151 public static void stopService() { 152 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 153 JoramTracing.dbgProxy.log(BasicLevel.DEBUG, "TcpProxyService.stop()"); 154 proxyService.stop(); 155 } 156 157 160 private ServerSocket serverSocket; 161 162 165 private Vector connections; 166 167 171 private TcpConnectionListener[] connectionListeners; 172 173 public TcpProxyService(ServerSocket serverSocket, 174 int poolSize, 175 int timeout) { 176 this.serverSocket = serverSocket; 177 this.connections = new Vector(); 178 connectionListeners = new TcpConnectionListener[poolSize]; 179 for (int i = 0; i < poolSize; i++) { 180 connectionListeners[i] = new TcpConnectionListener(serverSocket, 181 this, 182 timeout); 183 } 184 } 185 186 protected void start() { 187 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 188 JoramTracing.dbgProxy.log( 189 BasicLevel.DEBUG, "TcpProxyService.start()"); 190 for (int i = 0; i < connectionListeners.length; i++) { 191 connectionListeners[i].start(); 192 } 193 } 194 195 void registerConnection(TcpConnection tcpConnection) { 196 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 197 JoramTracing.dbgProxy.log( 198 BasicLevel.DEBUG, "TcpProxyService.registerConnection(" + 199 tcpConnection + ')'); 200 connections.addElement(tcpConnection); 201 } 202 203 void unregisterConnection(TcpConnection tcpConnection) { 204 if (JoramTracing.dbgProxy.isLoggable(BasicLevel.DEBUG)) 205 JoramTracing.dbgProxy.log( 206 BasicLevel.DEBUG, "TcpProxyService.unregisterConnection(" + 207 tcpConnection + ')'); 208 connections.removeElement(tcpConnection); 209 } 210 211 TcpConnection getConnection(AgentId proxyId, int key) { 212 for (int i = 0; i < connections.size(); i++) { 213 TcpConnection tc = (TcpConnection)connections.elementAt(i); 214 if (tc.getProxyId() == proxyId && 215 tc.getKey() == key) { 216 return tc; 217 } 218 } 219 return null; 220 } 221 222 private void stop() { 223 Vector stopList = (Vector)connections.clone(); 224 for (int i = 0; i < stopList.size(); i++) { 225 TcpConnection tc = 226 (TcpConnection)stopList.elementAt(i); 227 tc.close(); 228 } 229 for (int i = 0; i < connectionListeners.length; i++) { 230 connectionListeners[i].stop(); 231 } 232 } 233 } 234 | Popular Tags |