-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(netobj): implemented zone based routing
- Loading branch information
1 parent
c2423e3
commit 556d2c1
Showing
7 changed files
with
156 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import dataclasses | ||
|
||
from magicnet.core.connection import ConnectionHandle | ||
from magicnet.core.net_globals import MNEvents, MNMathTargets | ||
from magicnet.core.net_message import NetMessage | ||
from magicnet.core.transport_handler import TransportMiddleware | ||
from magicnet.netobjects.network_object import NetworkObject | ||
from magicnet.protocol.protocol_globals import StandardMessageTypes | ||
from magicnet.util.messenger import StandardEvents | ||
|
||
|
||
@dataclasses.dataclass | ||
class ZoneBasedRouter(TransportMiddleware): | ||
# NOTE: the current implementation is probably quite slow | ||
# (this is an euristic and I don't have any benchmarks, but it would | ||
# have to run on a lower level than middlewares to speed it up) | ||
|
||
PROCESSED_MESSAGE_TYPES = { | ||
StandardMessageTypes.SET_OBJECT_FIELD, | ||
StandardMessageTypes.GENERATE_OBJECT, | ||
StandardMessageTypes.OBJECT_GENERATE_DONE, | ||
StandardMessageTypes.DESTROY_OBJECT, | ||
} | ||
|
||
def __post_init__(self): | ||
self.listen(MNEvents.BEFORE_LAUNCH, self.do_before_launch) | ||
|
||
def do_before_launch(self): | ||
self.add_message_operator(self.validate_message_zone, None) | ||
self.add_math_target(MNMathTargets.VISIBLE_OBJECTS, self.only_visibles) | ||
|
||
def only_visibles(self, objects: list[NetworkObject], handle: ConnectionHandle): | ||
success, viszones = handle.get_shared_parameter("vz", list[int]) | ||
if not success: | ||
self.emit(StandardEvents.WARNING, f"{handle.uuid}: incorrectly set viszones!") | ||
return [] | ||
|
||
vz_set = set(viszones) | ||
return [obj for obj in objects if obj.zone in vz_set] | ||
|
||
def validate_message_zone(self, message: NetMessage, handle: ConnectionHandle): | ||
if message.message_type not in self.PROCESSED_MESSAGE_TYPES: | ||
return message | ||
|
||
oid = message.parameters[0] | ||
obj = self.transport.manager.managed_objects.get(oid) | ||
if obj is None: | ||
# Strange but ok | ||
# Note that we still have to do this even if obj is falsey because | ||
# we might be generating it still | ||
self.emit(StandardEvents.WARNING, f"Message sent but the object is missing: {oid}") | ||
return None | ||
|
||
zone = obj.zone | ||
success, viszones = handle.get_shared_parameter("vz", list[int]) | ||
if not success: | ||
self.emit(StandardEvents.WARNING, f"{handle.uuid}: incorrectly set viszones!") | ||
return None | ||
|
||
if zone in viszones: | ||
return message | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import dataclasses | ||
|
||
from magicnet.batteries.middlewares.zone_routing import ZoneBasedRouter | ||
from magicnet.netobjects.network_field import NetworkField | ||
from magicnet.netobjects.network_object import NetworkObject | ||
from net_objects.net_tester_netobj import FlexibleNetworkObjectTester | ||
|
||
|
||
class ZoneTester(FlexibleNetworkObjectTester): | ||
server_middlewares = [*FlexibleNetworkObjectTester.middlewares, ZoneBasedRouter] | ||
|
||
|
||
def test_object_zones(): | ||
@dataclasses.dataclass | ||
class TestNetObject(NetworkObject): | ||
network_name = "test_obj" | ||
object_role = 0 | ||
|
||
value: int = 0 | ||
|
||
@NetworkField | ||
def set_value(self, value): | ||
self.value = value | ||
|
||
def net_create(self) -> None: | ||
pass | ||
|
||
def net_delete(self) -> None: | ||
pass | ||
|
||
tester = ZoneTester.create_and_start(TestNetObject, TestNetObject) | ||
|
||
first_client = tester.make_client() | ||
first_client.get_handle("server").set_shared_parameter("vz", [1]) | ||
second_client = tester.make_client() | ||
second_client.get_handle("server").set_shared_parameter("vz", [2]) | ||
|
||
first_object = TestNetObject(tester.server) | ||
first_object.send_message("set_value", [100]) | ||
first_object.request_generate(zone=1) | ||
|
||
second_object = TestNetObject(tester.server) | ||
second_object.send_message("set_value", [105]) | ||
second_object.request_generate(zone=2) | ||
|
||
assert first_object.oid not in second_client.managed_objects | ||
assert second_client.managed_objects.get(second_object.oid).value == 105 | ||
assert first_client.managed_objects.get(first_object.oid).value == 100 | ||
assert second_object.oid not in first_client.managed_objects | ||
|
||
first_client.get_handle("server").set_shared_parameter("vz", [2]) | ||
second_client.get_handle("server").set_shared_parameter("vz", [1]) | ||
# Note: invisible object unloading is currently not implemented | ||
first_client.object_manager.request_visible_objects() | ||
assert first_client.managed_objects.get(second_object.oid).value == 105 | ||
|
||
second_object.send_message("set_value", [110]) | ||
assert first_client.managed_objects.get(second_object.oid).value == 110 | ||
# we get a discrepancy here because second client no longer sees updates | ||
assert second_client.managed_objects.get(second_object.oid).value == 105 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters