aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPacien TRAN-GIRARD2016-05-11 15:38:15 +0200
committerPacien TRAN-GIRARD2016-05-11 15:38:15 +0200
commit42098c03b4296e440e2825b7d41965da72a6f970 (patch)
treededf866a40707c96b5ad783617e6a40e3aaf906d
parentff03812ce4ccdfb09fd5ac464ff1d993ccc16cb0 (diff)
downloadxblast-42098c03b4296e440e2825b7d41965da72a6f970.tar.gz
Implement action collection
-rw-r--r--src/ch/epfl/xblast/server/Server.java43
1 files changed, 39 insertions, 4 deletions
diff --git a/src/ch/epfl/xblast/server/Server.java b/src/ch/epfl/xblast/server/Server.java
index 10d5a85..58f3c07 100644
--- a/src/ch/epfl/xblast/server/Server.java
+++ b/src/ch/epfl/xblast/server/Server.java
@@ -41,14 +41,27 @@ public class Server {
41 } 41 }
42 } 42 }
43 43
44 private static Optional<Map.Entry<SocketAddress, PlayerAction>> receiveAction(DatagramChannel chan, boolean block) { 44 private static Optional<Map.Entry<SocketAddress, Byte>> receiveByte(DatagramChannel chan, boolean block) {
45 try { 45 try {
46 ByteBuffer buf = ByteBuffer.allocate(1); 46 ByteBuffer buf = ByteBuffer.allocate(1);
47 chan.configureBlocking(block); 47 chan.configureBlocking(block);
48 SocketAddress client = chan.receive(buf); 48 SocketAddress client = chan.receive(buf);
49 PlayerAction action = PlayerAction.fromByte(buf.get(0)); 49
50 return Optional.of(new AbstractMap.SimpleImmutableEntry<>(client, action)); 50 if (Objects.isNull(client) || buf.position() == 0)
51 } catch (IOException | IllegalArgumentException e) { 51 throw new IOException();
52
53 return Optional.of(new AbstractMap.SimpleImmutableEntry<>(client, buf.get(0)));
54 } catch (IOException e) {
55 return Optional.empty();
56 }
57 }
58
59 private static Optional<Map.Entry<SocketAddress, PlayerAction>> receiveAction(DatagramChannel chan, boolean block) {
60 try {
61 Map.Entry<SocketAddress, Byte> actionByte = receiveByte(chan, block).get();
62 PlayerAction playerAction = PlayerAction.fromByte(actionByte.getValue());
63 return Optional.of(new AbstractMap.SimpleImmutableEntry<>(actionByte.getKey(), playerAction));
64 } catch (NoSuchElementException | IllegalArgumentException e) {
52 return Optional.empty(); 65 return Optional.empty();
53 } 66 }
54 } 67 }
@@ -63,6 +76,19 @@ public class Server {
63 return action.get(); 76 return action.get();
64 } 77 }
65 78
79 private static Map<SocketAddress, PlayerAction> collectActions(DatagramChannel chan) {
80 Map<SocketAddress, PlayerAction> actions = new HashMap<>();
81 Optional<Map.Entry<SocketAddress, PlayerAction>> action;
82
83 while (true) {
84 action = receiveAction(chan, false);
85 if (!action.isPresent()) break;
86 actions.put(action.get().getKey(), action.get().getValue());
87 }
88
89 return Collections.unmodifiableMap(actions);
90 }
91
66 private static SocketAddress acceptRegistration(DatagramChannel chan) { 92 private static SocketAddress acceptRegistration(DatagramChannel chan) {
67 Map.Entry<SocketAddress, PlayerAction> clientAction; 93 Map.Entry<SocketAddress, PlayerAction> clientAction;
68 94
@@ -100,6 +126,15 @@ public class Server {
100 this.registeredClients = Lists.linearAdjustedMap(clients, Arrays.asList(PlayerID.values())); 126 this.registeredClients = Lists.linearAdjustedMap(clients, Arrays.asList(PlayerID.values()));
101 127
102 System.out.println(this.registeredClients); 128 System.out.println(this.registeredClients);
129
130 try {
131 Thread.sleep(10000);
132 } catch (InterruptedException e) {
133 e.printStackTrace();
134 }
135
136 Map<SocketAddress, PlayerAction> actions = collectActions(chan);
137 System.out.println(actions);
103 } 138 }
104 139
105} 140}