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: | 130 |  | Methods: | 11
| NCLOC: | 83 |  | 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
SynchornizedSynchChannel.java | 0% | 0% | 0% | 0% |

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 |  |
     package org.activeio.filter;
18 |  |
19 |  |
     import java.io.IOException;
20 |  |
     import java.io.InterruptedIOException;
21 |  |
22 |  |
     import org.activeio.FilterSynchChannel;
23 |  |
     import org.activeio.Packet;
24 |  |
     import org.activeio.SynchChannel;
25 |  |
     import org.activeio.SynchChannelServer;
26 |  |
27 |  |
     import EDU.oswego.cs.dl.util.concurrent.Mutex;
28 |  |
     import EDU.oswego.cs.dl.util.concurrent.Sync;
29 |  |
30 |  |
     /**
31 |  |
      * Used to synchronize concurrent access to a SynchChannel.
32 |  |
      *
33 |  |
      * Uses two different {@see EDU.oswego.cs.dl.util.concurrent.Sync} objects
34 |  |
      * for write and read operations.  All other operations such as {@see #stop(long)}
35 |  |
      * and {@see #stop} just do a normal java synchronization against the SynchornizedSynchChannel
36 |  |
      * object instance.
37 |  |
      *
38 |  |
      */
39 |  |
     public class SynchornizedSynchChannel extends FilterSynchChannel {
40 |  |
41 |  |
         private final Sync readLock;
42 |  |
         private final Sync writeLock;
43 |  |
44 | 0 |
         public SynchornizedSynchChannel(SynchChannel next) {
45 | 0 |
             this(next, new Mutex(), new Mutex());
46 |  |
         }
47 |  |
48 | 0 |
         public SynchornizedSynchChannel(SynchChannel next, Sync readLock, Sync writeLock) {
49 | 0 |
             super(next);
50 | 0 |
             this.readLock = readLock;
51 | 0 |
             this.writeLock = writeLock;
52 |  |
         }
53 |  |
54 | 0 |
         public Packet read(long timeout) throws IOException {
55 | 0 |
             try {
56 |  |
57 | 0 |
                 if( timeout==SynchChannelServer.WAIT_FOREVER_TIMEOUT ) {
58 | 0 |
                     readLock.acquire();
59 |  |
                 } else {
60 | 0 |
                     long start = System.currentTimeMillis();
61 | 0 |
                     if( !readLock.attempt(0) ) {
62 | 0 |
                         return null;
63 |  |
                     }
64 |  |
                     // Adjust the resulting timeout down to account for time taken to
65 |  |
                     // get the readLock.
66 | 0 |
                     timeout = Math.max(0, timeout-(System.currentTimeMillis()-start));
67 |  |
                 }
68 |  |
69 |  |
             } catch (InterruptedException e) {
70 | 0 |
                 throw new InterruptedIOException(e.getMessage());
71 |  |
             }
72 |  |
73 | 0 |
             try {
74 | 0 |
                 return getNext().read(timeout);
75 |  |
             } finally {
76 | 0 |
                 readLock.release();
77 |  |
             }
78 |  |
         }
79 |  |
80 | 0 |
         public void write(Packet packet) throws IOException {
81 | 0 |
             try {
82 | 0 |
                 writeLock.acquire();
83 |  |
             } catch (InterruptedException e) {
84 | 0 |
                 throw new InterruptedIOException(e.getMessage());
85 |  |
             }
86 | 0 |
             try {
87 | 0 |
                 getNext().write(packet);
88 |  |
             } finally {
89 | 0 |
                 writeLock.release();
90 |  |
             }
91 |  |
         }
92 |  |
93 | 0 |
         public void flush() throws IOException {
94 | 0 |
             try {
95 | 0 |
                 writeLock.acquire();
96 |  |
             } catch (InterruptedException e) {
97 | 0 |
                 throw new InterruptedIOException(e.getMessage());
98 |  |
             }
99 | 0 |
             try {
100 | 0 |
                 getNext().flush();
101 |  |
             } finally {
102 | 0 |
                 writeLock.release();
103 |  |
             }
104 |  |
         }
105 |  |
106 | 0 |
         synchronized public void dispose() {
107 | 0 |
             super.dispose();
108 |  |
         }
109 |  |
110 | 0 |
         synchronized public Object narrow(Class target) {
111 | 0 |
             return super.narrow(target);
112 |  |
         }
113 |  |
114 | 0 |
         synchronized public void start() throws IOException {
115 | 0 |
             super.start();
116 |  |
         }
117 |  |
118 | 0 |
         synchronized public void stop(long timeout) throws IOException {
119 | 0 |
             super.stop(timeout);
120 |  |
         }
121 |  |
122 | 0 |
         public Sync getReadLock() {
123 | 0 |
             return readLock;
124 |  |
         }
125 |  |
126 | 0 |
         public Sync getWriteLock() {
127 | 0 |
             return writeLock;
128 |  |
         }
129 |  |
     }
130 |  |