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