HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NET_WebSocketAPIRouter.h
Go to the documentation of this file.
1 /*
2  * POPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: NET_WebSocketAPIRouter.h
7  *
8  * COMMENTS:
9  *
10  */
11 
12 #ifndef __NET_WEBSOCKETAPIROUTER_H__
13 #define __NET_WEBSOCKETAPIROUTER_H__
14 
15 #include "NET_API.h"
16 
17 #include "NET_RadixTree.h"
18 #include "NET_WebAPIHandler.h"
19 #include "NET_WebSocket.h"
20 #include "NET_WebStream.h"
22 
23 #include <UT/UT_JSONValue.h>
24 #include <UT/UT_JSONValueArray.h>
25 #include <UT/UT_JSONValueMap.h>
26 #include <UT/UT_StringHolder.h>
27 #include <UT/UT_UniquePtr.h>
28 
29 #include <functional>
30 
32 {
33  // TODO: Use a unique ptr and not a shared pointer for api functions. This
34  // requires NET_ServerApp to be a pointer of some kind. Likely a shared ptr
35  // to allow for sharing the app in the webserver and the HOM wrapper.
36 public:
37  using Middleware = std::function<void(NET_WebSocketAPIRequest&)>;
38 
39  /// @brief Handle an incoming websocket message.
40  ///
41  /// @param stream The webstream the message came from.
42  /// @param message The message that was received.
43  ///
44  /// @return True if the api function has decided the websocket should close.
45  bool handle(NET_WebStream* stream, const UT_StringHolder& message);
47 
49  {
50  myRequestMiddlewares.emplace_back(middleware);
51  }
53  {
54  myResponseMiddlewares.emplace_back(middleware);
55  }
56 
57  /// {@
58  /// Register a new api function with the router.
59  template <typename... Args>
61  const UT_StringRef& name,
62  void (*func)(NET_WebSocketAPIRequest& req, Args...),
64  {
65  myApiFunctions.insert(
66  name,
68  name, parr,
69  [func](NET_WebSocketAPIRequest& req, Args... args) {
70  (func)(req, std::forward<Args>(args)...);
71  }));
72  }
73  template <typename Cls, typename... Args>
75  const UT_StringRef& name,
76  void (Cls::*func)(NET_WebSocketAPIRequest& req, Args...),
77  Cls* owner,
79  {
80  myApiFunctions.insert(
81  name,
83  name, parr,
84  [func, owner](
85  NET_WebSocketAPIRequest& req, Args... args) {
86  (owner->*func)(req, std::forward<Args>(args)...);
87  }));
88  }
89  /// @}
90 
91 private:
92  bool callApi_(NET_WebSocketAPIRequest& req, const UT_StringHolder& msg);
93  void runRequestMiddleware_(NET_WebSocketAPIRequest& req);
94  void runResponseMiddleware_(NET_WebSocketAPIRequest& req);
95 
97 
98  UT_Array<Middleware> myRequestMiddlewares;
99  UT_Array<Middleware> myResponseMiddlewares;
100 };
101 
103 {
104 public:
105  static void responseMiddleware(NET_WebSocketAPIRequest& req);
106 };
107 
108 #endif // __NET_WEBSOCKETAPIROUTER_H__
109 
GLuint GLuint stream
Definition: glew.h:7265
GLuint const GLchar * name
Definition: glew.h:1814
A simple radix tree implementation.
Definition: NET_RadixTree.h:32
const Args & args
Definition: printf.h:628
std::shared_ptr< T > UT_SharedPtr
Wrapper around std::shared_ptr.
Definition: UT_SharedPtr.h:28
GLsizei GLenum GLuint GLuint GLsizei GLchar * message
Definition: glew.h:2581
#define UTmakeShared
Definition: UT_SharedPtr.h:33
GLenum func
Definition: glcorearb.h:782
void registerFunction(const UT_StringRef &name, void(Cls::*func)(NET_WebSocketAPIRequest &req, Args...), Cls *owner, const NET_WebAPIHandler::ParamArray &parr)
void addRequestMiddleware(Middleware middleware)
void addResponseMiddleware(Middleware middleware)
std::function< void(NET_WebSocketAPIRequest &)> Middleware
void registerFunction(const UT_StringRef &name, void(*func)(NET_WebSocketAPIRequest &req, Args...), const NET_WebAPIHandler::ParamArray &parr)