7 from __future__
import annotations
8 from .server_configuration
import JsonDict, Configuration
9 from .contributor_list
import ContributorList
10 from typing
import cast
16 Current state of the server
20 next_contributor_index: int,
21 num_contributors: int,
22 next_contributor_deadline: float):
24 self.num_contributors: int = num_contributors
26 assert self.num_contributors != 0
33 return ServerState._from_json_dict(
34 json.loads(state_json))
38 returns True if all contributions have been received
44 Update the state after new contribution has been successfully received.
49 def update(self, now: float, interval: float) -> bool:
51 Check whether a contributor has missed his chance and update internal
52 state accordingly. If the deadline has passed, return True. Otherwise
63 def _next_contributor(self, next_deadline: float) ->
None:
70 def _to_json_dict(self) -> JsonDict:
73 "num_contributors": self.num_contributors,
78 def _from_json_dict(json_dict: JsonDict) -> ServerState:
80 next_contributor_index=cast(int, json_dict[
"next_contributor_index"]),
81 num_contributors=cast(int, json_dict[
"num_contributors"]),
82 next_contributor_deadline=float(
83 cast(str, json_dict[
"next_contributor_deadline"])))
87 configuration: Configuration,
88 contributors: ContributorList) -> ServerState:
90 Create an initial server state, given a configuration and contributor list.
92 assert configuration.start_time_utc != 0.0
93 assert configuration.contribution_interval != 0.0
94 assert len(contributors) != 0
98 configuration.start_time_utc + configuration.contribution_interval)