Merge pull request #419 from deathbeam/cleanup
Cleanup net.runelite.client.account
This commit is contained in:
@@ -25,73 +25,15 @@
|
||||
package net.runelite.client.account;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(of = "uuid")
|
||||
public class AccountSession
|
||||
{
|
||||
private UUID uuid;
|
||||
private final UUID uuid;
|
||||
private final Instant created;
|
||||
private String username;
|
||||
private Instant created;
|
||||
|
||||
@Override
|
||||
public int hashCode()
|
||||
{
|
||||
int hash = 3;
|
||||
hash = 29 * hash + Objects.hashCode(this.uuid);
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj)
|
||||
{
|
||||
if (this == obj)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
if (obj == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
if (getClass() != obj.getClass())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
final AccountSession other = (AccountSession) obj;
|
||||
if (!Objects.equals(this.uuid, other.uuid))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public UUID getUuid()
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
|
||||
public void setUuid(UUID uuid)
|
||||
{
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
public String getUsername()
|
||||
{
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username)
|
||||
{
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public Instant getCreated()
|
||||
{
|
||||
return created;
|
||||
}
|
||||
|
||||
public void setCreated(Instant created)
|
||||
{
|
||||
this.created = created;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,7 +127,7 @@ public class SessionManager
|
||||
public void openSession(AccountSession session)
|
||||
{
|
||||
// If the ws session already exists, don't need to do anything
|
||||
if (wsclient == null || !wsclient.getSession().equals(session))
|
||||
if (wsclient == null || !wsclient.checkSession(session))
|
||||
{
|
||||
if (wsclient != null)
|
||||
{
|
||||
|
||||
@@ -47,29 +47,27 @@ import okhttp3.WebSocketListener;
|
||||
public class WSClient extends WebSocketListener implements AutoCloseable
|
||||
{
|
||||
private static final Duration PING_TIME = Duration.ofSeconds(30);
|
||||
|
||||
private static final Gson gson = WebsocketGsonFactory.build();
|
||||
private static final Gson GSON = WebsocketGsonFactory.build();
|
||||
|
||||
private final OkHttpClient client = new OkHttpClient();
|
||||
|
||||
private final EventBus eventBus;
|
||||
private final ScheduledFuture pingFuture;
|
||||
private final AccountSession session;
|
||||
private WebSocket webSocket;
|
||||
private final ScheduledFuture pingFuture;
|
||||
|
||||
public WSClient(EventBus eventBus, ScheduledExecutorService executor, AccountSession session)
|
||||
WSClient(EventBus eventBus, ScheduledExecutorService executor, AccountSession session)
|
||||
{
|
||||
this.eventBus = eventBus;
|
||||
this.session = session;
|
||||
this.pingFuture = executor.scheduleWithFixedDelay(this::ping, PING_TIME.getSeconds(), PING_TIME.getSeconds(), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public AccountSession getSession()
|
||||
boolean checkSession(AccountSession session)
|
||||
{
|
||||
return session;
|
||||
return session.equals(this.session);
|
||||
}
|
||||
|
||||
public void connect()
|
||||
void connect()
|
||||
{
|
||||
Request request = new Request.Builder()
|
||||
.url(RuneLiteAPI.getWsEndpoint())
|
||||
@@ -82,14 +80,14 @@ public class WSClient extends WebSocketListener implements AutoCloseable
|
||||
send(handshake);
|
||||
}
|
||||
|
||||
public void ping()
|
||||
private void ping()
|
||||
{
|
||||
Ping ping = new Ping();
|
||||
ping.setTime(Instant.now());
|
||||
send(ping);
|
||||
}
|
||||
|
||||
public void send(WebsocketMessage message)
|
||||
private void send(WebsocketMessage message)
|
||||
{
|
||||
if (webSocket == null)
|
||||
{
|
||||
@@ -98,7 +96,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
|
||||
connect();
|
||||
}
|
||||
|
||||
String json = gson.toJson(message, WebsocketMessage.class);
|
||||
String json = GSON.toJson(message, WebsocketMessage.class);
|
||||
webSocket.send(json);
|
||||
|
||||
log.debug("Sent: {}", json);
|
||||
@@ -127,7 +125,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
|
||||
@Override
|
||||
public void onMessage(WebSocket webSocket, String text)
|
||||
{
|
||||
WebsocketMessage message = gson.fromJson(text, WebsocketMessage.class);
|
||||
WebsocketMessage message = GSON.fromJson(text, WebsocketMessage.class);
|
||||
log.debug("Got message: {}", message);
|
||||
|
||||
eventBus.post(message);
|
||||
|
||||
@@ -132,11 +132,7 @@ public class AccountPlugin extends Plugin
|
||||
}
|
||||
|
||||
// Create new session
|
||||
AccountSession session = new AccountSession();
|
||||
session.setUuid(login.getUid());
|
||||
session.setCreated(Instant.now());
|
||||
|
||||
sessionManager.openSession(session);
|
||||
sessionManager.openSession(new AccountSession(login.getUid(), Instant.now()));
|
||||
|
||||
if (!Desktop.isDesktopSupported())
|
||||
{
|
||||
|
||||
@@ -68,10 +68,8 @@ public class ConfigManagerTest
|
||||
@Test
|
||||
public void testGetConfig() throws IOException
|
||||
{
|
||||
AccountSession accountSession = new AccountSession();
|
||||
accountSession.setUuid(UUID.randomUUID());
|
||||
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
|
||||
accountSession.setUsername("test");
|
||||
accountSession.setCreated(Instant.now());
|
||||
|
||||
manager.setConfiguration("test", "key", "moo");
|
||||
|
||||
@@ -82,10 +80,8 @@ public class ConfigManagerTest
|
||||
@Test
|
||||
public void testGetConfigDefault() throws IOException
|
||||
{
|
||||
AccountSession accountSession = new AccountSession();
|
||||
accountSession.setUuid(UUID.randomUUID());
|
||||
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
|
||||
accountSession.setUsername("test");
|
||||
accountSession.setCreated(Instant.now());
|
||||
|
||||
TestConfig conf = manager.getConfig(TestConfig.class);
|
||||
Assert.assertEquals("default", conf.key());
|
||||
@@ -94,10 +90,8 @@ public class ConfigManagerTest
|
||||
@Test
|
||||
public void testSetConfig() throws IOException
|
||||
{
|
||||
AccountSession accountSession = new AccountSession();
|
||||
accountSession.setUuid(UUID.randomUUID());
|
||||
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
|
||||
accountSession.setUsername("test");
|
||||
accountSession.setCreated(Instant.now());
|
||||
|
||||
TestConfig conf = manager.getConfig(TestConfig.class);
|
||||
conf.key("new value");
|
||||
@@ -108,10 +102,8 @@ public class ConfigManagerTest
|
||||
@Test
|
||||
public void testGetConfigDescriptor() throws IOException
|
||||
{
|
||||
AccountSession accountSession = new AccountSession();
|
||||
accountSession.setUuid(UUID.randomUUID());
|
||||
AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
|
||||
accountSession.setUsername("test");
|
||||
accountSession.setCreated(Instant.now());
|
||||
|
||||
TestConfig conf = manager.getConfig(TestConfig.class);
|
||||
ConfigDescriptor descriptor = manager.getConfigDescriptor(conf);
|
||||
|
||||
Reference in New Issue
Block a user