Debian Codegraph @

Server/Web/WebClient.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 RailControl - Model Railway Control Software
3
4 Copyright (c) 2017-2025 by Teddy / Dominik Mahrer - www.railcontrol.org
5
6 RailControl is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
10
11 RailControl is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with RailControl; see the file LICENCE. If not see
18 <http://www.gnu.org/licenses/>.
19 */
20
21 #pragma once
22
23 #include <cstring>
24 #include <deque>
25 #include <map>
26 #include <string>
27 #include <thread>
28 #include <vector>
29
30 #include "DataModel/AccessoryBase.h"
31 #include "DataModel/ObjectIdentifier.h"
32 #include "Languages.h"
33 #include "Manager.h"
34 #include "Network/TcpConnection.h"
35 #include "ResponseHtml.h"
36 #include "Server/Web/WebClientCluster.h"
•37 #include "Server/Web/WebClientCounter.h"
38 #include "Server/Web/WebClientRoute.h"
39 #include "Server/Web/WebClientSignal.h"
40 #include "Server/Web/WebClientStatic.h"
41 #include "Server/Web/WebClientText.h"
42 #include "Server/Web/WebClientTrack.h"
43
44 namespace DataModel
45 {
46 class ObjectIdentifier;
47 class Cluster;
48 }
49
50 namespace Server { namespace Web
51 {
52 class WebServer;
53
54 class WebClient
55 {
56 public:
57 enum ResponseType : unsigned char
58 {
59 ResponseInfo = 'i',
60 ResponseWarning = 'w',
61 ResponseError = 'e'
62 };
63
64 WebClient() = delete;
65 WebClient(const WebClient&) = delete;
66 WebClient& operator=(const WebClient&) = delete;
67
68 inline WebClient(const unsigned int id,
69 Network::TcpConnection* connection, // connection must be deleted after using!
70 WebServer &webserver,
71 Manager& manager)
72 : logger(Logger::Logger::GetLogger("WebClient # " + std::to_string(id))),
73 id(id),
74 connection(connection),
75 run(false),
76 terminated(false),
77 server(webserver),
78 clientThread(&WebClient::Worker, this),
79 manager(manager),
80 cluster(manager, *this),
81 track(manager, *this, logger),
82 signal(manager, *this, logger),
83 route(manager, *this, logger),
84 text(manager, *this),
•85 counter(manager, *this),
86 headOnly(false),
87 buttonID(0)
88 {
89 }
90
91 ~WebClient();
92
93 void Worker();
94
95 inline void Stop()
96 {
97 run = false;
98 }
99
100 inline bool IsTerminated()
101 {
102 return terminated;
103 }
104
105 inline void ReplyHtmlWithHeader(const HtmlTag& tag)
106 {
107 connection->Send(ResponseHtml(tag));
108 }
109
110 inline void ReplyResponse(const std::string& text)
111 {
112 ReplyHtmlWithHeader(HtmlTag().AddContent(text));
113 }
114
115 inline void ReplyResponse(ResponseType type, const std::string& text)
116 {
117 std::string s(1, static_cast<unsigned char>(type));
118 s.append(text);
119 ReplyResponse(s);
120 }
121
122 template<typename... Args>
123 inline void ReplyResponse(ResponseType type, Languages::TextSelector text, Args... args)
124 {
125 std::string s(1, static_cast<unsigned char>(type));
126 s.append(Logger::Logger::Format(Languages::GetText(text), args...));
127 ReplyResponse(s);
128 }
129
130 inline void ReplyHtmlWithHeaderAndParagraph(const std::string& content)
131 {
132 ReplyHtmlWithHeader(HtmlTag("p").AddContent(content));
133 }
134
135 inline void ReplyHtmlWithHeaderAndParagraph(const char* content)
136 {
137 ReplyHtmlWithHeaderAndParagraph(std::string(content));
138 }
139
140 template<typename... Args>
141 inline void ReplyHtmlWithHeaderAndParagraph(const Languages::TextSelector text, Args... args)
142 {
143 ReplyHtmlWithHeaderAndParagraph(Logger::Logger::Format(Languages::GetText(text), args...));
144 }
145
146 HtmlTag HtmlTagSelectSlave(const std::string& prefix,
147 const std::vector<DataModel::Relation*>& relations,
148 const std::map<std::string,ObjectID>& options,
149 const bool allowNew = true) const;
150
151 inline HtmlTag HtmlTagTabPosition(const DataModel::LayoutItem::LayoutPosition posx,
152 const DataModel::LayoutItem::LayoutPosition posy,
153 const DataModel::LayoutItem::LayoutPosition posz,
154 const DataModel::LayoutItem::Visible visible) const
155 {
156 return HtmlTagTabPosition(posx, posy, posz, DataModel::LayoutItem::RotationNotRelevant, visible);
157 }
158
159 HtmlTag HtmlTagTabPosition(const DataModel::LayoutItem::LayoutPosition posx,
160 const DataModel::LayoutItem::LayoutPosition posy,
161 const DataModel::LayoutItem::LayoutPosition posz,
162 const DataModel::LayoutItem::LayoutRotation rotation = DataModel::LayoutItem::RotationNotRelevant,
163 const DataModel::LayoutItem::Visible visible = DataModel::LayoutItem::VisibleNotRelevant) const ;
164
165 HtmlTag HtmlTagControlLoco(ControlID& controlId, const std::string& objectType, const ObjectID objectID) const;
166 HtmlTag HtmlTagControlMultipleUnit(ControlID& controlId, const std::string& objectType, const ObjectID objectID) const;
167 HtmlTag HtmlTagControlAccessory(ControlID& controlID, const std::string& objectType, const ObjectID objectID) const;
168 HtmlTag HtmlTagControlFeedback(ControlID& controlId, const std::string& objectType, const ObjectID objectID) const;
169
170 HtmlTag HtmlTagProtocolAccessory(const ControlID controlID, const Protocol selectedProtocol);
171
172 HtmlTag HtmlTagSelectFeedbackForTrack(const unsigned int counter,
173 const TrackID trackID,
174 const FeedbackID feedbackID = FeedbackNone) const;
175
176 HtmlTag HtmlTagRelationSignalState(const std::string& name,
177 const SignalID signalId,
178 const DataModel::Relation::Data data = DataModel::SignalStateStop);
179
180 static std::string ProtocolName(const Protocol protocol)
181 {
182 return ProtocolSymbols[protocol <= ProtocolEnd ? protocol : ProtocolNone];
183 }
184
185 HtmlTag HtmlTagMatchKeyProtocolAccessory(const ControlID controlID,
186 const std::string& selectedMatchKey,
187 const Protocol selectedProtocol) const;
188
189 HtmlTag HtmlTagMatchKeyFeedback(const ControlID controlID,
190 const std::string& selectedMatchKey) const;
191
192 private:
193 void InterpretClientRequest(const std::deque<std::string>& lines, std::string& method, std::string& uri, std::string& protocol, std::map<std::string,std::string>& arguments, std::map<std::string,std::string>& headers);
194 void HandleLoco(const std::map<std::string, std::string>& arguments);
195 void PrintMainHTML();
196 void DeliverFile(const std::string& file);
197 void DeliverFileInternal(FILE* f, const char* realFile, const std::string& file);
198 HtmlTag HtmlTagLocoSelector(const std::string& selector, const LocoID locoID = LocoNone) const;
199 HtmlTag HtmlTagLayerSelector(const LayerID layerID = LayerNone) const;
200
201 HtmlTag HtmlTagMatchKeyProtocolLoco(const ControlID controlID,
202 const std::string& selectedMatchKey,
203 const Protocol selectedProtocol) const;
204
205 HtmlTag HtmlTagPosition(const DataModel::LayoutItem::LayoutPosition posx,
206 const DataModel::LayoutItem::LayoutPosition posy,
207 const DataModel::LayoutItem::LayoutPosition posz) const;
208
209 HtmlTag HtmlTagPosition(const DataModel::LayoutItem::LayoutPosition posx,
210 const DataModel::LayoutItem::LayoutPosition posy,
211 const DataModel::LayoutItem::LayoutPosition posz,
212 const DataModel::LayoutItem::Visible visible) const;
213
214 HtmlTag HtmlTagProgramModeSelector(const ControlID controlID, ProgramMode& mode) const;
215 HtmlTag HtmlTagCvFields(const ControlID controlID, const ProgramMode programMode) const;
216
217 HtmlTag HtmlTagInput8BitValueWithLabel() const;
218
219 HtmlTag HtmlTagInputBitValue(std::string name) const;
220
221 std::map<std::string,ObjectID> GetMultipleUnitSlaveOptions() const;
222
223 void HandleAskShutdown();
224 void HandleSelectLoco(const std::map<std::string, std::string>& arguments);
225 void HandleLayerEdit(const std::map<std::string, std::string>& arguments);
226 void HandleLayerSave(const std::map<std::string, std::string>& arguments);
227 void HandleLayerList();
228 void HandleLayerAskDelete(const std::map<std::string, std::string>& arguments);
229 void HandleLayerDelete(const std::map<std::string, std::string>& arguments);
230 void HandleControlEdit(const std::map<std::string, std::string>& arguments);
231 void HandleControlSave(const std::map<std::string, std::string>& arguments);
232 void HandleControlList();
233 void HandleControlAskDelete(const std::map<std::string, std::string>& arguments);
234 void HandleControlDelete(const std::map<std::string, std::string>& arguments);
235 void HandleLocoBaseSpeed(const std::map<std::string,std::string>& arguments);
236 void HandleLocoBaseOrientation(const std::map<std::string,std::string>& arguments);
237 void HandleLocoFunction(const std::map<std::string, std::string>& arguments);
238 void HandleLocoEdit(const std::map<std::string, std::string>& arguments);
239 void HandleLocoSave(const std::map<std::string, std::string>& arguments);
240 void HandleLocoList();
241 void HandleLocoAskDelete(const std::map<std::string, std::string>& arguments);
242 void HandleLocoDelete(const std::map<std::string, std::string>& arguments);
243 void HandleLocoRelease(const std::map<std::string, std::string>& arguments);
244 void HandleLocoAddTimeTable(const std::map<std::string, std::string>& arguments);
245 void HandleMultipleUnitEdit(const std::map<std::string, std::string>& arguments);
246 void HandleMultipleUnitSave(const std::map<std::string, std::string>& arguments);
247 void HandleMultipleUnitList();
248 void HandleMultipleUnitAskDelete(const std::map<std::string, std::string>& arguments);
249 void HandleMultipleUnitDelete(const std::map<std::string, std::string>& arguments);
250 void HandleMultipleUnitRelease(const std::map<std::string, std::string>& arguments);
251 void HandleProtocol(const std::map<std::string, std::string>& arguments);
252 void HandleAccessoryAddress(const std::map<std::string, std::string>& arguments);
253 void HandleLayout(const std::map<std::string,std::string>& arguments);
254 void HandleAccessoryEdit(const std::map<std::string,std::string>& arguments);
255 void HandleAccessorySave(const std::map<std::string,std::string>& arguments);
256 void HandleAccessoryState(const std::map<std::string,std::string>& arguments);
257 void HandleAccessoryList();
258 void HandleAccessoryAskDelete(const std::map<std::string,std::string>& arguments);
259 void HandleAccessoryDelete(const std::map<std::string,std::string>& arguments);
260 void HandleAccessoryGet(const std::map<std::string,std::string>& arguments);
261 void HandleAccessoryRelease(const std::map<std::string,std::string>& arguments);
262 void HandleSwitchEdit(const std::map<std::string,std::string>& arguments);
263 void HandleSwitchSave(const std::map<std::string,std::string>& arguments);
264 void HandleSwitchState(const std::map<std::string,std::string>& arguments);
265 void HandleSwitchList();
266 void HandleSwitchAskDelete(const std::map<std::string,std::string>& arguments);
267 void HandleSwitchDelete(const std::map<std::string,std::string>& arguments);
268 void HandleSwitchGet(const std::map<std::string,std::string>& arguments);
269 void HandleSwitchRelease(const std::map<std::string,std::string>& arguments);
270 void HandleFeedbackEdit(const std::map<std::string,std::string>& arguments);
271 void HandleFeedbackSave(const std::map<std::string,std::string>& arguments);
272 void HandleFeedbackState(const std::map<std::string,std::string>& arguments);
273 void HandleFeedbackList();
274 void HandleFeedbackAskDelete(const std::map<std::string,std::string>& arguments);
275 void HandleFeedbackDelete(const std::map<std::string,std::string>& arguments);
276 void HandleFeedbackGet(const std::map<std::string,std::string>& arguments);
277 void HandleLocoSelector(const std::map<std::string,std::string>& arguments);
278 void HandleLayerSelector(const std::map<std::string,std::string>& arguments);
279 void HandleFeedbackAdd(const std::map<std::string,std::string>& arguments);
280 void HandleSettingsEdit();
281 void HandleSettingsSave(const std::map<std::string, std::string>& arguments);
282 void HandleSlaveAdd(const std::map<std::string,std::string>& arguments);
283 void HandleTimestamp(const std::map<std::string,std::string>& arguments);
284 void HandleControlArguments(const std::map<std::string,std::string>& arguments);
285 void HandleProgram();
286 void HandleProgramModeSelector(const std::map<std::string,std::string>& arguments);
287 void HandleProgramRead(const std::map<std::string,std::string>& arguments);
288 void HandleProgramWrite(const std::map<std::string,std::string>& arguments);
289 void HandleCvFields(const std::map<std::string,std::string>& arguments);
290 void HandleNewPosition(const std::map<std::string,std::string>& arguments);
291 void HandleNewPositionInternal(const std::map<std::string,std::string>& arguments, std::string& result);
292 void HandleRotate(const std::map<std::string,std::string>& arguments);
293 void HandleUpdater(const std::map<std::string,std::string>& headers);
294 void WorkerImpl();
295
296 Logger::Logger* logger;
297 unsigned int id;
298 Network::TcpConnection* connection;
299 volatile bool run;
300 bool terminated;
301 WebServer& server;
302 std::thread clientThread;
303 Manager& manager;
304 WebClientCluster cluster;
305 WebClientTrack track;
306 WebClientSignal signal;
307 WebClientRoute route;
308 WebClientText text;
•309 WebClientCounter counter;
310 bool headOnly;
311 unsigned int buttonID;
312 };
313
314 }} // namespace Server::Web
315