Dispatcher
gradysim.protocol.plugin.dispatcher
This module contains a function that creates a dispatcher which wraps a protocol instance and it's methods. Implements a call chain for each of the protocol interface's methods.
Use this module through the create_dispatcher][gradysim.protocol.plugin.dispatcher.create_dispatcher] method, never instantiate the ProtocolWrapper directly.
This module is useful if you want to implement an plugin on or some other functionality that relies on overriding the protocol's methods. Protocols using this plugin will still be compatible with all execution modes as only the protocol itself is tampered with and not any of the layers that allow it to run on different environments.
Beware that this module uses monkey patching and may result in broken protocols if someone else tries to tamper with the protocol's methods.
DispatchReturn
Bases: Enum
Return value for the dispatched funcions. INTERRUPT should be returned if your handler completely handled the call and it shouldn't be passed forward. CONTINUE should be called if the call should continue down the call chain.
Source code in gradysim/protocol/plugin/dispatcher.py
ProtocolWrapper
Do not use this class directly, instead use create_dispatcher.
Wraps the protocol's calls into a call chain. Instead of going directly to the protocol's methods calls to the protocol interface will be passed down a chain of registered handlers. The protocol's own method is at the end of the chain. Methods should return a value in the DispatchReturn Enum, INTERRUPT do interrupt the chain and CONTINUE if the message should be passed through.
Source code in gradysim/protocol/plugin/dispatcher.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
|
__init__(protocol)
Instantiates a protocol wrapper. Should not be instantiated directly, create a dispatcher using the create_dispatcher method.
Do not instantiate this class directly
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
IProtocol
|
Protocol whose calls will be wrapped |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
register_finish(handler)
Registers a handler for the handle_timer method. Handlers should have the same signature as the finish. This method doesn't support returning DispatchReturn values, the call chain is always followed.
Not following this chain, since it is only called once, could result in improper finalization of protocols and other handlers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol], None]
|
Handler being registered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
register_handle_packet(handler)
Registers a handler for the handle_packet method. Handlers should have the same signature as the handle_packet method but return a value in the DispatcherReturn enum.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, str], DispatchReturn]
|
Handler being registered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
register_handle_telemetry(handler)
Registers a handler for the handle_telemetry method. Handlers should have the same signature as the handle_telemetry method but return a value in the DispatcherReturn enum.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, Telemetry], DispatchReturn]
|
Handler being registered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
register_handle_timer(handler)
Registers a handler for the handle_timer method. Handlers should have the same signature as the handle_timer method but return a value in the DispatcherReturn enum.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, str], DispatchReturn]
|
Handler being registered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
register_initialize(handler)
Registers a handler for the initialize method. Handlers should have the same signature as the initialize method. DispatcherReturn is not supported for this method, the call chain is always followed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, int], None]
|
Handler being registered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
unregister_finish(handler)
Unregisters a handle_timer handler. Raises ValueError if handler was not registered
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol], None]
|
Handler instance being unregistered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
unregister_handle_packet(handler)
Unregisters a handle_timer handler. Raises ValueError if handler was not registered
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, str], DispatchReturn]
|
Handler instance being unregistered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
unregister_handle_telemetry(handler)
Unregisters a handle_timer handler. Raises ValueError if handler was not registered
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, Telemetry], DispatchReturn]
|
Handler instance being unregistered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
unregister_handle_timer(handler)
Unregisters a handle_timer handler. Raises ValueError if handler was not registered
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, str], DispatchReturn]
|
Handler instance being unregistered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
unregister_initialize(handler)
Unregisters a handle_initialize handler. Raises ValueError if handler was not registered
Parameters:
Name | Type | Description | Default |
---|---|---|---|
handler |
Callable[[IProtocol, int], None]
|
Handler instance being unregistered |
required |
Source code in gradysim/protocol/plugin/dispatcher.py
create_dispatcher(protocol)
Creates a dispatcher which wraps a protocol instance and it's methods. Implements a call chain for each of the protocol interface's methods. The class returned from this function can be used to add functions to the call chain of those wrapped methods. The original method implementation is not lost.
Is a protocol that was already wrapped is passed as an argument, return the wrapper for that protocol.
Beware that this module uses monkey patching and may result in broken protocols if someone else tries to tamper with the protocol's methods.
If you want to implement an plugin or some other behaviour that requires overriding protocol's methods you should use this function
Parameters:
Name | Type | Description | Default |
---|---|---|---|
protocol |
IProtocol
|
Protocol being wrapped |
required |
Returns:
Type | Description |
---|---|
ProtocolWrapper
|
ProtocolWrapper instance that allows methods to be added to the call chain |