- Trying to fix already connected error

- Improving pause all button
This commit is contained in:
tonikelope 2020-01-09 23:52:48 +01:00
parent a06f4eb3cd
commit 98f6f82b0d
12 changed files with 202 additions and 84 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.tonikelope</groupId>
<artifactId>MegaBasterd</artifactId>
<version>7.8</version>
<version>7.9</version>
<packaging>jar</packaging>
<dependencies>
<dependency>

View File

@ -24,7 +24,6 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
public static final double SLOW_PROXY_PERC = 0.5;
private static final Logger LOG = Logger.getLogger(ChunkDownloader.class.getName());
private final boolean FORCE_SMART_PROXY = false; //True for debugging SmartProxy
private final int _id;
private final Download _download;
private volatile boolean _exit;
@ -130,7 +129,7 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
SmartMegaProxyManager proxy_manager = MainPanel.getProxy_manager();
if (FORCE_SMART_PROXY) {
if (MainPanel.FORCE_SMART_PROXY) {
_current_smart_proxy = proxy_manager.getProxy(_excluded_proxy_list);
@ -141,12 +140,16 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
while (!_download.getMain_panel().isExit() && !_exit && !_download.isStopped()) {
if (_download.isPaused() && !_download.isStopped()) {
if (_download.isPaused() && !_download.isStopped() && !_download.getChunkmanager().isExit()) {
_download.pause_worker();
pause_init_time = System.currentTimeMillis();
secureWait();
paused += System.currentTimeMillis() - pause_init_time;
}
if (worker_url == null || http_error == 403) {
@ -362,17 +365,13 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
}
*/
if (!FORCE_SMART_PROXY) {
_current_smart_proxy = null;
}
_excluded_proxy_list.clear();
_download.getChunkmanager().secureNotify();
}
}
} catch (IOException ex) {
} catch (IOException | IllegalStateException ex) {
if (ex instanceof SocketTimeoutException) {
timeout = true;
@ -417,6 +416,9 @@ public class ChunkDownloader implements Runnable, SecureSingleThreadNotifiable {
} else if (http_error == 503 && _current_smart_proxy == null && !_download.isTurbo()) {
setExit(true);
}
} else if (!FORCE_SMART_PROXY) {
_current_smart_proxy = null;
}
con.disconnect();

View File

@ -412,7 +412,6 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
public void stop() {
if (!isExit()) {
getMain_panel().getDownload_manager().setPaused_all(false);
_canceled = true;
stopDownloader();
}
@ -425,8 +424,6 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
setPause(false);
getMain_panel().getDownload_manager().setPaused_all(false);
setPaused_workers(0);
synchronized (_workers_lock) {
@ -438,6 +435,8 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
getView().resume();
_main_panel.getDownload_manager().setPaused_all(false);
} else {
setPause(true);
@ -1119,7 +1118,7 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
}
public String getDownloadUrlForWorker() throws IOException {
public String getDownloadUrlForWorker() {
synchronized (_dl_url_lock) {
@ -1156,7 +1155,7 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
error = true;
}
} catch (APIException ex) {
} catch (Exception ex) {
error = true;
@ -1449,10 +1448,10 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
if (Arrays.asList(FATAL_API_ERROR_CODES).contains(error_code)) {
stopDownloader(ex.getMessage() + " " + truncateText(link, 80));
_auto_retry_on_error = Arrays.asList(FATAL_API_ERROR_CODES_WITH_RETRY).contains(error_code);
stopDownloader(ex.getMessage() + " " + truncateText(link, 80));
} else {
if (!retry_request) {
@ -1535,10 +1534,10 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
if (Arrays.asList(FATAL_API_ERROR_CODES).contains(error_code)) {
stopDownloader(ex.getMessage() + " " + truncateText(link, 80));
_auto_retry_on_error = Arrays.asList(FATAL_API_ERROR_CODES_WITH_RETRY).contains(error_code);
stopDownloader(ex.getMessage() + " " + truncateText(link, 80));
} else {
_retrying_request = true;
@ -1711,4 +1710,14 @@ public class Download implements Transference, Runnable, SecureSingleThreadNotif
return _closed;
}
@Override
public int getPausedWorkers() {
return _paused_workers;
}
@Override
public int getTotWorkers() {
return getChunkworkers().size();
}
}

View File

@ -55,7 +55,8 @@ import javax.swing.UIManager;
*/
public final class MainPanel {
public static final String VERSION = "7.8";
public static final String VERSION = "7.9";
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;
public static final int STREAMER_PORT = 1337;

View File

@ -1266,7 +1266,17 @@ public final class MainPanelView extends javax.swing.JFrame {
private void pause_all_down_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_pause_all_down_buttonActionPerformed
_main_panel.getDownload_manager().pauseAll();
pause_all_down_button.setEnabled(false);
if (!_main_panel.getDownload_manager().isPaused_all()) {
_main_panel.getDownload_manager().pauseAll();
} else {
_main_panel.getDownload_manager().resumeAll();
}
}//GEN-LAST:event_pause_all_down_buttonActionPerformed
private void new_stream_menuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_new_stream_menuActionPerformed

View File

@ -364,13 +364,19 @@ public class MegaAPI implements Serializable {
SmartMegaProxyManager proxy_manager = MainPanel.getProxy_manager();
if (MainPanel.FORCE_SMART_PROXY) {
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
}
do {
try {
if ((current_smart_proxy != null || http_error == 509) && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()) {
if (current_smart_proxy != null && (http_error != 0 || mega_error != 0)) {
if (current_smart_proxy != null && (http_error != 0 || empty_response)) {
if (http_error == 509) {
proxy_manager.blockProxy(current_smart_proxy);
@ -419,20 +425,20 @@ public class MegaAPI implements Serializable {
mega_error = 0;
empty_response = true;
con.setRequestMethod("POST");
empty_response = false;
con.setRequestProperty("Content-type", "text/plain;charset=UTF-8");
con.setRequestProperty("User-Agent", MainPanel.DEFAULT_USER_AGENT);
con.setUseCaches(false);
con.setRequestMethod("POST");
con.setDoOutput(true);
con.getOutputStream().write(request.getBytes("UTF-8"));
con.getOutputStream().flush();
con.getOutputStream().close();
http_status = con.getResponseCode();
@ -462,10 +468,17 @@ public class MegaAPI implements Serializable {
if (response.length() > 0) {
empty_response = false;
mega_error = checkMEGAError(response);
if (mega_error != 0 && !Arrays.asList(MEGA_ERROR_NO_EXCEPTION_CODES).contains(mega_error)) {
throw new MegaAPIException(mega_error);
}
} else {
empty_response = true;
}
}
@ -483,12 +496,7 @@ public class MegaAPI implements Serializable {
}
if (!empty_response && mega_error != 0 && http_error != 509) {
if (!Arrays.asList(MEGA_ERROR_NO_EXCEPTION_CODES).contains(mega_error)) {
throw new MegaAPIException(mega_error);
}
if ((empty_response || mega_error != 0 || http_error != 0) && http_error != 509) {
LOG.log(Level.WARNING, "{0} MegaAPI ERROR {1} Waiting for retry...", new Object[]{Thread.currentThread().getName(), String.valueOf(mega_error)});
@ -498,12 +506,9 @@ public class MegaAPI implements Serializable {
LOG.log(Level.SEVERE, ex.getMessage());
}
} else if (!empty_response && mega_error == 0 && http_error == 0) {
conta_error = 0;
}
} while (empty_response || mega_error != 0 || (http_error == 509 && MainPanel.isUse_smart_proxy()));
} while (empty_response || mega_error != 0 || (http_error == 509 && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()));
_seqno++;
@ -511,7 +516,7 @@ public class MegaAPI implements Serializable {
}
public String getMegaFileDownloadUrl(String link) throws IOException, MegaAPIException {
public String getMegaFileDownloadUrl(String link) throws MegaAPIException, MalformedURLException, IOException {
String file_id = findFirstRegex("#.*?!([^!]+)", link, 1);

View File

@ -851,8 +851,6 @@ public class MiscTools {
HttpURLConnection con = null;
boolean error = false;
int http_status = 0, http_error = 0;
SmartMegaProxyManager proxy_manager = MainPanel.getProxy_manager();
@ -861,54 +859,42 @@ public class MiscTools {
ArrayList<String> excluded_proxy_list = new ArrayList<>();
if (MainPanel.FORCE_SMART_PROXY) {
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
}
do {
try {
URL url = new URL(string_url + "/0-0");
if (con == null || error) {
if ((current_smart_proxy != null || http_error == 509) && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()) {
if ((current_smart_proxy != null || http_error == 509) && MainPanel.isUse_smart_proxy() && !MainPanel.isUse_proxy()) {
if (current_smart_proxy != null && http_error != 0) {
if (current_smart_proxy != null && error) {
if (http_error == 509) {
proxy_manager.blockProxy(current_smart_proxy);
}
excluded_proxy_list.add(current_smart_proxy);
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
} else if (current_smart_proxy == null) {
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
if (http_error == 509) {
proxy_manager.blockProxy(current_smart_proxy);
}
if (current_smart_proxy != null) {
excluded_proxy_list.add(current_smart_proxy);
String[] proxy_info = current_smart_proxy.split(":");
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy_info[0], Integer.parseInt(proxy_info[1])));
} else if (current_smart_proxy == null) {
con = (HttpURLConnection) url.openConnection(proxy);
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
}
} else {
if (current_smart_proxy != null) {
if (MainPanel.isUse_proxy()) {
String[] proxy_info = current_smart_proxy.split(":");
con = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(MainPanel.getProxy_host(), MainPanel.getProxy_port())));
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy_info[0], Integer.parseInt(proxy_info[1])));
if (MainPanel.getProxy_user() != null && !"".equals(MainPanel.getProxy_user())) {
con.setRequestProperty("Proxy-Authorization", "Basic " + MiscTools.Bin2BASE64((MainPanel.getProxy_user() + ":" + MainPanel.getProxy_pass()).getBytes("UTF-8")));
}
} else {
con = (HttpURLConnection) url.openConnection();
}
}
con = (HttpURLConnection) url.openConnection(proxy);
} else {
@ -926,6 +912,20 @@ public class MiscTools {
}
}
} else {
if (MainPanel.isUse_proxy()) {
con = (HttpURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(MainPanel.getProxy_host(), MainPanel.getProxy_port())));
if (MainPanel.getProxy_user() != null && !"".equals(MainPanel.getProxy_user())) {
con.setRequestProperty("Proxy-Authorization", "Basic " + MiscTools.Bin2BASE64((MainPanel.getProxy_user() + ":" + MainPanel.getProxy_pass()).getBytes("UTF-8")));
}
} else {
con = (HttpURLConnection) url.openConnection();
}
}
if (current_smart_proxy != null) {
@ -941,15 +941,18 @@ public class MiscTools {
if (http_status != 200) {
http_error = http_status;
} else {
http_error = 0;
}
} catch (IOException ex) {
} catch (Exception ex) {
Logger.getLogger(MiscTools.class.getName()).log(Level.SEVERE, ex.getMessage());
} finally {
if (con != null) {
con.disconnect();
}
}
} while (http_error == 509);

View File

@ -55,6 +55,12 @@ public class StreamChunkDownloader implements Runnable {
ArrayList<String> excluded_proxy_list = new ArrayList<>();
if (MainPanel.FORCE_SMART_PROXY) {
current_smart_proxy = proxy_manager.getProxy(excluded_proxy_list);
}
while (!_exit && !_chunkmanager.isExit()) {
while (!_exit && !_chunkmanager.isExit() && _chunkmanager.getChunk_queue().size() >= StreamChunkManager.BUFFER_CHUNKS_SIZE) {

View File

@ -10,8 +10,8 @@ public interface Transference {
int MIN_WORKERS = 1;
int MAX_WORKERS = 20;
int HTTP_PROXY_CONNECT_TIMEOUT = 15000;
int HTTP_PROXY_READ_TIMEOUT = 15000;
int HTTP_PROXY_CONNECT_TIMEOUT = 20000;
int HTTP_PROXY_READ_TIMEOUT = 20000;
int HTTP_CONNECT_TIMEOUT = 60000;
int HTTP_READ_TIMEOUT = 60000;
int MAX_SIM_TRANSFERENCES = 20;
@ -79,4 +79,8 @@ public interface Transference {
int getSlotsCount();
int getPausedWorkers();
int getTotWorkers();
}

View File

@ -39,6 +39,7 @@ abstract public class TransferenceManager implements Runnable, SecureSingleThrea
private final MainPanel _main_panel;
private final Object _secure_notify_lock;
private final Object _wait_queue_lock;
private final Object _pause_all_lock;
private boolean _notified;
private volatile boolean _removing_transferences;
private volatile boolean _provisioning_transferences;
@ -67,6 +68,7 @@ abstract public class TransferenceManager implements Runnable, SecureSingleThrea
_max_running_trans = max_running_trans;
_scroll_panel = scroll_panel;
_status = status;
_pause_all_lock = new Object();
_close_all_button = close_all_button;
_pause_all_button = pause_all_button;
_clean_all_menu = clean_all_menu;
@ -96,6 +98,10 @@ abstract public class TransferenceManager implements Runnable, SecureSingleThrea
this._sort_wait_start_queue = sort_wait_start_queue;
}
public void setPaused_all(boolean _paused_all) {
this._paused_all = _paused_all;
}
public boolean isFrozen() {
return _frozen;
}
@ -112,10 +118,6 @@ abstract public class TransferenceManager implements Runnable, SecureSingleThrea
return _wait_queue_lock;
}
public void setPaused_all(boolean paused_all) {
_paused_all = paused_all;
}
public ConcurrentLinkedQueue<Object> getTransference_preprocess_global_queue() {
return _transference_preprocess_global_queue;
}
@ -481,10 +483,78 @@ abstract public class TransferenceManager implements Runnable, SecureSingleThrea
public void pauseAll() {
_paused_all = !_paused_all;
_transference_running_list.forEach((transference) -> {
if (!transference.isPaused()) {
transference.pause();
}
});
secureNotify();
THREAD_POOL.execute(() -> {
boolean running;
do {
running = false;
for (Transference t : _transference_running_list) {
if (t.getPausedWorkers() != t.getTotWorkers()) {
running = true;
break;
}
}
if (running) {
synchronized (_pause_all_lock) {
try {
_pause_all_lock.wait(1000);
} catch (InterruptedException ex) {
Logger.getLogger(TransferenceManager.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} while (running);
_paused_all = true;
swingInvoke(() -> {
_pause_all_button.setText("RESUME ALL");
_pause_all_button.setEnabled(true);
});
secureNotify();
});
}
public void resumeAll() {
_transference_running_list.forEach((transference) -> {
transference.pause();
if (transference.isPaused()) {
transference.pause();
}
});
_paused_all = false;
swingInvoke(() -> {
_pause_all_button.setText("PAUSE ALL");
_pause_all_button.setEnabled(true);
});
secureNotify();

View File

@ -488,7 +488,6 @@ public class Upload implements Transference, Runnable, SecureSingleThreadNotifia
@Override
public void stop() {
if (!isExit()) {
getMain_panel().getUpload_manager().setPaused_all(false);
_canceled = true;
stopUploader();
}
@ -501,8 +500,6 @@ public class Upload implements Transference, Runnable, SecureSingleThreadNotifia
setPause(false);
getMain_panel().getUpload_manager().setPaused_all(false);
setPaused_workers(0);
synchronized (_workers_lock) {
@ -514,6 +511,8 @@ public class Upload implements Transference, Runnable, SecureSingleThreadNotifia
getView().resume();
_main_panel.getUpload_manager().setPaused_all(false);
} else {
setPause(true);
@ -1311,4 +1310,13 @@ public class Upload implements Transference, Runnable, SecureSingleThreadNotifia
return _closed;
}
@Override
public int getPausedWorkers() {
return _paused_workers;
}
@Override
public int getTotWorkers() {
return getChunkworkers().size();
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 192 KiB

After

Width:  |  Height:  |  Size: 189 KiB