1 | # Generated from java by J2P
|
---|
2 | from __future__ import annotations
|
---|
3 | from geniusweb.actions.Action import Action
|
---|
4 | from geniusweb.connection.ConnectionEnd import ConnectionEnd
|
---|
5 | from geniusweb.inform.Inform import Inform
|
---|
6 | from geniusweb.party.Party import Party
|
---|
7 | from tudelft.utilities.listener.Listener import Listener
|
---|
8 | from tudelft_utilities_logging.ReportToFile import ReportToFile
|
---|
9 | from tudelft_utilities_logging.Reporter import Reporter
|
---|
10 | from typing import Optional
|
---|
11 | import tempfile
|
---|
12 |
|
---|
13 |
|
---|
14 | class DefaultParty(Party, Listener[Inform]):
|
---|
15 | '''
|
---|
16 | Party with default implementation to handle the connection.
|
---|
17 |
|
---|
18 |
|
---|
19 | '''
|
---|
20 |
|
---|
21 | def __init__(self):
|
---|
22 | '''
|
---|
23 | Creates new party, setting reporter to a file with the class name in the
|
---|
24 | temp directory. This default is to avoid parties printing to stdout,
|
---|
25 | which is confusing (eg when this happens on a server) and even fatal (if
|
---|
26 | the party is a python party running in a PyRunner). It is highly
|
---|
27 | recommended that {@link #setReporter(Reporter)} is called immediately and
|
---|
28 | that noghing is logged until after that.
|
---|
29 |
|
---|
30 | '''
|
---|
31 | self.__connection:Optional[ConnectionEnd[Inform,Action]] = None
|
---|
32 | self._reporter:Reporter = None
|
---|
33 | super().__init__()
|
---|
34 | try:
|
---|
35 | self._reporter=ReportToFile(tempfile.NamedTemporaryFile(prefix=type(self).__name__, suffix=".log").name)
|
---|
36 | except IOError as e:
|
---|
37 | raise BaseException("Failed to create log file for new party") from e
|
---|
38 |
|
---|
39 | def setReporter(self,reporter:Reporter) -> Optional[Party]:
|
---|
40 | '''
|
---|
41 | Set the reporter. If needed, use immediately after constructing. This is
|
---|
42 | mainly used for debugging and tests.
|
---|
43 |
|
---|
44 | '''
|
---|
45 | if (reporter is None):
|
---|
46 | raise ValueError("reporter must be not null")
|
---|
47 | self._reporter=reporter
|
---|
48 | return self
|
---|
49 |
|
---|
50 | #Override
|
---|
51 | def connect(self,connection:ConnectionEnd[Inform,Action]) -> None:
|
---|
52 | self.__connection=connection
|
---|
53 | connection.addListener(self)
|
---|
54 |
|
---|
55 | #Override
|
---|
56 | def disconnect(self) -> None:
|
---|
57 | if (self.__connection is not None):
|
---|
58 | self.__connection.removeListener(self)
|
---|
59 | self.__connection.close()
|
---|
60 | self.__connection=None
|
---|
61 |
|
---|
62 | #Override
|
---|
63 | def terminate(self) -> None:
|
---|
64 | self.disconnect()
|
---|
65 |
|
---|
66 | def getConnection(self) -> Optional[ConnectionEnd[Optional[Inform],Optional[Action]]]:
|
---|
67 | '''
|
---|
68 | @return currently available connection, or null if not currently
|
---|
69 | connected.
|
---|
70 |
|
---|
71 | '''
|
---|
72 | return self.__connection
|
---|
73 |
|
---|
74 | def getReporter(self) -> Reporter:
|
---|
75 | return self._reporter
|
---|