Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
|
EPOS-inspired Agent implementation using composition over inheritance. More...
#include <agent.h>
Public Member Functions | |
Agent (CAN *bus, const std::string &name, Unit unit, Type type, Address address, DataProducer producer, ResponseHandler handler, std::unique_ptr< ComponentData > data, bool external=true) | |
Constructor for EPOS-inspired Agent using composition. | |
~Agent () | |
Destructor with proper cleanup order. | |
Value | get (Unit unit) |
Get data using function pointer instead of virtual method. | |
void | handle_response (Message *msg) |
Handle response using function pointer instead of virtual method. | |
int | send (Unit unit, Microseconds period) |
int | receive (Message *msg) |
bool | running () |
void | external (const bool external) |
const bool | external () const |
std::string | name () const |
void | set_csv_logger (const std::string &log_dir) |
void | log_message (const Message &msg, const std::string &direction) |
int | start_periodic_interest (Unit unit, Microseconds period) |
Start sending periodic INTEREST messages for the specified unit and period. | |
void | stop_periodic_interest () |
Stop sending periodic INTEREST messages. | |
void | send_interest (Unit unit) |
Send a single INTEREST message (called by periodic thread) | |
void | update_interest_period (Microseconds new_period) |
Update the period for periodic interest sending. | |
Static Public Member Functions | |
static void * | run (void *arg) |
Static Public Attributes | |
static const unsigned int | _units_per_vehicle = 5 |
static constexpr int | MAX_RESPONSES_PER_INTEREST = 5 |
Protected Member Functions | |
virtual void | reply (Unit unit) |
Reply method that calls function pointer instead of virtual method. | |
bool | thread_running () |
int | can_send (Message *msg) |
Address | address () const |
EPOS-inspired Agent implementation using composition over inheritance.
This Agent class eliminates the "pure virtual method called" race condition by using function pointers instead of virtual methods. Following EPOS SmartData principles, components are pure data + function pairs rather than inheritance-based.
Key improvements:
|
inline |
Constructor for EPOS-inspired Agent using composition.
Creates an Agent that uses function pointers instead of virtual methods, eliminating the vtable race condition during destruction.
bus | CAN bus for communication |
name | Agent name for identification |
unit | Data unit this agent handles |
type | Agent type (INTEREST for producers, RESPONSE for consumers) |
address | Network address |
producer | Function pointer for data production (can be nullptr for consumers) |
handler | Function pointer for response handling (can be nullptr for producers) |
data | Component-specific data structure |
|
inline |
Destructor with proper cleanup order.
Ensures threads are properly joined before object destruction, eliminating the race condition that occurred with virtual methods.
|
inlineprotected |
|
inline |
Get data using function pointer instead of virtual method.
This is the key method that eliminates the race condition. Instead of calling a virtual method that could crash during destruction, we call a function pointer that was set during construction.
unit | The data unit being requested |
Handle response using function pointer instead of virtual method.
Similar to get(), this uses a function pointer to avoid virtual method calls that could cause race conditions during destruction.
msg | Pointer to the received message |
|
inline |
|
inline |
|
inline |
Send a single INTEREST message (called by periodic thread)
unit | The data type unit |
|
inline |
Start sending periodic INTEREST messages for the specified unit and period.
unit | The data type unit to request |
period | The desired response period from producers |
|
inline |
Stop sending periodic INTEREST messages.
|
inlineprotected |
|
inline |
Update the period for periodic interest sending.
new_period | The new period to use |