oysstu / pyimc

Python bindings for Inter-Module Communication Protocol (IMC)
Other
9 stars 6 forks source link

Implement events and event handlers #3

Open oysstu opened 5 years ago

oysstu commented 5 years ago

The current actor structure allows for a base class to expose functions that can be overridden in child classes; e.g. on_node_disconnect can be called by IMCBase and implemented in user code. When implementing nested actors (e.g. to handle different aspects of DUNE/IMC interactivity) through inheritance, this is limiting as multiple actors cannot override the same function. This leads to either duplicated calls or function implementation.

Another way to get around this issue is to add a decorator which subscribes to events, for example through an enumeration

from enum import Enum, auto

class IMCEvent(Enum):
    NODE_CONNECT    = auto()
    NODE_TIMEOUT    = auto()
    PLAN_START      = auto()
    PLAN_STOP       = auto()

class TestActor(IMCBase):
    @OnEvent(IMCEvent.NODE_CONNECT)
    def node_connected(self, node):
        ...

If the number of events are limited, this can also be handled through individual decorators

class TestActor(IMCBase):
    @OnConnect()
    def node_connected(self, node):
        ...