Merge pull request #419 from deathbeam/cleanup

Cleanup net.runelite.client.account
This commit is contained in:
Adam
2018-01-30 17:45:11 -05:00
committed by GitHub
5 changed files with 23 additions and 95 deletions

View File

@@ -25,73 +25,15 @@
package net.runelite.client.account; package net.runelite.client.account;
import java.time.Instant; import java.time.Instant;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(of = "uuid")
public class AccountSession public class AccountSession
{ {
private UUID uuid; private final UUID uuid;
private final Instant created;
private String username; 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;
}
}

View File

@@ -127,7 +127,7 @@ public class SessionManager
public void openSession(AccountSession session) public void openSession(AccountSession session)
{ {
// If the ws session already exists, don't need to do anything // 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) if (wsclient != null)
{ {

View File

@@ -47,29 +47,27 @@ import okhttp3.WebSocketListener;
public class WSClient extends WebSocketListener implements AutoCloseable public class WSClient extends WebSocketListener implements AutoCloseable
{ {
private static final Duration PING_TIME = Duration.ofSeconds(30); 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 OkHttpClient client = new OkHttpClient();
private final EventBus eventBus; private final EventBus eventBus;
private final ScheduledFuture pingFuture;
private final AccountSession session; private final AccountSession session;
private WebSocket webSocket; 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.eventBus = eventBus;
this.session = session; this.session = session;
this.pingFuture = executor.scheduleWithFixedDelay(this::ping, PING_TIME.getSeconds(), PING_TIME.getSeconds(), TimeUnit.SECONDS); 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() Request request = new Request.Builder()
.url(RuneLiteAPI.getWsEndpoint()) .url(RuneLiteAPI.getWsEndpoint())
@@ -82,14 +80,14 @@ public class WSClient extends WebSocketListener implements AutoCloseable
send(handshake); send(handshake);
} }
public void ping() private void ping()
{ {
Ping ping = new Ping(); Ping ping = new Ping();
ping.setTime(Instant.now()); ping.setTime(Instant.now());
send(ping); send(ping);
} }
public void send(WebsocketMessage message) private void send(WebsocketMessage message)
{ {
if (webSocket == null) if (webSocket == null)
{ {
@@ -98,7 +96,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
connect(); connect();
} }
String json = gson.toJson(message, WebsocketMessage.class); String json = GSON.toJson(message, WebsocketMessage.class);
webSocket.send(json); webSocket.send(json);
log.debug("Sent: {}", json); log.debug("Sent: {}", json);
@@ -127,7 +125,7 @@ public class WSClient extends WebSocketListener implements AutoCloseable
@Override @Override
public void onMessage(WebSocket webSocket, String text) 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); log.debug("Got message: {}", message);
eventBus.post(message); eventBus.post(message);

View File

@@ -132,11 +132,7 @@ public class AccountPlugin extends Plugin
} }
// Create new session // Create new session
AccountSession session = new AccountSession(); sessionManager.openSession(new AccountSession(login.getUid(), Instant.now()));
session.setUuid(login.getUid());
session.setCreated(Instant.now());
sessionManager.openSession(session);
if (!Desktop.isDesktopSupported()) if (!Desktop.isDesktopSupported())
{ {

View File

@@ -68,10 +68,8 @@ public class ConfigManagerTest
@Test @Test
public void testGetConfig() throws IOException public void testGetConfig() throws IOException
{ {
AccountSession accountSession = new AccountSession(); AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUuid(UUID.randomUUID());
accountSession.setUsername("test"); accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
manager.setConfiguration("test", "key", "moo"); manager.setConfiguration("test", "key", "moo");
@@ -82,10 +80,8 @@ public class ConfigManagerTest
@Test @Test
public void testGetConfigDefault() throws IOException public void testGetConfigDefault() throws IOException
{ {
AccountSession accountSession = new AccountSession(); AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUuid(UUID.randomUUID());
accountSession.setUsername("test"); accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
Assert.assertEquals("default", conf.key()); Assert.assertEquals("default", conf.key());
@@ -94,10 +90,8 @@ public class ConfigManagerTest
@Test @Test
public void testSetConfig() throws IOException public void testSetConfig() throws IOException
{ {
AccountSession accountSession = new AccountSession(); AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUuid(UUID.randomUUID());
accountSession.setUsername("test"); accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
conf.key("new value"); conf.key("new value");
@@ -108,10 +102,8 @@ public class ConfigManagerTest
@Test @Test
public void testGetConfigDescriptor() throws IOException public void testGetConfigDescriptor() throws IOException
{ {
AccountSession accountSession = new AccountSession(); AccountSession accountSession = new AccountSession(UUID.randomUUID(), Instant.now());
accountSession.setUuid(UUID.randomUUID());
accountSession.setUsername("test"); accountSession.setUsername("test");
accountSession.setCreated(Instant.now());
TestConfig conf = manager.getConfig(TestConfig.class); TestConfig conf = manager.getConfig(TestConfig.class);
ConfigDescriptor descriptor = manager.getConfigDescriptor(conf); ConfigDescriptor descriptor = manager.getConfigDescriptor(conf);