| 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/WebClientRoute.h" |
| 38 |
#include "Server/Web/WebClientSignal.h" |
| 39 |
#include "Server/Web/WebClientStatic.h" |
| 40 |
#include "Server/Web/WebClientText.h" |
| 41 |
#include "Server/Web/WebClientTrack.h" |
| 42 |
|
| 43 |
namespace DataModel |
| 44 |
{ |
| 45 |
class ObjectIdentifier; |
| 46 |
class Cluster; |
| 47 |
} |
| 48 |
|
| 49 |
namespace Server { namespace Web |
| 50 |
{ |
| 51 |
class WebServer; |
| 52 |
|
| 53 |
class WebClient |
| 54 |
{ |
| 55 |
public: |
| 56 |
enum ResponseType : unsigned char |
| 57 |
{ |
| 58 |
ResponseInfo = 'i', |
| 59 |
ResponseWarning = 'w', |
| 60 |
ResponseError = 'e' |
| 61 |
}; |
| 62 |
|
| 63 |
WebClient() = delete; |
| 64 |
WebClient(const WebClient&) = delete; |
| 65 |
WebClient& operator=(const WebClient&) = delete; |
| 66 |
|
| 67 |
inline WebClient(const unsigned int id, |
| 68 |
Network::TcpConnection* connection, // connection must be deleted after using! |
| 69 |
WebServer &webserver, |
| 70 |
Manager& manager) |
| 71 |
: logger(Logger::Logger::GetLogger("WebClient # " + std::to_string(id))), |
| 72 |
id(id), |
| 73 |
connection(connection), |
| 74 |
run(false), |
| 75 |
terminated(false), |
| 76 |
server(webserver), |
| 77 |
clientThread(&WebClient::Worker, this), |
| 78 |
manager(manager), |
| 79 |
cluster(manager, *this), |
| 80 |
track(manager, *this, logger), |
| 81 |
signal(manager, *this, logger), |
| 82 |
route(manager, *this, logger), |
| 83 |
text(manager, *this), |
| 84 |
headOnly(false), |
| 85 |
buttonID(0) |
| 86 |
{ |
| 87 |
} |
| 88 |
|
| 89 |
~WebClient(); |
| 90 |
|
| 91 |
void Worker(); |
| 92 |
|
| 93 |
inline void Stop() |
| 94 |
{ |
| 95 |
run = false; |
| 96 |
} |
| 97 |
|
| 98 |
inline bool IsTerminated() |
| 99 |
{ |
| 100 |
return terminated; |
| 101 |
} |
| 102 |
|
| 103 |
inline void ReplyHtmlWithHeader(const HtmlTag& tag) |
| 104 |
{ |
| 105 |
connection->Send(ResponseHtml(tag)); |
| 106 |
} |
| 107 |
|
| 108 |
inline void ReplyResponse(const std::string& text) |
| 109 |
{ |
| 110 |
ReplyHtmlWithHeader(HtmlTag().AddContent(text)); |
| 111 |
} |
| 112 |
|
| 113 |
inline void ReplyResponse(ResponseType type, const std::string& text) |
| 114 |
{ |
| 115 |
std::string s(1, static_cast<unsigned char>(type)); |
| 116 |
s.append(text); |
| 117 |
ReplyResponse(s); |
| 118 |
} |
| 119 |
|
| 120 |
template<typename... Args> |
| 121 |
inline void ReplyResponse(ResponseType type, Languages::TextSelector text, Args... args) |
| 122 |
{ |
| 123 |
std::string s(1, static_cast<unsigned char>(type)); |
| 124 |
s.append(Logger::Logger::Format(Languages::GetText(text), args...)); |
| 125 |
ReplyResponse(s); |
| 126 |
} |
| 127 |
|
| 128 |
inline void ReplyHtmlWithHeaderAndParagraph(const std::string& content) |
| 129 |
{ |
| 130 |
ReplyHtmlWithHeader(HtmlTag("p").AddContent(content)); |
| 131 |
} |
| 132 |
|
| 133 |
inline void ReplyHtmlWithHeaderAndParagraph(const char* content) |
| 134 |
{ |
| 135 |
ReplyHtmlWithHeaderAndParagraph(std::string(content)); |
| 136 |
} |
| 137 |
|
| 138 |
template<typename... Args> |
| 139 |
inline void ReplyHtmlWithHeaderAndParagraph(const Languages::TextSelector text, Args... args) |
| 140 |
{ |
| 141 |
ReplyHtmlWithHeaderAndParagraph(Logger::Logger::Format(Languages::GetText(text), args...)); |
| 142 |
} |
| 143 |
|
| 144 |
HtmlTag HtmlTagSelectSlave(const std::string& prefix, |
| 145 |
const std::vector<DataModel::Relation*>& relations, |
| 146 |
const std::map<std::string,ObjectID>& options, |
| 147 |
const bool allowNew = true) const; |
| 148 |
|
| 149 |
inline HtmlTag HtmlTagTabPosition(const DataModel::LayoutItem::LayoutPosition posx, |
| 150 |
const DataModel::LayoutItem::LayoutPosition posy, |
| 151 |
const DataModel::LayoutItem::LayoutPosition posz, |
| 152 |
const DataModel::LayoutItem::Visible visible) const |
| 153 |
{ |
| 154 |
return HtmlTagTabPosition(posx, posy, posz, DataModel::LayoutItem::RotationNotRelevant, visible); |
| 155 |
} |
| 156 |
|
| 157 |
HtmlTag HtmlTagTabPosition(const DataModel::LayoutItem::LayoutPosition posx, |
| 158 |
const DataModel::LayoutItem::LayoutPosition posy, |
| 159 |
const DataModel::LayoutItem::LayoutPosition posz, |
| 160 |
const DataModel::LayoutItem::LayoutRotation rotation = DataModel::LayoutItem::RotationNotRelevant, |
| 161 |
const DataModel::LayoutItem::Visible visible = DataModel::LayoutItem::VisibleNotRelevant) const ; |
| 162 |
|
| 163 |
HtmlTag HtmlTagControlLoco(ControlID& controlId, const std::string& objectType, const ObjectID objectID) const; |
| 164 |
HtmlTag HtmlTagControlMultipleUnit(ControlID& controlId, const std::string& objectType, const ObjectID objectID) const; |
| 165 |
HtmlTag HtmlTagControlAccessory(ControlID& controlID, const std::string& objectType, const ObjectID objectID) const; |
| 166 |
HtmlTag HtmlTagControlFeedback(ControlID& controlId, const std::string& objectType, const ObjectID objectID) const; |
| 167 |
|
| 168 |
HtmlTag HtmlTagProtocolAccessory(const ControlID controlID, const Protocol selectedProtocol); |
| 169 |
|
| 170 |
HtmlTag HtmlTagSelectFeedbackForTrack(const unsigned int counter, |
| 171 |
const TrackID trackID, |
| 172 |
const FeedbackID feedbackID = FeedbackNone) const; |
| 173 |
|
| 174 |
HtmlTag HtmlTagRelationSignalState(const std::string& name, |
| 175 |
const SignalID signalId, |
| 176 |
const DataModel::Relation::Data data = DataModel::SignalStateStop); |
| 177 |
|
| 178 |
static std::string ProtocolName(const Protocol protocol) |
| 179 |
{ |
| 180 |
return ProtocolSymbols[protocol <= ProtocolEnd ? protocol : ProtocolNone]; |
| 181 |
} |
| 182 |
|
| 183 |
HtmlTag HtmlTagMatchKeyProtocolAccessory(const ControlID controlID, |
| 184 |
const std::string& selectedMatchKey, |
| 185 |
const Protocol selectedProtocol) const; |
| 186 |
|
| 187 |
HtmlTag HtmlTagMatchKeyFeedback(const ControlID controlID, |
| 188 |
const std::string& selectedMatchKey) const; |
| 189 |
|
| 190 |
private: |
| 191 |
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); |
| 192 |
void HandleLoco(const std::map<std::string, std::string>& arguments); |
| 193 |
void PrintMainHTML(); |
| 194 |
void DeliverFile(const std::string& file); |
| 195 |
void DeliverFileInternal(FILE* f, const char* realFile, const std::string& file); |
| 196 |
HtmlTag HtmlTagLocoSelector(const std::string& selector, const LocoID locoID = LocoNone) const; |
| 197 |
HtmlTag HtmlTagLayerSelector(const LayerID layerID = LayerNone) const; |
| 198 |
|
| 199 |
HtmlTag HtmlTagMatchKeyProtocolLoco(const ControlID controlID, |
| 200 |
const std::string& selectedMatchKey, |
| 201 |
const Protocol selectedProtocol) const; |
| 202 |
|
| 203 |
HtmlTag HtmlTagPosition(const DataModel::LayoutItem::LayoutPosition posx, |
| 204 |
const DataModel::LayoutItem::LayoutPosition posy, |
| 205 |
const DataModel::LayoutItem::LayoutPosition posz) const; |
| 206 |
|
| 207 |
HtmlTag HtmlTagPosition(const DataModel::LayoutItem::LayoutPosition posx, |
| 208 |
const DataModel::LayoutItem::LayoutPosition posy, |
| 209 |
const DataModel::LayoutItem::LayoutPosition posz, |
| 210 |
const DataModel::LayoutItem::Visible visible) const; |
| 211 |
|
| 212 |
HtmlTag HtmlTagProgramModeSelector(const ControlID controlID, ProgramMode& mode) const; |
| 213 |
HtmlTag HtmlTagCvFields(const ControlID controlID, const ProgramMode programMode) const; |
| 214 |
|
| 215 |
HtmlTag HtmlTagInput8BitValueWithLabel() const; |
| 216 |
|
| 217 |
HtmlTag HtmlTagInputBitValue(std::string name) const; |
| 218 |
|
| 219 |
std::map<std::string,ObjectID> GetMultipleUnitSlaveOptions() const; |
| 220 |
|
| 221 |
void HandleAskShutdown(); |
| 222 |
void HandleSelectLoco(const std::map<std::string, std::string>& arguments); |
| 223 |
void HandleLayerEdit(const std::map<std::string, std::string>& arguments); |
| 224 |
void HandleLayerSave(const std::map<std::string, std::string>& arguments); |
| 225 |
void HandleLayerList(); |
| 226 |
void HandleLayerAskDelete(const std::map<std::string, std::string>& arguments); |
| 227 |
void HandleLayerDelete(const std::map<std::string, std::string>& arguments); |
| 228 |
void HandleControlEdit(const std::map<std::string, std::string>& arguments); |
| 229 |
void HandleControlSave(const std::map<std::string, std::string>& arguments); |
| 230 |
void HandleControlList(); |
| 231 |
void HandleControlAskDelete(const std::map<std::string, std::string>& arguments); |
| 232 |
void HandleControlDelete(const std::map<std::string, std::string>& arguments); |
| 233 |
void HandleLocoBaseSpeed(const std::map<std::string,std::string>& arguments); |
| 234 |
void HandleLocoBaseOrientation(const std::map<std::string,std::string>& arguments); |
| 235 |
void HandleLocoFunction(const std::map<std::string, std::string>& arguments); |
| 236 |
void HandleLocoEdit(const std::map<std::string, std::string>& arguments); |
| 237 |
void HandleLocoSave(const std::map<std::string, std::string>& arguments); |
| 238 |
void HandleLocoList(); |
| 239 |
void HandleLocoAskDelete(const std::map<std::string, std::string>& arguments); |
| 240 |
void HandleLocoDelete(const std::map<std::string, std::string>& arguments); |
| 241 |
void HandleLocoRelease(const std::map<std::string, std::string>& arguments); |
| 242 |
void HandleLocoAddTimeTable(const std::map<std::string, std::string>& arguments); |
| 243 |
void HandleMultipleUnitEdit(const std::map<std::string, std::string>& arguments); |
| 244 |
void HandleMultipleUnitSave(const std::map<std::string, std::string>& arguments); |
| 245 |
void HandleMultipleUnitList(); |
| 246 |
void HandleMultipleUnitAskDelete(const std::map<std::string, std::string>& arguments); |
| 247 |
void HandleMultipleUnitDelete(const std::map<std::string, std::string>& arguments); |
| 248 |
void HandleMultipleUnitRelease(const std::map<std::string, std::string>& arguments); |
| 249 |
void HandleProtocol(const std::map<std::string, std::string>& arguments); |
| 250 |
void HandleAccessoryAddress(const std::map<std::string, std::string>& arguments); |
| 251 |
void HandleLayout(const std::map<std::string,std::string>& arguments); |
| 252 |
void HandleAccessoryEdit(const std::map<std::string,std::string>& arguments); |
| 253 |
void HandleAccessorySave(const std::map<std::string,std::string>& arguments); |
| 254 |
void HandleAccessoryState(const std::map<std::string,std::string>& arguments); |
| 255 |
void HandleAccessoryList(); |
| 256 |
void HandleAccessoryAskDelete(const std::map<std::string,std::string>& arguments); |
| 257 |
void HandleAccessoryDelete(const std::map<std::string,std::string>& arguments); |
| 258 |
void HandleAccessoryGet(const std::map<std::string,std::string>& arguments); |
| 259 |
void HandleAccessoryRelease(const std::map<std::string,std::string>& arguments); |
| 260 |
void HandleSwitchEdit(const std::map<std::string,std::string>& arguments); |
| 261 |
void HandleSwitchSave(const std::map<std::string,std::string>& arguments); |
| 262 |
void HandleSwitchState(const std::map<std::string,std::string>& arguments); |
| 263 |
void HandleSwitchList(); |
| 264 |
void HandleSwitchAskDelete(const std::map<std::string,std::string>& arguments); |
| 265 |
void HandleSwitchDelete(const std::map<std::string,std::string>& arguments); |
| 266 |
void HandleSwitchGet(const std::map<std::string,std::string>& arguments); |
| 267 |
void HandleSwitchRelease(const std::map<std::string,std::string>& arguments); |
| 268 |
void HandleFeedbackEdit(const std::map<std::string,std::string>& arguments); |
| 269 |
void HandleFeedbackSave(const std::map<std::string,std::string>& arguments); |
| 270 |
void HandleFeedbackState(const std::map<std::string,std::string>& arguments); |
| 271 |
void HandleFeedbackList(); |
| 272 |
void HandleFeedbackAskDelete(const std::map<std::string,std::string>& arguments); |
| 273 |
void HandleFeedbackDelete(const std::map<std::string,std::string>& arguments); |
| 274 |
void HandleFeedbackGet(const std::map<std::string,std::string>& arguments); |
| 275 |
void HandleLocoSelector(const std::map<std::string,std::string>& arguments); |
| 276 |
void HandleLayerSelector(const std::map<std::string,std::string>& arguments); |
| 277 |
void HandleFeedbackAdd(const std::map<std::string,std::string>& arguments); |
| 278 |
void HandleSettingsEdit(); |
| 279 |
void HandleSettingsSave(const std::map<std::string, std::string>& arguments); |
| 280 |
void HandleSlaveAdd(const std::map<std::string,std::string>& arguments); |
| 281 |
void HandleTimestamp(const std::map<std::string,std::string>& arguments); |
| 282 |
void HandleControlArguments(const std::map<std::string,std::string>& arguments); |
| 283 |
void HandleProgram(); |
| 284 |
void HandleProgramModeSelector(const std::map<std::string,std::string>& arguments); |
| 285 |
void HandleProgramRead(const std::map<std::string,std::string>& arguments); |
| 286 |
void HandleProgramWrite(const std::map<std::string,std::string>& arguments); |
| 287 |
void HandleCvFields(const std::map<std::string,std::string>& arguments); |
| 288 |
void HandleNewPosition(const std::map<std::string,std::string>& arguments); |
| 289 |
void HandleNewPositionInternal(const std::map<std::string,std::string>& arguments, std::string& result); |
| 290 |
void HandleRotate(const std::map<std::string,std::string>& arguments); |
| 291 |
void HandleUpdater(const std::map<std::string,std::string>& headers); |
| 292 |
void WorkerImpl(); |
| 293 |
|
| 294 |
Logger::Logger* logger; |
| 295 |
unsigned int id; |
| 296 |
Network::TcpConnection* connection; |
| 297 |
volatile bool run; |
| 298 |
bool terminated; |
| 299 |
WebServer& server; |
| 300 |
std::thread clientThread; |
| 301 |
Manager& manager; |
| 302 |
WebClientCluster cluster; |
| 303 |
WebClientTrack track; |
| 304 |
WebClientSignal signal; |
| 305 |
WebClientRoute route; |
| 306 |
WebClientText text; |
| 307 |
bool headOnly; |
| 308 |
unsigned int buttonID; |
| 309 |
}; |
| 310 |
|
| 311 |
}} // namespace Server::Web |
| 312 |
|