Merge pull request #1758 from Lucwousin/sesh-wimme-bra

client: Run SessionClient asynchronously
This commit is contained in:
Lucwousin
2019-10-11 22:11:59 +02:00
committed by GitHub
2 changed files with 68 additions and 47 deletions

View File

@@ -37,7 +37,7 @@ import lombok.extern.slf4j.Slf4j;
@Slf4j @Slf4j
public class ClientSessionManager public class ClientSessionManager
{ {
private final SessionClient sessionClient = new SessionClient(); private final SessionClient sessionClient = new SessionClient(this);
private final ScheduledExecutorService executorService; private final ScheduledExecutorService executorService;
private ScheduledFuture<?> scheduledFuture; private ScheduledFuture<?> scheduledFuture;
@@ -52,19 +52,23 @@ public class ClientSessionManager
public void start() public void start()
{ {
try sessionClient.open();
{
sessionId = sessionClient.open();
log.debug("Opened session {}", sessionId);
}
catch (IOException ex)
{
log.warn("error opening session", ex);
}
scheduledFuture = executorService.scheduleWithFixedDelay(this::ping, 1, 10, TimeUnit.MINUTES); scheduledFuture = executorService.scheduleWithFixedDelay(this::ping, 1, 10, TimeUnit.MINUTES);
} }
void setUuid(UUID uuid)
{
this.sessionId = uuid;
log.debug("Opened session {}", sessionId);
}
void error(IOException e)
{
log.warn("Client session error, resetting UUID", e.getCause());
sessionId = null;
}
public void shutdown() public void shutdown()
{ {
if (sessionId != null) if (sessionId != null)
@@ -85,29 +89,12 @@ public class ClientSessionManager
private void ping() private void ping()
{ {
try if (sessionId == null)
{ {
if (sessionId == null) sessionClient.open();
{ return;
sessionId = sessionClient.open();
log.debug("Opened session {}", sessionId);
return;
}
}
catch (IOException ex)
{
log.warn(null, ex);
}
try
{
sessionClient.ping(sessionId);
}
catch (IOException ex)
{
log.warn("Resetting session", ex);
sessionId = null;
} }
sessionClient.ping(sessionId);
} }
} }

View File

@@ -30,14 +30,24 @@ import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.UUID; import java.util.UUID;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.HttpUrl; import okhttp3.HttpUrl;
import okhttp3.Request; import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import okhttp3.ResponseBody; import okhttp3.ResponseBody;
import org.jetbrains.annotations.NotNull;
class SessionClient class SessionClient
{ {
UUID open() throws IOException private final ClientSessionManager manager;
SessionClient(ClientSessionManager manager)
{
this.manager = manager;
}
void open()
{ {
HttpUrl url = RuneLiteAPI.getopenosrsSessionBase().newBuilder() HttpUrl url = RuneLiteAPI.getopenosrsSessionBase().newBuilder()
.build(); .build();
@@ -46,20 +56,34 @@ class SessionClient
.url(url) .url(url)
.build(); .build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{ {
ResponseBody body = response.body(); @Override
public void onFailure(@NotNull Call call, @NotNull IOException e)
InputStream in = body.byteStream(); {
return RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), UUID.class); manager.error(e);
} }
catch (JsonParseException | IllegalArgumentException ex) // UUID.fromString can throw IllegalArgumentException
{ @Override
throw new IOException(ex); public void onResponse(@NotNull Call call, @NotNull Response response)
} {
try
{
ResponseBody body = response.body();
InputStream in = body.byteStream();
manager.setUuid(RuneLiteAPI.GSON.fromJson(new InputStreamReader(in), UUID.class));
}
catch (JsonParseException | IllegalArgumentException ex) // UUID.fromString can throw IllegalArgumentException
{
manager.error(new IOException(ex));
}
}
});
} }
void ping(UUID uuid) throws IOException void ping(UUID uuid)
{ {
HttpUrl url = RuneLiteAPI.getopenosrsSessionBase().newBuilder() HttpUrl url = RuneLiteAPI.getopenosrsSessionBase().newBuilder()
.addPathSegment("ping") .addPathSegment("ping")
@@ -70,13 +94,23 @@ class SessionClient
.url(url) .url(url)
.build(); .build();
try (Response response = RuneLiteAPI.CLIENT.newCall(request).execute()) RuneLiteAPI.CLIENT.newCall(request).enqueue(new Callback()
{ {
if (!response.isSuccessful()) @Override
public void onFailure(@NotNull Call call, @NotNull IOException e)
{ {
throw new IOException("Unsuccessful ping"); manager.error(e);
} }
}
@Override
public void onResponse(@NotNull Call call, @NotNull Response response)
{
if (!response.isSuccessful())
{
manager.error(new IOException("Failed ping"));
}
}
});
} }
void delete(UUID uuid) throws IOException void delete(UUID uuid) throws IOException