TDME2 1.9.121
ServerClientRequestHandlerHub.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <string>
5
6#include <tdme/tdme.h>
9
10using std::map;
11using std::string;
12
15
16namespace tdme {
17namespace network {
18namespace udpserver {
19
20/**
21 * @brief Network server client request handler hub
22 * @author Andreas Drewke
23 */
24template <class CLIENT, class REQUEST>
26public:
27 /**
28 * @brief Public constructor
29 */
31 }
32
33 /**
34 * @brief Public destructor
35 */
37 // delete handler
38 for (typename RequestHandlerMap::iterator i = requestHandlerMap.begin(); i != requestHandlerMap.end(); ++i) {
39 ServerClientRequestHandler<CLIENT,REQUEST>* handler = (*i).second;
40 // delete reference
41 delete handler;
42 }
43 }
44
45 /**
46 * @brief Adds a client request handler
47 * @param handler request handler
48 * @throws TCPServerClientRequestHandlerHubException
49 */
51 typename RequestHandlerMap::iterator it = requestHandlerMap.find(handler->getCommand());
52 if (it == requestHandlerMap.end()) {
53 requestHandlerMap[handler->getCommand()] = handler;
54 } else {
55 throw ServerClientRequestHandlerHubException("Handler for given command already exists");
56 }
57 }
58
59 /**
60 * @brief Sets the client request default handler, will be used if command not found in request handler map
61 * @param handler request handler
62 */
64 defaultHandler = handler;
65 }
66
67 /**
68 * Handles a client request
69 * @param client client
70 * @param command command
71 * @param request request
72 * @param messageId message id (udp server only)
73 * @param retries retries (udp server only)
74 */
75 void handleRequest(CLIENT* client, const string& command, REQUEST& request, const uint32_t messageId, const uint8_t retries) {
76 typename RequestHandlerMap::iterator it = requestHandlerMap.find(command);
77 // handler not identified?
78 if (it == requestHandlerMap.end()) {
79 // use default / fall back if exists
80 if (defaultHandler != NULL) {
81 defaultHandler->handleRequest(client, request, messageId, retries);
82 } else {
83 // otherwise report an exception
84 throw ServerClientRequestHandlerHubException("Handler for given command not found");
85 }
86 } else {
87 // found handler
89 handler->handleRequest(client, request, messageId, retries);
90 }
91 }
92private:
93 typedef map<string, ServerClientRequestHandler<CLIENT,REQUEST>*> RequestHandlerMap;
96};
97
98};
99};
100};
ServerClientRequestHandler< CLIENT, REQUEST > * defaultHandler
void addHandler(ServerClientRequestHandler< CLIENT, REQUEST > *handler)
Adds a client request handler.
void handleRequest(CLIENT *client, const string &command, REQUEST &request, const uint32_t messageId, const uint8_t retries)
Handles a client request.
void setDefaultHandler(ServerClientRequestHandler< CLIENT, REQUEST > *handler)
Sets the client request default handler, will be used if command not found in request handler map.
map< string, ServerClientRequestHandler< CLIENT, REQUEST > * > RequestHandlerMap
virtual void handleRequest(CLIENT *client, REQUEST &request, const uint32_t messageId, const uint8_t retries)=0
Request handler implementation.
const string & getCommand()
Returns this requests handler command name.
Definition: fwd-tdme.h:4