aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/ch/epfl/xblast/server/Server.java83
1 files changed, 79 insertions, 4 deletions
diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java
index 76f332e..10d5a85 100644
--- a/src/ch/epfl/xblast/server/Server.java
+++ b/src/ch/epfl/xblast/server/Server.java
@@ -1,9 +1,16 @@
1package ch.epfl.xblast.server; 1package ch.epfl.xblast.server;
2 2
3import ch.epfl.xblast.Lists;
4import ch.epfl.xblast.PlayerAction;
3import ch.epfl.xblast.PlayerID; 5import ch.epfl.xblast.PlayerID;
4 6
7import java.io.IOException;
5import java.net.InetSocketAddress; 8import java.net.InetSocketAddress;
6import java.util.Optional; 9import java.net.SocketAddress;
10import java.net.StandardProtocolFamily;
11import java.nio.ByteBuffer;
12import java.nio.channels.DatagramChannel;
13import java.util.*;
7 14
8/** 15/**
9 * The Server class. 16 * The Server class.
@@ -13,18 +20,86 @@ import java.util.Optional;
13public class Server { 20public class Server {
14 21
15 public static final int DEFAULT_PORT = 2016; 22 public static final int DEFAULT_PORT = 2016;
16 public static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length; 23 private static final int DEFAULT_EXPECTED_CLIENTS = PlayerID.values().length;
24
25 private static InetSocketAddress listeningInterface(String host, int port) {
26 if (Objects.isNull(host))
27 return new InetSocketAddress(port);
28 else
29 return new InetSocketAddress(host, port);
30 }
31
32 private static DatagramChannel openChannel(InetSocketAddress iface) {
33 try {
34 DatagramChannel chan = DatagramChannel.open(StandardProtocolFamily.INET);
35 chan.bind(iface);
36 return chan;
37 } catch (IOException e) {
38 e.printStackTrace();
39 System.exit(1);
40 return null;
41 }
42 }
43
44 private static Optional<Map.Entry<SocketAddress, PlayerAction>> receiveAction(DatagramChannel chan, boolean block) {
45 try {
46 ByteBuffer buf = ByteBuffer.allocate(1);
47 chan.configureBlocking(block);
48 SocketAddress client = chan.receive(buf);
49 PlayerAction action = PlayerAction.fromByte(buf.get(0));
50 return Optional.of(new AbstractMap.SimpleImmutableEntry<>(client, action));
51 } catch (IOException | IllegalArgumentException e) {
52 return Optional.empty();
53 }
54 }
55
56 private static Map.Entry<SocketAddress, PlayerAction> acceptAction(DatagramChannel chan) {
57 Optional<Map.Entry<SocketAddress, PlayerAction>> action;
58
59 do {
60 action = receiveAction(chan, true);
61 } while (!action.isPresent());
62
63 return action.get();
64 }
65
66 private static SocketAddress acceptRegistration(DatagramChannel chan) {
67 Map.Entry<SocketAddress, PlayerAction> clientAction;
68
69 do {
70 clientAction = acceptAction(chan);
71 } while (clientAction.getValue() != PlayerAction.JOIN_GAME);
72
73 return clientAction.getKey();
74 }
75
76 private static List<SocketAddress> acceptRegistrations(DatagramChannel chan, int registrations) {
77 List<SocketAddress> clients = new ArrayList<>(registrations);
78
79 while (clients.size() < registrations) {
80 SocketAddress client = acceptRegistration(chan);
81 if (!clients.contains(client))
82 clients.add(client);
83 }
84
85 return Collections.unmodifiableList(clients);
86 }
17 87
18 private final InetSocketAddress iface; 88 private final InetSocketAddress iface;
19 private final int expectedClients; 89 private final int expectedClients;
90 private Map<SocketAddress, PlayerID> registeredClients;
20 91
21 public Server(String iface, Integer port, Integer expectedClients) { 92 public Server(String iface, Integer port, Integer expectedClients) {
22 this.iface = new InetSocketAddress(iface, Optional.ofNullable(port).orElse(DEFAULT_PORT)); 93 this.iface = listeningInterface(iface, Optional.ofNullable(port).orElse(DEFAULT_PORT));
23 this.expectedClients = Optional.ofNullable(expectedClients).orElse(DEFAULT_EXPECTED_CLIENTS); 94 this.expectedClients = Optional.ofNullable(expectedClients).orElse(DEFAULT_EXPECTED_CLIENTS);
24 } 95 }
25 96
26 public void run() { 97 public void run() {
27 // TODO 98 DatagramChannel chan = openChannel(this.iface);
99 List<SocketAddress> clients = acceptRegistrations(chan, this.expectedClients);
100 this.registeredClients = Lists.linearAdjustedMap(clients, Arrays.asList(PlayerID.values()));
101
102 System.out.println(this.registeredClients);
28 } 103 }
29 104
30} 105}