1 package org.sapia.ubik.rmi.server.transport.nio.tcp; 2 3 import java.net.InetSocketAddress ; 4 import java.rmi.RemoteException ; 5 import java.util.Properties ; 6 7 import org.sapia.ubik.net.Connection; 8 import org.sapia.ubik.net.ServerAddress; 9 import org.sapia.ubik.rmi.PropUtil; 10 import org.sapia.ubik.rmi.examples.HelloWorldCommand; 11 import org.sapia.ubik.rmi.server.Log; 12 import org.sapia.ubik.rmi.server.Server; 13 import org.sapia.ubik.rmi.server.transport.Connections; 14 import org.sapia.ubik.rmi.server.transport.TransportProvider; 15 16 31 public class NioTcpTransportProvider implements TransportProvider { 32 33 36 public static final String TRANSPORT_TYPE = NioAddress.TRANSPORT_TYPE; 37 38 42 public static final String BIND_ADDRESS = "ubik.rmi.transport.nio-tcp.bind-address"; 43 44 48 public static final String PORT = "ubik.rmi.transport.nio-tcp.port"; 49 50 54 public static final String BUFFER_SIZE = "ubik.rmi.transport.nio-tcp.buffer-size"; 55 56 61 public static final int DEFAULT_BUFFER_SIZE = 512; 62 63 private int _bufsize = DEFAULT_BUFFER_SIZE; 64 65 68 public Connections getPoolFor(ServerAddress address) throws RemoteException { 69 return NioTcpClientConnectionPool.getInstance(address, _bufsize); 70 } 71 72 75 public String getTransportType() { 76 return TRANSPORT_TYPE; 77 } 78 79 82 public Server newDefaultServer() throws RemoteException { 83 return newServer(System.getProperties()); 84 } 85 86 89 public Server newServer(Properties props) throws RemoteException { 90 PropUtil pu = new PropUtil().addProperties(props).addProperties(System.getProperties()); 91 int port = 0; 92 if(props.getProperty(PORT) != null) { 93 try { 94 port = Integer.parseInt(props.getProperty(PORT)); 95 } catch(NumberFormatException e) { 96 Log.error(getClass(), "Could not parse integer from property " 97 + BIND_ADDRESS + ": " + PORT); 98 } 99 } 100 InetSocketAddress addr; 101 String bindAddress = props.getProperty(BIND_ADDRESS); 102 if(bindAddress != null) { 103 addr = new InetSocketAddress (bindAddress, port); 104 } else { 105 addr = new InetSocketAddress (port); 106 } 107 108 _bufsize = pu.getIntProperty(BUFFER_SIZE, DEFAULT_BUFFER_SIZE); 109 110 NioServer server = new NioServer(addr, DispatcherStrategyFactory.newStrategy(props) 111 .newDispatcher(props), _bufsize); 112 return server; 113 } 114 115 118 public void shutdown() { 119 NioTcpClientConnectionPool.shutdown(); 120 } 121 122 public static void main(String [] args) { 123 try { 124 NioTcpTransportProvider provider = new NioTcpTransportProvider(); 125 Properties props = new Properties (); 126 props.put(NioTcpTransportProvider.PORT, "8080"); 127 Server server = provider.newServer(props); 128 System.out.println("Starting server"); 129 server.start(); 130 System.out.println("Server started"); 131 Connections conns = provider.getPoolFor(new NioAddress("localhost", 8080)); 132 System.out.println("acquiring connection"); 133 Connection conn = conns.acquire(); 134 HelloWorldCommand command = new HelloWorldCommand(); 135 for(int i = 0; i < 10; i++){ 136 System.out.println("sending command"); 137 conn.send(command); 138 System.out.println("Received: " + conn.receive()); 139 } 140 conns.release(conn); 141 conns.clear(); 142 server.close(); 143 144 } catch(Exception e) { 145 e.printStackTrace(); 146 } 147 } 148 } 149 | Popular Tags |