/
ModeE.java
371 lines (325 loc) · 13.5 KB
/
ModeE.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package org.dcache.ftp.data;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import org.dcache.pool.repository.RepositoryChannel;
/**
* Implementation of MODE E.
*
* Be aware that it is quite easy to introduce race conditions, so
* please keep this in mind when making changes. In particular the EOD
* and EOD count handling is a little tricky.
*/
public class ModeE extends Mode
{
/**
* Header length of a mode E block.
*/
public static final int HEADER_LENGTH = 17;
public static final int EOR_DESCRIPTOR = 128;
public static final int EOF_DESCRIPTOR = 64;
public static final int SUSPECTED_ERROR_DESCRIPTOR = 32;
public static final int RESTART_MARKER_DESCRIPTOR = 16;
public static final int EOD_DESCRIPTOR = 8;
public static final int SENDER_CLOSES_THIS_STREAM_DESCRIPTOR = 4;
public static final int KNOWN_DESCRIPTORS =
EOF_DESCRIPTOR | EOD_DESCRIPTOR | SENDER_CLOSES_THIS_STREAM_DESCRIPTOR;
/**
* The chunk size used when sending files.
*
* Large blocks will reduce the overhead of sending. However, it
* case of multible concurrent streams, large blocks will make
* disk access less sequential on both the sending and receiving
* side.
*/
private final int _blockSize;
/** Position in file when sending data. Used by the sender. */
private long _currentPosition;
/**
* Number of bytes that have to be transferred. Used by the sender.
*/
private long _currentCount;
/**
* EOD count received. Zero as long as no EOD count was received.
*/
private long _eodc;
/**
* Implementation of send in mode E. There will be an instance per
* data channel. The sender repeatedly bites _blockSize bytes of
* the file and transfers it as a single block. I.e.
* _currentPosition is incremented by _blockSize bytes at a time.
*/
private class Sender extends AbstractMultiplexerListener
{
protected static final int PREPARE_BLOCK = 0;
protected static final int SEND_HEADER = 1;
protected static final int SEND_DATA = 2;
/** Socket used for data channel. */
protected SocketChannel _socket;
/** State of the sender. */
protected int _state;
/** Position in file, which we will send next. */
protected long _position;
/** Bytes remaining from current block. */
protected long _count;
/** True if this sender must send the EOF. */
protected boolean _sendEOF;
/** Buffer for sending the block header. */
protected ByteBuffer _header =
ByteBuffer.allocate(HEADER_LENGTH);
public Sender(SocketChannel socket) {
_socket = socket;
_state = PREPARE_BLOCK;
_sendEOF = (_opened == 1); // First sender sends EOF
}
@Override
public void register(Multiplexer multiplexer) throws IOException {
multiplexer.register(this, SelectionKey.OP_WRITE, _socket);
}
@Override
public void write(Multiplexer multiplexer, SelectionKey key)
throws Exception
{
switch (_state) {
case PREPARE_BLOCK:
/* Prepare new block. We 'bite' up to _blockSize bytes
* of the file and reserve it for this data channel.
*/
_position = _currentPosition;
_count = Math.min(_currentCount, _blockSize);
_currentPosition += _count;
_currentCount -= _count;
/* Prepare header.
*/
_header.clear();
if (_count > 0) {
// Regular block.
_header.put((byte)0);
_header.putLong(_count); // Count
_header.putLong(_position); // Position
} else if (_sendEOF) {
/* This would fail if we are a passive
* sender. Luckily, senders are never passive.
*/
if (!waitForConnectionCompletion(key)) {
return;
}
// Send EOD and EOD count. Since all connections
// have been established by now, we know that
// _opened is the actual number of connections
// that have been established.
_header.put((byte)(EOF_DESCRIPTOR | EOD_DESCRIPTOR | SENDER_CLOSES_THIS_STREAM_DESCRIPTOR));
_header.putLong(0); // Unused
_header.putLong(_opened); // EOD count
} else {
// No more data. Send EOD.
_header.put((byte)(EOD_DESCRIPTOR | SENDER_CLOSES_THIS_STREAM_DESCRIPTOR));
_header.putLong(0); // Count
_header.putLong(0); // Position
}
_header.flip();
_state = SEND_HEADER;
case SEND_HEADER:
/* Send header.
*/
_socket.write(_header);
if (_header.position() < _header.limit()) {
break;
}
/* If at end of stream, close the channel.
*
* Notice that we allow close() to shut down the
* multiplexer if all connections have been closed
* (the third argument is true). This is valid because
* the first sender being created does not close the
* connection until it has sent EOF, and it does not
* sent EOF until all connections have been
* established.
*/
if (_count == 0) {
close(multiplexer, key, true);
break;
}
_state = SEND_DATA;
case SEND_DATA:
/* Send data.
*/
long nbytes = transferTo(_position, _count, _socket);
_monitor.sentBlock(_position, nbytes);
_position += nbytes;
_count -= nbytes;
if (_count == 0) {
_state = PREPARE_BLOCK;
}
break;
}
}
}
/**
* Implementation of receive in mode E. There will be an instance
* per data channel.
*/
class Receiver extends AbstractMultiplexerListener
{
/** Socket used for data channel. */
protected SocketChannel _socket;
/** Number of bytes left of current block. */
protected long _count;
/** The file position at which we will receive data next. */
protected long _position;
/** Header flags from the current block. */
protected int _flags;
/** True if any data has flown over this data channel. */
protected boolean _used;
/** Buffer for receiving the block header. */
protected ByteBuffer _header =
ByteBuffer.allocate(HEADER_LENGTH);
public Receiver(SocketChannel socket) {
_socket = socket;
_count = 0;
_position = 0;
_flags = 0;
_used = false;
}
@Override
public void register(Multiplexer multiplexer) throws IOException {
multiplexer.register(this, SelectionKey.OP_READ, _socket);
}
@Override
public void read(Multiplexer multiplexer, SelectionKey key)
throws Exception
{
/* _count is zero when we have received all of the
* previous block. We expect to read the header of the
* next block.
*/
if (_count == 0) {
long nbytes = _socket.read(_header);
if (nbytes == -1) {
/* Stream was closed. The GridFTP 1 spec states
* that the sender must send EOD when no more data
* is to be send on a channel. However, the Globus
* GridFTP client library seems to consider it
* acceptable to close a data channel without
* sending EOD as long as it has not transferred
* any data on the channel. (REVISIT: Is this
* actually the case?)
*/
if (_used) {
throw new FTPException("Stream ended before EOD");
}
close(multiplexer, key, _opened == _eodc);
return;
}
_used = true;
if (_header.position() < _header.limit()) {
/* Incomplete header.
*/
return;
}
_header.rewind();
_flags = _header.get();
_count = _header.getLong();
_position = _header.getLong();
_header.clear();
/* The GridFTP spec states that we should generate an
* error whenever we receive a descriptor we don't
* know how to handle.
*/
if ((_flags & ~KNOWN_DESCRIPTORS) != 0) {
throw new FTPException("Received block with unknown descriptor (" + _flags + ")");
}
/* Exactly one EOF must be received on one of the data
* channels. It contains the number of EOD markers
* that must be received. Such blocks have a different
* interpretation of the two fields following the
* descriptor. Therefore these blocks cannot contain data.
*
* The GridFTP spec is not clear about EOF being
* received after EOD. As far as I can see, there
* would be a race condition if EOF after EOD were
* allowed when caching data channels: We have two
* channels A and B. We send EOD on both, and then
* EOF(2) on A. Now sender considers the transfer to
* be completed and initiates a new transfer on cached
* data channels A and B, however the file is so small
* that only an EOF is sent on B. It may now happen
* that the receiver sees an EOF on both channels and
* is not able to distinguish them. I therefore
* consider EOF after EOD to be disallowed. REMARK: We
* do not support caching of data channels.
*
* The GridFTP spec is also not clear about data being
* send after EOF, however we handle that case.
*/
if ((_flags & EOF_DESCRIPTOR) != 0) {
if (_eodc != 0) {
throw new FTPException("Multible EODC received");
}
if (_position <= 0) {
throw new FTPException("Non-positive EODC received");
}
_eodc = (int)_position;
_count = _position = 0; // No data
}
/* transferFrom does not like empty reads. Therefore
* we exit early in case of empty blocks.
*/
if (_count == 0) {
/* If EOD was received, then close channel.
*/
if ((_flags & EOD_DESCRIPTOR) != 0) {
close(multiplexer, key, _opened == _eodc);
}
return;
}
_monitor.preallocate(_position + _count);
}
/* Receive data.
*/
long nbytes = transferFrom(_socket, _position, _count);
if (nbytes == -1) {
throw new FTPException("Stream was closed in the middle of a block");
}
_monitor.receivedBlock(_position, nbytes);
_position += nbytes;
_count -= nbytes;
/* If EOD was received, then close channel.
*/
if (_count == 0 && (_flags & EOD_DESCRIPTOR) != 0) {
close(multiplexer, key, _opened == _eodc);
}
}
}
public ModeE(Role role, RepositoryChannel file, ConnectionMonitor monitor,
int blockSize)
throws IOException
{
super(role, file, monitor);
_currentPosition = getStartPosition();
_currentCount = getSize();
_eodc = 0;
_blockSize = blockSize;
}
@Override
public void setPartialRetrieveParameters(long position, long size)
{
super.setPartialRetrieveParameters(position, size);
_currentPosition = getStartPosition();
_currentCount = getSize();
}
@Override
public void newConnection(Multiplexer multiplexer, SocketChannel socket)
throws Exception
{
switch (_role) {
case Sender:
multiplexer.add(new Sender(socket));
break;
case Receiver:
multiplexer.add(new Receiver(socket));
break;
}
}
}