From e683ad1bbb2c6d5bccb26aab46f6d1856588926b Mon Sep 17 00:00:00 2001 From: Manevolent Date: Mon, 17 Sep 2018 21:43:36 -0400 Subject: [PATCH] Added helper methods to connect() Refactored packet handlers --- .../client/ClientConnectionState.java | 31 +++---------------- .../client/AbstractTeamspeakClientSocket.java | 20 +++++++++--- .../client/LocalTeamspeakClientSocket.java | 29 +++++++++++++++++ .../manevolent/ts3j/ServerConnectionTest.java | 5 +-- 4 files changed, 51 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/github/manevolent/ts3j/protocol/client/ClientConnectionState.java b/src/main/java/com/github/manevolent/ts3j/protocol/client/ClientConnectionState.java index b4884c7..f042882 100644 --- a/src/main/java/com/github/manevolent/ts3j/protocol/client/ClientConnectionState.java +++ b/src/main/java/com/github/manevolent/ts3j/protocol/client/ClientConnectionState.java @@ -1,32 +1,11 @@ package com.github.manevolent.ts3j.protocol.client; -import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler; -import com.github.manevolent.ts3j.protocol.packet.handler.client.*; -import com.github.manevolent.ts3j.protocol.socket.client.TeamspeakClientSocket; -import org.apache.commons.lang.reflect.ConstructorUtils; - public enum ClientConnectionState { - CONNECTING(LocalClientHandlerConnecting.class), - RETRIEVING_DATA(LocalClientHandlerRetrievingData.class), - CONNECTED(LocalClientHandlerConnected.class), - DISCONNECTED(LocalClientHandlerDisconnected.class); + CONNECTING(), + RETRIEVING_DATA(), + CONNECTED(), + DISCONNECTED(); - private final Class clazz; - - ClientConnectionState(Class clazz) { - this.clazz = clazz; - } - - public Class getHandlerClass() { - return clazz; - } - - public PacketHandler createHandler(TeamspeakClientSocket client) { - try { - return (PacketHandler) ConstructorUtils.invokeConstructor(clazz, client); - } catch (ReflectiveOperationException ex) { - throw new RuntimeException(ex); - } - } + ClientConnectionState() {} } \ No newline at end of file diff --git a/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/AbstractTeamspeakClientSocket.java b/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/AbstractTeamspeakClientSocket.java index b79567d..a0d24bb 100644 --- a/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/AbstractTeamspeakClientSocket.java +++ b/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/AbstractTeamspeakClientSocket.java @@ -24,6 +24,7 @@ import com.github.manevolent.ts3j.util.QuickLZ; import com.github.manevolent.ts3j.util.Ts3Crypt; import com.github.manevolent.ts3j.util.Ts3Debugging; import com.github.manevolent.ts3j.util.Pair; +import org.apache.commons.lang.reflect.ConstructorUtils; import org.bouncycastle.crypto.InvalidCipherTextException; import java.io.EOFException; @@ -144,6 +145,8 @@ public abstract class AbstractTeamspeakClientSocket return packetStatistics.get(kind); } + protected abstract Class getHandlerClass(ClientConnectionState state); + protected void start() { Thread networkThread = new Thread(networkReader); networkThread.setDaemon(true); @@ -195,12 +198,18 @@ public abstract class AbstractTeamspeakClientSocket if (handler != null) ((LocalClientHandler) handler).handleConnectionStateChanging(state); - if (handler == null || handler.getClass() != state.getHandlerClass()) { - Ts3Debugging.debug("Assigning " + state.getHandlerClass().getName() + " handler..."); + Class handlerClass = getHandlerClass(state); - setHandler(state.createHandler(this)); + if (handler == null || handler.getClass() != handlerClass) { + Ts3Debugging.debug("Assigning " + handlerClass + " handler..."); - Ts3Debugging.debug("Assigned " + state.getHandlerClass().getName() + " handler."); + try { + setHandler(createHandler(handlerClass)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException(e); + } + + Ts3Debugging.debug("Assigned " + handlerClass + " handler."); } } @@ -1430,4 +1439,7 @@ public abstract class AbstractTeamspeakClientSocket } } + private PacketHandler createHandler(Class clazz) throws ReflectiveOperationException { + return (PacketHandler) ConstructorUtils.invokeConstructor(clazz, this); + } } diff --git a/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java b/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java index 9e54c3e..7a55749 100644 --- a/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java +++ b/src/main/java/com/github/manevolent/ts3j/protocol/socket/client/LocalTeamspeakClientSocket.java @@ -17,6 +17,11 @@ import com.github.manevolent.ts3j.protocol.client.ClientConnectionState; import com.github.manevolent.ts3j.protocol.header.PacketHeader; import com.github.manevolent.ts3j.protocol.packet.PacketBody0Voice; import com.github.manevolent.ts3j.protocol.packet.PacketBody2Command; +import com.github.manevolent.ts3j.protocol.packet.handler.PacketHandler; +import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnected; +import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerConnecting; +import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerDisconnected; +import com.github.manevolent.ts3j.protocol.packet.handler.client.LocalClientHandlerRetrievingData; import com.github.manevolent.ts3j.protocol.packet.statistics.PacketStatistics; import com.github.manevolent.ts3j.protocol.packet.transformation.InitPacketTransformation; import com.github.manevolent.ts3j.util.HighPrecisionRecurrentTask; @@ -170,6 +175,22 @@ public class LocalTeamspeakClientSocket return this.listeners.remove(listener); } + @Override + protected Class getHandlerClass(ClientConnectionState state) { + switch (state) { + case DISCONNECTED: + return LocalClientHandlerDisconnected.class; + case CONNECTED: + return LocalClientHandlerConnected.class; + case RETRIEVING_DATA: + return LocalClientHandlerRetrievingData.class; + case CONNECTING: + return LocalClientHandlerConnecting.class; + default: + throw new UnsupportedOperationException(); + } + } + @Override protected NetworkPacket readNetworkPacket(int timeout) throws IOException, @@ -273,6 +294,14 @@ public class LocalTeamspeakClientSocket remote = null; } + /** + * Initiates a connection to a server + * @param hostname Hostname to contact + * @param timeout timeout, in milliseconds, to complete a connection. + */ + public void connect(String hostname, long timeout) throws IOException, TimeoutException { + connect(hostname, null, timeout); + } /** * Initiates a connection to a server diff --git a/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java b/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java index 46d3b45..907d5cf 100644 --- a/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java +++ b/src/test/java/com/github/manevolent/ts3j/ServerConnectionTest.java @@ -30,13 +30,10 @@ public class ServerConnectionTest { client.setNickname("ts3j issue 2"); client.setHWID("TestTestTest"); - while (true) { try { - client.connect( - new InetSocketAddress("178.32.109.167", 11079), - null, + "teamlixo.net", 10000L );