ActiveIO - 1.0

Clover coverage report - ActiveIO - 1.0 Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT FRAMES NO FRAMES |
file stats: | LOC: | 132 |  | Methods: | 7
| NCLOC: | 65 |  | Classes: | 1
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover
Source file | Conditionals | Statements | Methods | TOTAL
NIOAsynchChannelFactory.java | 50% | 83.3% | 71.4% | 77.1% |

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.AsynchChannel;
29 |  |
     import org.activeio.AsynchChannelFactory;
30 |  |
     import org.activeio.AsynchChannelServer;
31 |  |
     import org.activeio.adapter.SynchToAsynchChannelServerAdapter;
32 |  |
     import org.activeio.filter.WriteBufferedAsynchChannel;
33 |  |
     import org.activeio.packet.ByteBufferPacket;
34 |  |
35 |  |
     /**
36 |  |
      * A TcpAsynchChannelFactory creates {@see org.activeio.net.TcpAsynchChannel}
37 |  |
      * and {@see org.activeio.net.TcpAsynchChannelServer} objects.
38 |  |
      *
39 |  |
      * @version $Revision$
40 |  |
      */
41 |  |
     public class NIOAsynchChannelFactory implements AsynchChannelFactory {
42 |  |
43 |  |
         protected static final int DEFAULT_BUFFER_SIZE = Integer.parseInt(System.getProperty("org.activeio.net.nio.BufferSize", ""+(64*1024)));
44 |  |
45 |  |
         protected static final int DEFAULT_BACKLOG = 500;
46 |  |
         boolean useDirectBuffers = true;
47 |  |
         private final boolean createWriteBufferedChannels;
48 |  |
         private int backlog = DEFAULT_BACKLOG;
49 |  |
50 | 2 |
         public NIOAsynchChannelFactory() {
51 | 2 |
             this(true);
52 |  |
         }
53 |  |
54 | 8 |
         public NIOAsynchChannelFactory(boolean createWriteBufferedChannels) {
55 | 8 |
             this.createWriteBufferedChannels = createWriteBufferedChannels;
56 |  |
         }
57 |  |
58 |  |
59 |  |
         /**
60 |  |
          * Uses the {@param location}'s host and port to create a tcp connection to a remote host.
61 |  |
          *
62 |  |
          * @see org.activeio.AsynchChannelFactory#openAsynchChannel(java.net.URI)
63 |  |
          */
64 | 8 |
         public AsynchChannel openAsynchChannel(URI location) throws IOException {
65 | 8 |
             SocketChannel channel = SocketChannel.open();
66 | 8 |
             channel.connect(new InetSocketAddress(location.getHost(), location.getPort()));
67 | 8 |
             return createAsynchChannel(channel);
68 |  |
         }
69 |  |
70 |  |
         /**
71 |  |
          * @param channel
72 |  |
          * @return
73 |  |
          * @throws IOException
74 |  |
          */
75 | 8 |
         protected AsynchChannel createAsynchChannel(SocketChannel socketChannel) throws IOException {
76 | 8 |
             AsynchChannel channel = new NIOAsynchChannel(socketChannel, useDirectBuffers);
77 | 8 |
             if( createWriteBufferedChannels ) {
78 | 8 |
                 channel = new WriteBufferedAsynchChannel(channel, ByteBufferPacket.createDefaultBuffer(useDirectBuffers), false);
79 |  |
             }
80 | 8 |
             return channel;
81 |  |
         }
82 |  |
83 |  |
         /**
84 |  |
          * Binds a server socket a the {@param location}'s port.
85 |  |
          *
86 |  |
          * @see org.activeio.AsynchChannelFactory#bindAsynchChannel(java.net.URI)
87 |  |
          */
88 | 8 |
         public AsynchChannelServer bindAsynchChannel(URI bindURI) throws IOException {
89 |  |
90 | 8 |
             String host = bindURI.getHost();
91 | 8 |
             InetSocketAddress address;
92 | 8 |
             if ( host == null || host.length() == 0 || host.equals("localhost") || host.equals("0.0.0.0") ) {
93 | 8 |
                 address = new InetSocketAddress(bindURI.getPort());
94 |  |
             } else {
95 | 0 |
                 address = new InetSocketAddress(bindURI.getHost(), bindURI.getPort());
96 |  |
             }
97 |  |
98 | 8 |
             ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
99 | 8 |
             serverSocketChannel.socket().bind(address,backlog);
100 |  |
101 | 8 |
             URI connectURI = bindURI;
102 | 8 |
             try {
103 | 8 |
                 connectURI = URISupport.changeHost(connectURI, InetAddress.getLocalHost().getHostName());
104 | 8 |
                 connectURI = URISupport.changePort(connectURI, serverSocketChannel.socket().getLocalPort());
105 |  |
             } catch (URISyntaxException e) {
106 | 0 |
                 throw (IOException)new IOException("Could not build connect URI: "+e).initCause(e);
107 |  |
             }
108 |  |
109 |  |
             // We won't use non blocking NIO for the server since you only need 1 thread for him anyways.
110 |  |
             // Just resuing the SocketChannelSynchChannelServer.
111 | 8 |
             return SynchToAsynchChannelServerAdapter.adapt(
112 |  |
                     new NIOAsynchChannelServer(serverSocketChannel, bindURI, connectURI, createWriteBufferedChannels, useDirectBuffers));
113 |  |
         }
114 |  |
115 |  |
         /**
116 |  |
          * @return Returns the backlog.
117 |  |
          */
118 | 0 |
         public int getBacklog() {
119 | 0 |
             return backlog;
120 |  |
         }
121 |  |
122 |  |
         /**
123 |  |
          * @param backlog
124 |  |
          *            The backlog to set.
125 |  |
          */
126 | 0 |
         public void setBacklog(int backlog) {
127 | 0 |
             this.backlog = backlog;
128 |  |
         }
129 |  |
130 |  |
131 |  |
     }
132 |  |