IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::server::handlers::internal::CommandGIBaseHandler Class Referenceabstract

Base handler for frame acquisition commands (gi, gis). More...

#include <command_gi_base.hpp>

Inheritance diagram for irsol::server::handlers::internal::CommandGIBaseHandler:
irsol::server::handlers::internal::HandlerBase< T, > irsol::server::handlers::CommandGIHandler irsol::server::handlers::CommandGISHandler

Public Member Functions

 CommandGIBaseHandler (std::shared_ptr< Context > ctx)
 Constructs the CommandGIBaseHandler.
 
- Public Member Functions inherited from irsol::server::handlers::internal::HandlerBase< T, >
virtual std::vector< out_message_toperator() (const irsol::types::client_id_t &clientId, T &&message)
 Invokes the handler for a given client and message.
 

Protected Member Functions

std::vector< out_message_tprocess (std::shared_ptr< irsol::server::ClientSession > session, protocol::Command &&message) final override
 Processes a frame acquisition command, starting the frame collection thread.
 
- Protected Member Functions inherited from irsol::server::handlers::internal::HandlerBase< T, >
 HandlerBase (std::shared_ptr< Context > ctx)
 Constructs the handler base.
 
virtual std::vector< out_message_tprocess (std::shared_ptr< irsol::server::ClientSession > session, T &&message)=0
 Processes the protocol message for the given session.
 

Private Member Functions

