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 import java.nio.channels.ServerSocketChannel;
26 import java.nio.channels.SocketChannel;
27
28 import org.activeio.SynchChannel;
29 import org.activeio.SynchChannelFactory;
30 import org.activeio.SynchChannelServer;
31 import org.activeio.filter.WriteBufferedSynchChannel;
32 import org.activeio.packet.ByteBufferPacket;
33
34 /***
35 * A TcpSynchChannelFactory creates {@see org.activeio.net.TcpSynchChannel}
36 * and {@see org.activeio.net.TcpSynchChannelServer} objects.
37 *
38 * @version $Revision$
39 */
40 public class NIOSynchChannelFactory implements SynchChannelFactory {
41
42 protected static final int DEFAULT_BUFFER_SIZE = Integer.parseInt(System.getProperty("org.activeio.net.nio.BufferSize", ""+(64*1024)));
43
44 protected static final int DEFAULT_BACKLOG = 500;
45 boolean useDirectBuffers = true;
46 private final boolean createWriteBufferedChannels;
47 private int backlog = DEFAULT_BACKLOG;
48
49 public NIOSynchChannelFactory() {
50 this(true);
51 }
52
53 public NIOSynchChannelFactory(boolean createWriteBufferedChannels) {
54 this.createWriteBufferedChannels = createWriteBufferedChannels;
55 }
56
57
58 /***
59 * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
60 *
61 * @see org.activeio.SynchChannelFactory#openSynchChannel(java.net.URI)
62 */
63 public SynchChannel openSynchChannel(URI location) throws IOException {
64 SocketChannel channel = SocketChannel.open();
65 channel.connect(new InetSocketAddress(location.getHost(), location.getPort()));
66 return createSynchChannel(channel);
67 }
68
69 /***
70 * @param channel
71 * @return
72 * @throws IOException
73 */
74 protected SynchChannel createSynchChannel(SocketChannel socketChannel) throws IOException {
75 SynchChannel channel = new NIOSynchChannel(socketChannel);
76 if( createWriteBufferedChannels ) {
77 channel = new WriteBufferedSynchChannel(channel, ByteBufferPacket.createDefaultBuffer(useDirectBuffers));
78 }
79 return channel;
80 }
81
82 /***
83 * Binds a server socket a the {@param location}'s port.
84 *
85 * @see org.activeio.SynchChannelFactory#bindSynchChannel(java.net.URI)
86 */
87 public SynchChannelServer bindSynchChannel(URI bindURI) throws IOException {
88
89 String host = bindURI.getHost();
90 InetSocketAddress address;
91 if ( host == null || host.length() == 0 || host.equals("localhost") || host.equals("0.0.0.0") ) {
92 address = new InetSocketAddress(bindURI.getPort());
93 } else {
94 address = new InetSocketAddress(bindURI.getHost(), bindURI.getPort());
95 }
96
97 ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
98 serverSocketChannel.socket().bind(address,backlog);
99
100 URI connectURI = bindURI;
101 try {
102 connectURI = URISupport.changeHost(connectURI, InetAddress.getLocalHost().getHostName());
103 connectURI = URISupport.changePort(connectURI, serverSocketChannel.socket().getLocalPort());
104 } catch (URISyntaxException e) {
105 throw (IOException)new IOException("Could not build connect URI: "+e).initCause(e);
106 }
107
108 return new NIOSynchChannelServer(serverSocketChannel, bindURI, connectURI, createWriteBufferedChannels, useDirectBuffers);
109 }
110
111 /***
112 * @return Returns the backlog.
113 */
114 public int getBacklog() {
115 return backlog;
116 }
117
118 /***
119 * @param backlog
120 * The backlog to set.
121 */
122 public void setBacklog(int backlog) {
123 this.backlog = backlog;
124 }
125
126
127 }