IRSOL
C++ code implementing socket server for interacting with Baumer camera.
app.hpp
Go to the documentation of this file.
1
16#pragma once
17
24#include "irsol/types.hpp"
25
26#include <atomic>
27#include <mutex>
28#include <thread>
29#include <unordered_map>
30
31namespace irsol {
32namespace server {
33
34// Forward declaration
35class ClientSession;
36
45class App
46{
48 std::unordered_map<irsol::types::client_id_t, std::shared_ptr<ClientSession>>;
49
50public:
55 explicit App(irsol::types::port_t port);
56
64 bool start();
65
72 void stop();
73
79 std::shared_ptr<ClientSession> getClientSession(const irsol::types::client_id_t& clientId);
80
89 protocol::OutMessage&& message,
90 const std::optional<irsol::types::client_id_t>& excludeClient = std::nullopt);
91
97 {
98 return *m_cameraInterface;
99 };
100
109
115 {
116 return *m_messageHandler;
117 }
118
119private:
122
125
127 std::thread m_acceptThread;
128
130 std::mutex m_clientsMutex;
131
134
136 std::unique_ptr<camera::Interface> m_cameraInterface;
137
139 std::unique_ptr<frame_collector::FrameCollector> m_frameCollector;
140
142 std::unique_ptr<handlers::MessageHandler> m_messageHandler;
143
151 void addClient(const irsol::types::client_id_t& clientId, irsol::types::socket_t&& sock);
152
159 void removeClient(const irsol::types::client_id_t& clientId);
160
167
181 template<typename InMessageT, typename HandlerT, typename... Args>
182 void registerMessageHandler(const std::string& identifier, Args&&... args)
183 {
184 auto handler = handlers::makeHandler<HandlerT>(std::forward<Args>(args)...);
186
187 if(!messageHandler.template registerHandler<InMessageT>(identifier, handler)) {
188 IRSOL_LOG_FATAL("Failed to register handler for identifier {}", identifier);
189 throw std::runtime_error("Failed to register handler for identifier '" + identifier + "'");
190 }
191 }
192
212 template<typename InMessageT, typename LambdaT>
214 const std::string& identifier,
215 std::shared_ptr<handlers::Context> ctx,
216 LambdaT&& lambda)
217 {
218 auto handler = handlers::makeLambdaHandler<InMessageT>(ctx, std::forward<LambdaT>(lambda));
220
221 if(!messageHandler.template registerHandler<InMessageT>(identifier, handler)) {
222 IRSOL_LOG_FATAL("Failed to register lambda handler for identifier {}", identifier);
223 throw std::runtime_error(
224 "Failed to register lambda handler for identifier '" + identifier + "'");
225 }
226 }
227};
228
229} // namespace server
230} // namespace irsol
Accepts and forwards incoming client connections to the session handler.
High-level wrapper around the NeoAPI camera for synchronized access.
Definition interface.hpp:61
Main server application that manages client connections and camera streaming.
Definition app.hpp:46
const irsol::types::port_t m_port
TCP port on which the server listens.
Definition app.hpp:121
std::unordered_map< irsol::types::client_id_t, std::shared_ptr< ClientSession > > client_map_t
Definition app.hpp:48
irsol::server::internal::ClientSessionAcceptor m_acceptor
Acceptor that handles incoming client connections.
Definition app.hpp:124
camera::Interface & camera()
Accessor for the camera interface.
Definition app.hpp:96
const handlers::MessageHandler & messageHandler() const
Accessor for the message handler.
Definition app.hpp:114
std::thread m_acceptThread
Thread running the connection acceptor loop.
Definition app.hpp:127
void addClient(const irsol::types::client_id_t &clientId, irsol::types::socket_t &&sock)
Adds a new client session.
Definition app.cpp:85
std::unique_ptr< frame_collector::FrameCollector > m_frameCollector
Frame collector for capturing and broadcasting camera frames.
Definition app.hpp:139
frame_collector::FrameCollector & frameCollector()
Accessor for the frame collector.
Definition app.hpp:105
std::shared_ptr< ClientSession > getClientSession(const irsol::types::client_id_t &clientId)
Retrieves an active client session.
Definition app.cpp:53
void registerMessageHandler(const std::string &identifier, Args &&... args)
Registers a message handler by type.
Definition app.hpp:182
void broadcastMessage(protocol::OutMessage &&message, const std::optional< irsol::types::client_id_t > &excludeClient=std::nullopt)
Broadcasts a message to all connected clients.
Definition app.cpp:61
void stop()
Stops the server.
Definition app.cpp:39
bool start()
Starts the server.
Definition app.cpp:26
void registerMessageHandlers()
Registers standard message handlers.
Definition app.cpp:132
client_map_t m_clients
Map of connected clients.
Definition app.hpp:133
std::unique_ptr< camera::Interface > m_cameraInterface
Interface to the camera.
Definition app.hpp:136
void removeClient(const irsol::types::client_id_t &clientId)
Removes a client session.
Definition app.cpp:116
void registerLambdaHandler(const std::string &identifier, std::shared_ptr< handlers::Context > ctx, LambdaT &&lambda)
Registers a lambda-based message handler.
Definition app.hpp:213
std::mutex m_clientsMutex
Mutex for protecting access to m_clients.
Definition app.hpp:130
std::unique_ptr< handlers::MessageHandler > m_messageHandler
Central handler for processing protocol messages.
Definition app.hpp:142
Coordinates frame acquisition from a camera and distributes frames to registered clients.
Definition collector.hpp:70
Binds incoming protocol messages to the appropriate per-client logic.
Accepts incoming TCP connections and triggers a callback for each new client.
Definition acceptor.hpp:49
Factory utilities for constructing handler instances.
#define IRSOL_LOG_FATAL(...)
Logs a fatal (critical) message using the default logger.
Definition logging.hpp:95
std::variant< Success, BinaryDataBuffer, ImageBinaryData, ColorImageBinaryData, Error > OutMessage
Variant type representing any outgoing message.
Definition variants.hpp:149
High-level wrapper around NeoAPI camera control for the irsol library.
Message routing layer between protocol and application logic.
constexpr auto makeHandler(std::shared_ptr< Context > ctx, Args &&... args)
Constructs a handler instance of the given type.
Definition factory.hpp:28
std::string client_id_t
Represents a unique client identifier. Typically used to identify connected clients by string IDs.
Definition types.hpp:55
uint16_t port_t
Represents a network port number. Typically used to specify TCP or UDP ports.
Definition types.hpp:48
sockpp::tcp_socket socket_t
Alias for a TCP socket.
Definition types.hpp:87
Core type definitions for networking, time handling, and protocol values used throughout the irsol li...