001: /*
002: * $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0-beta1/module-nio/src/main/java/org/apache/http/impl/nio/reactor/SessionRequestImpl.java $
003: * $Revision: 613298 $
004: * $Date: 2008-01-18 23:09:22 +0100 (Fri, 18 Jan 2008) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.impl.nio.reactor;
033:
034: import java.io.IOException;
035: import java.net.SocketAddress;
036: import java.nio.channels.SelectionKey;
037:
038: import org.apache.http.nio.reactor.IOSession;
039: import org.apache.http.nio.reactor.SessionRequest;
040: import org.apache.http.nio.reactor.SessionRequestCallback;
041:
042: public class SessionRequestImpl implements SessionRequest {
043:
044: private volatile boolean completed;
045: private volatile SelectionKey key;
046:
047: private final SocketAddress remoteAddress;
048: private final SocketAddress localAddress;
049: private final Object attachment;
050: private final SessionRequestCallback callback;
051:
052: private int connectTimeout;
053: private IOSession session = null;
054: private IOException exception = null;
055:
056: public SessionRequestImpl(final SocketAddress remoteAddress,
057: final SocketAddress localAddress, final Object attachment,
058: final SessionRequestCallback callback) {
059: super ();
060: if (remoteAddress == null) {
061: throw new IllegalArgumentException(
062: "Remote address may not be null");
063: }
064: this .remoteAddress = remoteAddress;
065: this .localAddress = localAddress;
066: this .attachment = attachment;
067: this .callback = callback;
068: this .connectTimeout = 0;
069: }
070:
071: public SocketAddress getRemoteAddress() {
072: return this .remoteAddress;
073: }
074:
075: public SocketAddress getLocalAddress() {
076: return this .localAddress;
077: }
078:
079: public Object getAttachment() {
080: return this .attachment;
081: }
082:
083: public boolean isCompleted() {
084: return this .completed;
085: }
086:
087: public void waitFor() throws InterruptedException {
088: if (this .completed) {
089: return;
090: }
091: synchronized (this ) {
092: while (!this .completed) {
093: wait();
094: }
095: }
096: }
097:
098: public IOSession getSession() {
099: synchronized (this ) {
100: return this .session;
101: }
102: }
103:
104: public IOException getException() {
105: synchronized (this ) {
106: return this .exception;
107: }
108: }
109:
110: public void completed(final IOSession session) {
111: if (session == null) {
112: throw new IllegalArgumentException(
113: "Session may not be null");
114: }
115: if (this .completed) {
116: return;
117: }
118: this .completed = true;
119: synchronized (this ) {
120: this .session = session;
121: if (this .callback != null) {
122: this .callback.completed(this );
123: }
124: notifyAll();
125: }
126: }
127:
128: public void failed(final IOException exception) {
129: if (exception == null) {
130: return;
131: }
132: if (this .completed) {
133: return;
134: }
135: this .completed = true;
136: synchronized (this ) {
137: this .exception = exception;
138: if (this .callback != null) {
139: this .callback.failed(this );
140: }
141: notifyAll();
142: }
143: }
144:
145: public void timeout() {
146: if (this .completed) {
147: return;
148: }
149: this .completed = true;
150: if (this .key != null) {
151: this .key.cancel();
152: }
153: synchronized (this ) {
154: if (this .callback != null) {
155: this .callback.timeout(this );
156: }
157: }
158: }
159:
160: public int getConnectTimeout() {
161: return this .connectTimeout;
162: }
163:
164: public void setConnectTimeout(int timeout) {
165: if (this .connectTimeout != timeout) {
166: this .connectTimeout = timeout;
167: if (this .key != null) {
168: this .key.selector().wakeup();
169: }
170: }
171: }
172:
173: protected void setKey(final SelectionKey key) {
174: this .key = key;
175: }
176:
177: public void cancel() {
178: if (this .completed) {
179: return;
180: }
181: this .completed = true;
182: if (this.key != null) {
183: this.key.cancel();
184: }
185: synchronized (this) {
186: if (this.callback != null) {
187: this.callback.cancelled(this);
188: }
189: notifyAll();
190: }
191: }
192:
193: }
|