1 |
/* |
/* |
2 |
* $Id$ |
* ActiveModeDTP.java |
3 |
* Copyright (C) 2003 The Free Software Foundation |
* Copyright (C) 2003 The Free Software Foundation |
4 |
* |
* |
5 |
* This file is part of GNU inetlib, a library. |
* This file is part of GNU inetlib, a library. |
52 |
* connection. It converts the socket input into a file stream for reading. |
* connection. It converts the socket input into a file stream for reading. |
53 |
* |
* |
54 |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a> |
|
* @version $Revision$ $Date$ |
|
55 |
*/ |
*/ |
56 |
final class ActiveModeDTP implements DTP, Runnable |
final class ActiveModeDTP |
57 |
|
implements DTP, Runnable |
58 |
{ |
{ |
59 |
|
|
60 |
ServerSocket server; |
ServerSocket server; |
68 |
Thread acceptThread; |
Thread acceptThread; |
69 |
int connectionTimeout; |
int connectionTimeout; |
70 |
|
|
71 |
ActiveModeDTP (InetAddress localhost, int port, |
ActiveModeDTP(InetAddress localhost, int port, |
72 |
int connectionTimeout, int timeout) |
int connectionTimeout, int timeout) |
73 |
throws IOException |
throws IOException |
74 |
{ |
{ |
75 |
completed = false; |
completed = false; |
76 |
inProgress = false; |
inProgress = false; |
77 |
server = new ServerSocket (port, 1, localhost); |
server = new ServerSocket(port, 1, localhost); |
78 |
if (timeout > 0) |
if (timeout > 0) |
79 |
{ |
{ |
80 |
server.setSoTimeout (timeout); |
server.setSoTimeout(timeout); |
81 |
} |
} |
82 |
if (connectionTimeout <= 0) |
if (connectionTimeout <= 0) |
83 |
{ |
{ |
84 |
connectionTimeout = 20000; |
connectionTimeout = 20000; |
85 |
} |
} |
86 |
this.connectionTimeout = connectionTimeout; |
this.connectionTimeout = connectionTimeout; |
87 |
acceptThread = new Thread (this, "ActiveModeDTP"); |
acceptThread = new Thread(this, "ActiveModeDTP"); |
88 |
acceptThread.start (); |
acceptThread.start(); |
89 |
} |
} |
90 |
|
|
91 |
/** |
/** |
92 |
* Start listening. |
* Start listening. |
93 |
*/ |
*/ |
94 |
public void run () |
public void run() |
95 |
{ |
{ |
96 |
try |
try |
97 |
{ |
{ |
98 |
socket = server.accept (); |
socket = server.accept(); |
99 |
//System.err.println("Accepted connection from "+socket.getInetAddress()+":"+socket.getPort()); |
//System.err.println("Accepted connection from "+socket.getInetAddress()+":"+socket.getPort()); |
100 |
} |
} |
101 |
catch (IOException e) |
catch (IOException e) |
102 |
{ |
{ |
103 |
exception = e; |
exception = e; |
104 |
} |
} |
105 |
} |
} |
106 |
|
|
107 |
/** |
/** |
108 |
* Waits until a client has connected. |
* Waits until a client has connected. |
109 |
*/ |
*/ |
110 |
public void waitFor () throws IOException |
public void waitFor() |
111 |
|
throws IOException |
112 |
{ |
{ |
113 |
try |
try |
114 |
{ |
{ |
115 |
acceptThread.join (connectionTimeout); |
acceptThread.join(connectionTimeout); |
116 |
} |
} |
117 |
catch (InterruptedException e) |
catch (InterruptedException e) |
118 |
{ |
{ |
123 |
} |
} |
124 |
if (socket == null) |
if (socket == null) |
125 |
{ |
{ |
126 |
server.close (); |
server.close(); |
127 |
throw new IOException ("client did not connect before timeout"); |
throw new IOException("client did not connect before timeout"); |
128 |
} |
} |
129 |
acceptThread = null; |
acceptThread = null; |
130 |
} |
} |
132 |
/** |
/** |
133 |
* Returns an input stream from which a remote file can be read. |
* Returns an input stream from which a remote file can be read. |
134 |
*/ |
*/ |
135 |
public InputStream getInputStream () throws IOException |
public InputStream getInputStream() |
136 |
|
throws IOException |
137 |
{ |
{ |
138 |
if (inProgress) |
if (inProgress) |
139 |
{ |
{ |
140 |
throw new IOException ("Transfer in progress"); |
throw new IOException("Transfer in progress"); |
141 |
} |
} |
142 |
if (acceptThread != null) |
if (acceptThread != null) |
143 |
{ |
{ |
144 |
waitFor (); |
waitFor(); |
145 |
} |
} |
146 |
switch (transferMode) |
switch (transferMode) |
147 |
{ |
{ |
148 |
case FTPConnection.MODE_STREAM: |
case FTPConnection.MODE_STREAM: |
149 |
in = new StreamInputStream (this, socket.getInputStream ()); |
in = new StreamInputStream(this, socket.getInputStream()); |
150 |
break; |
break; |
151 |
case FTPConnection.MODE_BLOCK: |
case FTPConnection.MODE_BLOCK: |
152 |
in = new BlockInputStream (this, socket.getInputStream ()); |
in = new BlockInputStream(this, socket.getInputStream()); |
153 |
break; |
break; |
154 |
case FTPConnection.MODE_COMPRESSED: |
case FTPConnection.MODE_COMPRESSED: |
155 |
in = new CompressedInputStream (this, socket.getInputStream ()); |
in = new CompressedInputStream(this, socket.getInputStream()); |
156 |
break; |
break; |
157 |
default: |
default: |
158 |
throw new IllegalStateException ("invalid transfer mode"); |
throw new IllegalStateException("invalid transfer mode"); |
159 |
} |
} |
160 |
in.setTransferComplete (false); |
in.setTransferComplete(false); |
161 |
return in; |
return in; |
162 |
} |
} |
163 |
|
|
165 |
* Returns an output stream to which a local file can be written for |
* Returns an output stream to which a local file can be written for |
166 |
* upload. |
* upload. |
167 |
*/ |
*/ |
168 |
public OutputStream getOutputStream () throws IOException |
public OutputStream getOutputStream() throws IOException |
169 |
{ |
{ |
170 |
if (inProgress) |
if (inProgress) |
171 |
{ |
{ |
172 |
throw new IOException ("Transfer in progress"); |
throw new IOException("Transfer in progress"); |
173 |
} |
} |
174 |
if (acceptThread != null) |
if (acceptThread != null) |
175 |
{ |
{ |
176 |
waitFor (); |
waitFor(); |
177 |
} |
} |
178 |
switch (transferMode) |
switch (transferMode) |
179 |
{ |
{ |
180 |
case FTPConnection.MODE_STREAM: |
case FTPConnection.MODE_STREAM: |
181 |
out = new StreamOutputStream (this, socket.getOutputStream ()); |
out = new StreamOutputStream(this, socket.getOutputStream()); |
182 |
break; |
break; |
183 |
case FTPConnection.MODE_BLOCK: |
case FTPConnection.MODE_BLOCK: |
184 |
out = new BlockOutputStream (this, socket.getOutputStream ()); |
out = new BlockOutputStream(this, socket.getOutputStream()); |
185 |
break; |
break; |
186 |
case FTPConnection.MODE_COMPRESSED: |
case FTPConnection.MODE_COMPRESSED: |
187 |
out = new CompressedOutputStream (this, socket.getOutputStream ()); |
out = new CompressedOutputStream(this, socket.getOutputStream()); |
188 |
break; |
break; |
189 |
default: |
default: |
190 |
throw new IllegalStateException ("invalid transfer mode"); |
throw new IllegalStateException("invalid transfer mode"); |
191 |
} |
} |
192 |
out.setTransferComplete (false); |
out.setTransferComplete(false); |
193 |
return out; |
return out; |
194 |
} |
} |
195 |
|
|
196 |
public void setTransferMode (int mode) |
public void setTransferMode(int mode) |
197 |
{ |
{ |
198 |
transferMode = mode; |
transferMode = mode; |
199 |
} |
} |
200 |
|
|
201 |
public void complete () |
public void complete() |
202 |
{ |
{ |
203 |
completed = true; |
completed = true; |
204 |
if (!inProgress) |
if (!inProgress) |
205 |
{ |
{ |
206 |
transferComplete (); |
transferComplete(); |
207 |
} |
} |
208 |
} |
} |
209 |
|
|
210 |
public boolean abort () |
public boolean abort() |
211 |
{ |
{ |
212 |
completed = true; |
completed = true; |
213 |
transferComplete (); |
transferComplete(); |
214 |
return inProgress; |
return inProgress; |
215 |
} |
} |
216 |
|
|
217 |
public void transferComplete () |
public void transferComplete() |
218 |
{ |
{ |
219 |
if (socket == null) |
if (socket == null) |
220 |
{ |
{ |
222 |
} |
} |
223 |
if (in != null) |
if (in != null) |
224 |
{ |
{ |
225 |
in.setTransferComplete (true); |
in.setTransferComplete(true); |
226 |
} |
} |
227 |
if (out != null) |
if (out != null) |
228 |
{ |
{ |
229 |
out.setTransferComplete (true); |
out.setTransferComplete(true); |
230 |
} |
} |
231 |
completed = completed || (transferMode == FTPConnection.MODE_STREAM); |
completed = completed || (transferMode == FTPConnection.MODE_STREAM); |
232 |
if (completed && socket != null) |
if (completed && socket != null) |
233 |
{ |
{ |
234 |
try |
try |
235 |
{ |
{ |
236 |
socket.close (); |
socket.close(); |
237 |
} |
} |
238 |
catch (IOException e) |
catch (IOException e) |
239 |
{ |
{ |
240 |
} |
} |
241 |
try |
try |
242 |
{ |
{ |
243 |
server.close (); |
server.close(); |
244 |
} |
} |
245 |
catch (IOException e) |
catch (IOException e) |
246 |
{ |
{ |
249 |
} |
} |
250 |
|
|
251 |
} |
} |
252 |
|
|