diff options
author | Pacien TRAN-GIRARD | 2016-05-11 13:41:49 +0200 |
---|---|---|
committer | Pacien TRAN-GIRARD | 2016-05-11 13:41:49 +0200 |
commit | ff03812ce4ccdfb09fd5ac464ff1d993ccc16cb0 (patch) | |
tree | 84d05775eac5e521226b7d7865e4d177949d9870 /src/ch/epfl | |
parent | 2392d36dfa15a262d69aeb6356408d90d5a32ab2 (diff) | |
download | xblast-ff03812ce4ccdfb09fd5ac464ff1d993ccc16cb0.tar.gz |
Implement server client registration
Diffstat (limited to 'src/ch/epfl')
-rw-r--r-- | src/ch/epfl/xblast/server/Server.java | 83 |
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 @@ | |||
1 | package ch.epfl.xblast.server; | 1 | package ch.epfl.xblast.server; |
2 | 2 | ||
3 | import ch.epfl.xblast.Lists; | ||
4 | import ch.epfl.xblast.PlayerAction; | ||
3 | import ch.epfl.xblast.PlayerID; | 5 | import ch.epfl.xblast.PlayerID; |
4 | 6 | ||
7 | import java.io.IOException; | ||
5 | import java.net.InetSocketAddress; | 8 | import java.net.InetSocketAddress; |
6 | import java.util.Optional; | 9 | import java.net.SocketAddress; |
10 | import java.net.StandardProtocolFamily; | ||
11 | import java.nio.ByteBuffer; | ||
12 | import java.nio.channels.DatagramChannel; | ||
13 | import java.util.*; | ||
7 | 14 | ||
8 | /** | 15 | /** |
9 | * The Server class. | 16 | * The Server class. |
@@ -13,18 +20,86 @@ import java.util.Optional; | |||
13 | public class Server { | 20 | public 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 | } |