aboutsummaryrefslogtreecommitdiff
path: root/src/ch/epfl
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-11 18:44:21 +0200
committerPacien TRAN-GIRARD2016-05-11 18:44:21 +0200
commit33bc4cab3f2a00db14e6d055d6cd55781c9dd2b4 (patch)
tree70b5207530221f2ab00e43272cc38d25fba11469 /src/ch/epfl
parent4ca823eb709a00ff93c1fdeae7f2bab6865cb73f (diff)
downloadxblast-33bc4cab3f2a00db14e6d055d6cd55781c9dd2b4.tar.gz
Implement game state broadcasting
Diffstat (limited to 'src/ch/epfl')
-rw-r--r--src/ch/epfl/xblast/server/Server.java31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java
index 6d8db2c..4929dd9 100644
--- a/src/ch/epfl/xblast/server/Server.java
+++ b/src/ch/epfl/xblast/server/Server.java
@@ -3,6 +3,7 @@ package ch.epfl.xblast.server;
3import ch.epfl.xblast.Lists; 3import ch.epfl.xblast.Lists;
4import ch.epfl.xblast.PlayerAction; 4import ch.epfl.xblast.PlayerAction;
5import ch.epfl.xblast.PlayerID; 5import ch.epfl.xblast.PlayerID;
6import ch.epfl.xblast.server.painter.BoardPainter;
6 7
7import java.io.IOException; 8import java.io.IOException;
8import java.net.InetSocketAddress; 9import java.net.InetSocketAddress;
@@ -26,6 +27,15 @@ public class Server {
26 27
27 private static class Channel { 28 private static class Channel {
28 29
30 private static byte[] toByteArray(List<Byte> l) {
31 byte[] arr = new byte[l.size()];
32
33 for (int i = 0; i < l.size(); ++i)
34 arr[i] = l.get(i);
35
36 return arr;
37 }
38
29 private static InetSocketAddress listeningInterface(String host, int port) { 39 private static InetSocketAddress listeningInterface(String host, int port) {
30 if (Objects.isNull(host)) 40 if (Objects.isNull(host))
31 return new InetSocketAddress(port); 41 return new InetSocketAddress(port);
@@ -88,6 +98,15 @@ public class Server {
88 return Collections.unmodifiableMap(actions); 98 return Collections.unmodifiableMap(actions);
89 } 99 }
90 100
101 void send(List<Byte> content, SocketAddress dst) {
102 try {
103 ByteBuffer buf = ByteBuffer.wrap(toByteArray(content));
104 this.channel.send(buf, dst);
105 } catch (IOException e) {
106 e.printStackTrace();
107 }
108 }
109
91 private Optional<Map.Entry<SocketAddress, Byte>> receiveByte(boolean block) { 110 private Optional<Map.Entry<SocketAddress, Byte>> receiveByte(boolean block) {
92 try { 111 try {
93 ByteBuffer buf = ByteBuffer.allocate(1); 112 ByteBuffer buf = ByteBuffer.allocate(1);
@@ -171,9 +190,8 @@ public class Server {
171 190
172 while (!gameState.isGameOver()) { 191 while (!gameState.isGameOver()) {
173 long computationStartTime = System.nanoTime(); 192 long computationStartTime = System.nanoTime();
174
175 gameState = updateGameState(updateGameState(gameState)); 193 gameState = updateGameState(updateGameState(gameState));
176 194 broadcastGameState(gameState);
177 delay(REFRESH_RATE - (computationStartTime - System.nanoTime())); 195 delay(REFRESH_RATE - (computationStartTime - System.nanoTime()));
178 } 196 }
179 } 197 }
@@ -183,4 +201,13 @@ public class Server {
183 return gs.next(events); 201 return gs.next(events);
184 } 202 }
185 203
204 private void broadcastGameState(GameState gs) {
205 List<Byte> serialized = GameStateSerializer.serialize(BoardPainter.DEFAULT_BOARD_PAINTER, gs);
206
207 for (Map.Entry<SocketAddress, PlayerID> client : this.registeredClientsMap.entrySet()) {
208 byte activePlayer = Objects.nonNull(client) ? client.getValue().toByte() : PlayerID.OBSERVER;
209 this.channel.send(Lists.prepended(serialized, activePlayer), client.getKey());
210 }
211 }
212
186} 213}