Search in sources :

Example 1 with ExternalSeedHTTPDownloader

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

the class ExternalSeedReaderGetRight method setActiveSupport.

@Override
protected void setActiveSupport(PeerManager peer_manager, boolean active) {
    if (linear_download) {
        if (peer_manager != null) {
            PiecePicker picker = PluginCoreUtils.unwrap(peer_manager).getPiecePicker();
            if (active) {
                piece_priorities = new long[peer_manager.getPieces().length];
                for (int i = 0; i < piece_priorities.length; i++) {
                    piece_priorities[i] = 10 * 1000 + i;
                }
                picker.addPriorityProvider(this);
            } else {
                piece_priorities = null;
                picker.removePriorityProvider(this);
            }
        }
        if (!active) {
            boolean downloaders_set;
            synchronized (this) {
                downloaders_set = http_downloaders != null;
            }
            if (downloaders_set) {
                for (ExternalSeedHTTPDownloader d : http_downloaders) {
                    d.deactivate();
                }
            }
        }
    }
}
Also used : ExternalSeedHTTPDownloader(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker)

Example 2 with ExternalSeedHTTPDownloader

use of com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader 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)

Example 3 with ExternalSeedHTTPDownloader

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

the class ExternalSeedReaderWebSeed method readData.

@Override
protected void readData(int piece_number, int piece_offset, int length, ExternalSeedHTTPDownloaderListener listener) throws ExternalSeedException {
    long piece_end = piece_offset + length - 1;
    String str = url_prefix + "&piece=" + piece_number + "&ranges=" + piece_offset + "-" + piece_end;
    setReconnectDelay(RECONNECT_DEFAULT, false);
    ExternalSeedHTTPDownloader http_downloader = null;
    try {
        http_downloader = new ExternalSeedHTTPDownloaderRange(new URL(str), getUserAgent());
        if (supports_503) {
            http_downloader.downloadSocket(length, listener, isTransient());
        } else {
            http_downloader.download(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);
        }
    } catch (MalformedURLException e) {
        throw (new ExternalSeedException("URL encode fails", e));
    }
}
Also used : ExternalSeedHTTPDownloaderRange(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderRange) ExternalSeedHTTPDownloader(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) ExternalSeedException(com.biglybt.plugin.extseed.ExternalSeedException)

Aggregations

ExternalSeedHTTPDownloader (com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader)3 ExternalSeedException (com.biglybt.plugin.extseed.ExternalSeedException)2 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)1 ExternalSeedHTTPDownloaderListener (com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderListener)1 ExternalSeedHTTPDownloaderRange (com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloaderRange)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1