summaryrefslogtreecommitdiff
path: root/server/src/kz/ilotterytea/maxon/MaxonServer.java
blob: 91ba049f9fc18fca5ba4309d8d9e49bcfa55e43a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package kz.ilotterytea.maxon;

import kz.ilotterytea.maxon.shared.Identity;
import kz.ilotterytea.maxon.shared.exceptions.PlayerKickException;
import org.java_websocket.WebSocket;
import org.java_websocket.framing.CloseFrame;
import org.java_websocket.handshake.ClientHandshake;
import org.java_websocket.server.WebSocketServer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.ObjectInputStream;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Optional;

public class MaxonServer extends WebSocketServer {
    private static MaxonServer instance;

    private final Logger log;
    private final ArrayList<PlayerConnection> connections;

    private MaxonServer() {
        super(new InetSocketAddress(31084));
        this.log = LoggerFactory.getLogger(MaxonServer.class);
        this.connections = new ArrayList<>();
    }

    @Override
    public void onOpen(WebSocket conn, ClientHandshake handshake) {
        PlayerConnection connection = new PlayerConnection(conn);
        log.info("{} ({}) connected!", connection, conn.getRemoteSocketAddress().getAddress().getHostAddress());
        this.connections.add(connection);
    }

    @Override
    public void onClose(WebSocket conn, int code, String reason, boolean remote) {
        Optional<PlayerConnection> connection = this.connections.stream().filter((x) -> x.getConnection().equals(conn)).findFirst();
        if (connection.isPresent()) {
            log.info("{} has left! Reason: {} {}", connection.get(), code, reason);
            this.connections.remove(connection.get());
        } else {
            log.info("Unknown connection was closed! Reason: {} {}", code, reason);
        }
    }

    @Override
    public void onMessage(WebSocket conn, String message) {
        this.connections.removeIf((x) -> x.getConnection().equals(conn));
        conn.send("Invalid input.");
        conn.close(CloseFrame.UNEXPECTED_CONDITION);
    }

    @Override
    public void onMessage(WebSocket conn, ByteBuffer message) {
        Optional<PlayerConnection> playerConnection = this.connections.stream().filter((x) -> x.getConnection().equals(conn)).findFirst();

        if (playerConnection.isEmpty()) {
            conn.close(5001, "Your PlayerConnection was not found!");
            return;
        }

        PlayerConnection c = playerConnection.get();

        try {
            // Deserialize the object
            ByteArrayInputStream bais = new ByteArrayInputStream(message.array());
            ObjectInputStream ois = new ObjectInputStream(bais);
            Object obj = ois.readObject();

            if (obj instanceof Identity identity) ServerHandlers.handleIdentity(c, identity);
            else kickConnection(c, PlayerKickException.internalServerError());
        } catch (PlayerKickException e) {
            kickConnection(c, e);
        } catch (Exception e) {
            log.error("An exception was thrown while processing message", e);
            kickConnection(c, PlayerKickException.internalServerError());
        }
    }

    @Override
    public void onError(WebSocket conn, Exception ex) {
        log.error("Something went wrong", ex);
    }

    @Override
    public void onStart() {
        log.info("Running the server on port {}!", getPort());
        setConnectionLostTimeout(0);
        setConnectionLostTimeout(100);
    }

    public void kickConnection(PlayerConnection connection, PlayerKickException e) {
        connection.send(e);
        connection.getConnection().close();
        this.connections.remove(connection);
        log.debug("Kicked out {}! Reason: {}", connection, e.getMessage());
    }

    public static MaxonServer getInstance() {
        if (instance == null) instance = new MaxonServer();
        return instance;
    }

    public ArrayList<PlayerConnection> getPlayerConnections() {
        return connections;
    }
}