KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > sapia > ubik > rmi > server > transport > nio > tcp > NioTcpTransportProvider


1 package org.sapia.ubik.rmi.server.transport.nio.tcp;
2
3 import java.net.InetSocketAddress JavaDoc;
4 import java.rmi.RemoteException JavaDoc;
5 import java.util.Properties JavaDoc;
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 /**
17  * @author Yanick Duchesne
18  *
19  * <dl>
20  * <dt><b>Copyright: </b>
21  * <dd>Copyright &#169; 2002-2005 <a HREF="http://www.sapia-oss.org">Sapia Open
22  * Source Software </a>. All Rights Reserved.</dd>
23  * </dt>
24  * <dt><b>License: </b>
25  * <dd>Read the license.txt file of the jar or visit the <a
26  * HREF="http://www.sapia-oss.org/license.html">license page </a> at the Sapia
27  * OSS web site</dd>
28  * </dt>
29  * </dl>
30  */

31 public class NioTcpTransportProvider implements TransportProvider {
32
33   /**
34    * Constant corresponding to this provider class' transport type.
35    */

36   public static final String JavaDoc TRANSPORT_TYPE = NioAddress.TRANSPORT_TYPE;
37
38   /**
39    * This constant corresponds to the
40    * <code>ubik.rmi.transport.nio-tcp.bind-address</code> system property.
41    */

42   public static final String JavaDoc BIND_ADDRESS = "ubik.rmi.transport.nio-tcp.bind-address";
43
44   /**
45    * This constant corresponds to the
46    * <code>ubik.rmi.transport.nio-tcp.port</code> system property.
47    */

48   public static final String JavaDoc PORT = "ubik.rmi.transport.nio-tcp.port";
49   
50   /**
51    * This constant corresponds to the
52    * <code>ubik.rmi.transport.nio-tcp.buffer-size</code> system property.
53    */

54   public static final String JavaDoc BUFFER_SIZE = "ubik.rmi.transport.nio-tcp.buffer-size";
55   
56   /**
57    * The default buffer size (512).
58    *
59    * @see #BUFFER_SIZE
60    */

61   public static final int DEFAULT_BUFFER_SIZE = 512;
62   
63   private int _bufsize = DEFAULT_BUFFER_SIZE;
64
65   /**
66    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#getPoolFor(org.sapia.ubik.net.ServerAddress)
67    */

68   public Connections getPoolFor(ServerAddress address) throws RemoteException JavaDoc {
69     return NioTcpClientConnectionPool.getInstance(address, _bufsize);
70   }
71
72   /**
73    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#getTransportType()
74    */

75   public String JavaDoc getTransportType() {
76     return TRANSPORT_TYPE;
77   }
78
79   /**
80    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#newDefaultServer()
81    */

82   public Server newDefaultServer() throws RemoteException JavaDoc {
83     return newServer(System.getProperties());
84   }
85
86   /**
87    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#newServer(java.util.Properties)
88    */

89   public Server newServer(Properties JavaDoc props) throws RemoteException JavaDoc {
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 JavaDoc e) {
96         Log.error(getClass(), "Could not parse integer from property "
97             + BIND_ADDRESS + ": " + PORT);
98       }
99     }
100     InetSocketAddress JavaDoc addr;
101     String JavaDoc bindAddress = props.getProperty(BIND_ADDRESS);
102     if(bindAddress != null) {
103       addr = new InetSocketAddress JavaDoc(bindAddress, port);
104     } else {
105       addr = new InetSocketAddress JavaDoc(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   /**
116    * @see org.sapia.ubik.rmi.server.transport.TransportProvider#shutdown()
117    */

118   public void shutdown() {
119     NioTcpClientConnectionPool.shutdown();
120   }
121   
122   public static void main(String JavaDoc[] args) {
123     try {
124       NioTcpTransportProvider provider = new NioTcpTransportProvider();
125       Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc e) {
145       e.printStackTrace();
146     }
147   }
148 }
149
Popular Tags