View Javadoc

1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.nio.reactor;
29  
30  import java.io.IOException;
31  import java.net.SocketAddress;
32  import java.nio.channels.Channel;
33  import java.nio.channels.SelectionKey;
34  
35  import org.apache.http.annotation.ThreadSafe;
36  import org.apache.http.nio.reactor.IOSession;
37  import org.apache.http.nio.reactor.SessionRequest;
38  import org.apache.http.nio.reactor.SessionRequestCallback;
39  import org.apache.http.util.Args;
40  
41  /**
42   * Default implementation of {@link SessionRequest}.
43   *
44   * @since 4.0
45   */
46  @ThreadSafe
47  public class SessionRequestImpl implements SessionRequest {
48  
49      private volatile boolean completed;
50      private volatile SelectionKey key;
51  
52      private final SocketAddress remoteAddress;
53      private final SocketAddress localAddress;
54      private final Object attachment;
55      private final SessionRequestCallback callback;
56  
57      private volatile int connectTimeout;
58      private volatile IOSession session = null;
59      private volatile IOException exception = null;
60  
61      public SessionRequestImpl(
62              final SocketAddress remoteAddress,
63              final SocketAddress localAddress,
64              final Object attachment,
65              final SessionRequestCallback callback) {
66          super();
67          Args.notNull(remoteAddress, "Remote address");
68          this.remoteAddress = remoteAddress;
69          this.localAddress = localAddress;
70          this.attachment = attachment;
71          this.callback = callback;
72          this.connectTimeout = 0;
73      }
74  
75      @Override
76      public SocketAddress getRemoteAddress() {
77          return this.remoteAddress;
78      }
79  
80      @Override
81      public SocketAddress getLocalAddress() {
82          return this.localAddress;
83      }
84  
85      @Override
86      public Object getAttachment() {
87          return this.attachment;
88      }
89  
90      @Override
91      public boolean isCompleted() {
92          return this.completed;
93      }
94  
95      protected void setKey(final SelectionKey key) {
96          this.key = key;
97      }
98  
99      @Override
100     public void waitFor() throws InterruptedException {
101         if (this.completed) {
102             return;
103         }
104         synchronized (this) {
105             while (!this.completed) {
106                 wait();
107             }
108         }
109     }
110 
111     @Override
112     public IOSession getSession() {
113         synchronized (this) {
114             return this.session;
115         }
116     }
117 
118     @Override
119     public IOException getException() {
120         synchronized (this) {
121             return this.exception;
122         }
123     }
124 
125     public void completed(final IOSession session) {
126         Args.notNull(session, "Session");
127         if (this.completed) {
128             return;
129         }
130         this.completed = true;
131         synchronized (this) {
132             this.session = session;
133             if (this.callback != null) {
134                 this.callback.completed(this);
135             }
136             notifyAll();
137         }
138     }
139 
140     public void failed(final IOException exception) {
141         if (exception == null) {
142             return;
143         }
144         if (this.completed) {
145             return;
146         }
147         this.completed = true;
148         final SelectionKey key = this.key;
149         if (key != null) {
150             key.cancel();
151             final Channel channel = key.channel();
152             try {
153                 channel.close();
154             } catch (final IOException ignore) {}
155         }
156         synchronized (this) {
157             this.exception = exception;
158             if (this.callback != null) {
159                 this.callback.failed(this);
160             }
161             notifyAll();
162         }
163     }
164 
165     public void timeout() {
166         if (this.completed) {
167             return;
168         }
169         this.completed = true;
170         final SelectionKey key = this.key;
171         if (key != null) {
172             key.cancel();
173             final Channel channel = key.channel();
174             if (channel.isOpen()) {
175                 try {
176                     channel.close();
177                 } catch (final IOException ignore) {}
178             }
179         }
180         synchronized (this) {
181             if (this.callback != null) {
182                 this.callback.timeout(this);
183             }
184         }
185     }
186 
187     @Override
188     public int getConnectTimeout() {
189         return this.connectTimeout;
190     }
191 
192     @Override
193     public void setConnectTimeout(final int timeout) {
194         if (this.connectTimeout != timeout) {
195             this.connectTimeout = timeout;
196             final SelectionKey key = this.key;
197             if (key != null) {
198                 key.selector().wakeup();
199             }
200         }
201     }
202 
203     @Override
204     public void cancel() {
205         if (this.completed) {
206             return;
207         }
208         this.completed = true;
209         final SelectionKey key = this.key;
210         if (key != null) {
211             key.cancel();
212             final Channel channel = key.channel();
213             if (channel.isOpen()) {
214                 try {
215                     channel.close();
216                 } catch (final IOException ignore) {}
217             }
218         }
219         synchronized (this) {
220             if (this.callback != null) {
221                 this.callback.cancelled(this);
222             }
223             notifyAll();
224         }
225     }
226 
227 }