std::string getDescription (const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const
 Generates a description string for the command execution.
 
virtual std::vector< irsol::protocol::OutMessagevalidate (const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const =0
 Validates parameters before starting listener thread.
 
virtual uint64_t getInputSequenceLength (const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const =0
 Retrieves the input sequence length to use to start the listening.
 
virtual double getFrameRate (const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const =0
 Retrieves the frame rate to use to start the listening.
 
void startListeningThread (std::shared_ptr< irsol::server::ClientSession > session, std::shared_ptr< irsol::server::frame_collector::FrameCollector::frame_queue_t > queue, protocol::Command &&command, const std::string &description)
 Starts the frame listening thread for the client session.
 

Additional Inherited Members

- Protected Attributes inherited from irsol::server::handlers::internal::HandlerBase< T, >
std::shared_ptr< Contextctx
 Handler context (provides access to app and utilities).
 

Detailed Description

Base handler for frame acquisition commands (gi, gis).

Provides common logic for starting frame collection and managing client registration.

See also
irsol::server::handlers::CommandGIHandler
irsol::server::handlers::CommandGISHandler

Definition at line 32 of file command_gi_base.hpp.

Constructor & Destructor Documentation

◆ CommandGIBaseHandler()

irsol::server::handlers::internal::CommandGIBaseHandler::CommandGIBaseHandler ( std::shared_ptr< Context ctx)

Constructs the CommandGIBaseHandler.

Parameters
ctxHandler context.

Definition at line 21 of file command_gi_base.cpp.

std::shared_ptr< Context > ctx
Handler context (provides access to app and utilities).
Definition base.hpp:81
internal::HandlerBase< protocol::Command > CommandHandler
Handler type for command messages.
Definition base.hpp:96

Member Function Documentation

◆ getDescription()

std::string irsol::server::handlers::internal::CommandGIBaseHandler::getDescription ( const protocol::Command message,
std::shared_ptr< irsol::server::ClientSession session 
) const
private

Generates a description string for the command execution.

Parameters
messageThe command message.
sessionThe client session.
Returns
Description string.

Definition at line 55 of file command_gi_base.cpp.

58{
59 std::stringstream ss;
60 ss << session->id() << " - " << message.toString();
61 return ss.str();
62}
constexpr auto makeHandler(std::shared_ptr< Context > ctx, Args &&... args)
Constructs a handler instance of the given type.
Definition factory.hpp:28

◆ getFrameRate()

virtual double irsol::server::handlers::internal::CommandGIBaseHandler::getFrameRate ( const protocol::Command message,
std::shared_ptr< irsol::server::ClientSession session 
) const
privatepure virtual

Retrieves the frame rate to use to start the listening.

Parameters
messageThe command message.
sessionThe client session.
Returns
Frame rate for acquisition.
Note
This method must be implemented by derived classes to provide specific frame rate logic.
See also
irsol::server::handlers::CommandGIHandler::getFrameRate
irsol::server::handlers::CommandGISHandler::getFrameRate

Implemented in irsol::server::handlers::CommandGIHandler, and irsol::server::handlers::CommandGISHandler.

◆ getInputSequenceLength()

virtual uint64_t irsol::server::handlers::internal::CommandGIBaseHandler::getInputSequenceLength ( const protocol::Command message,
std::shared_ptr< irsol::server::ClientSession session 
) const
privatepure virtual

Retrieves the input sequence length to use to start the listening.

Parameters
messageThe command message.
sessionThe client session.
Returns
Number of frames to acquire.
Note
This method must be implemented by derived classes to provide specific sequence length logic.
See also
irsol::server::handlers::CommandGIHandler::getInputSequenceLength
irsol::server::handlers::CommandGISHandler::getInputSequenceLength

Implemented in irsol::server::handlers::CommandGIHandler, and irsol::server::handlers::CommandGISHandler.

◆ process()

std::vector< out_message_t > irsol::server::handlers::internal::CommandGIBaseHandler::process ( std::shared_ptr< irsol::server::ClientSession session,
protocol::Command &&  message 
)
finaloverrideprotected

Processes a frame acquisition command, starting the frame collection thread.

Parameters
sessionThe client session.
messageThe command message.
Returns
Vector of outbound messages (success or error).

Definition at line 24 of file command_gi_base.cpp.

27{
28
29 auto& collector = ctx->app.frameCollector();
30 auto& state = session->userData().frameListeningState;
31
32 if(state.running()) {
33 IRSOL_NAMED_LOG_WARN(session->id(), "Session already listening to frames. Ignoring request.");
34 std::vector<out_message_t> result;
35 result.emplace_back(protocol::Error::from(message, "Session is already listening to frames"));
36 return result;
37 }
38
39 if(auto errors = validate(message, session); errors.size()) {
40 return errors;
41 }
42
43 auto queue = collector.makeQueuePtr();
45
47 const double fps = getFrameRate(message, session);
48 collector.registerClient(session->id(), fps, queue, numFrames);
49
50 IRSOL_NAMED_LOG_INFO(session->id(), "Client registered for {} frames at FPS {}", numFrames, fps);
51 return {};
52}
void startListeningThread(std::shared_ptr< irsol::server::ClientSession > session, std::shared_ptr< irsol::server::frame_collector::FrameCollector::frame_queue_t > queue, protocol::Command &&command, const std::string &description)
Starts the frame listening thread for the client session.
virtual std::vector< irsol::protocol::OutMessage > validate(const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const =0
Validates parameters before starting listener thread.
virtual double getFrameRate(const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const =0
Retrieves the frame rate to use to start the listening.
std::string getDescription(const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const
Generates a description string for the command execution.
virtual uint64_t getInputSequenceLength(const protocol::Command &message, std::shared_ptr< irsol::server::ClientSession > session) const =0
Retrieves the input sequence length to use to start the listening.
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
Definition logging.hpp:176
#define IRSOL_NAMED_LOG_WARN(name,...)
Logs a warning-level message using a named logger.
Definition logging.hpp:178
static Error from(const T &msg, const std::string &description)
Creates an error from a specific incoming message type.
Definition error.hpp:63
auto result

◆ startListeningThread()

void irsol::server::handlers::internal::CommandGIBaseHandler::startListeningThread ( std::shared_ptr< irsol::server::ClientSession session,
std::shared_ptr< irsol::server::frame_collector::FrameCollector::frame_queue_t queue,
protocol::Command &&  command,
const std::string &  description 
)
private

Starts the frame listening thread for the client session.

Parameters
sessionThe client session.
queueThe frame queue for collected frames.
commandThe command message.
descriptionDescription string for logging.
See also
irsol::server::internal::FrameListeningState::start

Definition at line 65 of file command_gi_base.cpp.

70{
71 auto& state = session->userData().frameListeningState;
72
73 state.start(
74 [session, queue, message = std::move(command)](
75 std::shared_ptr<std::atomic<bool>> stopRequest) mutable {
76 // Reset the state of the user-data related to frame-listening
77 auto& state = session->userData().frameListeningState;
78 state.gisParams.inputSequenceNumber = 0;
79
81 session->id(), "Started frame listening thread for {}", message.toString());
82
83 std::unique_ptr<frame_collector::Frame> framePtr;
84 bool interrupted = false;
85 while(!interrupted && (!queue->done() && queue->pop(framePtr))) {
87 session->id(),
88 "Sending frame {} to client: {}",
89 state.gisParams.inputSequenceNumber,
90 framePtr->image.toString());
91 {
92 auto lock = std::scoped_lock(session->socketMutex());
93 std::vector<irsol::protocol::OutMessage> result;
94 result.emplace_back(irsol::protocol::OutMessage(std::move(framePtr->image)));
96 "isn", {static_cast<int>(state.gisParams.inputSequenceNumber)}));
97 session->handleOutMessages(std::move(result));
98 }
99 ++state.gisParams.inputSequenceNumber;
100 if(stopRequest->load()) {
101 interrupted = true;
102 }
103 }
104
105 if(interrupted) {
107 session->id(), "Stopping execution of frame-collection due to stop-request.");
108 // TODO: on user-stop requests do we want to return a success?
109 return;
110 }
111
112 {
113 auto lock = std::scoped_lock(session->socketMutex());
114 session->handleOutMessage(protocol::Success::from(message));
115 }
116 IRSOL_NAMED_LOG_INFO(session->id(), "{} frame sending complete", message.toString());
117 },
119}
#define IRSOL_NAMED_LOG_DEBUG(name,...)
Logs a debug-level message using a named logger.
Definition logging.hpp:174
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
Definition variants.hpp:149
static Success from(const Assignment &msg, std::optional< irsol::types::protocol_value_t > overrideValue=std::nullopt)
Creates a success message from an Assignment.
Definition success.hpp:75
static Success asStatus(const std::string &identifier, irsol::types::protocol_value_t value)
Creates a standalone status message with a value.
Definition success.hpp:116
auto description

◆ validate()

virtual std::vector< irsol::protocol::OutMessage > irsol::server::handlers::internal::CommandGIBaseHandler::validate ( const protocol::Command message,
std::shared_ptr< irsol::server::ClientSession session 
) const
privatepure virtual

Validates parameters before starting listener thread.

Parameters
messageThe command message.
sessionThe client session.
Returns
Vector of protocol::Error messages (empty if no error).
Note
This method must be implemented by derived classes to provide specific validation logic.
See also
irsol::server::handlers::CommandGIHandler::validate
irsol::server::handlers::CommandGISHandler::validate

Implemented in irsol::server::handlers::CommandGIHandler, and irsol::server::handlers::CommandGISHandler.


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