Search in sources :

Example 1 with ExternalSeedHTTPDownloaderListener

use of com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderListener in project BiglyBT by BiglySoftware.

the class ExternalSeedReaderImpl method read.

@Override
public byte[] read(int piece_number, int piece_offset, int length, final int timeout) throws ExternalSeedException {
    final byte[] result = new byte[length];
    ExternalSeedHTTPDownloaderListener listener = new ExternalSeedHTTPDownloaderListener() {

        private int bp;

        private long start_time = SystemTime.getCurrentTime();

        @Override
        public byte[] getBuffer() throws ExternalSeedException {
            return (result);
        }

        @Override
        public void setBufferPosition(int position) {
            bp = position;
        }

        @Override
        public int getBufferPosition() {
            return (bp);
        }

        @Override
        public int getBufferLength() {
            return (result.length);
        }

        @Override
        public int getPermittedBytes() throws ExternalSeedException {
            return (result.length);
        }

        @Override
        public int getPermittedTime() {
            if (timeout == 0) {
                return (0);
            }
            int rem = timeout - (int) (SystemTime.getCurrentTime() - start_time);
            if (rem <= 0) {
                return (-1);
            }
            return (rem);
        }

        @Override
        public void reportBytesRead(int num) {
        }

        @Override
        public boolean isCancelled() {
            return false;
        }

        @Override
        public void done() {
        }
    };
    readData(piece_number, piece_offset, length, listener);
    return (result);
}
Also used : ExternalSeedHTTPDownloaderListener(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderListener)

Example 2 with ExternalSeedHTTPDownloaderListener

use of com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderListener in project BiglyBT by BiglySoftware.

the class ExternalSeedReaderGetRight method readData.

@Override
protected void readData(int start_piece_number, int start_piece_offset, int length, final ExternalSeedHTTPDownloaderListener listener) throws ExternalSeedException {
    setupDownloaders();
    setReconnectDelay(RECONNECT_DEFAULT, false);
    long request_start = start_piece_number * (long) piece_size + start_piece_offset;
    int request_length = length;
    if (http_downloaders.length == 1) {
        ExternalSeedHTTPDownloader http_downloader = http_downloaders[0];
        try {
            http_downloader.downloadRange(request_start, request_length, listener, isTransient());
        } catch (ExternalSeedException ese) {
            if (http_downloader.getLastResponse() == 503 && http_downloader.getLast503RetrySecs() >= 0) {
                int retry_secs = http_downloader.getLast503RetrySecs();
                setReconnectDelay(retry_secs * 1000, true);
                throw (new ExternalSeedException("Server temporarily unavailable, retrying in " + retry_secs + " seconds"));
            } else {
                throw (ese);
            }
        }
    } else {
        long request_end = request_start + request_length;
        // System.out.println( "Req: start=" + request_start + ", len=" + request_length );
        final byte[][] overlap_buffer = { null };
        final int[] overlap_buffer_position = { 0 };
        for (int i = 0; i < http_downloaders.length; i++) {
            long this_start = downloader_offsets[i];
            long this_end = this_start + downloader_lengths[i];
            if (this_end <= request_start) {
                continue;
            }
            if (this_start >= request_end) {
                break;
            }
            long sub_request_start = Math.max(request_start, this_start);
            long sub_request_end = Math.min(request_end, this_end);
            final int sub_len = (int) (sub_request_end - sub_request_start);
            if (sub_len == 0) {
                continue;
            }
            ExternalSeedHTTPDownloader http_downloader = http_downloaders[i];
            // System.out.println( "    sub_req: start=" + sub_request_start + ", len=" + sub_len + ",url=" + http_downloader.getURL());
            ExternalSeedHTTPDownloaderListener sub_request = new ExternalSeedHTTPDownloaderListener() {

                private int bytes_read;

                private byte[] current_buffer = overlap_buffer[0];

                private int current_buffer_position = overlap_buffer_position[0];

                private int current_buffer_length = current_buffer == null ? -1 : Math.min(current_buffer.length, current_buffer_position + sub_len);

                @Override
                public byte[] getBuffer() throws ExternalSeedException {
                    if (current_buffer == null) {
                        current_buffer = listener.getBuffer();
                        current_buffer_position = 0;
                        current_buffer_length = Math.min(current_buffer.length, sub_len - bytes_read);
                    }
                    return (current_buffer);
                }

                @Override
                public void setBufferPosition(int position) {
                    current_buffer_position = position;
                    listener.setBufferPosition(position);
                }

                @Override
                public int getBufferPosition() {
                    return (current_buffer_position);
                }

                @Override
                public int getBufferLength() {
                    return (current_buffer_length);
                }

                @Override
                public int getPermittedBytes() throws ExternalSeedException {
                    return (listener.getPermittedBytes());
                }

                @Override
                public int getPermittedTime() {
                    return (listener.getPermittedTime());
                }

                @Override
                public void reportBytesRead(int num) {
                    bytes_read += num;
                    listener.reportBytesRead(num);
                }

                @Override
                public boolean isCancelled() {
                    return (listener.isCancelled());
                }

                @Override
                public void done() {
                    // the current buffer is full up to the declared length
                    int rem = current_buffer.length - current_buffer_length;
                    if (bytes_read == sub_len) {
                        if (rem == 0) {
                            overlap_buffer[0] = null;
                            overlap_buffer_position[0] = 0;
                        } else {
                            overlap_buffer[0] = current_buffer;
                            overlap_buffer_position[0] = current_buffer_length;
                        }
                    }
                    // prepare for next buffer if needed
                    current_buffer = null;
                    if (rem == 0) {
                        listener.done();
                    }
                }
            };
            try {
                http_downloader.downloadRange(sub_request_start - this_start, sub_len, sub_request, isTransient());
            } catch (ExternalSeedException ese) {
                if (http_downloader.getLastResponse() == 503 && http_downloader.getLast503RetrySecs() >= 0) {
                    int retry_secs = http_downloader.getLast503RetrySecs();
                    setReconnectDelay(retry_secs * 1000, true);
                    throw (new ExternalSeedException("Server temporarily unavailable, retrying in " + retry_secs + " seconds"));
                } else {
                    throw (ese);
                }
            }
        }
    }
}
Also used : ExternalSeedHTTPDownloader(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader) ExternalSeedHTTPDownloaderListener(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderListener) ExternalSeedException(com.biglybt.plugin.extseed.ExternalSeedException)

Aggregations

ExternalSeedHTTPDownloaderListener (com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderListener)2 ExternalSeedException (com.biglybt.plugin.extseed.ExternalSeedException)1 ExternalSeedHTTPDownloader (com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader)1