1 | package geniusweb.protocol.partyconnection;
|
---|
2 |
|
---|
3 | import java.io.IOException;
|
---|
4 | import java.util.HashMap;
|
---|
5 | import java.util.Iterator;
|
---|
6 | import java.util.LinkedList;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.Map;
|
---|
9 | import java.util.stream.Collectors;
|
---|
10 | import java.util.stream.Stream;
|
---|
11 |
|
---|
12 | import geniusweb.actions.PartyId;
|
---|
13 | import geniusweb.inform.Inform;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Contains all parties with their connections. immutable
|
---|
17 | */
|
---|
18 | public class ProtocolToPartyConnections
|
---|
19 | implements Iterable<ProtocolToPartyConn> {
|
---|
20 | private List<ProtocolToPartyConn> connections;
|
---|
21 |
|
---|
22 | public ProtocolToPartyConnections(List<ProtocolToPartyConn> connections) {
|
---|
23 | this.connections = connections;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @param id the {@link PartyId} needed
|
---|
28 | * @return a connection with that party, or null if no such connection.
|
---|
29 | */
|
---|
30 | public ProtocolToPartyConn get(PartyId id) {
|
---|
31 | return connections.stream().filter(conn -> id.equals(conn.getParty()))
|
---|
32 | .findAny().orElse(null);
|
---|
33 | }
|
---|
34 |
|
---|
35 | /**
|
---|
36 | *
|
---|
37 | * @return true iff all parties have one connection only
|
---|
38 | */
|
---|
39 | public boolean allunique() {
|
---|
40 | return getParties().size() == connections.size();
|
---|
41 | }
|
---|
42 |
|
---|
43 | /**
|
---|
44 | *
|
---|
45 | * @return list of parties currently in the connections.
|
---|
46 | */
|
---|
47 | private List<PartyId> getParties() {
|
---|
48 | return connections.stream().map(conn -> conn.getParty()).distinct()
|
---|
49 | .collect(Collectors.toList());
|
---|
50 | }
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Broadcast info to all parties. Notice that the broadcast immediately
|
---|
54 | * aborts if an error occurs and remaining parties will not receive the
|
---|
55 | * event. Therefore it is recommended to instead send to all parties
|
---|
56 | * individually to ensure all parties are handled individually.
|
---|
57 | *
|
---|
58 | * @param info the {@link Inform} to broadcast
|
---|
59 | * @return a Map<PartyId, IOException> where possible exceptions for
|
---|
60 | * each party are stored. Normally a protocol would kick these
|
---|
61 | * parties.
|
---|
62 | */
|
---|
63 | public Map<PartyId, IOException> broadcast(Inform info) {
|
---|
64 | Map<PartyId, IOException> exceptions = new HashMap<>();
|
---|
65 | for (ProtocolToPartyConn conn : connections) {
|
---|
66 | try {
|
---|
67 | conn.send(info);
|
---|
68 | } catch (IOException e) {
|
---|
69 | exceptions.put(conn.getParty(), e);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | return exceptions;
|
---|
73 | }
|
---|
74 |
|
---|
75 | /**
|
---|
76 | *
|
---|
77 | * @return number of connections available
|
---|
78 | */
|
---|
79 | public int size() {
|
---|
80 | return connections.size();
|
---|
81 | }
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * @param i the connection number
|
---|
85 | * @return the ith connection
|
---|
86 | */
|
---|
87 | public ProtocolToPartyConn get(int i) {
|
---|
88 | return connections.get(i);
|
---|
89 | }
|
---|
90 |
|
---|
91 | public Stream<ProtocolToPartyConn> stream() {
|
---|
92 | return connections.stream();
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public Iterator<ProtocolToPartyConn> iterator() {
|
---|
97 | return connections.iterator();
|
---|
98 | }
|
---|
99 |
|
---|
100 | public ProtocolToPartyConnections with(ProtocolToPartyConn conn) {
|
---|
101 | List<ProtocolToPartyConn> newconnections = new LinkedList<>(
|
---|
102 | connections);
|
---|
103 | newconnections.add(conn);
|
---|
104 | return new ProtocolToPartyConnections(newconnections);
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public String toString() {
|
---|
109 | return "ConnectionWithParties" + connections;
|
---|
110 | }
|
---|
111 |
|
---|
112 | @Override
|
---|
113 | public int hashCode() {
|
---|
114 | final int prime = 31;
|
---|
115 | int result = 1;
|
---|
116 | result = prime * result
|
---|
117 | + ((connections == null) ? 0 : connections.hashCode());
|
---|
118 | return result;
|
---|
119 | }
|
---|
120 |
|
---|
121 | @Override
|
---|
122 | public boolean equals(Object obj) {
|
---|
123 | if (this == obj)
|
---|
124 | return true;
|
---|
125 | if (obj == null)
|
---|
126 | return false;
|
---|
127 | if (getClass() != obj.getClass())
|
---|
128 | return false;
|
---|
129 | ProtocolToPartyConnections other = (ProtocolToPartyConnections) obj;
|
---|
130 | if (connections == null) {
|
---|
131 | if (other.connections != null)
|
---|
132 | return false;
|
---|
133 | } else if (!connections.equals(other.connections))
|
---|
134 | return false;
|
---|
135 | return true;
|
---|
136 | }
|
---|
137 |
|
---|
138 | }
|
---|