View Javadoc

1   /*** 
2    * 
3    * Copyright 2004 Hiram Chirino
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.activeio.net;
19  
20  import java.io.IOException;
21  import java.net.InetAddress;
22  import java.net.InetSocketAddress;
23  import java.net.URI;
24  import java.net.URISyntaxException;
25  
26  import org.activeio.AsynchChannel;
27  import org.activeio.AsynchChannelFactory;
28  import org.activeio.AsynchChannelServer;
29  import org.activeio.adapter.SynchToAsynchChannelServerAdapter;
30  import org.activeio.filter.WriteBufferedAsynchChannel;
31  import org.activeio.packet.ByteBufferPacket;
32  
33  import com.ibm.io.async.AsyncServerSocketChannel;
34  import com.ibm.io.async.AsyncSocketChannel;
35  
36  /***
37   * A TcpAsynchChannelFactory creates {@see org.activeio.net.TcpAsynchChannel}
38   * and {@see org.activeio.net.TcpAsynchChannelServer} objects.
39   * 
40   * @version $Revision$
41   */
42  public class AIOAsynchChannelFactory implements AsynchChannelFactory {
43  
44      protected static final int DEFAULT_BACKLOG = 500;
45      private int backlog = DEFAULT_BACKLOG;
46          
47      /***
48       * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
49       * 
50       * @see org.activeio.AsynchChannelFactory#openAsynchChannel(java.net.URI)
51       */
52      public AsynchChannel openAsynchChannel(URI location) throws IOException {
53          AsyncSocketChannel channel = AsyncSocketChannel.open();
54          channel.connect(new InetSocketAddress(location.getHost(), location.getPort()));
55          return createAsynchChannel(channel);
56      }
57  
58      /***
59       * @param channel
60       * @return
61       * @throws IOException
62       */
63      protected AsynchChannel createAsynchChannel(AsyncSocketChannel socketChannel) throws IOException {
64          AsynchChannel channel = new AIOAsynchChannel(socketChannel);
65          channel = new WriteBufferedAsynchChannel(channel, ByteBufferPacket.createDefaultBuffer(true), false);
66          return channel;
67      }
68  
69      /***
70       * Binds a server socket a the {@param location}'s port. 
71       * 
72       * @see org.activeio.AsynchChannelFactory#bindAsynchChannel(java.net.URI)
73       */
74      public AsynchChannelServer bindAsynchChannel(URI bindURI) throws IOException {
75          
76          String host = bindURI.getHost();
77          InetSocketAddress address;
78          if ( host == null || host.length() == 0 || host.equals("localhost") || host.equals("0.0.0.0") ) {
79              address = new InetSocketAddress(bindURI.getPort());
80          } else {
81              address = new InetSocketAddress(bindURI.getHost(), bindURI.getPort());
82          }
83          
84          AsyncServerSocketChannel serverSocketChannel = AsyncServerSocketChannel.open();
85          serverSocketChannel.socket().bind(address,backlog);
86          
87          URI connectURI = bindURI;
88          try {
89              connectURI = URISupport.changeHost(connectURI, InetAddress.getLocalHost().getHostName());
90              connectURI = URISupport.changePort(connectURI, serverSocketChannel.socket().getLocalPort());
91          } catch (URISyntaxException e) {
92              throw (IOException)new IOException("Could not build connect URI: "+e).initCause(e);
93          }
94          
95          return SynchToAsynchChannelServerAdapter.adapt( 
96                  new AIOSynchChannelServer(serverSocketChannel, bindURI, connectURI));
97      }
98      
99      /***
100      * @return Returns the backlog.
101      */
102     public int getBacklog() {
103         return backlog;
104     }
105 
106     /***
107      * @param backlog
108      *            The backlog to set.
109      */
110     public void setBacklog(int backlog) {
111         this.backlog = backlog;
112     }
113 
114 
115 }