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: | 175 |  | Methods: | 15
| NCLOC: | 94 |  | 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
AsynchToSynchChannelServerAdapter.java | 64.3% | 65% | 66.7% | 65.2% |

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.adapter;
19 |  |
20 |  |
     import java.io.IOException;
21 |  |
     import java.io.InterruptedIOException;
22 |  |
     import java.net.URI;
23 |  |
24 |  |
     import org.activeio.AcceptListener;
25 |  |
     import org.activeio.AsynchChannelServer;
26 |  |
     import org.activeio.Channel;
27 |  |
     import org.activeio.ChannelServer;
28 |  |
     import org.activeio.SynchChannelServer;
29 |  |
30 |  |
     import EDU.oswego.cs.dl.util.concurrent.LinkedQueue;
31 |  |
32 |  |
     /**
33 |  |
      * Adapts a {@see org.activeio.AsynchChannelServer} so that it provides an
34 |  |
      * {@see org.activeio.SynchChannelServer} interface.
35 |  |
      *
36 |  |
      * This object buffers asynchronous accepts from the {@see org.activeio.AsynchChannelServer}
37 |  |
      * abs buffers them in a {@see EDU.oswego.cs.dl.util.concurrent.Channel} util the client accepts the
38 |  |
      * connection.
39 |  |
      *
40 |  |
      * @version $Revision$
41 |  |
      */
42 |  |
     final public class AsynchToSynchChannelServerAdapter implements SynchChannelServer, AcceptListener {
43 |  |
44 |  |
         private final AsynchChannelServer asynchChannelServer;
45 |  |
         private final EDU.oswego.cs.dl.util.concurrent.Channel acceptBuffer;
46 |  |
47 | 62 |
         static public SynchChannelServer adapt(ChannelServer channel) {
48 | 62 |
             return adapt(channel, new LinkedQueue());
49 |  |
         }
50 |  |
51 | 62 |
         static public SynchChannelServer adapt(ChannelServer channel, EDU.oswego.cs.dl.util.concurrent.Channel upPacketChannel) {
52 |  |
53 |  |
             // It might not need adapting
54 | 62 |
             if( channel instanceof SynchChannelServer ) {
55 | 30 |
                 return (SynchChannelServer) channel;
56 |  |
             }
57 |  |
58 |  |
             // Can we just just undo the adaptor
59 | 32 |
             if( channel.getClass() == SynchToAsynchChannelAdapter.class ) {
60 | 0 |
                 return ((SynchToAsynchChannelServerAdapter)channel).getSynchChannelServer();
61 |  |
             }
62 |  |
63 | 32 |
             return new AsynchToSynchChannelServerAdapter((AsynchChannelServer)channel, upPacketChannel);
64 |  |
         }
65 |  |
66 |  |
         /**
67 |  |
          * @deprecated {@see #adapt(ChannelServer)}
68 |  |
          */
69 | 0 |
         public AsynchToSynchChannelServerAdapter(AsynchChannelServer asynchChannelServer) {
70 | 0 |
             this(asynchChannelServer,new LinkedQueue());
71 |  |
         }
72 |  |
73 |  |
         /**
74 |  |
          * @deprecated {@see #adapt(ChannelServer, EDU.oswego.cs.dl.util.concurrent.Channel)}
75 |  |
          */
76 | 32 |
         public AsynchToSynchChannelServerAdapter(AsynchChannelServer asynchChannelServer, EDU.oswego.cs.dl.util.concurrent.Channel acceptBuffer) {
77 | 32 |
             this.asynchChannelServer = asynchChannelServer;
78 | 32 |
             this.acceptBuffer=acceptBuffer;
79 | 32 |
             this.asynchChannelServer.setAcceptListener(this);
80 |  |
         }
81 |  |
82 |  |
         /**
83 |  |
          * @see org.activeio.SynchChannelServer#accept(long)
84 |  |
          */
85 | 36 |
         public org.activeio.Channel accept(long timeout) throws IOException {
86 | 36 |
             try {
87 |  |
88 | 36 |
                 Object o;
89 | 36 |
                 if( timeout == NO_WAIT_TIMEOUT ) {
90 | 0 |
                     o = acceptBuffer.poll(0);
91 | 36 |
                 } else if( timeout == WAIT_FOREVER_TIMEOUT ) {
92 | 8 |
                     o = acceptBuffer.take();
93 |  |
                 } else {
94 | 28 |
                     o = acceptBuffer.poll(timeout);
95 |  |
                 }
96 |  |
97 | 24 |
                 if( o == null )
98 | 0 |
                     return null;
99 |  |
100 | 24 |
                 if( o instanceof Channel )
101 | 24 |
                     return (Channel)o;
102 |  |
103 | 0 |
                 Throwable e = (Throwable)o;
104 | 0 |
                 throw (IOException)new IOException("Asynch error occured: "+e).initCause(e);
105 |  |
106 |  |
             } catch (InterruptedException e) {
107 | 12 |
                 throw new InterruptedIOException(e.getMessage());
108 |  |
             }
109 |  |
         }
110 |  |
         /**
111 |  |
          * @see org.activeio.Disposable#dispose()
112 |  |
          */
113 | 32 |
         public void dispose() {
114 | 32 |
             asynchChannelServer.dispose();
115 |  |
         }
116 |  |
117 |  |
         /**
118 |  |
          * @see org.activeio.Service#start()
119 |  |
          */
120 | 32 |
         public void start() throws IOException {
121 | 32 |
             asynchChannelServer.start();
122 |  |
         }
123 |  |
124 |  |
         /**
125 |  |
          * @see org.activeio.Service#stop(long)
126 |  |
          */
127 | 0 |
         public void stop(long timeout) throws IOException {
128 | 0 |
             asynchChannelServer.stop(timeout);
129 |  |
         }
130 |  |
131 | 18 |
         public URI getBindURI() {
132 | 18 |
             return asynchChannelServer.getBindURI();
133 |  |
         }
134 |  |
135 | 50 |
         public URI getConnectURI() {
136 | 50 |
             return asynchChannelServer.getConnectURI();
137 |  |
         }
138 |  |
139 |  |
         /**
140 |  |
          * @see org.activeio.AcceptListener#onAccept(org.activeio.Channel)
141 |  |
          */
142 | 24 |
         public void onAccept(org.activeio.Channel channel) {
143 | 24 |
             try {
144 | 24 |
                 acceptBuffer.put(channel);
145 |  |
             } catch (InterruptedException e) {
146 | 0 |
                 Thread.currentThread().interrupt();
147 |  |
             }
148 |  |
         }
149 |  |
150 |  |
         /**
151 |  |
          * @see org.activeio.AcceptListener#onAcceptError(java.io.IOException)
152 |  |
          */
153 | 0 |
         public void onAcceptError(IOException error) {
154 | 0 |
             try {
155 | 0 |
                 acceptBuffer.put(error);
156 |  |
             } catch (InterruptedException e) {
157 | 0 |
                 Thread.currentThread().interrupt();
158 |  |
             }
159 |  |
         }
160 |  |
161 | 0 |
         public AsynchChannelServer getAsynchChannelServer() {
162 | 0 |
             return asynchChannelServer;
163 |  |
         }
164 |  |
165 | 2 |
         public Object narrow(Class target) {
166 | 2 |
             if( target.isAssignableFrom(getClass()) ) {
167 | 0 |
                 return this;
168 |  |
             }
169 | 2 |
             return asynchChannelServer.narrow(target);
170 |  |
         }
171 |  |
172 | 0 |
         public String toString() {
173 | 0 |
             return asynchChannelServer.toString();
174 |  |
         }
175 |  |
     }