Skip to content

Commit

Permalink
Tweak msg handlers and add SteamClientTest
Browse files Browse the repository at this point in the history
  • Loading branch information
LossyDragon committed Aug 10, 2024
1 parent a8cf72b commit 78b54c1
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public class SteamClient extends CMClient {

private static final Logger logger = LogManager.getLogger(SteamClient.class);

private final Map<Class<? extends ClientMsgHandler>, ClientMsgHandler> handlers = new HashMap<>();
private static final int HANDLERS_COUNT = 13;

private final Map<Class<? extends ClientMsgHandler>, ClientMsgHandler> handlers = new HashMap<>(HANDLERS_COUNT);

private final AsyncJobManager jobManager;

Expand Down Expand Up @@ -75,20 +77,20 @@ public SteamClient(SteamConfiguration configuration) {

// add this library's handlers
// notice: SteamFriends should be added before SteamUser due to AccountInfoCallback
addHandler(new SteamFriends());
addHandler(new SteamUser());
addHandler(new SteamApps());
addHandler(new SteamGameCoordinator());
addHandler(new SteamGameServer());
addHandler(new SteamMasterServer());
addHandler(new SteamCloud());
addHandler(new SteamWorkshop());
addHandler(new SteamTrading());
addHandler(new SteamUnifiedMessages());
addHandler(new SteamScreenshots());
addHandler(new SteamNetworking());
addHandler(new SteamNotifications());
addHandler(new SteamUserStats());
addHandlerCore(new SteamFriends());
addHandlerCore(new SteamUser());
addHandlerCore(new SteamApps());
addHandlerCore(new SteamGameCoordinator());
addHandlerCore(new SteamGameServer());
addHandlerCore(new SteamMasterServer());
addHandlerCore(new SteamCloud());
addHandlerCore(new SteamWorkshop());
addHandlerCore(new SteamTrading());
addHandlerCore(new SteamUnifiedMessages());
addHandlerCore(new SteamScreenshots());
addHandlerCore(new SteamNetworking());
addHandlerCore(new SteamNotifications());
addHandlerCore(new SteamUserStats());

processStartTime = new Date();

Expand All @@ -113,6 +115,10 @@ public void addHandler(ClientMsgHandler handler) {
throw new IllegalArgumentException("A handler of type " + handler.getClass() + " is already registered.");
}

addHandlerCore(handler);
}

private void addHandlerCore(ClientMsgHandler handler) {
handler.setup(this);
handlers.put(handler.getClass(), handler);
}
Expand Down Expand Up @@ -354,11 +360,11 @@ protected boolean onClientMsgReceived(IPacketMsg packetMsg) {
dispatcher.accept(packetMsg);
}

for (Map.Entry<Class<? extends ClientMsgHandler>, ClientMsgHandler> entry : handlers.entrySet()) {
for (var handler : handlers.entrySet()) {
try {
entry.getValue().handleMsg(packetMsg);
handler.getValue().handleMsg(packetMsg);
} catch (Exception e) {
logger.debug("Unhandled exception from " + entry.getKey().getName() + " handlers", e);
logger.debug("Unhandled exception from " + handler.getKey().getName() + " handlers", e);
SteamClient.this.disconnect();
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package in.dragonbra.javasteam.steam.steamclient;

import in.dragonbra.javasteam.base.IPacketMsg;
import in.dragonbra.javasteam.handlers.ClientMsgHandler;
import in.dragonbra.javasteam.steam.handlers.steamapps.SteamApps;
import in.dragonbra.javasteam.steam.handlers.steamcloud.SteamCloud;
import in.dragonbra.javasteam.steam.handlers.steamfriends.SteamFriends;
import in.dragonbra.javasteam.steam.handlers.steamgamecoordinator.SteamGameCoordinator;
import in.dragonbra.javasteam.steam.handlers.steamgameserver.SteamGameServer;
import in.dragonbra.javasteam.steam.handlers.steammasterserver.SteamMasterServer;
import in.dragonbra.javasteam.steam.handlers.steamnetworking.SteamNetworking;
import in.dragonbra.javasteam.steam.handlers.steamnotifications.SteamNotifications;
import in.dragonbra.javasteam.steam.handlers.steamscreenshots.SteamScreenshots;
import in.dragonbra.javasteam.steam.handlers.steamtrading.SteamTrading;
import in.dragonbra.javasteam.steam.handlers.steamunifiedmessages.SteamUnifiedMessages;
import in.dragonbra.javasteam.steam.handlers.steamuser.SteamUser;
import in.dragonbra.javasteam.steam.handlers.steamuserstats.SteamUserStats;
import in.dragonbra.javasteam.steam.handlers.steamworkshop.SteamWorkshop;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class SteamClientTest {

private SteamClient client;

@BeforeEach
public void setUp() {
client = new SteamClient();
}

@Test
public void constructorSetsInitialHandlers() {
Assertions.assertNotNull(client.getHandler(SteamFriends.class));
Assertions.assertNotNull(client.getHandler(SteamUser.class));
Assertions.assertNotNull(client.getHandler(SteamApps.class));
Assertions.assertNotNull(client.getHandler(SteamGameCoordinator.class));
Assertions.assertNotNull(client.getHandler(SteamGameServer.class));
Assertions.assertNotNull(client.getHandler(SteamMasterServer.class));
Assertions.assertNotNull(client.getHandler(SteamCloud.class));
Assertions.assertNotNull(client.getHandler(SteamWorkshop.class));
Assertions.assertNotNull(client.getHandler(SteamTrading.class));
Assertions.assertNotNull(client.getHandler(SteamUnifiedMessages.class));
Assertions.assertNotNull(client.getHandler(SteamScreenshots.class));
Assertions.assertNotNull(client.getHandler(SteamNetworking.class));
Assertions.assertNotNull(client.getHandler(SteamNotifications.class));
Assertions.assertNotNull(client.getHandler(SteamUserStats.class));
}

@Test
public void addHandlerAddsHandler() {
var handler = new TestMsgHandler();
Assertions.assertNull(client.getHandler(handler.getClass()));

client.addHandler(handler);
Assertions.assertEquals(handler, client.getHandler(handler.getClass()));
}

@Test
public void removeHandlerRemovesHandler() {
client.addHandler(new TestMsgHandler());
Assertions.assertNotNull(client.getHandler(TestMsgHandler.class));

client.removeHandler(TestMsgHandler.class);
Assertions.assertNull(client.getHandler(TestMsgHandler.class));
}

@Test
public void removeHandlerRemovesHandlerByInstance() {
var handler = new TestMsgHandler();
client.addHandler(handler);
Assertions.assertNotNull(client.getHandler(TestMsgHandler.class));

client.removeHandler(handler);
Assertions.assertNull(client.getHandler(TestMsgHandler.class));
}

@Test
public void getNextJobIDSetsProcessIDToZero() {
var jobID = client.getNextJobID();

Assertions.assertEquals(0, jobID.getProcessID());
}

// @Test
// public void getNextJobIDFillsProcessStartTime() {
// var jobID = client.getNextJobID();
//
// var dateNow = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS);
// var processStartTime = Date.from(dateNow.toInstant(ZoneOffset.UTC));
//
// Assertions.assertEquals(processStartTime, jobID.getStartTime());
// }

@Test
public void getNextJobIDSetsBoxIDToZero() {
var jobID = client.getNextJobID();

Assertions.assertEquals(0, jobID.getBoxID());
}

static class TestMsgHandler extends ClientMsgHandler {
@Override
public void handleMsg(IPacketMsg packetMsg) {
// nothing
}
}
}

0 comments on commit 78b54c1

Please sign in to comment.