STAR
Cal STAR — firmware, DAQ server, and engine design
Loading...
Searching...
No Matches
fsw::elodin::ElodinClient Class Reference

Standalone Elodin database client. More...

#include <ElodinClient.hpp>

Collaboration diagram for fsw::elodin::ElodinClient:
[legend]

Public Member Functions

 ElodinClient ()
 ~ElodinClient ()
bool connect (const std::string &host, uint16_t port=2240)
 Connect to Elodin database.
void disconnect ()
 Disconnect from database.
bool is_connected () const
 Check if connected.
template<typename MessageType>
bool publish (uint16_t message_id, const MessageType &message)
 Publish a message to a table.
template<typename MessageType>
bool publish (const std::array< uint8_t, 2 > &table_id, const MessageType &message)
bool subscribe_tables (const std::vector< std::pair< uint8_t, uint8_t > > &table_ids)
 Subscribe to specific tables by ID.
bool subscribe_table (uint8_t hi, uint8_t lo, uint8_t request_id)
 One table, under request_id, which the DB echoes if it refuses it (ErrorResponse [0xE0, 0x1D]).
void begin_batch ()
 Begin batching — subsequent publish() calls go into an internal buffer.
bool flush_batch ()
 Flush (send) the accumulated batch in a single TCP write.
bool is_batching () const
 Returns true if we're between begin_batch() and flush_batch().
bool reconnect ()
 Attempt to reconnect using the last-used host/port.
void register_table (const std::array< uint8_t, 2 > &table_id, const std::string &name)
 Register a table ID with a name (for debugging/logging).
bool send_msg (const std::array< uint8_t, 2 > &packet_id, const std::vector< uint8_t > &data)
 Send a MSG packet (for VTable registration, etc.).
std::string last_error () const
 Get last error message.
void flush_buffer ()
 Flush the write buffer to the database.
void set_recv_timeout_ms (int timeout_ms)
 Set socket receive timeout so read_packet() can yield.
ssize_t read_data (void *buffer, size_t max_len)
 Read incoming data packet from Elodin (non-blocking).
bool read_packet_header (uint8_t *header_buffer)
 Read packet header from Elodin (12 bytes).
bool read_bytes_exact (uint8_t *buffer, size_t len)
 Blocking read of exactly len bytes (for payload reads after header).
ssize_t read_packet (uint8_t *packet_buffer, size_t max_len)
 Read complete packet from Elodin (header + payload).
template<typename MessageType>
std::vector< uint8_t > serialize_msg (uint16_t message_id, const MessageType &msg)
 Serialize a message for Elodin (public for testing).

Private Member Functions

bool connect_locked (const std::string &host, uint16_t port)
void disconnect_locked ()

Private Attributes

std::unique_ptr< daq_comms::transport::TCPClient > socket_
std::mutex publish_mutex_
std::unordered_map< uint16_t, std::string > table_names_
std::string last_error_
bool batching_ = false
std::vector< uint8_t > batch_buffer_
std::string last_host_
uint16_t last_port_ = 0

Detailed Description

Standalone Elodin database client.

No FSW dependencies - uses our own TCP client and protocol implementation.

Definition at line 29 of file ElodinClient.hpp.

Constructor & Destructor Documentation

◆ ElodinClient()

fsw::elodin::ElodinClient::ElodinClient ( )

Definition at line 10 of file ElodinClient.cpp.

References socket_.

◆ ~ElodinClient()

fsw::elodin::ElodinClient::~ElodinClient ( )

Definition at line 14 of file ElodinClient.cpp.

References disconnect().

Member Function Documentation

◆ begin_batch()

void fsw::elodin::ElodinClient::begin_batch ( )

Begin batching — subsequent publish() calls go into an internal buffer.

Call flush_batch() to send everything in one TCP write.

Definition at line 121 of file ElodinClient.cpp.

References batch_buffer_, and batching_.

Referenced by main(), and anonymous_namespace{t7_bridge_main.cpp}::run_board().

◆ connect()

bool fsw::elodin::ElodinClient::connect ( const std::string & host,
uint16_t port = 2240 )

Connect to Elodin database.

Parameters
hostDatabase host address
portDatabase port (default 2240)
Returns
true if connection successful, false otherwise

Definition at line 18 of file ElodinClient.cpp.

References connect_locked(), lock, port, and publish_mutex_.

Referenced by main(), main(), anonymous_namespace{t7_bridge_main.cpp}::run_board(), and smart_calibration_gui.SmartCalibrationGUI::toggle_connection().

◆ connect_locked()

bool fsw::elodin::ElodinClient::connect_locked ( const std::string & host,
uint16_t port )
private

