Skip to content

Commit

Permalink
7.87
Browse files Browse the repository at this point in the history
Improve force reset chunk
  • Loading branch information
tonikelope committed Oct 17, 2023
1 parent 970db10 commit 393d3e9
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 36 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.tonikelope</groupId>
<artifactId>MegaBasterd</artifactId>
<version>7.86</version>
<version>7.87</version>
<packaging>jar</packaging>
<repositories>
<repository>
Expand Down
66 changes: 36 additions & 30 deletions src/main/java/com/tonikelope/megabasterd/ChunkDownloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,24 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
private boolean _notified;
private final ArrayList<String> _excluded_proxy_list;
private volatile boolean _reset_current_chunk;
private volatile InputStream _chunk_inputstream = null;

private String _current_smart_proxy;

public void setReset_current_chunk(boolean _reset_current_chunk) {
this._reset_current_chunk = _reset_current_chunk;
public void RESET_CURRENT_CHUNK() {

if (_chunk_inputstream != null) {

this._reset_current_chunk = true;

try {
_chunk_inputstream.close();
} catch (IOException ex) {
Logger.getLogger(ChunkDownloader.class.getName()).log(Level.SEVERE, null, ex);
}

_chunk_inputstream = null;
}
}

public ChunkDownloader(int id, Download download) {
Expand Down Expand Up @@ -299,7 +312,9 @@ public void run() {

tmp_chunk_file = new File(_download.getChunkmanager().getChunks_dir() + "/" + new File(_download.getFile_name()).getName() + ".chunk" + chunk_id + ".tmp");

try (InputStream is = new ThrottledInputStream(con.getInputStream(), _download.getMain_panel().getStream_supervisor()); OutputStream tmp_chunk_file_os = new BufferedOutputStream(new FileOutputStream(tmp_chunk_file))) {
_chunk_inputstream = new ThrottledInputStream(con.getInputStream(), _download.getMain_panel().getStream_supervisor());

try (OutputStream tmp_chunk_file_os = new BufferedOutputStream(new FileOutputStream(tmp_chunk_file))) {

init_chunk_time = System.currentTimeMillis();

Expand All @@ -309,7 +324,7 @@ public void run() {

if (!_exit && !_download.isStopped() && !_download.getChunkmanager().isExit()) {

while (!_reset_current_chunk && !_exit && !_download.isStopped() && !_download.getChunkmanager().isExit() && chunk_reads < chunk_size && (reads = is.read(buffer, 0, Math.min((int) (chunk_size - chunk_reads), buffer.length))) != -1) {
while (!_reset_current_chunk && !_exit && !_download.isStopped() && !_download.getChunkmanager().isExit() && chunk_reads < chunk_size && (reads = _chunk_inputstream.read(buffer, 0, Math.min((int) (chunk_size - chunk_reads), buffer.length))) != -1) {
tmp_chunk_file_os.write(buffer, 0, reads);

chunk_reads += reads;
Expand All @@ -331,12 +346,6 @@ public void run() {
}
}

if (this._reset_current_chunk) {
LOG.log(Level.WARNING, "{0} Worker [{1}] FORCE RESET CHUNK [{2}]! {3}", new Object[]{Thread.currentThread().getName(), _id, chunk_id, _download.getFile_name()});
_current_smart_proxy = null;
this._reset_current_chunk = false;
}

}

finish_chunk_time = System.currentTimeMillis();
Expand Down Expand Up @@ -376,25 +385,6 @@ public void run() {

http_error = 0;

/*
//Proxy speed benchmark (TO DO: change of strategy. Perform a MEGA speed test without proxies and compare the download speed of the chunks with SmartProxy with respect to this speed (with an adjustment coefficient) in order to know if the proxy is slow.).
if (_current_smart_proxy != null && finish_chunk_time != -1) {
long chunk_speed = Math.round(chunk_size / (((double) (finish_chunk_time - init_chunk_time - paused)) / 1000));
long expected_chunk_speed = ( MEGA_DOWNLOAD_SPEED / CURRENT_MEGABASTERD_CHUNK_DOWNLOADERS_COUNT ) * SLOW_PROXY_PERC;
if (chunk_speed < expected_chunk_speed) {
LOG.log(Level.INFO, "{0} Worker [{1}] WARNING -> PROXY {2} CHUNK DOWNLOAD SPEED: {3}/s SEEMS TO BE SLOW (expected is {4}/s) {4}", new Object[]{Thread.currentThread().getName(), _id, _current_smart_proxy, formatBytes(chunk_speed), formatBytes(expected_chunk_speed), _download.getFile_name()});
slow_proxy = true;
}
}
*/
_excluded_proxy_list.clear();

_download.getChunkmanager().secureNotify();
Expand All @@ -413,6 +403,22 @@ public void run() {

} finally {

if (_chunk_inputstream != null) {

try {
_chunk_inputstream.close();
} catch (IOException ex) {
Logger.getLogger(ChunkDownloader.class.getName()).log(Level.SEVERE, null, ex);
}
_chunk_inputstream = null;
}

if (_reset_current_chunk) {
LOG.log(Level.WARNING, "Worker [{0}] FORCE RESET CHUNK [{1}]! {2}", new Object[]{_id, chunk_id, _download.getFile_name()});
_current_smart_proxy = null;
_reset_current_chunk = false;
}

if (chunk_error) {

LOG.log(Level.INFO, "{0} Worker [{1}] has FAILED downloading chunk [{2}]! {3}", new Object[]{Thread.currentThread().getName(), _id, chunk_id, _download.getFile_name()});
Expand Down Expand Up @@ -447,7 +453,7 @@ public void run() {
setExit(true);
}

} else if (!FORCE_SMART_PROXY) {
} else {
_current_smart_proxy = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public synchronized void forceResetAllChunks() {
ArrayList<ChunkDownloader> chunkworkers = ((Download) transference).getChunkworkers();

chunkworkers.forEach((worker) -> {
worker.setReset_current_chunk(true);
worker.RESET_CURRENT_CHUNK();
});

});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ private void add_files_buttonActionPerformed(java.awt.event.ActionEvent evt) {//

_base_path = files_selected[0].getParentFile().getAbsolutePath();

dir_name_textfield.setText(files_selected[0].getParentFile().getName() + "_" + String.valueOf(System.currentTimeMillis()/1000));
dir_name_textfield.setText(files_selected[0].getParentFile().getName() + "_" + String.valueOf(System.currentTimeMillis() / 1000));

dir_name_textfield.setEnabled(true);

Expand Down Expand Up @@ -624,7 +624,7 @@ private void add_folder_buttonActionPerformed(java.awt.event.ActionEvent evt) {/

_base_path = filechooser.getSelectedFile().getAbsolutePath();

dir_name_textfield.setText(filechooser.getSelectedFile().getName() + "_" + String.valueOf(System.currentTimeMillis()/1000));
dir_name_textfield.setText(filechooser.getSelectedFile().getName() + "_" + String.valueOf(System.currentTimeMillis() / 1000));

dir_name_textfield.setEnabled(true);

Expand Down Expand Up @@ -988,7 +988,7 @@ private void _file_drop_notify(List<File> files) {
_base_path = (files.size() == 1 && files.get(0).isDirectory()) ? files.get(0).getAbsolutePath() : files.get(0).getParentFile().getAbsolutePath();

MiscTools.GUIRunAndWait(() -> {
dir_name_textfield.setText(((files.size() == 1 && files.get(0).isDirectory()) ? files.get(0).getName() : files.get(0).getParentFile().getName()) + "_" + String.valueOf(System.currentTimeMillis()/1000));
dir_name_textfield.setText(((files.size() == 1 && files.get(0).isDirectory()) ? files.get(0).getName() : files.get(0).getParentFile().getName()) + "_" + String.valueOf(System.currentTimeMillis() / 1000));

dir_name_textfield.setEnabled(true);

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tonikelope/megabasterd/MainPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
*/
public final class MainPanel {

public static final String VERSION = "7.86";
public static final String VERSION = "7.87";
public static final boolean FORCE_SMART_PROXY = false; //TRUE FOR DEBUGING SMART PROXY
public static final int THROTTLE_SLICE_SIZE = 16 * 1024;
public static final int DEFAULT_BYTE_BUFFER_SIZE = 16 * 1024;
Expand Down

0 comments on commit 393d3e9

Please sign in to comment.