This repository was archived by the owner on May 11, 2026. It is now read-only.
forked from Dmitry666/HTTPCommandService
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.h
More file actions
464 lines (385 loc) · 13 KB
/
Copy pathcontroller.h
File metadata and controls
464 lines (385 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#ifndef HTTP_CONTROLLER_H
#define HTTP_CONTROLLER_H
#include "common.h"
#include "sessionmanager.h"
namespace openrc {
//typedef std::map<std::string, std::string> ControllerArguments;
/**
* @brief Controller map input argument.
*/
struct ControllerArguments
{
typedef std::map<std::string, std::string> ArgumentsContainer;
ControllerArguments()
{}
ControllerArguments(const ControllerArguments& ca)
: _argumentMap(ca._argumentMap)
{}
ControllerArguments(const ArgumentsContainer& argumentMap)
: _argumentMap(argumentMap)
{}
std::string operator [](const std::string& key) const
{
auto it = _argumentMap.find(key);
if(it != _argumentMap.end())
{
return it->second;
}
return "";
}
const std::map<std::string, std::string>& ToArgumentMap() const { return _argumentMap; }
// From stl.
ArgumentsContainer::iterator begin() {return _argumentMap.begin();}
ArgumentsContainer::const_iterator begin() const {return _argumentMap.begin();}
ArgumentsContainer::iterator end() {return _argumentMap.end();}
ArgumentsContainer::const_iterator end() const {return _argumentMap.end();}
ArgumentsContainer::iterator find(const std::string& key) {return _argumentMap.find(key);}
ArgumentsContainer::const_iterator find(const std::string& key) const {return _argumentMap.find(key);}
private:
ArgumentsContainer _argumentMap;
};
/**
* @brief Controller output body.
*/
struct ControllerOutput
{
ControllerOutput()
{}
HCORE_API void Append(const std::string& value);
// From stl.
void append(const std::string& value)
{
Append(value);
}
const std::string& GetBody() const { return _body; }
private:
std::string _body;
};
#define CONTROLLER_REGISTER(classname, textName, description) \
private: \
static const TControllerRegistrar<classname, ControllerManager> creator##classname; \
public: \
classname() \
: IController(textName, description) \
{} \
\
virtual bool Construct() override; \
virtual const char* ClassName() { return GetClassNameStatic(); } \
/*virtual void RegisterMethods();*/ \
static const char* GetClassNameStatic() { return textName; } \
private:
#define CONTROLLERVALIDATE_REGISTER(classname, textName, description) \
private: \
static const TControllerRegistrar<classname, ControllerManager> creator##classname; \
public: \
classname() \
: IController(textName, description) \
{} \
\
virtual bool Construct() override; \
virtual const char* ClassName() { return GetClassNameStatic(); } \
virtual bool Validate(SessionWeak session, const ControllerArguments& arguments) override;\
/*virtual void RegisterMethods();*/ \
static const char* GetClassNameStatic() { return textName; } \
private:
#define CONTROLLER_REGISTERIMPL(classname) \
const TControllerRegistrar<classname, ControllerManager> classname::creator##classname;
#define CONTROLLER_ACTION(classname, methodname) \
void methodname(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContents); \
static const TMethodRegistrar<classname> creator##classname##methodname;
#define CONTROLLER_ACTIONIMPL(classname, methodname, actionname, description) \
const TMethodRegistrar<classname> classname::creator##classname##methodname(actionname, description, &classname::methodname);
#define CONTROLLER_ACTIONVALIDATE(classname, methodname) \
void methodname(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContents); \
bool methodname##Validate(SessionWeak session, const ControllerArguments& arguments); \
static const TMethodRegistrar<classname> creator##classname##methodname;
#define CONTROLLER_ACTIONVALIDATEIMPL(classname, methodname, actionname, description) \
const TMethodRegistrar<classname> \
classname::creator##classname##methodname \
(actionname, description, &classname::methodname, &classname::methodname##Validate);
/*
class ControllerMethod
{
public:
ControllerMethod();
protected:
std::string _name;
};
*/
/**
* @brief Controller action.
*/
class ControllerMethod
{
public:
ControllerMethod(const std::string& name, const std::string& description)
: _name(name)
, _description(description)
{}
virtual ~ControllerMethod()
{}
/**
* @brief Method contain validate expression.
* @return success.
*/
virtual bool IsValidateMethod() const = 0;
/**
* @brief Validate method from session.
* @param sessionId session identificator.
* @param arguments input arguments.
* @return validation success.
*/
virtual bool Validate(class IController* obj,
SessionWeak session,
const ControllerArguments& arguments) = 0;
/**
* @brief Execute this controller action.
* @param obj controller pointer.
* @param sessionId session identificator.
* @param arguments request arguments.
* @param contents out content.
*/
virtual void Execute(class IController* obj,
SessionWeak session,
const ControllerArguments& arguments,
ControllerOutput& contents) = 0;
/**
* @brief Execute operator.
* @param obj controller pointer.
* @param sessionId session identificator.
* @param arguments request arguments.
* @param contents out content.
* @return controller method reference.
*/
HCORE_API ControllerMethod& operator ()(class IController* obj,
SessionWeak session,
const ControllerArguments& arguments,
ControllerOutput& contents);
//
const std::string& GetName() const {return _name;}
const std::string& GetDescription() const {return _description;}
private:
std::string _name;
std::string _description;
};
/**
* @brief Template controller action.
*/
template<typename ClassType>
class TControllerMethod : public ControllerMethod
{
public:
#ifdef _MSC_VER
typedef void(ClassType::*FunctionType)(SessionWeak session, const ControllerArguments&, ControllerOutput& outContent);
typedef bool(ClassType::*ValidateType)(SessionWeak session, const ControllerArguments&);
#else
typedef std::function<void(ClassType&, SessionWeak session, const ControllerArguments&, ControllerOutput& outContent)> FunctionType;
typedef std::function<bool(ClassType&, SessionWeak session, const ControllerArguments&)> ValidateType;
#endif
public:
TControllerMethod(const std::string& name,
const std::string& description,
FunctionType function,
ValidateType validate)
: ControllerMethod(name, description)
, _function(function)
, _validate(validate)
{}
virtual bool IsValidateMethod() const override
{
return _validate ? true : false;
}
virtual bool Validate(class IController* obj,
SessionWeak session,
const ControllerArguments& arguments) override
{
if(_validate)
{
ClassType* class_ = static_cast<ClassType*>(obj);
#ifdef _MSC_VER
return (class_->*_validate)(session, arguments);
#else
return _validate(*class_, session, arguments);
#endif
}
return true;
}
virtual void Execute(class IController* obj,
SessionWeak session,
const ControllerArguments& arguments,
ControllerOutput& contents) override
{
ClassType* class_ = static_cast<ClassType*>(obj);
#ifdef _MSC_VER
(class_->*_function)(session, arguments, contents);
#else
_function(*class_, session, arguments, contents);
#endif
}
private:
FunctionType _function;
ValidateType _validate;
};
typedef ControllerMethod* ControllerMethodRef;
/**
* @brief Controller interface.
*/
class IController
{
//typedef std::function<void(const IController&, const std::vector<std::string>&, std::string&)> ControllerMethod;
public:
HCORE_API IController(const std::string& name, const std::string& description);
virtual ~IController()
{}
virtual bool Construct() = 0;
virtual const char* ClassName() = 0;
virtual bool BeginAction()
{
return true;
}
virtual bool EndAction()
{
return true;
}
/**
* @brief Validate controller from session.
* @param sessionId session identificator.
* @param arguments input arguments.
* @return validation success.
*/
virtual bool Validate(SessionWeak session, const ControllerArguments& arguments)
{
UNUSED(session)
UNUSED(arguments)
return true;
}
/**
* @brief Find method
* @param name method name.
* @return method.
*/
HCORE_API virtual ControllerMethodRef FindMethod(const std::string& name);
/**
* @brief Register new method.
* @param name method name.
* @param method method.
*/
HCORE_API void RegisterMethod(ControllerMethodRef method);
template<typename ClassType>
void TRegisterMethod(const std::string& name,
const std::string& description,
void(ClassType:: *method)(SessionWeak, const ControllerArguments&, ControllerOutput& outContent),
bool(ClassType:: *validate)(SessionWeak, const ControllerArguments&) = nullptr)
{
typename TControllerMethod<ClassType>::FunctionType actionFunc = method;
typename TControllerMethod<ClassType>::ValidateType validateFunc =
validate != nullptr ? validate : typename TControllerMethod<ClassType>::ValidateType();
RegisterMethod(new TControllerMethod<ClassType>(name, description, actionFunc, validateFunc));
}
/**
* @brief Call action by name.
* @param actionName controller action name.
* @param arguments action arguments.
* @param outContent ouput data.
* @return is action finded.
*/
virtual bool ActionExecute(
const std::string& actionName,
SessionWeak session,
const ControllerArguments& arguments,
ControllerOutput& outContent)
{
auto methodRef = FindMethod(actionName);
if( methodRef == nullptr )
{
return false;
}
(*methodRef)(this, session, arguments, outContent);
return true;
}
//
const std::string& GetName() const {return _name;}
const std::string& GetDescription() const {return _description;}
const std::map<std::string, ControllerMethodRef>& GetActions() const {return _methods;}
bool IsEnable() const { return _enable; }
void BindCurrentMethod(ControllerMethodRef method);
void UnBindCurrentMethod(ControllerMethodRef method);
protected:
ControllerMethodRef GetCurrentMethod() const { return _currentMethod; }
std::string GetCurrentMethodName() const { return _currentMethod != nullptr ? _currentMethod->GetName() : ""; }
protected:
std::string _name;
std::string _description;
std::map<std::string, ControllerMethodRef> _methods;
//
bool _enable;
private:
ControllerMethodRef _currentMethod;
};
/**
* @brief Static controller container class.
*/
class ControllerManager
{
public:
/**
* @brief Add new controller.
* @param controller controller pointer.
*/
HCORE_API static void RegisterController(IController* controller);
/**
* @brief Construct all controllers.
*/
HCORE_API static void ConstructControllers();
/**
* @brief Find controller by name.
* @param name controller name.
* @return controller pointer.
*/
HCORE_API static IController* FindController(const std::string& name);
/**
* @brief Get all controllers.
* @return Controllers map.
*/
HCORE_API static const std::map<std::string, IController*>& GetControllers();
HCORE_API static IController* GetController(const std::string& name);
template<typename ClassType>
static IController* Get()
{
return GetController(ClassType::GetClassNameStatic());
}
private:
//static ControllerManager& Instance();
static std::map<std::string, IController*> _controllers;
};
/**
* @brief Template class from registration controller.
*/
template<typename Type, typename Collection>
class TControllerRegistrar
{
public:
TControllerRegistrar()
{
Collection::RegisterController(new Type() );
}
};
/**
* @brief Template class from registration controller method.
*/
template<typename ClassType>
class TMethodRegistrar
{
public:
TMethodRegistrar(const std::string& name,
const std::string& description,
void(ClassType:: *function)(SessionWeak session, const ControllerArguments&, ControllerOutput&),
bool(ClassType:: *validate)(SessionWeak session, const ControllerArguments&) = nullptr
)
{
IController* controller = ControllerManager::Get<ClassType>();
controller->TRegisterMethod(name, description, function, validate);
}
};
} // End http.
#endif // CONTROLLER