Skip to content

Commit 137405a

Browse files
committed
rework(network): protocol message and packet inheritance
Most notably, separate RoomMessage from ProtocolMessage to indicate what is sent where.
1 parent af28b5d commit 137405a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+205
-195
lines changed

helpers/test-client/src/sc/TestClient.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import sc.api.plugins.IGamePlugin;
1010
import sc.framework.plugins.Player;
1111
import sc.networking.clients.XStreamClient;
12+
import sc.protocol.CloseConnection;
13+
import sc.protocol.ProtocolPacket;
14+
import sc.protocol.RoomPacket;
1215
import sc.protocol.requests.*;
1316
import sc.protocol.responses.*;
1417
import sc.server.Configuration;
@@ -185,7 +188,7 @@ public TestClient(String host, int port, int totalTests) throws IOException {
185188
private boolean gameProgressing = false;
186189

187190
@Override
188-
protected void onObject(@NotNull ProtocolMessage message) {
191+
protected void onObject(@NotNull ProtocolPacket message) {
189192
if (message == null) {
190193
logger.warn("Received null message");
191194
return;

plugin/src/client/sc/plugin2021/AbstractClient.kt

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
package sc.plugin2021
22

33
import org.slf4j.LoggerFactory
4-
import sc.api.plugins.IGamePlugin
54
import sc.api.plugins.IGameState
65
import sc.framework.plugins.protocol.MoveRequest
76
import sc.networking.clients.AbstractLobbyClientListener
87
import sc.networking.clients.IControllableGame
98
import sc.networking.clients.ILobbyClientListener
109
import sc.networking.clients.LobbyClient
10+
import sc.protocol.RoomMessage
1111
import sc.protocol.responses.GamePreparedResponse
12-
import sc.protocol.responses.ProtocolErrorMessage
13-
import sc.protocol.responses.ProtocolMessage
12+
import sc.protocol.responses.ErrorMessage
1413
import sc.shared.GameResult
1514
import sc.shared.WelcomeMessage
1615
import java.net.ConnectException
@@ -57,10 +56,14 @@ abstract class AbstractClient(
5756
client.observe(handle.roomId)
5857

5958
/** Called for any new message sent to the game room, e.g., move requests. */
60-
override fun onRoomMessage(roomId: String, data: ProtocolMessage) {
59+
override fun onRoomMessage(roomId: String, data: RoomMessage) {
6160
when(data) {
6261
is MoveRequest -> handler?.onRequestAction()
6362
is WelcomeMessage -> team = Team.valueOf(data.color.toUpperCase())
63+
is ErrorMessage -> {
64+
logger.debug("onError: Client $this received error ${data.message} in $roomId")
65+
this.error = data.message
66+
}
6467
}
6568
this.roomId = roomId
6669
}
@@ -69,12 +72,6 @@ abstract class AbstractClient(
6972
fun sendMove(move: Move) =
7073
client.sendMessageToRoom(roomId, move)
7174

72-
/** Called when an erroneous message is sent to the room. */
73-
override fun onError(roomId: String?, error: ProtocolErrorMessage) {
74-
logger.debug("onError: Client $this received error ${error.message} in $roomId")
75-
this.error = error.message
76-
}
77-
7875
/**
7976
* Called when game state has been received.
8077
* Happens after a client made a move.

plugin/src/server/sc/plugin2021/Game.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import sc.plugin2021.util.Constants
88
import sc.plugin2021.util.GameRuleLogic
99
import sc.plugin2021.util.MoveMistake
1010
import sc.plugin2021.util.WinReason
11-
import sc.protocol.responses.ProtocolMessage
11+
import sc.protocol.RoomMessage
1212
import sc.shared.InvalidMoveException
1313
import sc.shared.PlayerScore
1414
import sc.shared.ScoreCause
@@ -137,7 +137,7 @@ class Game(override val currentState: GameState = GameState()): AbstractGame<Pla
137137
ActionTimeout(true, Constants.HARD_TIMEOUT, Constants.SOFT_TIMEOUT)
138138

139139
@Throws(InvalidMoveException::class)
140-
override fun onRoundBasedAction(fromPlayer: Player, data: ProtocolMessage) {
140+
override fun onRoundBasedAction(fromPlayer: Player, data: RoomMessage) {
141141
if (data !is Move)
142142
throw InvalidMoveException(MoveMistake.INVALID_FORMAT)
143143

sdk/src/framework/sc/shared/GameResult.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package sc.shared
33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamImplicit
55
import sc.framework.plugins.Player
6-
import sc.protocol.responses.ProtocolMessage
6+
import sc.protocol.RoomMessage
77

88
/**
99
* Das Endergebnis eines Spiels.
@@ -17,7 +17,7 @@ data class GameResult(
1717
val scores: List<PlayerScore>,
1818
@XStreamImplicit(itemFieldName = "winner")
1919
val winners: List<Player>?
20-
): ProtocolMessage {
20+
): RoomMessage {
2121

2222
val isRegular: Boolean
2323
get() = scores.all { it.cause == ScoreCause.REGULAR }

sdk/src/framework/sc/shared/WelcomeMessage.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package sc.shared
33
import com.thoughtworks.xstream.annotations.XStreamAlias
44
import com.thoughtworks.xstream.annotations.XStreamAsAttribute
55
import sc.api.plugins.ITeam
6-
import sc.protocol.responses.ProtocolMessage
6+
import sc.protocol.RoomMessage
77

88
/** Nachricht, die zu Beginn eines Spiels an einen Client geschickt wird, um ihm seine Spielerfarbe mitzuteilen. */
99
@Suppress("DataClassPrivateConstructor")
1010
@XStreamAlias(value = "welcomeMessage")
1111
data class WelcomeMessage private constructor(
1212
@XStreamAsAttribute val color: String
13-
): ProtocolMessage {
13+
): RoomMessage {
1414
constructor(color: ITeam): this(color.name)
1515
}

sdk/src/server-api/sc/api/plugins/IGameInstance.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import sc.api.plugins.exceptions.GameLogicException
44
import sc.api.plugins.exceptions.TooManyPlayersException
55
import sc.api.plugins.host.IGameListener
66
import sc.framework.plugins.Player
7-
import sc.protocol.responses.ProtocolMessage
7+
import sc.protocol.RoomMessage
88
import sc.shared.InvalidMoveException
99
import sc.shared.PlayerScore
1010
import sc.shared.ScoreCause
@@ -30,7 +30,7 @@ interface IGameInstance {
3030
* @throws InvalidMoveException if the received move violates the rules
3131
*/
3232
@Throws(GameLogicException::class, InvalidMoveException::class)
33-
fun onAction(fromPlayer: Player, data: ProtocolMessage)
33+
fun onAction(fromPlayer: Player, data: RoomMessage)
3434
fun addGameListener(listener: IGameListener)
3535
fun removeGameListener(listener: IGameListener)
3636

sdk/src/server-api/sc/api/plugins/IGameState.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package sc.api.plugins
22

3-
import sc.protocol.responses.ProtocolMessage
3+
import sc.protocol.RoomMessage
44

55
/**
66
* Ein `GameState` beinhaltet alle Informationen, die den Spielstand zu
@@ -25,7 +25,7 @@ import sc.protocol.responses.ProtocolMessage
2525
*
2626
* @author Niklas, Sören, Janek
2727
*/
28-
interface IGameState : ProtocolMessage, Cloneable {
28+
interface IGameState : RoomMessage, Cloneable {
2929
/** Aktuelle Zugzahl */
3030
val turn: Int
3131

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
package sc.api.plugins
22

3-
import sc.protocol.responses.ProtocolMessage
3+
import sc.protocol.RoomMessage
44

5-
interface IMove: ProtocolMessage
5+
interface IMove: RoomMessage
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package sc.api.plugins.exceptions
22

33
import sc.framework.plugins.Player
4-
import sc.protocol.responses.ProtocolMessage
4+
import sc.protocol.RoomMessage
55

66
data class NotYourTurnException(
77
val expected: Player?,
88
val actual: Player,
9-
val data: ProtocolMessage):
10-
GameLogicException("It's not your turn yet; expected: $expected, got $actual (msg was $data).")
9+
val data: RoomMessage
10+
): GameLogicException("It's not your turn yet; expected: $expected, got $actual (msg was $data).")

sdk/src/server-api/sc/api/plugins/host/GameLoader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import sc.api.plugins.IGameState;
66
import sc.networking.clients.GameLoaderClient;
77
import sc.networking.clients.IHistoryListener;
8-
import sc.protocol.responses.ProtocolErrorMessage;
8+
import sc.protocol.responses.ErrorMessage;
99
import sc.shared.GameResult;
1010

1111
import java.io.File;
@@ -68,7 +68,7 @@ public Object loadGame(InputStream file) throws IOException {
6868
}
6969

7070
@Override
71-
public void onGameError(String roomId, ProtocolErrorMessage error) {
71+
public void onGameError(String roomId, ErrorMessage error) {
7272
}
7373

7474
@Override

0 commit comments

Comments
 (0)