Skip to content

Commit 8ad7bf5

Browse files
committed
Add debug logging and fix websocket dying
1 parent 7635d24 commit 8ad7bf5

5 files changed

Lines changed: 37 additions & 8 deletions

File tree

bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneConfig.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class StandaloneConfig {
88
public SessionConfig sessionConfig;
99
@JsonProperty("friend-sync")
1010
public FriendSyncConfig friendSyncConfig;
11+
@JsonProperty("debug-log")
12+
public boolean debugLog;
1113

1214
public static class SessionConfig {
1315
@JsonProperty("update-interval")

bootstrap/standalone/src/main/java/com/rtm516/mcxboxbroadcast/bootstrap/standalone/StandaloneMain.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import com.rtm516.mcxboxbroadcast.core.models.FollowerResponse;
1313
import org.java_websocket.util.NamedThreadFactory;
1414
import org.slf4j.LoggerFactory;
15+
import org.slf4j.impl.SimpleLogger;
1516

1617
import java.io.File;
1718
import java.io.FileOutputStream;
1819
import java.io.IOException;
1920
import java.io.InputStream;
21+
import java.lang.reflect.Field;
2022
import java.net.InetSocketAddress;
2123
import java.util.concurrent.ExecutionException;
2224
import java.util.concurrent.Executors;
@@ -32,8 +34,6 @@ public static void main(String[] args) throws Exception {
3234

3335
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(2, new NamedThreadFactory("Scheduled Thread"));
3436

35-
SessionManager sessionManager = new SessionManager("./cache", logger);
36-
3737
String configFileName = "config.yml";
3838
File configFile = new File(configFileName);
3939

@@ -63,6 +63,15 @@ public static void main(String[] args) throws Exception {
6363
return;
6464
}
6565

66+
// Use reflection to put the logger in debug mode
67+
if (config.debugLog) {
68+
Field currentLogLevel = SimpleLogger.class.getDeclaredField("currentLogLevel");
69+
currentLogLevel.setAccessible(true);
70+
currentLogLevel.set(LoggerFactory.getLogger(StandaloneMain.class), 10);
71+
}
72+
73+
SessionManager sessionManager = new SessionManager("./cache", logger);
74+
6675
SessionInfo sessionInfo = config.sessionConfig.sessionInfo;
6776

6877
// Sync the session info from the server if needed
@@ -78,6 +87,10 @@ public static void main(String[] args) throws Exception {
7887
updateSessionInfo(sessionInfo);
7988

8089
try {
90+
// Make sure the connection is still active
91+
sessionManager.checkConnection();
92+
93+
// Update the session
8194
sessionManager.updateSession(sessionInfo);
8295
logger.info("Updated session!");
8396
} catch (SessionUpdateException e) {
@@ -88,7 +101,7 @@ public static void main(String[] args) throws Exception {
88101
if (config.friendSyncConfig.autoFollow || config.friendSyncConfig.autoUnfollow) {
89102
scheduledThreadPool.scheduleWithFixedDelay(() -> {
90103
try {
91-
for (FollowerResponse.Person person : sessionManager.getXboxFriends(true, true)) {
104+
for (FollowerResponse.Person person : sessionManager.getXboxFriends(config.friendSyncConfig.autoFollow, config.friendSyncConfig.autoUnfollow)) {
92105
// Follow the person back
93106
if (config.friendSyncConfig.autoFollow && person.isFollowingCaller && !person.isFollowedByCaller) {
94107
logger.info("Added " + person.displayName + " (" + person.xuid + ") as a friend");

bootstrap/standalone/src/main/resources/config.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,7 @@ friend-sync:
2828
auto-follow: true
2929

3030
# Should we automatically unfollow people that no longer follow us
31-
auto-unfollow: true
31+
auto-unfollow: true
32+
33+
# Enable debug logs
34+
debug-log: false

core/src/main/java/com/rtm516/mcxboxbroadcast/core/RtaWebsocketClient.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@
1111
*/
1212
public class RtaWebsocketClient extends WebSocketClient {
1313
private String connectionId;
14+
private final Logger logger;
15+
private boolean firstConnectionId = true;
1416

1517
/**
1618
* Create a new websocket and add the Authorization header
1719
*
1820
* @param authenticationToken The token to use for authentication
1921
*/
20-
public RtaWebsocketClient(String authenticationToken) {
22+
public RtaWebsocketClient(String authenticationToken, Logger logger) {
2123
super(Constants.RTA_WEBSOCKET);
2224
addHeader("Authorization", authenticationToken);
25+
this.logger = logger;
2326
}
2427

2528
/**
@@ -53,12 +56,15 @@ public void onOpen(ServerHandshake serverHandshake) {
5356
*/
5457
@Override
5558
public void onMessage(String message) {
56-
if (message.contains("ConnectionId")) {
59+
if (message.contains("ConnectionId") && firstConnectionId) {
5760
try {
5861
Object[] parts = Constants.OBJECT_MAPPER.readValue(message, Object[].class);
5962
connectionId = ((Map<String, String>) parts[4]).get("ConnectionId");
63+
firstConnectionId = false;
6064
} catch (JsonProcessingException ignored) {
6165
}
66+
} else {
67+
logger.debug("Websocket message: " + message);
6268
}
6369
}
6470

core/src/main/java/com/rtm516/mcxboxbroadcast/core/SessionManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ private void updateSession() throws SessionUpdateException {
239239
}
240240

241241
if (createSessionResponse.statusCode() != 200 && createSessionResponse.statusCode() != 201) {
242+
logger.debug("Got session response: " + createSessionResponse.body());
242243
throw new SessionUpdateException("Unable to update session information, got status " + createSessionResponse.statusCode() + " trying to update");
243244
}
244245
}
@@ -277,9 +278,11 @@ public List<FollowerResponse.Person> getXboxFriends(boolean includeFollowing, bo
277278
.GET()
278279
.build();
279280

281+
String lastResponse = "";
280282
try {
281283
// Get the list of friends from the api
282-
FollowerResponse xboxFollowerResponse = Constants.OBJECT_MAPPER.readValue(httpClient.send(xboxFollowersRequest, HttpResponse.BodyHandlers.ofString()).body(), FollowerResponse.class);
284+
lastResponse = httpClient.send(xboxFollowersRequest, HttpResponse.BodyHandlers.ofString()).body();
285+
FollowerResponse xboxFollowerResponse = Constants.OBJECT_MAPPER.readValue(lastResponse, FollowerResponse.class);
283286

284287
// Parse through the returned list to make sure we are friends and
285288
// add them to the list to return
@@ -291,6 +294,7 @@ public List<FollowerResponse.Person> getXboxFriends(boolean includeFollowing, bo
291294
}
292295
}
293296
} catch (IOException | InterruptedException e) {
297+
logger.debug("Follower request response: " + lastResponse);
294298
throw new XboxFriendsException(e.getMessage());
295299
}
296300

@@ -317,6 +321,7 @@ public List<FollowerResponse.Person> getXboxFriends(boolean includeFollowing, bo
317321
}
318322
}
319323
} catch (IOException | InterruptedException e) {
324+
logger.debug("Social request response: " + lastResponse);
320325
throw new XboxFriendsException(e.getMessage());
321326
}
322327
}
@@ -408,7 +413,7 @@ private Future<String> waitForConnectionId() {
408413
* @param token The authentication token to use
409414
*/
410415
private void setupWebsocket(String token) {
411-
rtaWebsocket = new RtaWebsocketClient(token);
416+
rtaWebsocket = new RtaWebsocketClient(token, logger);
412417
rtaWebsocket.connect();
413418
}
414419
}

0 commit comments

Comments
 (0)