Definition at line 23 of file ElodinClient.cpp.

References disconnect_locked(), last_error_, last_host_, last_port_, port, and socket_.

Referenced by connect(), and reconnect().

◆ disconnect()

void fsw::elodin::ElodinClient::disconnect ( )

◆ disconnect_locked()

void fsw::elodin::ElodinClient::disconnect_locked ( )
private

Definition at line 62 of file ElodinClient.cpp.

References socket_.

Referenced by connect_locked(), and disconnect().

◆ flush_batch()

bool fsw::elodin::ElodinClient::flush_batch ( )

Flush (send) the accumulated batch in a single TCP write.

Returns
true if the write succeeded (or batch was empty).

Definition at line 127 of file ElodinClient.cpp.

References batch_buffer_, batching_, is_connected(), last_error_, lock, ok, publish_mutex_, and socket_.

Referenced by main(), and anonymous_namespace{t7_bridge_main.cpp}::run_board().

◆ flush_buffer()

void fsw::elodin::ElodinClient::flush_buffer ( )

Flush the write buffer to the database.

Definition at line 84 of file ElodinClient.cpp.

References socket_.

◆ is_batching()

bool fsw::elodin::ElodinClient::is_batching ( ) const
inline

Returns true if we're between begin_batch() and flush_batch().

Definition at line 105 of file ElodinClient.hpp.

References batching_.

◆ is_connected()

bool fsw::elodin::ElodinClient::is_connected ( ) const

◆ last_error()

std::string fsw::elodin::ElodinClient::last_error ( ) const

Get last error message.

Definition at line 80 of file ElodinClient.cpp.

References last_error_.

Referenced by anonymous_namespace{t7_bridge_main.cpp}::run_board().

◆ publish() [1/2]

template<typename MessageType>
bool fsw::elodin::ElodinClient::publish ( const std::array< uint8_t, 2 > & table_id,
const MessageType & message )
inline

Definition at line 64 of file ElodinClient.hpp.

References fsw::elodin::msg_id(), and publish().

◆ publish() [2/2]

template<typename MessageType>
bool fsw::elodin::ElodinClient::publish ( uint16_t message_id,
const MessageType & message )

Publish a message to a table.

Template Parameters
MessageTypeCommsMessage type
Parameters
table_idTable identifier (2-byte array)
messageMessage to publish
Returns
true if published successfully

Definition at line 260 of file ElodinClient.hpp.

References batch_buffer_, batching_, is_connected(), last_error_, lock, publish_mutex_, serialize_msg(), and socket_.

Referenced by main(), publish(), and anonymous_namespace{t7_bridge_main.cpp}::run_board().

◆ read_bytes_exact()

bool fsw::elodin::ElodinClient::read_bytes_exact ( uint8_t * buffer,
size_t len )

Blocking read of exactly len bytes (for payload reads after header).

Unlike read_data (non-blocking), this waits until all bytes arrive. Use after read_packet_header to avoid packet misalignment.

Returns
true if all bytes were read, false on error or disconnect

Definition at line 167 of file ElodinClient.cpp.

References is_connected(), last_error_, lock, publish_mutex_, and socket_.

◆ read_data()

ssize_t fsw::elodin::ElodinClient::read_data ( void * buffer,
size_t max_len )

Read incoming data packet from Elodin (non-blocking).

Parameters
bufferBuffer to read into
max_lenMaximum bytes to read
Returns
Number of bytes read, 0 if no data available, -1 on error

Definition at line 147 of file ElodinClient.cpp.

References is_connected(), last_error_, lock, publish_mutex_, and socket_.

Referenced by main(), and anonymous_namespace{t7_bridge_main.cpp}::run_board().

◆ read_packet()

ssize_t fsw::elodin::ElodinClient::read_packet ( uint8_t * packet_buffer,
size_t max_len )

Read complete packet from Elodin (header + payload).

Parameters
packet_bufferBuffer to read packet into
max_lenMaximum buffer size
Returns
Packet length if successful, 0 if no data, -1 on error

Definition at line 176 of file ElodinClient.cpp.

References chunk, is_connected(), last_error_, lock, publish_mutex_, scratch, and socket_.

Referenced by main(), and main().

◆ read_packet_header()

bool fsw::elodin::ElodinClient::read_packet_header ( uint8_t * header_buffer)

Read packet header from Elodin (12 bytes).

Parameters
header_bufferBuffer to read header into (must be at least 12 bytes)
Returns
true if header read successfully, false otherwise

Definition at line 157 of file ElodinClient.cpp.

References is_connected(), last_error_, lock, publish_mutex_, and socket_.

◆ reconnect()

