Skip to content

Commit

Permalink
Close clients used for testing (drawer) + release LoadingActivity client
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Feb 27, 2019
1 parent f57b404 commit a1cab82
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/gianlu/aria2app/LoadingActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ private void launchWebView() {

@Override
public boolean onConnected(@NonNull AbstractClient client) {
ongoingTest = null;

if (shareData != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.useWebView)
Expand Down Expand Up @@ -375,6 +377,8 @@ private void failedConnecting(@NonNull Throwable ex) {

@Override
public void onFailedConnecting(@NonNull MultiProfile.UserProfile profile, @NonNull Throwable ex) {
ongoingTest = null;

if (profile.couldBeAria2Android(this)) mayStartAria2Android(profile, ex);
else failedConnecting(ex);
}
Expand Down
16 changes: 10 additions & 6 deletions app/src/main/java/com/gianlu/aria2app/NetIO/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.security.GeneralSecurityException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -24,7 +25,7 @@

public class WebSocketClient extends AbstractClient {
private final Map<Long, InternalResponse> requests = new ConcurrentHashMap<>();
private final WebSocket webSocket;
private final WeakReference<WebSocket> webSocket;
private final long initializedAt;
private final ExecutorService executorService = Executors.newCachedThreadPool();
private final boolean closeAfterTest;
Expand All @@ -33,7 +34,7 @@ public class WebSocketClient extends AbstractClient {
@UiThread
private WebSocketClient(@NonNull MultiProfile.UserProfile profile, boolean close) throws GeneralSecurityException, NetUtils.InvalidUrlException, IOException {
super(profile);
webSocket = client.newWebSocket(NetUtils.createWebsocketRequest(profile), new Listener());
webSocket = new WeakReference<>(client.newWebSocket(NetUtils.createWebsocketRequest(profile), new Listener()));
initializedAt = System.currentTimeMillis();
closeAfterTest = close;
}
Expand All @@ -47,7 +48,7 @@ private WebSocketClient(@NonNull MultiProfile.UserProfile profile, @Nullable OnC
@NonNull
static WebSocketClient instantiate(@NonNull MultiProfile.UserProfile profile) throws InitializationException {
try {
return new WebSocketClient(profile, true);
return new WebSocketClient(profile, false);
} catch (NetUtils.InvalidUrlException | GeneralSecurityException | IOException ex) {
throw new InitializationException(ex);
}
Expand All @@ -66,7 +67,10 @@ public static Closeable checkConnection(@NonNull MultiProfile.UserProfile profil
@Override
protected void closeClient() {
connectionListener = null;
if (webSocket != null) webSocket.close(1000, null);
if (webSocket.get() != null) {
webSocket.get().close(1000, null);
webSocket.clear();
}

for (InternalResponse internal : requests.values())
internal.exception(new IOException("Client has been closed."));
Expand All @@ -88,13 +92,13 @@ public void send(long id, @NonNull JSONObject request, @NonNull OnJson listener)
@Override
@WorkerThread
public JSONObject sendSync(long id, @NonNull JSONObject request) throws Exception {
if (closed)
if (closed || webSocket.get() == null)
throw new IllegalStateException("Client is closed: " + this);

InternalResponse internal = new InternalResponse();

requests.put(id, internal);
webSocket.send(request.toString());
webSocket.get().send(request.toString());

synchronized (internal) {
internal.wait(5000);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void itemChanged(@NonNull String profileId, long ping) {

@Override
protected void runTest(int pos) {
executorService.execute(new NetTester(context, getItem(pos).getProfile(context), this));
executorService.execute(new NetTester(context, getItem(pos).getProfile(context), true, this));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
import androidx.annotation.UiThread;

public class NetTester extends BaseTester<AbstractClient> {
private final boolean close;
private final ProfileTesterCallback profileListener;

NetTester(Context context, MultiProfile.UserProfile profile, PublishListener<AbstractClient> listener) {
super(context, profile, listener);
this.profileListener = null;
this.close = false;
}

public NetTester(Context context, MultiProfile.UserProfile profile, ProfileTesterCallback profileListener) {
public NetTester(Context context, MultiProfile.UserProfile profile, boolean close, ProfileTesterCallback profileListener) {
super(context, profile, null);
this.close = close;
this.profileListener = profileListener;
}

Expand Down Expand Up @@ -76,7 +79,9 @@ public void onFailedConnecting(@NonNull MultiProfile.UserProfile profile, @NonNu
Logging.log(ex);
}

return lock.get();
AbstractClient client = lock.get();
if (close && client != null) client.close();
return client;
}
}

Expand Down

0 comments on commit a1cab82

Please sign in to comment.