Fixed Disconnecting Online Status

This commit is contained in:
Julia Butenhoff 2022-07-19 18:58:18 -05:00
parent 91bf75c040
commit c0724a3e4e
4 changed files with 28 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import com.icedberries.UBFunkeysServer.ArkOne.Plugins.UserPlugin;
import com.icedberries.UBFunkeysServer.domain.User;
import com.icedberries.UBFunkeysServer.service.UserService;
import javagrinko.spring.tcp.Connection;
import javagrinko.spring.tcp.Server;
import javagrinko.spring.tcp.TcpController;
import javagrinko.spring.tcp.TcpHandler;
import org.springframework.beans.factory.annotation.Autowired;
@ -15,12 +16,16 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@TcpController
public class ArkOneController implements TcpHandler {
public static final String IP_ADDRESS = "127.0.0.1";
@Autowired
Server server;
// Services
@Autowired
private UserService userService;
@ -83,7 +88,7 @@ public class ArkOneController implements TcpHandler {
responses.add(userPlugin.ChangePhoneStatus(commandInfo, connection));
break;
case "u_abd":
responses.add(userPlugin.AddBuddy(commandInfo));
responses.add(userPlugin.AddBuddy(commandInfo, connection));
break;
case "u_abr":
responses.add(userPlugin.AddBuddyResponse(commandInfo, connection));
@ -161,13 +166,13 @@ public class ArkOneController implements TcpHandler {
@Override
public void disconnectEvent(Connection connection) {
User user = userService.findByConnectionId(connection.getClientIdentifier()).orElse(null);
User user = server.getConnectedUsers().get(connection.getClientIdentifier());
if (user != null) {
// Update the online status to offline and clear the connection ID
user.setIsOnline(0);
user.setChatStatus(0);
user.setConnectionId(null);
user.setConnectionId(new UUID(0L, 0L));
// Update the user in the DB
userService.save(user);

View File

@ -68,7 +68,6 @@ public class ArkOneSender {
public void SendToUser(UUID clientId, String message) {
for (Connection conn : server.getConnections()) {
if (conn.getClientIdentifier().equals(clientId)) {
System.out.println("Found");
try {
// Append a 0x00 to the end of the response
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

View File

@ -186,9 +186,10 @@ public class UserPlugin {
return ArkOneParser.RemoveXMLTag(doc);
}
public String AddBuddy(Element element) throws ParserConfigurationException, TransformerException {
public String AddBuddy(Element element, Connection connection) throws ParserConfigurationException, TransformerException {
// Try to get a buddy with the username passed
User buddy = userService.findByUsername(element.getAttribute("n")).orElse(null);
User thisUser = server.getConnectedUsers().get(connection.getClientIdentifier());
boolean fail = false;
boolean isAlreadyBuddy = false;
@ -224,6 +225,7 @@ public class UserPlugin {
fail = true;
}
rootElement.setAttribute("n", element.getAttribute("n"));
resp.appendChild(rootElement);
// Check if failed
@ -232,8 +234,9 @@ public class UserPlugin {
} else {
Document send = dBuilder.newDocument();
Element sendRoot = send.createElement("u_abr");
sendRoot.setAttribute("b", String.valueOf(buddy.getUUID()));
sendRoot.setAttribute("n", buddy.getUsername());
sendRoot.setAttribute("b", String.valueOf(thisUser.getUUID()));
sendRoot.setAttribute("n", thisUser.getUsername());
send.appendChild(sendRoot);
// Send the request to the other user
arkOneSender.SendToUser(buddy.getConnectionId(), ArkOneParser.RemoveXMLTag(send));
@ -263,11 +266,21 @@ public class UserPlugin {
rootElement.setAttribute("r", "0");
// Append to each buddy list the other users id
String modifiedBuddyListOne = buddy.getRawBuddyList() + "," + thisUser.getUUID();
String modifiedBuddyListOne;
if (buddy.getRawBuddyList() == null || buddy.getRawBuddyList().equals("")) {
modifiedBuddyListOne = String.valueOf(thisUser.getUUID());
} else {
modifiedBuddyListOne = buddy.getRawBuddyList() + "," + thisUser.getUUID();
}
buddy.setRawBuddyList(modifiedBuddyListOne);
userService.updateUserOnServer(buddy.getConnectionId(), buddy);
String modifiedBuddyListTwo = thisUser.getRawBuddyList() + "," + buddy.getUUID();
String modifiedBuddyListTwo;
if (thisUser.getRawBuddyList() == null || thisUser.getRawBuddyList().equals("")) {
modifiedBuddyListTwo = String.valueOf(buddy.getUUID());
} else {
modifiedBuddyListTwo = thisUser.getRawBuddyList() + "," + buddy.getUUID();
}
thisUser.setRawBuddyList(modifiedBuddyListTwo);
userService.updateUserOnServer(thisUser.getConnectionId(), thisUser);
@ -279,7 +292,7 @@ public class UserPlugin {
// If accepted -> Build message to send to other original user
Document send = dBuilder.newDocument();
Element sendRootElement = resp.createElement("u_abd");
Element sendRootElement = send.createElement("u_abd");
if (element.getAttribute("r").equals("1")) {
sendRootElement.setAttribute("r", "0");
sendRootElement.setAttribute("ph", String.valueOf(thisUser.getPhoneStatus()));
@ -297,6 +310,7 @@ public class UserPlugin {
arkOneSender.SendToUser(buddy.getConnectionId(), ArkOneParser.RemoveXMLTag(send));
rootElement.setAttribute("n", element.getAttribute("n"));
resp.appendChild(rootElement);
// Send response
if (accepted) {

View File

@ -130,9 +130,6 @@ public class TcpServer implements Server, Connection.Listener {
throws InvocationTargetException, IllegalAccessException {
logger.info("Disconnect! Ip: " + connection.getAddress().getCanonicalHostName() + ".");
// Remove the UUID
connection.setClientIdentifier(null);
// Remove it from all connections
connections.remove(connection);