bool fsw::elodin::ElodinClient::reconnect ( )

Attempt to reconnect using the last-used host/port.

Returns
true if reconnect succeeds.

Definition at line 46 of file ElodinClient.cpp.

References connect_locked(), last_error_, last_host_, last_port_, lock, and publish_mutex_.

Referenced by main().

◆ register_table()

void fsw::elodin::ElodinClient::register_table ( const std::array< uint8_t, 2 > & table_id,
const std::string & name )

Register a table ID with a name (for debugging/logging).

Definition at line 75 of file ElodinClient.cpp.

References table_names_.

◆ send_msg()

bool fsw::elodin::ElodinClient::send_msg ( const std::array< uint8_t, 2 > & packet_id,
const std::vector< uint8_t > & data )

Send a MSG packet (for VTable registration, etc.).

Parameters
packet_idPacket identifier (2-byte array)
dataRaw message data (already encoded with PacketHeader)
Returns
true if sent successfully

Definition at line 255 of file ElodinClient.cpp.

References is_connected(), last_error_, lock, publish_mutex_, and socket_.

Referenced by subscribe_table().

◆ serialize_msg()

template<typename MessageType>
std::vector< uint8_t > fsw::elodin::ElodinClient::serialize_msg ( uint16_t message_id,
const MessageType & msg )

Serialize a message for Elodin (public for testing).

Definition at line 216 of file ElodinClient.hpp.

References fsw::elodin::message_id_to_packet_id(), result, fsw::elodin::TABLE, and MessageSize< T >::value.

Referenced by publish().

◆ set_recv_timeout_ms()

void fsw::elodin::ElodinClient::set_recv_timeout_ms ( int timeout_ms)

Set socket receive timeout so read_packet() can yield.

0 = blocking (default). Call after connect().

Definition at line 90 of file ElodinClient.cpp.

References socket_.

Referenced by main().

◆ subscribe_table()

bool fsw::elodin::ElodinClient::subscribe_table ( uint8_t hi,
uint8_t lo,
uint8_t request_id )

One table, under request_id, which the DB echoes if it refuses it (ErrorResponse [0xE0, 0x1D]).

subscribe_tables() sends 0 for all, so a refusal there names nothing.

Definition at line 105 of file ElodinClient.cpp.

References fsw::elodin::MSG, and send_msg().

Referenced by main(), and subscribe_tables().

◆ subscribe_tables()

bool fsw::elodin::ElodinClient::subscribe_tables ( const std::vector< std::pair< uint8_t, uint8_t > > & table_ids)

Subscribe to specific tables by ID.

There is deliberately no "subscribe to everything" call. There used to be — subscribe_stream(), documented as "subscribe to all stream data" — but it was really the calibration service's table list under a general name: 480 raw sensor ids plus the calibration command table. heartbeat_service called it expecting everything, received 481 tables it discarded, and never received the one table it reads, so its engine_state sat at 0 forever. Elodin has no wildcard; every subscriber must name what it consumes.

See raw_sensor_tables() / calibrated_sensor_tables() in DatabaseConfig.hpp for the sensor id sets, and kTableSequencerState / kTableCalibrationCommand for the non-sensor ones.

Parameters
table_idsVector of (hi, lo) byte pairs identifying each table.
Returns
false if any subscribe message failed to send.

Definition at line 95 of file ElodinClient.cpp.

References ok, and subscribe_table().

Member Data Documentation

◆ batch_buffer_

std::vector<uint8_t> fsw::elodin::ElodinClient::batch_buffer_
private

Definition at line 197 of file ElodinClient.hpp.

Referenced by begin_batch(), flush_batch(), and publish().

◆ batching_

bool fsw::elodin::ElodinClient::batching_ = false
private

Definition at line 196 of file ElodinClient.hpp.

Referenced by begin_batch(), flush_batch(), is_batching(), and publish().

◆ last_error_

std::string fsw::elodin::ElodinClient::last_error_
private

◆ last_host_

std::string fsw::elodin::ElodinClient::last_host_
private

Definition at line 200 of file ElodinClient.hpp.

Referenced by connect_locked(), and reconnect().

◆ last_port_

uint16_t fsw::elodin::ElodinClient::last_port_ = 0
private

Definition at line 201 of file ElodinClient.hpp.

Referenced by connect_locked(), and reconnect().

◆ publish_mutex_

std::mutex fsw::elodin::ElodinClient::publish_mutex_
private

◆ socket_

◆ table_names_

std::unordered_map<uint16_t, std::string> fsw::elodin::ElodinClient::table_names_
private

Definition at line 192 of file ElodinClient.hpp.

Referenced by register_table().


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