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 static public SynchChannelServer adapt(ChannelServer channel) {
48 return adapt(channel, new LinkedQueue());
49 }
50
51 static public SynchChannelServer adapt(ChannelServer channel, EDU.oswego.cs.dl.util.concurrent.Channel upPacketChannel) {
52
53
54 if( channel instanceof SynchChannelServer ) {
55 return (SynchChannelServer) channel;
56 }
57
58
59 if( channel.getClass() == SynchToAsynchChannelAdapter.class ) {
60 return ((SynchToAsynchChannelServerAdapter)channel).getSynchChannelServer();
61 }
62
63 return new AsynchToSynchChannelServerAdapter((AsynchChannelServer)channel, upPacketChannel);
64 }
65
66 /***
67 * @deprecated {@see #adapt(ChannelServer)}
68 */
69 public AsynchToSynchChannelServerAdapter(AsynchChannelServer asynchChannelServer) {
70 this(asynchChannelServer,new LinkedQueue());
71 }
72
73 /***
74 * @deprecated {@see #adapt(ChannelServer, EDU.oswego.cs.dl.util.concurrent.Channel)}
75 */
76 public AsynchToSynchChannelServerAdapter(AsynchChannelServer asynchChannelServer, EDU.oswego.cs.dl.util.concurrent.Channel acceptBuffer) {
77 this.asynchChannelServer = asynchChannelServer;
78 this.acceptBuffer=acceptBuffer;
79 this.asynchChannelServer.setAcceptListener(this);
80 }
81
82 /***
83 * @see org.activeio.SynchChannelServer#accept(long)
84 */
85 public org.activeio.Channel accept(long timeout) throws IOException {
86 try {
87
88 Object o;
89 if( timeout == NO_WAIT_TIMEOUT ) {
90 o = acceptBuffer.poll(0);
91 } else if( timeout == WAIT_FOREVER_TIMEOUT ) {
92 o = acceptBuffer.take();
93 } else {
94 o = acceptBuffer.poll(timeout);
95 }
96
97 if( o == null )
98 return null;
99
100 if( o instanceof Channel )
101 return (Channel)o;
102
103 Throwable e = (Throwable)o;
104 throw (IOException)new IOException("Asynch error occured: "+e).initCause(e);
105
106 } catch (InterruptedException e) {
107 throw new InterruptedIOException(e.getMessage());
108 }
109 }
110 /***
111 * @see org.activeio.Disposable#dispose()
112 */
113 public void dispose() {
114 asynchChannelServer.dispose();
115 }
116
117 /***
118 * @see org.activeio.Service#start()
119 */
120 public void start() throws IOException {
121 asynchChannelServer.start();
122 }
123
124 /***
125 * @see org.activeio.Service#stop(long)
126 */
127 public void stop(long timeout) throws IOException {
128 asynchChannelServer.stop(timeout);
129 }
130
131 public URI getBindURI() {
132 return asynchChannelServer.getBindURI();
133 }
134
135 public URI getConnectURI() {
136 return asynchChannelServer.getConnectURI();
137 }
138
139 /***
140 * @see org.activeio.AcceptListener#onAccept(org.activeio.Channel)
141 */
142 public void onAccept(org.activeio.Channel channel) {
143 try {
144 acceptBuffer.put(channel);
145 } catch (InterruptedException e) {
146 Thread.currentThread().interrupt();
147 }
148 }
149
150 /***
151 * @see org.activeio.AcceptListener#onAcceptError(java.io.IOException)
152 */
153 public void onAcceptError(IOException error) {
154 try {
155 acceptBuffer.put(error);
156 } catch (InterruptedException e) {
157 Thread.currentThread().interrupt();
158 }
159 }
160
161 public AsynchChannelServer getAsynchChannelServer() {
162 return asynchChannelServer;
163 }
164
165 public Object narrow(Class target) {
166 if( target.isAssignableFrom(getClass()) ) {
167 return this;
168 }
169 return asynchChannelServer.narrow(target);
170 }
171
172 public String toString() {
173 return asynchChannelServer.toString();
174 }
175 }