From a43d1f0013c7f2cb7595a1fe1a5cfd99a441d0b4 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 11 May 2016 17:39:14 +0200 Subject: Implement delay management --- src/ch/epfl/xblast/server/Server.java | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'src') 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.*; public class Server { public static final int DEFAULT_PORT = 2016; + + private static final long REFRESH_RATE = (long) (50 * 1E6); // nanosecond private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; private static class Channel { @@ -133,6 +135,14 @@ public class Server { } + private static void delay(long ns) { + try { + Thread.sleep((long) (ns / 1E6), (int) (ns % 1E6)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + private final Channel channel; private final int expectedClients; @@ -160,15 +170,11 @@ public class Server { GameState gameState = GameState.DEFAULT_GAME_STATE; while (!gameState.isGameOver()) { + long computationStartTime = System.nanoTime(); + gameState = updateGameState(updateGameState(gameState)); - // TODO: send updated game state to clients - try { - Thread.sleep(10000); - // TODO: adapt sleeping time - } catch (InterruptedException e) { - e.printStackTrace(); - } + delay(REFRESH_RATE - (computationStartTime - System.nanoTime())); } } -- cgit v1.2.3