sync controller database to disk and load it on start
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import typing
|
||||
import uuid
|
||||
|
||||
import capport.database
|
||||
import capport.comm.hub
|
||||
import capport.comm.message
|
||||
import capport.config
|
||||
import capport.database
|
||||
import capport.utils.cli
|
||||
import capport.utils.nft_set
|
||||
import trio
|
||||
@@ -22,15 +23,15 @@ class ControlApp(capport.comm.hub.HubApplication):
|
||||
super().__init__()
|
||||
self.nft_set = capport.utils.nft_set.NftSet()
|
||||
|
||||
def is_controller(self) -> bool:
|
||||
return True
|
||||
|
||||
async def mac_states_changed(self, *, from_peer_id: uuid.UUID, pending_updates: capport.database.PendingUpdates) -> None:
|
||||
self.apply_db_entries(pending_updates.changes())
|
||||
|
||||
def apply_db_entries(self, entries: typing.Iterable[typing.Tuple[cptypes.MacAddress, capport.database.MacEntry]]) -> None:
|
||||
# deploy changes to netfilter set
|
||||
inserts = []
|
||||
removals = []
|
||||
now = cptypes.Timestamp.now()
|
||||
for mac, state in pending_updates.macs.items():
|
||||
for mac, state in entries:
|
||||
rem = state.allowed_remaining(now)
|
||||
if rem > 0:
|
||||
inserts.append((mac, rem))
|
||||
@@ -42,9 +43,12 @@ class ControlApp(capport.comm.hub.HubApplication):
|
||||
|
||||
async def amain(config: capport.config.Config) -> None:
|
||||
app = ControlApp()
|
||||
hub = capport.comm.hub.Hub(config=config, app=app)
|
||||
hub = capport.comm.hub.Hub(config=config, app=app, is_controller=True)
|
||||
app.hub = hub
|
||||
await hub.run()
|
||||
async with trio.open_nursery() as nursery:
|
||||
# hub.run loads the statefile from disk before signalling it was "started"
|
||||
await nursery.start(hub.run)
|
||||
app.apply_db_entries(hub.database.entries())
|
||||
|
||||
|
||||
def main() -> None:
|
||||
|
||||
Reference in New Issue
Block a user