-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhelpcontroller.cpp
More file actions
194 lines (158 loc) · 5.23 KB
/
Copy pathhelpcontroller.cpp
File metadata and controls
194 lines (158 loc) · 5.23 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
#include "helpcontroller.h"
#ifdef WITH_JSONCPP
#include <json/writer.h>
#endif
#include "serverstats.h"
using namespace std;
namespace openrc {
CONTROLLER_REGISTERIMPL(HelpController)
bool HelpController::Construct()
{
return true;
}
bool HelpController::Validate(SessionWeak session, const ControllerArguments& arguments)
{
UNUSED(session)
UNUSED(arguments)
return true;
}
CONTROLLER_ACTIONIMPL(HelpController, ShowControllers, "ShowControllers", "Print all controller with their actions.")
void HelpController::ShowControllers(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContent)
{
enum OutputFormat
{
OF_Text,
OF_Html,
OF_Json,
OF_Xml
};
OutputFormat outputFormat = OutputFormat::OF_Text;
auto it = arguments.find("format");
if(it != arguments.end())
{
const string formatText = it->second;
if(formatText == "text")
outputFormat = OutputFormat::OF_Text;
else if(formatText == "html")
outputFormat = OutputFormat::OF_Html;
else if(formatText == "json")
outputFormat = OutputFormat::OF_Json;
else if(formatText == "xml")
outputFormat = OutputFormat::OF_Xml;
}
switch (outputFormat)
{
case OF_Text:
ShowControllersText(session, arguments, outContent);
break;
case OF_Html:
ShowControllersHTML(session, arguments, outContent);
break;
case OF_Json:
ShowControllersJSON(session, arguments, outContent);
break;
case OF_Xml:
ShowControllersXML(session, arguments, outContent);
break;
}
}
void HelpController::ShowControllersText(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContent)
{
//UNUSED(sessionId)
//UNUSED(arguments)
outContent.append("Controllers:\n\n");
for(auto& controllerPair : ControllerManager::GetControllers())
{
IController* controller = controllerPair.second;
if(controller->IsEnable())
{
outContent.append("\t" +
controller->GetName() + " - " +
controller->GetDescription() +
"\n");
for(auto& actionPair : controller->GetActions())
{
outContent.append("\t\t" +
actionPair.second->GetName() + " - " +
actionPair.second->GetDescription() +
"\n");
}
}
}
}
void HelpController::ShowControllersHTML(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContent)
{
UNUSED(session)
UNUSED(arguments)
#ifdef WITH_CTPP
outContent.append("Format html don't sapport\n");
#else
outContent.append("Format html don't sapport\n");
#endif
}
void HelpController::ShowControllersJSON(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContent)
{
UNUSED(session)
UNUSED(arguments)
#ifdef WITH_JSONCPP
Json::Value controllersValue;
for(auto& controllerPair : ControllerManager::GetControllers())
{
IController* controller = controllerPair.second;
if(controller->IsEnable())
{
Json::Value controllerValue;
Json::Value actionsValue(Json::arrayValue);
for(auto& actionPair : controller->GetActions())
{
Json::Value actionValue;
actionValue["name"] = actionPair.second->GetName();
actionValue["description"] = actionPair.second->GetDescription();
actionsValue.append(actionValue);
}
controllerValue["name"] = controllerPair.second->GetName();
controllerValue["description"] = controllerPair.second->GetDescription();
controllerValue["actions"] = actionsValue;
controllersValue[controller->GetName()] = controllerValue;
//controllersValue.append(controllerValue);
}
}
const std::string jsonText = controllersValue.toStyledString();
outContent.append(jsonText);
#else
outContent.append("Format json don't sapport\n");
#endif
}
void HelpController::ShowControllersXML(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContent)
{
UNUSED(session)
UNUSED(arguments)
#ifdef WITH_TINYXML2
outContent.append("Format xml don't sapport\n");
#else
outContent.append("Format xml don't sapport\n");
#endif
}
//
CONTROLLER_ACTIONIMPL(HelpController, Stats, "Stats", "Print server statistic.")
void HelpController::Stats(SessionWeak session, const ControllerArguments& arguments, ControllerOutput& outContent)
{
UNUSED(session)
UNUSED(arguments)
#ifdef WITH_JSONCPP
Json::Value resultValue;
ServerStats& serverStats = ServerStats::Instance();
resultValue["requests"]["number"] = int32(serverStats.GetNbRequest());
resultValue["requests"]["nps"] = serverStats.GetRequestPerSecond();
resultValue["requests"]["bytes"] = int32(serverStats.GetNbRequestBytes());
resultValue["requests"]["bps"] = serverStats.GetRequestBytesPerSecond();
resultValue["response"]["number"] = int32(serverStats.GetNbResponse());
resultValue["response"]["nps"] = serverStats.GetResponsePerSecond();
resultValue["response"]["bytes"] = int32(serverStats.GetNbResponseBytes());
resultValue["response"]["bps"] = serverStats.GetResponseBytesPerSecond();
outContent.append(resultValue.toStyledString());
#else
outContent.append("Format json don't sapport\n");
#endif
}
} // End http.