diff options
author | Pacien TRAN-GIRARD | 2016-05-11 17:39:14 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-11 17:39:14 +0200 |
commit | a43d1f0013c7f2cb7595a1fe1a5cfd99a441d0b4 (patch) | |
tree | be3d5ae3b065da758c272a68dbc1138a3c72a81f /src | |
parent | e4b599c9bb3f2c1bfcebadc832f9d291ab8e4e93 (diff) | |
download | xblast-a43d1f0013c7f2cb7595a1fe1a5cfd99a441d0b4.tar.gz |
Implement delay management
Diffstat (limited to 'src')
-rw-r--r-- | src/ch/epfl/xblast/server/Server.java | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java index 0989d55..6d8db2c 100644 --- a/src/ch/epfl/xblast/server/Server.java +++ b/src/ch/epfl/xblast/server/Server.java | |||
@@ -20,6 +20,8 @@ import java.util.*; | |||
20 | public class Server { | 20 | public class Server { |
21 | 21 | ||
22 | public static final int DEFAULT_PORT = 2016; | 22 | public static final int DEFAULT_PORT = 2016; |
23 | |||
24 | private static final long REFRESH_RATE = (long) (50 * 1E6); // nanosecond | ||
23 | private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; | 25 | private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; |
24 | 26 | ||
25 | private static class Channel { | 27 | private static class Channel { |
@@ -133,6 +135,14 @@ public class Server { | |||
133 | 135 | ||
134 | } | 136 | } |
135 | 137 | ||
138 | private static void delay(long ns) { | ||
139 | try { | ||
140 | Thread.sleep((long) (ns / 1E6), (int) (ns % 1E6)); | ||
141 | } catch (InterruptedException e) { | ||
142 | e.printStackTrace(); | ||
143 | } | ||
144 | } | ||
145 | |||
136 | private final Channel channel; | 146 | private final Channel channel; |
137 | private final int expectedClients; | 147 | private final int expectedClients; |
138 | 148 | ||
@@ -160,15 +170,11 @@ public class Server { | |||
160 | GameState gameState = GameState.DEFAULT_GAME_STATE; | 170 | GameState gameState = GameState.DEFAULT_GAME_STATE; |
161 | 171 | ||
162 | while (!gameState.isGameOver()) { | 172 | while (!gameState.isGameOver()) { |
173 | long computationStartTime = System.nanoTime(); | ||
174 | |||
163 | gameState = updateGameState(updateGameState(gameState)); | 175 | gameState = updateGameState(updateGameState(gameState)); |
164 | // TODO: send updated game state to clients | ||
165 | 176 | ||
166 | try { | 177 | delay(REFRESH_RATE - (computationStartTime - System.nanoTime())); |
167 | Thread.sleep(10000); | ||
168 | // TODO: adapt sleeping time | ||
169 | } catch (InterruptedException e) { | ||
170 | e.printStackTrace(); | ||
171 | } | ||
172 | } | 178 | } |
173 | } | 179 | } |
174 | 180 | ||