RoutingWsWebApp.hpp
Go to the documentation of this file.
1 // @formatter:off
2 //
3 // Balau core C++ library
4 //
5 // Copyright (C) 2017 Bora Software (contact@borasoftware.com)
6 //
7 // Licensed under the Boost Software License - Version 1.0 - August 17th, 2003.
8 // See the LICENSE file for the full license text.
9 //
10 
16 
17 #ifndef COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_WS_WEB_APPS__ROUTING_WS_WEB_APP
18 #define COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_WS_WEB_APPS__ROUTING_WS_WEB_APP
19 
22 
24 
49 class RoutingWsWebApp : public WsWebApp {
53  public: using WsWebAppPtr = std::shared_ptr<WsWebApp>;
54 
58  public: using Value = std::tuple<std::string, WsWebAppPtr, WsWebAppPtr, WsWebAppPtr>;
59 
64 
69 
73  public: explicit RoutingWsWebApp(Routing && routing_);
74 
76 
77  public: void handleTextMessage(WsSession & session, std::string_view path) override;
78 
79  public: void handleBinaryMessage(WsSession & session, std::string_view path) override;
80 
81  public: void handleClose(WsSession & session, std::string_view path) override;
82 
83  public: void handlePing(WsSession & session, std::string_view path) override;
84 
85  public: void handlePong(WsSession & session, std::string_view path) override;
86 
88 
89  private: WsWebApp * resolve(WsSession & session, std::string_view path);
90 
91  private: static constexpr size_t KeyIndex = 0;
92  private: static constexpr size_t ControlHandlerIndex = 1;
93  private: static constexpr size_t TextHandlerIndex = 2;
94 
95  private: Routing routing;
96 };
97 
104 template <typename HandlerT, typename ... ParamT>
105 inline RoutingWsWebApp::Value routingNode(const std::string & key, ParamT && ... param) {
106  auto handler = std::shared_ptr<WsWebApp>(new HandlerT(std::move(param) ...));
107  return RoutingWsWebApp::Value(key, handler, handler, handler);
108 }
109 
116 template <typename HandlerT>
117 inline RoutingWsWebApp::Value routingNode(const std::string & key) {
118  auto handler = std::shared_ptr<WsWebApp>(new HandlerT);
119  return RoutingWsWebApp::Value(key, handler, handler, handler);
120 }
121 
125 inline RoutingWsWebApp::Value routingNode(const std::string & key) {
126  auto handler = std::shared_ptr<WsWebApp>(nullptr);
127  return RoutingWsWebApp::Value(key, handler, handler, handler);
128 }
129 
130 } // namespace Balau::Network::Http::WsWebApps
131 
132 #endif // COM_BORA_SOFTWARE__BALAU_NETWORK_HTTP_SERVER_WS_WEB_APPS__ROUTING_WS_WEB_APP
Abstract base class of WebSocket web application handlers.
Abstract base class of WebSocket web application handlers.
Definition: WsWebApp.hpp:36
std::shared_ptr< WsWebApp > WsWebAppPtr
Shared pointer container for WebSocket web app instances.
Definition: RoutingWsWebApp.hpp:53
void handleTextMessage(WsSession &session, std::string_view path) override
Handle received text data.
std::tuple< std::string, WsWebAppPtr, WsWebAppPtr, WsWebAppPtr > Value
The type of the routing node values added to the routing trie.
Definition: RoutingWsWebApp.hpp:58
RoutingWsWebApp(Routing &&routing_)
Construct a routing HTTP handler, by supplying a preformed routing trie.
void handleBinaryMessage(WsSession &session, std::string_view path) override
Handle received binary data.
void handlePing(WsSession &session, std::string_view path) override
Handle a received ping control message.
void handlePong(WsSession &session, std::string_view path) override
Handle a received pong control message.
void handleClose(WsSession &session, std::string_view path) override
Handle a received close control message.
A WebSocket web application handler that routes to other handlers.
Definition: RoutingWsWebApp.hpp:49
An object based trie used for parent-child hierarchies.
Manages the handling of WebSocket messages in a WebSocket connection.
Definition: WsSession.hpp:35
RoutingWsWebApp::Value routingNode(const std::string &key, ParamT &&... param)
Convenience function to make a routing node from a handler type, a string key, and the handler constr...
Definition: RoutingWsWebApp.hpp:105
A set of WebSocket web apps provided by the Balau library.
Definition: EchoingWsWebApp.hpp:22