Clover coverage report - ActiveIO - 1.0
Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT
file stats: LOC: 128   Methods: 7
NCLOC: 63   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
NIOSynchChannelFactory.java 50% 83.3% 71.4% 77.1%
coverage coverage
 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  2
     public NIOSynchChannelFactory() {
 50  2
         this(true);
 51   
     }
 52   
     
 53  14
     public NIOSynchChannelFactory(boolean createWriteBufferedChannels) {
 54  14
         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  14
     public SynchChannel openSynchChannel(URI location) throws IOException {
 64  14
         SocketChannel channel = SocketChannel.open();
 65  14
         channel.connect(new InetSocketAddress(location.getHost(), location.getPort()));
 66  14
         return createSynchChannel(channel);
 67   
     }
 68   
 
 69   
     /**
 70   
      * @param channel
 71   
      * @return
 72   
      * @throws IOException
 73   
      */
 74  14
     protected SynchChannel createSynchChannel(SocketChannel socketChannel) throws IOException {
 75  14
         SynchChannel channel = new NIOSynchChannel(socketChannel);
 76  14
         if( createWriteBufferedChannels ) {
 77  14
             channel = new WriteBufferedSynchChannel(channel, ByteBufferPacket.createDefaultBuffer(useDirectBuffers));
 78   
         }
 79  14
         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  14
     public SynchChannelServer bindSynchChannel(URI bindURI) throws IOException {
 88   
         
 89  14
         String host = bindURI.getHost();
 90  14
         InetSocketAddress address;
 91  14
         if ( host == null || host.length() == 0 || host.equals("localhost") || host.equals("0.0.0.0") ) {
 92  14
             address = new InetSocketAddress(bindURI.getPort());
 93   
         } else {
 94  0
             address = new InetSocketAddress(bindURI.getHost(), bindURI.getPort());
 95   
         }
 96   
         
 97  14
         ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
 98  14
         serverSocketChannel.socket().bind(address,backlog);
 99   
         
 100  14
         URI connectURI = bindURI;
 101  14
         try {
 102  14
             connectURI = URISupport.changeHost(connectURI, InetAddress.getLocalHost().getHostName());
 103  14
             connectURI = URISupport.changePort(connectURI, serverSocketChannel.socket().getLocalPort());
 104   
         } catch (URISyntaxException e) {
 105  0
             throw (IOException)new IOException("Could not build connect URI: "+e).initCause(e);
 106   
         }
 107   
         
 108  14
         return new NIOSynchChannelServer(serverSocketChannel, bindURI, connectURI, createWriteBufferedChannels, useDirectBuffers);
 109   
     }
 110   
     
 111   
     /**
 112   
      * @return Returns the backlog.
 113   
      */
 114  0
     public int getBacklog() {
 115  0
         return backlog;
 116   
     }
 117   
 
 118   
     /**
 119   
      * @param backlog
 120   
      *            The backlog to set.
 121   
      */
 122  0
     public void setBacklog(int backlog) {
 123  0
         this.backlog = backlog;
 124   
     }
 125   
 
 126   
 
 127   
 }
 128