45 |
final class ActiveModeDTP implements DTP, Runnable |
final class ActiveModeDTP implements DTP, Runnable |
46 |
{ |
{ |
47 |
|
|
48 |
ServerSocket server; |
ServerSocket server; |
49 |
Socket socket; |
Socket socket; |
50 |
DTPInputStream in; |
DTPInputStream in; |
51 |
DTPOutputStream out; |
DTPOutputStream out; |
52 |
boolean completed; |
boolean completed; |
53 |
boolean inProgress; |
boolean inProgress; |
54 |
int transferMode; |
int transferMode; |
55 |
IOException exception; |
IOException exception; |
56 |
Thread acceptThread; |
Thread acceptThread; |
57 |
|
|
58 |
ActiveModeDTP(InetAddress localhost, int port) |
ActiveModeDTP(InetAddress localhost, int port) throws IOException |
59 |
throws IOException |
{ |
60 |
{ |
completed = false; |
61 |
completed = false; |
inProgress = false; |
62 |
inProgress = false; |
server = new ServerSocket(port, 1, localhost); |
63 |
server = new ServerSocket(port, 1, localhost); |
acceptThread = new Thread(this, "ActiveModeDTP"); |
64 |
acceptThread = new Thread(this, "ActiveModeDTP"); |
acceptThread.start(); |
65 |
acceptThread.start(); |
} |
|
} |
|
66 |
|
|
67 |
/** |
/** |
68 |
* Start listening. |
* Start listening. |
69 |
*/ |
*/ |
70 |
public void run() |
public void run() |
71 |
{ |
{ |
72 |
try |
try |
73 |
{ |
{ |
74 |
socket = server.accept(); |
socket = server.accept(); |
75 |
//System.err.println("Accepted connection from "+socket.getInetAddress()+":"+socket.getPort()); |
//System.err.println("Accepted connection from "+socket.getInetAddress()+":"+socket.getPort()); |
76 |
} |
} |
77 |
catch (IOException e) |
catch(IOException e) |
78 |
{ |
{ |
79 |
exception = e; |
exception = e; |
80 |
} |
} |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* Waits until a client has connected. |
* Waits until a client has connected. |
85 |
*/ |
*/ |
86 |
public void waitFor() |
public void waitFor() throws IOException |
87 |
throws IOException |
{ |
88 |
{ |
long timeout = 6000; // TODO review this |
89 |
long timeout = 6000; // TODO review this |
try |
90 |
try |
{ |
91 |
{ |
acceptThread.join(timeout); |
92 |
acceptThread.join(timeout); |
} |
93 |
} |
catch(InterruptedException e) |
94 |
catch (InterruptedException e) |
{ |
95 |
{ |
} |
96 |
} |
if (exception != null) |
97 |
if (exception!=null) |
throw exception; |
98 |
throw exception; |
if (socket == null) |
99 |
if (socket==null) |
{ |
100 |
{ |
server.close(); |
101 |
server.close(); |
throw new IOException("client did not connect before timeout"); |
102 |
throw new IOException("client did not connect before timeout"); |
} |
103 |
} |
acceptThread = null; |
104 |
acceptThread = null; |
} |
105 |
} |
|
106 |
|
/** |
|
/** |
|
107 |
* Returns an input stream from which a remote file can be read. |
* Returns an input stream from which a remote file can be read. |
108 |
*/ |
*/ |
109 |
public InputStream getInputStream() |
public InputStream getInputStream() throws IOException |
110 |
throws IOException |
{ |
111 |
{ |
if (inProgress) |
112 |
if (inProgress) |
throw new IOException("Transfer in progress"); |
113 |
throw new IOException("Transfer in progress"); |
if (acceptThread != null) |
114 |
if (acceptThread!=null) |
waitFor(); |
115 |
waitFor(); |
switch (transferMode) |
116 |
switch (transferMode) |
{ |
117 |
{ |
case FTPConnection.MODE_STREAM: |
118 |
case FTPConnection.MODE_STREAM: |
in = new StreamInputStream(this, socket.getInputStream()); |
119 |
in = new StreamInputStream(this, socket.getInputStream()); |
break; |
120 |
break; |
case FTPConnection.MODE_BLOCK:in = |
121 |
case FTPConnection.MODE_BLOCK: |
new BlockInputStream(this, socket.getInputStream()); |
122 |
in = new BlockInputStream(this, socket.getInputStream()); |
break; |
123 |
break; |
case FTPConnection.MODE_COMPRESSED:in = |
124 |
case FTPConnection.MODE_COMPRESSED: |
new CompressedInputStream(this, socket.getInputStream()); |
125 |
in = new CompressedInputStream(this, socket.getInputStream()); |
break; |
126 |
break; |
default:throw new IllegalStateException("invalid transfer mode"); |
127 |
default: |
} |
128 |
throw new IllegalStateException("invalid transfer mode"); |
in.setTransferComplete(false); |
129 |
} |
return in; |
130 |
in.setTransferComplete(false); |
} |
|
return in; |
|
|
} |
|
131 |
|
|
132 |
/** |
/** |
133 |
* 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 |
134 |
* upload. |
* upload. |
135 |
*/ |
*/ |
136 |
public OutputStream getOutputStream() |
public OutputStream getOutputStream() throws IOException |
137 |
throws IOException |
{ |
138 |
{ |
if (inProgress) |
139 |
if (inProgress) |
throw new IOException("Transfer in progress"); |
140 |
throw new IOException("Transfer in progress"); |
if (acceptThread != null) |
141 |
if (acceptThread!=null) |
waitFor(); |
142 |
waitFor(); |
switch (transferMode) |
143 |
switch (transferMode) |
{ |
144 |
{ |
case FTPConnection.MODE_STREAM: |
145 |
case FTPConnection.MODE_STREAM: |
out = new StreamOutputStream(this, socket.getOutputStream()); |
146 |
out = new StreamOutputStream(this, socket.getOutputStream()); |
break; |
147 |
break; |
case FTPConnection.MODE_BLOCK:out = |
148 |
case FTPConnection.MODE_BLOCK: |
new BlockOutputStream(this, socket.getOutputStream()); |
149 |
out = new BlockOutputStream(this, socket.getOutputStream()); |
break; |
150 |
break; |
case FTPConnection.MODE_COMPRESSED:out = |
151 |
case FTPConnection.MODE_COMPRESSED: |
new CompressedOutputStream(this, socket.getOutputStream()); |
152 |
out = new CompressedOutputStream(this, socket.getOutputStream()); |
break; |
153 |
break; |
default:throw new IllegalStateException("invalid transfer mode"); |
154 |
default: |
} |
155 |
throw new IllegalStateException("invalid transfer mode"); |
out.setTransferComplete(false); |
156 |
} |
return out; |
157 |
out.setTransferComplete(false); |
} |
158 |
return out; |
|
159 |
} |
public void setTransferMode(int mode) |
160 |
|
{ |
161 |
public void setTransferMode(int mode) |
transferMode = mode; |
162 |
{ |
} |
163 |
transferMode = mode; |
|
164 |
} |
public void complete() |
165 |
|
{ |
166 |
public void complete() |
completed = true; |
167 |
{ |
if (!inProgress) |
168 |
completed = true; |
transferComplete(); |
169 |
if (!inProgress) |
} |
170 |
transferComplete(); |
|
171 |
} |
public boolean abort() |
172 |
|
{ |
173 |
public boolean abort() |
completed = true; |
174 |
{ |
transferComplete(); |
175 |
completed = true; |
return inProgress; |
176 |
transferComplete(); |
} |
177 |
return inProgress; |
|
178 |
} |
public void transferComplete() |
179 |
|
{ |
180 |
public void transferComplete() |
if (socket == null) |
181 |
{ |
return; |
182 |
if (socket==null) |
in.setTransferComplete(true); |
183 |
return; |
out.setTransferComplete(true); |
184 |
in.setTransferComplete(true); |
completed = completed || (transferMode == FTPConnection.MODE_STREAM); |
185 |
out.setTransferComplete(true); |
if (completed) |
186 |
completed = completed || (transferMode==FTPConnection.MODE_STREAM); |
{ |
187 |
if (completed) |
try |
188 |
{ |
{ |
189 |
try |
socket.close(); |
190 |
{ |
} |
191 |
socket.close(); |
catch(IOException e) |
192 |
} |
{ |
193 |
catch (IOException e) |
} |
194 |
{ |
try |
195 |
} |
{ |
196 |
try |
server.close(); |
197 |
{ |
} |
198 |
server.close(); |
catch(IOException e) |
199 |
} |
{ |
200 |
catch (IOException e) |
} |
201 |
{ |
} |
202 |
} |
} |
|
} |
|
|
} |
|
203 |
|
|
204 |
} |
} |