|
STAR
Cal STAR — firmware, DAQ server, and engine design
|
Standalone Elodin database client. More...
#include <ElodinClient.hpp>
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 |
Standalone Elodin database client.
No FSW dependencies - uses our own TCP client and protocol implementation.
Definition at line 29 of file ElodinClient.hpp.
| fsw::elodin::ElodinClient::ElodinClient | ( | ) |
Definition at line 10 of file ElodinClient.cpp.
References socket_.
| fsw::elodin::ElodinClient::~ElodinClient | ( | ) |
Definition at line 14 of file ElodinClient.cpp.
References disconnect().
| 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().
| bool fsw::elodin::ElodinClient::connect | ( | const std::string & | host, |
| uint16_t | port = 2240 ) |
Connect to Elodin database.
| host | Database host address |
| port | Database port (default 2240) |
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().
|
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().
| void fsw::elodin::ElodinClient::disconnect | ( | ) |
Disconnect from database.
Definition at line 57 of file ElodinClient.cpp.
References disconnect_locked(), lock, and publish_mutex_.
Referenced by anonymous_namespace{t7_bridge_main.cpp}::run_board(), smart_calibration_gui.SmartCalibrationGUI::toggle_connection(), and ~ElodinClient().
|
private |
Definition at line 62 of file ElodinClient.cpp.
References socket_.
Referenced by connect_locked(), and disconnect().
| bool fsw::elodin::ElodinClient::flush_batch | ( | ) |
Flush (send) the accumulated batch in a single TCP write.
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().
| void fsw::elodin::ElodinClient::flush_buffer | ( | ) |
Flush the write buffer to the database.
Definition at line 84 of file ElodinClient.cpp.
References socket_.
|
inline |
Returns true if we're between begin_batch() and flush_batch().
Definition at line 105 of file ElodinClient.hpp.
References batching_.
| bool fsw::elodin::ElodinClient::is_connected | ( | ) | const |
Check if connected.
Definition at line 71 of file ElodinClient.cpp.
References socket_.
Referenced by flush_batch(), main(), main(), publish(), read_bytes_exact(), read_data(), read_packet(), read_packet_header(), anonymous_namespace{t7_bridge_main.cpp}::run_board(), and send_msg().
| 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().
|
inline |
Definition at line 64 of file ElodinClient.hpp.
References fsw::elodin::msg_id(), and publish().
| bool fsw::elodin::ElodinClient::publish | ( | uint16_t | message_id, |
| const MessageType & | message ) |
Publish a message to a table.
| MessageType | CommsMessage type |
| table_id | Table identifier (2-byte array) |
| message | Message to publish |
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().
| 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.
Definition at line 167 of file ElodinClient.cpp.
References is_connected(), last_error_, lock, publish_mutex_, and socket_.
| ssize_t fsw::elodin::ElodinClient::read_data | ( | void * | buffer, |
| size_t | max_len ) |
Read incoming data packet from Elodin (non-blocking).
| buffer | Buffer to read into |
| max_len | Maximum bytes to read |
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().
| ssize_t fsw::elodin::ElodinClient::read_packet | ( | uint8_t * | packet_buffer, |
| size_t | max_len ) |
Read complete packet from Elodin (header + payload).
| packet_buffer | Buffer to read packet into |
| max_len | Maximum buffer size |
Definition at line 176 of file ElodinClient.cpp.
References chunk, is_connected(), last_error_, lock, publish_mutex_, scratch, and socket_.
| bool fsw::elodin::ElodinClient::read_packet_header | ( | uint8_t * | header_buffer | ) |
Read packet header from Elodin (12 bytes).
| header_buffer | Buffer to read header into (must be at least 12 bytes) |
Definition at line 157 of file ElodinClient.cpp.
References is_connected(), last_error_, lock, publish_mutex_, and socket_.
| bool fsw::elodin::ElodinClient::reconnect | ( | ) |
Attempt to reconnect using the last-used host/port.
Definition at line 46 of file ElodinClient.cpp.
References connect_locked(), last_error_, last_host_, last_port_, lock, and publish_mutex_.
Referenced by main().
| 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_.
| 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.).
| packet_id | Packet identifier (2-byte array) |
| data | Raw message data (already encoded with PacketHeader) |
Definition at line 255 of file ElodinClient.cpp.
References is_connected(), last_error_, lock, publish_mutex_, and socket_.
Referenced by subscribe_table().
| 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().
| 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().
| 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().
| 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.
| table_ids | Vector of (hi, lo) byte pairs identifying each table. |
Definition at line 95 of file ElodinClient.cpp.
References ok, and subscribe_table().
|
private |
Definition at line 197 of file ElodinClient.hpp.
Referenced by begin_batch(), flush_batch(), and publish().
|
private |
Definition at line 196 of file ElodinClient.hpp.
Referenced by begin_batch(), flush_batch(), is_batching(), and publish().
|
private |
Definition at line 193 of file ElodinClient.hpp.
Referenced by connect_locked(), flush_batch(), last_error(), publish(), read_bytes_exact(), read_data(), read_packet(), read_packet_header(), reconnect(), and send_msg().
|
private |
Definition at line 200 of file ElodinClient.hpp.
Referenced by connect_locked(), and reconnect().
|
private |
Definition at line 201 of file ElodinClient.hpp.
Referenced by connect_locked(), and reconnect().
|
private |
Definition at line 191 of file ElodinClient.hpp.
Referenced by connect(), disconnect(), flush_batch(), publish(), read_bytes_exact(), read_data(), read_packet(), read_packet_header(), reconnect(), and send_msg().
|
private |
Definition at line 190 of file ElodinClient.hpp.
Referenced by connect_locked(), disconnect_locked(), ElodinClient(), flush_batch(), flush_buffer(), is_connected(), publish(), read_bytes_exact(), read_data(), read_packet(), read_packet_header(), send_msg(), and set_recv_timeout_ms().
|
private |
Definition at line 192 of file ElodinClient.hpp.
Referenced by register_table().