otupy.core.consumer.Consumer
- class Consumer(consumer: str, actuators: [] = None, encoder: Encoder = None, transfer: Transfer = None)
Bases:
objectOpenC2 Consumer
The Consumer is designed to dispatch OpenC2 Message`s to the relevant `Actuator. The current implementation receives the configuration at initialization time. It is therefore not conceived to be runned itself as a service, but to be integrated in an external component that reads the relevant configuration from file and passes it to the Consumer.
The Consumer has two main tasks:
creating the OpenC2 stack to process Messages (namely the combination of an Encoding format and a Transfer protocol);
dispatching incoming Command`s to the relevant `Actuator.
Each Consumer will only run a single Transfer protocol. All registered Encoder`s can be used, and a default `Encoder is explicitely given that will be used when no other selection is available (e.g., to answer Messages that the Consumer does not understand).
Methods
Dispatches Commands to Actuators
Runs a Consumer
- __init__(consumer: str, actuators: [] = None, encoder: Encoder = None, transfer: Transfer = None)
Create a Consumer
- Parameters:
consumer – This is a string that identifies the Consumer and is used in from and to fields of the OpenC2 Message (see Table 3.1 of the Language Specification.
actuators – This must be a list of available Actuators. The list contains the Actuator instances that will be used by the Consumer.
encoder – This is an instance of the Encoder that will be used by default.
transfer – This is the Transfer protocol that will be used to send/receive Messages.
- dispatch(msg)
Dispatches Commands to Actuators
This method scans the actuator profile carried in the Command and select one or more Actuator`s that will process the `Command.
The current implementation is only meant to be used within the implementation of Transfer protocols as a callback for returning control to the main code. This approach is motivated by those Transfer protocols that replies to messages on the same TCP connection, so to avoid errors with NAT and firewalls (if a Command were passed back from the Transfer.receive() and processed within the Consumer.run(), the following Transfer.send() would use a different TCP connection).
- Parameters:
msg – The full otupy Message that embeds the Command to be processed.
- Returns:
A Message that embeds the Response (from the Actuator or elaborated by the Consumer in case of errors).
- run(encoder: Encoder = None, transfer: Transfer = None)
Runs a Consumer
This is the entry point of the Consumer. It must be invoked to start operation of the Consumer. This method may be blocking, depending on the implementation of the receive() method of the used Transfer.
The arguments of this method can be used to create multiple OpenC2 stacks (e.g., using different Encoder`s and `Transfer`s). This feature clearly requires the `Transfer implementation to be non-blocking.
- Parameters:
encoder – A different Encoder that might be passed to overwrite what set at initialization time.
transfer – A different Transfer that might be passed to overwrite what set at initialization time.
- Returns:
None.