From 21b25ef18f9a4c11cf0066b2d750a36e7fad2533 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Wed, 11 May 2016 16:05:05 +0200 Subject: Refactor server class --- src/ch/epfl/xblast/server/Server.java | 177 ++++++++++++++++++++-------------- 1 file changed, 103 insertions(+), 74 deletions(-) (limited to 'src/ch/epfl') diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java index 58f3c07..6d1d62f 100644 --- a/src/ch/epfl/xblast/server/Server.java +++ b/src/ch/epfl/xblast/server/Server.java @@ -22,119 +22,148 @@ public class Server { public static final int DEFAULT_PORT = 2016; private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; - private static InetSocketAddress listeningInterface(String host, int port) { - if (Objects.isNull(host)) - return new InetSocketAddress(port); - else - return new InetSocketAddress(host, port); - } + private static class Channel { - private static DatagramChannel openChannel(InetSocketAddress iface) { - try { - DatagramChannel chan = DatagramChannel.open(StandardProtocolFamily.INET); - chan.bind(iface); - return chan; - } catch (IOException e) { - e.printStackTrace(); - System.exit(1); - return null; + private static InetSocketAddress listeningInterface(String host, int port) { + if (Objects.isNull(host)) + return new InetSocketAddress(port); + else + return new InetSocketAddress(host, port); } - } - private static Optional> receiveByte(DatagramChannel chan, boolean block) { - try { - ByteBuffer buf = ByteBuffer.allocate(1); - chan.configureBlocking(block); - SocketAddress client = chan.receive(buf); + private static DatagramChannel openChannel(InetSocketAddress iface) { + try { + DatagramChannel chan = DatagramChannel.open(StandardProtocolFamily.INET); + chan.bind(iface); + return chan; + } catch (IOException e) { + e.printStackTrace(); + System.exit(1); + return null; + } + } - if (Objects.isNull(client) || buf.position() == 0) - throw new IOException(); + private final DatagramChannel channel; - return Optional.of(new AbstractMap.SimpleImmutableEntry<>(client, buf.get(0))); - } catch (IOException e) { - return Optional.empty(); + Channel(InetSocketAddress iface) { + this.channel = openChannel(iface); } - } - private static Optional> receiveAction(DatagramChannel chan, boolean block) { - try { - Map.Entry actionByte = receiveByte(chan, block).get(); - PlayerAction playerAction = PlayerAction.fromByte(actionByte.getValue()); - return Optional.of(new AbstractMap.SimpleImmutableEntry<>(actionByte.getKey(), playerAction)); - } catch (NoSuchElementException | IllegalArgumentException e) { - return Optional.empty(); + Channel(String host, Integer port) { + this(listeningInterface(host, Optional.ofNullable(port).orElse(DEFAULT_PORT))); + } + + void closeChannel() { + try { + this.channel.close(); + } catch (IOException e) { + e.printStackTrace(); + } } - } - private static Map.Entry acceptAction(DatagramChannel chan) { - Optional> action; + List acceptRegistrations(int registrations) { + List clients = new ArrayList<>(registrations); - do { - action = receiveAction(chan, true); - } while (!action.isPresent()); + while (clients.size() < registrations) { + SocketAddress client = this.acceptRegistration(); + if (!clients.contains(client)) + clients.add(client); + } - return action.get(); - } + return Collections.unmodifiableList(clients); + } - private static Map collectActions(DatagramChannel chan) { - Map actions = new HashMap<>(); - Optional> action; + Map collectActions() { + Map actions = new HashMap<>(); + Optional> action; - while (true) { - action = receiveAction(chan, false); - if (!action.isPresent()) break; - actions.put(action.get().getKey(), action.get().getValue()); + while (true) { + action = this.receiveAction(false); + if (!action.isPresent()) break; + actions.put(action.get().getKey(), action.get().getValue()); + } + + return Collections.unmodifiableMap(actions); } - return Collections.unmodifiableMap(actions); - } + private Optional> receiveByte(boolean block) { + try { + ByteBuffer buf = ByteBuffer.allocate(1); + this.channel.configureBlocking(block); + SocketAddress client = this.channel.receive(buf); - private static SocketAddress acceptRegistration(DatagramChannel chan) { - Map.Entry clientAction; + if (Objects.isNull(client) || buf.position() == 0) + throw new IOException(); - do { - clientAction = acceptAction(chan); - } while (clientAction.getValue() != PlayerAction.JOIN_GAME); + return Optional.of(new AbstractMap.SimpleImmutableEntry<>(client, buf.get(0))); + } catch (IOException e) { + return Optional.empty(); + } + } - return clientAction.getKey(); - } + private Optional> receiveAction(boolean block) { + try { + Map.Entry actionByte = this.receiveByte(block).get(); + PlayerAction playerAction = PlayerAction.fromByte(actionByte.getValue()); + return Optional.of(new AbstractMap.SimpleImmutableEntry<>(actionByte.getKey(), playerAction)); + } catch (NoSuchElementException | IllegalArgumentException e) { + return Optional.empty(); + } + } + + private Map.Entry acceptAction() { + Optional> action; - private static List acceptRegistrations(DatagramChannel chan, int registrations) { - List clients = new ArrayList<>(registrations); + do { + action = this.receiveAction(true); + } while (!action.isPresent()); - while (clients.size() < registrations) { - SocketAddress client = acceptRegistration(chan); - if (!clients.contains(client)) - clients.add(client); + return action.get(); + } + + private SocketAddress acceptRegistration() { + Map.Entry clientAction; + + do { + clientAction = this.acceptAction(); + } while (clientAction.getValue() != PlayerAction.JOIN_GAME); + + return clientAction.getKey(); } - return Collections.unmodifiableList(clients); } - private final InetSocketAddress iface; + private final Channel channel; private final int expectedClients; - private Map registeredClients; + + private Map registeredClientsMap; + private Map playersAddressMap; public Server(String iface, Integer port, Integer expectedClients) { - this.iface = listeningInterface(iface, Optional.ofNullable(port).orElse(DEFAULT_PORT)); + this.channel = new Channel(iface, port); this.expectedClients = Optional.ofNullable(expectedClients).orElse(DEFAULT_EXPECTED_CLIENTS); } public void run() { - DatagramChannel chan = openChannel(this.iface); - List clients = acceptRegistrations(chan, this.expectedClients); - this.registeredClients = Lists.linearAdjustedMap(clients, Arrays.asList(PlayerID.values())); + this.acceptClientRegistrations(); + this.runGame(); + this.channel.closeChannel(); + } - System.out.println(this.registeredClients); + private void acceptClientRegistrations() { + List clients = this.channel.acceptRegistrations(this.expectedClients); + this.registeredClientsMap = Lists.linearAdjustedMap(clients, Arrays.asList(PlayerID.values())); + this.playersAddressMap = Lists.invertMap(this.registeredClientsMap); + } + private void runGame() { try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } - Map actions = collectActions(chan); - System.out.println(actions); + Map actions = this.channel.collectActions(); } } -- cgit v1.2.3