IRSOL
C++ code implementing socket server for interacting with Baumer camera.
command_gi_base.cpp
Go to the documentation of this file.
2
3#include "irsol/logging.hpp"
4#include "irsol/macros.hpp"
5#include "irsol/protocol.hpp"
8#include "irsol/types.hpp"
9#include "irsol/utils.hpp"
10
11#include <memory>
12#include <mutex>
13#include <sstream>
14#include <vector>
15
16namespace irsol {
17namespace server {
18namespace handlers {
19namespace internal {
20
21CommandGIBaseHandler::CommandGIBaseHandler(std::shared_ptr<Context> ctx): CommandHandler(ctx) {}
22
23std::vector<out_message_t>
25 std::shared_ptr<irsol::server::ClientSession> session,
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}
53
54std::string
57 std::shared_ptr<irsol::server::ClientSession> session) const
58{
59 std::stringstream ss;
60 ss << session->id() << " - " << message.toString();
61 return ss.str();
62}
63
64void
66 std::shared_ptr<irsol::server::ClientSession> session,
67 std::shared_ptr<irsol::server::frame_collector::FrameCollector::frame_queue_t> queue,
69 const std::string& description)
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}
120} // namespace internal
121} // namespace handlers
122} // namespace server
123} // namespace irsol
Main server application managing client connections and camera streaming.
std::vector< out_message_t > process(std::shared_ptr< irsol::server::ClientSession > session, protocol::Command &&message) final override
Processes a frame acquisition command, starting the frame collection thread.
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.
CommandGIBaseHandler(std::shared_ptr< Context > ctx)
Constructs the CommandGIBaseHandler.
Generic handler base class for protocol messages.
Definition base.hpp:39
std::shared_ptr< Context > ctx
Handler context (provides access to app and utilities).
Definition base.hpp:81
Declaration of the CommandGIBaseHandler class.
#define IRSOL_NAMED_LOG_INFO(name,...)
Logs an info-level message using a named logger.
Definition logging.hpp:176
#define IRSOL_NAMED_LOG_DEBUG(name,...)
Logs a debug-level message using a named logger.
Definition logging.hpp:174
#define IRSOL_NAMED_LOG_WARN(name,...)
Logs a warning-level message using a named logger.
Definition logging.hpp:178
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
Definition variants.hpp:149
Logging utilities and configuration for the irsol library.
Common portability and diagnostic macros for the irsol library.
#define IRSOL_MAYBE_UNUSED
Suppresses compiler warnings about unused variables or parameters.
Definition macros.hpp:39
constexpr auto makeHandler(std::shared_ptr< Context > ctx, Args &&... args)
Constructs a handler instance of the given type.
Definition factory.hpp:28
Represents a command invocation in the protocol.
Definition command.hpp:33
static Error from(const T &msg, const std::string &description)
Creates an error from a specific incoming message type.
Definition error.hpp:63
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
auto result
Core type definitions for networking, time handling, and protocol values used throughout the irsol li...
General utility functions used throughout the irsol library.