Communication Library for Autonomous Systems v1.0
Reliable and secure communication library for autonomous vehicle systems
Loading...
Searching...
No Matches
Agent Class Reference

EPOS-inspired Agent implementation using composition over inheritance. More...

#include <agent.h>

Inheritance diagram for Agent:
CSVAgent

Public Types

typedef CAN::Message Message
 
typedef Message::Origin Address
 
typedef Message::Array Value
 
typedef Message::Unit Unit
 
typedef Message::Type Type
 
typedef Message::Microseconds Microseconds
 
typedef Periodic_Thread< AgentThread
 
typedef CAN::Observer Observer
 

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 voidrun (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
 

Detailed Description

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:

  • No virtual methods = no vtable race conditions
  • Function-based composition instead of inheritance
  • Type-safe component data management
  • Same public API as original Agent for compatibility

Member Typedef Documentation

◆ Address

◆ Message

◆ Microseconds

◆ Observer

◆ Thread

◆ Type

◆ Unit

◆ Value

Constructor & Destructor Documentation

◆ Agent()

Agent::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 
)
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.

Parameters
busCAN bus for communication
nameAgent name for identification
unitData unit this agent handles
typeAgent type (INTEREST for producers, RESPONSE for consumers)
addressNetwork address
producerFunction pointer for data production (can be nullptr for consumers)
handlerFunction pointer for response handling (can be nullptr for producers)
dataComponent-specific data structure

◆ ~Agent()

Agent::~Agent ( )
inline

Destructor with proper cleanup order.

Ensures threads are properly joined before object destruction, eliminating the race condition that occurred with virtual methods.

Member Function Documentation

◆ address()

Agent::Address Agent::address ( ) const
inlineprotected

◆ can_send()

int Agent::can_send ( Message msg)
inlineprotected

◆ external() [1/2]

const bool Agent::external ( ) const
inline

◆ external() [2/2]

void Agent::external ( const bool  external)
inline

◆ get()

Agent::Value Agent::get ( Unit  unit)
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.

Parameters
unitThe data unit being requested
Returns
Value containing the generated data

◆ handle_response()

void Agent::handle_response ( Message msg)
inline

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.

Parameters
msgPointer to the received message

◆ log_message()

void Agent::log_message ( const Message msg,
const std::string &  direction 
)
inline

◆ name()

std::string Agent::name ( ) const
inline

◆ receive()

int Agent::receive ( Message msg)
inline

◆ reply()

void Agent::reply ( Unit  unit)
inlineprotectedvirtual

Reply method that calls function pointer instead of virtual method.

This is the critical method where the race condition used to occur. Instead of calling the virtual get() method, we now call the function pointer directly, eliminating the vtable dependency.

Reimplemented in CSVAgent.

◆ run()

void * Agent::run ( void arg)
inlinestatic

◆ running()

bool Agent::running ( )
inline

◆ send()

int Agent::send ( Unit  unit,
Microseconds  period 
)
inline

◆ send_interest()

void Agent::send_interest ( Unit  unit)
inline

Send a single INTEREST message (called by periodic thread)

Parameters
unitThe data type unit

◆ set_csv_logger()

void Agent::set_csv_logger ( const std::string &  log_dir)
inline

◆ start_periodic_interest()

int Agent::start_periodic_interest ( Unit  unit,
Microseconds  period 
)
inline

Start sending periodic INTEREST messages for the specified unit and period.

Parameters
unitThe data type unit to request
periodThe desired response period from producers
Returns
int Success/failure status

◆ stop_periodic_interest()

void Agent::stop_periodic_interest ( )
inline

Stop sending periodic INTEREST messages.

◆ thread_running()

bool Agent::thread_running ( )
inlineprotected

◆ update_interest_period()

void Agent::update_interest_period ( Microseconds  new_period)
inline

Update the period for periodic interest sending.

Parameters
new_periodThe new period to use

Member Data Documentation

◆ _units_per_vehicle

const unsigned int Agent::_units_per_vehicle = 5
static

◆ MAX_RESPONSES_PER_INTEREST

constexpr int Agent::MAX_RESPONSES_PER_INTEREST = 5
staticconstexpr

The documentation for this class was generated from the following file: