Clover coverage report - ActiveIO - 1.0
Coverage timestamp: Fri Apr 22 2005 14:27:22 PDT
file stats: LOC: 172   Methods: 17
NCLOC: 125   Classes: 2
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
VMPipeAsynchChannelPipe.java 50% 61.4% 70.6% 59.4%
coverage coverage
 1   
 /** 
 2   
  * 
 3   
  * Copyright 2004 Protique Ltd
 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.io.InterruptedIOException;
 22   
 
 23   
 import org.activeio.AsynchChannel;
 24   
 import org.activeio.AsynchChannelListener;
 25   
 import org.activeio.Packet;
 26   
 import org.activeio.packet.EOSPacket;
 27   
 
 28   
 import EDU.oswego.cs.dl.util.concurrent.Semaphore;
 29   
 
 30   
 /**
 31   
  * Used to connect the bottom ends of two Asynch channel stacks.
 32   
  * 
 33   
  */
 34   
 final public class VMPipeAsynchChannelPipe {
 35   
 
 36   
     final PipeChannel leftChannel = new PipeChannel();
 37   
     final PipeChannel rightChannel = new PipeChannel();
 38   
 
 39   
     final public static class PipeChannel implements AsynchChannel {        
 40   
         
 41   
         private PipeChannel sibiling;        
 42   
         private AsynchChannelListener channelListener;
 43   
         private final Semaphore runMutext = new Semaphore(0);
 44   
         private boolean disposed;
 45   
         private boolean running;
 46   
         
 47  32
         public PipeChannel() {
 48   
         }
 49   
         
 50  28
         public void setAsynchChannelListener(AsynchChannelListener channelListener) {
 51  28
             this.channelListener = channelListener;
 52   
         }
 53  0
         public AsynchChannelListener getAsynchChannelListener() {
 54  0
             return channelListener;
 55   
         }
 56   
         
 57  4008
         public void write(Packet packet) throws IOException {
 58  4008
             if( disposed )
 59  0
                 throw new IOException("Conneciton closed.");
 60  4008
             sibiling.onPacket(packet, WAIT_FOREVER_TIMEOUT);
 61   
         }
 62   
         
 63  4040
         private void onPacket(Packet packet, long timeout) throws IOException {
 64  4040
             try {
 65  4040
                 if( timeout == NO_WAIT_TIMEOUT ) {
 66  32
                     if( !runMutext.attempt(0) )
 67  4
                         return;
 68  4008
                 } else if( timeout == WAIT_FOREVER_TIMEOUT ) {
 69  4008
                     runMutext.acquire();
 70   
                 } else {
 71  0
                     if( !runMutext.attempt(timeout) ) 
 72  0
                         return;
 73   
                 }
 74   
             } catch (InterruptedException e) {
 75  0
                 throw new InterruptedIOException();
 76   
             }
 77  4036
             try {
 78  4036
                 if( disposed ) {
 79  16
                     throw new IOException("Peer connection closed.");
 80   
                 }            
 81  4020
                 channelListener.onPacket(packet);
 82   
             } finally {
 83  4036
                 runMutext.release();
 84   
             }
 85   
         }
 86   
 
 87  2004
         public void flush() throws IOException {
 88   
         }
 89   
         
 90  24
         public void start() throws IOException {
 91  24
             if(running)
 92  0
                 return;
 93  24
             if( channelListener==null )
 94  0
                 throw new IOException("channelListener has not been set.");
 95  24
             running=true;
 96  24
             runMutext.release();
 97   
         }
 98   
         
 99  0
         public void stop(long timeout) throws IOException {
 100  0
             if(!running)
 101  0
                 return;            
 102  0
             try {
 103  0
                 if( timeout == NO_WAIT_TIMEOUT ) {
 104  0
                     if( !runMutext.attempt(0) )
 105  0
                         return;
 106  0
                 } else if( timeout == WAIT_FOREVER_TIMEOUT ) {
 107  0
                     runMutext.acquire();
 108   
                 } else {
 109  0
                     if( !runMutext.attempt(timeout) ) 
 110  0
                         return;
 111   
                 }
 112  0
                 running=false;
 113   
             } catch (InterruptedException e) {
 114  0
                 throw new InterruptedIOException();
 115   
             }
 116   
         }
 117   
         
 118  36
         public void dispose() {
 119  36
             if( disposed )
 120  4
                 return;
 121   
             
 122  32
             if( running && channelListener!=null ) {
 123  24
                 this.channelListener.onPacketError(new IOException("Pipe closed."));
 124  24
                 running=false;
 125   
             }
 126  32
             disposed = true;
 127  32
             runMutext.release();
 128   
             
 129  32
             try {
 130   
                 // Inform the peer of the End Of Stream if he's listening.
 131  32
                 sibiling.onPacket(EOSPacket.EOS_PACKET, NO_WAIT_TIMEOUT);
 132   
             } catch (IOException e) {
 133   
             }
 134   
         }
 135   
         
 136  0
         public PipeChannel getSibiling() {
 137  0
             return sibiling;
 138   
         }
 139  32
         public void setSibiling(PipeChannel sibiling) {
 140  32
             this.sibiling = sibiling;
 141   
         }
 142   
 
 143  26
         public Object narrow(Class target) {
 144  26
             if( target.isAssignableFrom(getClass()) ) {
 145  8
                 return this;
 146   
             }
 147  18
             return null;
 148   
         }
 149   
         
 150  0
         public String getId() {
 151  0
             return "0x"+Integer.toHexString(System.identityHashCode(this));
 152   
         }
 153   
         
 154  0
         public String toString() {
 155  0
             return "Pipe Channel from "+getId()+" to "+sibiling.getId();
 156   
         }        
 157   
     }
 158   
     
 159  16
     public VMPipeAsynchChannelPipe() {
 160  16
         leftChannel.setSibiling(rightChannel);
 161  16
         rightChannel.setSibiling(leftChannel);
 162   
     }
 163   
     
 164  16
     public AsynchChannel getLeftAsynchChannel() {
 165  16
         return leftChannel;
 166   
     }
 167   
 
 168  16
     public AsynchChannel getRightAsynchChannel() {
 169  16
         return rightChannel;
 170   
     }
 171   
 }
 172