1 package org.sapia.ubik.rmi.server.transport.socket; 2 3 import java.io.IOException ; 4 import java.net.InetAddress ; 5 import java.rmi.RemoteException ; 6 import java.util.Properties ; 7 8 import org.sapia.ubik.net.mplex.MultiplexServerSocket; 9 import org.sapia.ubik.net.mplex.MultiplexSocketConnector; 10 import org.sapia.ubik.net.mplex.StreamSelector; 11 import org.sapia.ubik.rmi.Consts; 12 import org.sapia.ubik.rmi.server.Log; 13 import org.sapia.ubik.rmi.server.Server; 14 15 16 37 public class MultiplexSocketTransportProvider extends SocketTransportProvider { 38 42 public static final String ACCEPTOR_THREADS = "ubik.rmi.transport.mplex.acceptor-threads"; 43 44 48 public static final String SELECTOR_THREADS = "ubik.rmi.transport.mplex.selector-threads"; 49 50 51 private MultiplexServerSocket _multiplexServer; 52 53 56 public MultiplexSocketTransportProvider() { 57 super(); 58 } 59 60 66 public MultiplexSocketConnector createSocketConnector( 67 StreamSelector aSelector) { 68 if (_multiplexServer == null) { 69 throw new IllegalStateException ( 70 "Could not create a connector - no multiplex server is created"); 71 } 72 73 return _multiplexServer.createSocketConnector(aSelector); 74 } 75 76 81 public void removeSocketConnector(MultiplexSocketConnector anInterceptor) { 82 _multiplexServer.removeSocketConnector(anInterceptor); 83 } 84 85 88 public Server newServer(Properties props) throws RemoteException { 89 SocketRmiServer server; 90 int maxThreads = 0; 91 int port = 0; 92 int acceptorCount = 0; 93 int selectorCount = 0; 94 95 if(Log.isDebug()){ 96 Log.debug(getClass(), "Received properties: " + props); 97 } 98 99 if (_multiplexServer != null) { 100 throw new IllegalStateException ( 101 "There is already one multiplex server running: " + _multiplexServer); 102 } 103 104 if (props.getProperty(Consts.SERVER_MAX_THREADS) != null) { 105 try { 106 maxThreads = Integer.parseInt(props.getProperty( 107 Consts.SERVER_MAX_THREADS)); 108 } catch (NumberFormatException e) { 109 Log.error(getClass(), 110 "could not parse integer from property: " + 111 Consts.SERVER_MAX_THREADS); 112 } 113 } 114 115 if (props.getProperty(MAX_THREADS) != null) { 116 try { 117 maxThreads = Integer.parseInt(props.getProperty(MAX_THREADS)); 118 } catch (NumberFormatException e) { 119 Log.error(getClass(), 120 "could not parse integer from property: " + MAX_THREADS); 121 } 122 } 123 124 if (props.getProperty(PORT) != null) { 125 try { 126 Log.debug(getClass(), "Got port: " + props.getProperty(PORT)); 127 port = Integer.parseInt(props.getProperty(PORT)); 128 } catch (NumberFormatException e) { 129 Log.error(getClass(), "could not parse integer from property: " + PORT); 130 } 131 } 132 else{ 133 Log.debug(getClass(), "Server port not specified"); 134 } 135 136 if (props.getProperty(ACCEPTOR_THREADS) != null) { 137 try { 138 acceptorCount = Integer.parseInt(props.getProperty(ACCEPTOR_THREADS)); 139 } catch (NumberFormatException e) { 140 Log.error(getClass(), 141 "could not parse integer from property: " + ACCEPTOR_THREADS); 142 } 143 } 144 145 if (props.getProperty(SELECTOR_THREADS) != null) { 146 try { 147 selectorCount = Integer.parseInt(props.getProperty(SELECTOR_THREADS)); 148 } catch (NumberFormatException e) { 149 Log.error(getClass(), 150 "could not parse integer from property: " + SELECTOR_THREADS); 151 } 152 } 153 154 try { 155 if (props.getProperty(BIND_ADDRESS) != null) { 156 String addr = props.getProperty(BIND_ADDRESS); 157 Log.debug(getClass(), "Creating server on " + addr + ":" + port); 158 _multiplexServer = new MultiplexServerSocket(port, 50, 159 InetAddress.getByName(addr)); 160 } else { 161 Log.debug(getClass(), "Creating server on port :" + port); 162 _multiplexServer = new MultiplexServerSocket(port); 163 } 164 165 if (acceptorCount > 0) { 166 _multiplexServer.setAcceptorDaemonThread(acceptorCount); 167 } 168 169 if (selectorCount > 0) { 170 _multiplexServer.setSelectorDaemonThread(selectorCount); 171 } 172 173 server = new SocketRmiServer(maxThreads, _multiplexServer); 174 } catch (IOException ioe) { 175 throw new RemoteException ("could not create multiplex server socket", ioe); 176 } 177 178 return server; 179 } 180 181 184 public Server newServer(int port) throws RemoteException { 185 SocketRmiServer server; 186 int maxThreads = 0; 187 int acceptorCount = 0; 188 int selectorCount = 0; 189 190 if (_multiplexServer != null) { 191 throw new IllegalStateException ( 192 "There is already one multiplex server running: " + _multiplexServer); 193 } 194 195 if (System.getProperty(Consts.SERVER_MAX_THREADS) != null) { 196 try { 197 maxThreads = Integer.parseInt(System.getProperty( 198 Consts.SERVER_MAX_THREADS)); 199 } catch (NumberFormatException e) { 200 Log.error(getClass(), 201 "could not parse integer from property: " + 202 Consts.SERVER_MAX_THREADS); 203 } 204 } 205 206 if (System.getProperty(ACCEPTOR_THREADS) != null) { 207 try { 208 acceptorCount = Integer.parseInt(System.getProperty(ACCEPTOR_THREADS)); 209 } catch (NumberFormatException e) { 210 Log.error(getClass(), 211 "could not parse integer from property: " + ACCEPTOR_THREADS); 212 } 213 } 214 215 if (System.getProperty(SELECTOR_THREADS) != null) { 216 try { 217 selectorCount = Integer.parseInt(System.getProperty(SELECTOR_THREADS)); 218 } catch (NumberFormatException e) { 219 Log.error(getClass(), 220 "could not parse integer from property: " + SELECTOR_THREADS); 221 } 222 } 223 224 try { 225 if (System.getProperty(BIND_ADDRESS) != null) { 226 String addr = System.getProperty(BIND_ADDRESS); 227 Log.debug(getClass(), "Creating server on " + addr + ":" + port); 228 _multiplexServer = new MultiplexServerSocket(port, 50, 229 InetAddress.getByName(addr)); 230 } else { 231 Log.debug(getClass(), "Creating server on port :" + port); 232 _multiplexServer = new MultiplexServerSocket(port); 233 } 234 235 if (acceptorCount > 0) { 236 _multiplexServer.setAcceptorDaemonThread(acceptorCount); 237 } 238 239 if (selectorCount > 0) { 240 _multiplexServer.setSelectorDaemonThread(selectorCount); 241 } 242 243 server = new SocketRmiServer(maxThreads, _multiplexServer); 244 } catch (java.io.IOException e) { 245 throw new java.rmi.RemoteException ("could not create singleton server", e); 246 } 247 248 return server; 249 } 250 251 } 252 | Popular Tags |