source: geniuswebcore/geniusweb/protocol/partyconnection/ProtocolToPartyConnections.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 2.6 KB
Line 
1from __future__ import annotations
2from typing import List, Optional, Dict
3
4from geniusweb.actions.PartyId import PartyId
5from geniusweb.inform.Inform import Inform
6from geniusweb.protocol.partyconnection.ProtocolToPartyConn import ProtocolToPartyConn
7
8
9class ProtocolToPartyConnections: #implements Iterable<ProtocolToPartyConn>
10 '''
11 Contains all parties with their connections. immutable
12 '''
13
14 def __init__(self, connections:List[ProtocolToPartyConn] ):
15 self._connections = connections
16
17 def get(self, pid:PartyId) -> Optional[ProtocolToPartyConn] :
18 '''
19 @param id the {@link PartyId} needed
20 @return a connection with that party, or null if no such connection.
21 '''
22 for conn in self._connections:
23 if pid==conn.getParty():
24 return conn
25 return None
26
27 def allunique(self) ->bool:
28 '''
29 @return true iff all parties have one connection only
30 '''
31 return len(self._getParties()) == len(self._connections)
32
33 def broadcast(self, info:Inform) -> Dict[PartyId, BaseException]:
34 '''
35 Broadcast info to all parties. Notice that the broadcast immediately
36 aborts if an error occurs and remaining parties will not receive the
37 event. Therefore it is recommended to instead send to all parties
38 individually to ensure all parties are handled individually.
39
40 @param info the {@link Inform} to broadcast
41 @return a Map&lt;PartyId, IOException&gt; where possible exceptions for
42 each party are stored. Normally a protocol would kick these
43 parties.
44 '''
45 exceptions:Dict[PartyId, BaseException] = {}
46 for conn in self._connections:
47 try:
48 conn.send(info);
49 except Exception as e:
50 exceptions[conn.getParty()]=e
51 return exceptions
52
53
54 def size(self) ->int:
55 '''
56 @return number of connections available
57 '''
58
59 return len(self._connections)
60
61 def getConn(self, i:int ) ->ProtocolToPartyConn :
62 '''
63 @param i the connection number
64 @return the ith connection
65 '''
66 return self._connections[i]
67
68 def __iter__(self):
69 return iter(self._connections)
70
71 def With(self, conn:ProtocolToPartyConn) ->"ProtocolToPartyConnections" :
72 newconnections:List[ProtocolToPartyConn] = list(self._connections)
73 newconnections.append(conn)
74 return ProtocolToPartyConnections(newconnections)
75
76 def __repr__(self)->str:
77 return "ConnectionWithParties" + str(self._connections)
78
79 def __hash__(self) -> int:
80 return hash(tuple(self._connections))
81
82 def __eq__(self, other):
83 return isinstance(other, self.__class__) \
84 and self._connections == other._connections
85
86
87 def _getParties(self) -> List[PartyId]:
88 '''
89 @return list (set) of parties currently in the connections.
90 '''
91 return list(set([ con.getParty() for con in self._connections ]))
Note: See TracBrowser for help on using the repository browser.