Debian Codegraph @

Server/Web/WebServer.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 1 of 2 — not the installed (fully patched) source.
2 Debian patches 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 #include <algorithm>
22 #include <arpa/inet.h>
23 #include <cstring> //memset
24 #include <ifaddrs.h>
25 #include <netinet/in.h>
26 #include <signal.h>
27 #include <sstream>
28 #include <sys/socket.h>
29 #include <thread>
30 #include <unistd.h>
31
32 #include "ControlInterface.h"
33 #include "DataTypes.h"
34 #include "DataModel/LocoBase.h"
35 #include "DataModel/LocoFunctions.h"
36 #include "DataModel/ObjectIdentifier.h"
37 #include "Languages.h"
38 #include "RailControl.h"
39 #include "Utils/Network.h"
40 #include "Version.h"
41 #include "Server/Web/WebClient.h"
42 #include "Server/Web/WebServer.h"
43
44 using std::map;
45 using std::thread;
46 using std::string;
47 using std::stringstream;
48 using std::to_string;
49 using std::vector;
50
51 using DataModel::LocoBase;
52 using DataModel::LocoFunctionNr;
53 using DataModel::LocoFunctionState;
54 using DataModel::Route;
55 using DataModel::Track;
56
57 namespace Server { namespace Web
58 {
59 WebServer::WebServer(Manager& manager, const std::string& webserveraddress, const unsigned short port)
60 : ControlInterface(ControlTypeWebServer),
61 Network::TcpServer(webserveraddress, port, "WebServer"),
62 logger(Logger::Logger::GetLogger("WebServer")),
63 lastClientID(0),
64 manager(manager),
65 updateID(1),
66 updateAvailable(false)
67 {
68 AddUpdate(Languages::TextRailControlStarted);
69
70 LogBrowserInfo(webserveraddress, port);
71 updateAvailable = Utils::Network::HostResolves(GetVersionInfoGitHash() + ".hash.railcontrol.org");
72 }
73
74 WebServer::~WebServer()
75 {
76 // delete all client memory
77 while (clients.size())
78 {
79 WebClient* client = clients.back();
80 clients.pop_back();
81 delete client;
82 }
83 logger->Info(Languages::TextWebServerStopped);
84 }
85
86 void WebServer::Start()
87 {
88 StartTcpServer();
89 logger->Info(Languages::TextWebServerStarted);
90 }
91
92 void WebServer::Stop()
93 {
94 AddUpdate(Languages::TextShutdownRailControl);
95 TerminateTcpServer();
96 // stopping all clients
97 for (auto client : clients)
98 {
99 client->Stop();
100 }
101 }
102
103 void WebServer::LogBrowserInfo(const std::string& webserveraddress, const unsigned short port)
104 {
105 static const string Http("\n http://");
106 static const string Port(to_string(port));
107
108 string localhostInfo(Http);
109 localhostInfo += "localhost:" + Port + "/";
110 string ipv4Info;
111 string ipv6Info;
112
113 struct ifaddrs* ifAddrStruct = nullptr;
114 struct ifaddrs* ifa = nullptr;
115 getifaddrs(&ifAddrStruct);
116
117 if (webserveraddress.compare("localhost") != 0)
118 {
119 for (ifa = ifAddrStruct; ifa != nullptr; ifa = ifa->ifa_next)
120 {
121 if (!ifa->ifa_addr)
122 {
123 continue;
124 }
125 if (ifa->ifa_addr->sa_family == AF_INET)
126 {
127 // is a valid IP4 Address
128 void* tmpAddrPtr = &((struct sockaddr_in*) ifa->ifa_addr)->sin_addr;
129 char addressBuffer[INET_ADDRSTRLEN];
130 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
131 string address(addressBuffer);
132 if (address.compare("0.0.0.0") == 0)
133 {
134 continue;
135 }
136 if (address.substr(0, 8).compare("169.254.") == 0)
137 {
138 continue;
139 }
140 ipv4Info += Http + addressBuffer + ":" + Port + "/";
141 }
142 else if (ifa->ifa_addr->sa_family == AF_INET6)
143 {
144 // is a valid IP6 Address
145 void* tmpAddrPtr = &((struct sockaddr_in6*) ifa->ifa_addr)->sin6_addr;
146 char addressBuffer[INET6_ADDRSTRLEN];
147 inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
148 ipv6Info += Http + "[" + addressBuffer + "]:" + Port + "/";
149 }
150 }
151 }
152
153 if (ifAddrStruct != NULL)
154 {
155 freeifaddrs(ifAddrStruct);
156 }
157
158 logger->Info(Languages::TextBrowserInfo, localhostInfo, ipv4Info, ipv6Info);
159
160 }
161
162 void WebServer::Work(Network::TcpConnection* connection)
163 {
164 clients.push_back(new WebClient(++lastClientID, connection, *this, manager));
165
166 // clean up unused clients
167 for (auto iterator = clients.begin(); iterator != clients.end();)
168 {
169 WebClient* client = *iterator;
170 if (client->IsTerminated())
171 {
172 iterator = clients.erase(iterator);
173 delete client;
174 }
175 else
176 {
177 ++iterator;
178 }
179 }
180 }
181
182 void WebServer::Booster(__attribute__((unused)) const ControlType controlType, const BoosterState status)
183 {
184 if (status)
185 {
186 AddUpdate("booster;on=true", Languages::TextTurningBoosterOn);
187 }
188 else
189 {
190 AddUpdate("booster;on=false", Languages::TextTurningBoosterOff);
191 }
192 }
193
194 void WebServer::LocoBaseSpeed(__attribute__((unused)) const ControlType controlType,
195 const LocoBase* loco,
196 const Speed speed)
197 {
198 const LocoID locoId = loco->GetLocoIdWithPrefix();
199 string command = "locospeed;loco=" + to_string(locoId) + ";speed=" + to_string(speed);
200 AddUpdate(command, Languages::TextLocoSpeedIs, loco->GetName(), speed);
201 }
202
203 void WebServer::LocoBaseOrientation(__attribute__((unused)) const ControlType controlType,
204 const LocoBase* loco,
205 const Orientation orientation)
206 {
207 const LocoID locoId = loco->GetLocoIdWithPrefix();
208 string command = "locoorientation;loco=" + to_string(locoId) + ";orientation=" + (orientation ? "true" : "false");
209 AddUpdate(command, orientation ? Languages::TextLocoDirectionOfTravelIsRight : Languages::TextLocoDirectionOfTravelIsLeft, loco->GetName());
210 }
211
212 void WebServer::LocoBaseFunction(__attribute__((unused)) const ControlType controlType,
213 const LocoBase* loco,
214 const LocoFunctionNr function,
215 const LocoFunctionState state)
216 {
217 const LocoID locoId = loco->GetLocoIdWithPrefix();
218 string command = "locofunction;loco=" + to_string(locoId) + ";function=" + to_string(function) + ";on=" + (state ? "true" : "false");
219 AddUpdate(command, state ? Languages::TextLocoFunctionIsOn : Languages::TextLocoFunctionIsOff, loco->GetName(), function);
220 }
221
222 void WebServer::AccessoryState(__attribute__((unused)) const ControlType controlType, const DataModel::Accessory* accessory)
223 {
224 stringstream command;
225 const DataModel::AccessoryState state = accessory->GetAccessoryState();
226 command << "accessory;accessory=" << accessory->GetID() << ";state=" << (state == DataModel::AccessoryStateOn ? "green" : "red");
227 AddUpdate(command.str(), state ? Languages::TextAccessoryStateIsGreen : Languages::TextAccessoryStateIsRed, accessory->GetName());
228 }
229
230 void WebServer::AccessorySettings(const AccessoryID accessoryID,
231 const std::string& name,
232 __attribute__((unused)) const std::string& matchkey)
233 {
234 stringstream command;
235 command << "accessorysettings;accessory=" << accessoryID;
236 AddUpdate(command.str(), Languages::TextAccessoryUpdated, name);
237 }
238
239 void WebServer::AccessoryDelete(const AccessoryID accessoryID,
240 const std::string& name,
241 __attribute__((unused)) const std::string& matchkey)
242 {
243 stringstream command;
244 command << "accessorydelete;accessory=" << accessoryID;
245 AddUpdate(command.str(), Languages::TextAccessoryDeleted, name);
246 }
247
248 void WebServer::FeedbackState(const std::string& name, const FeedbackID feedbackID, const DataModel::Feedback::FeedbackState state)
249 {
250 stringstream command;
251 command << "feedback;feedback=" << feedbackID << ";state=" << (state ? "on" : "off");
252 AddUpdate(command.str(), state ? Languages::TextFeedbackStateIsOn : Languages::TextFeedbackStateIsOff, name);
253 }
254
255 void WebServer::FeedbackSettings(const FeedbackID feedbackID, const std::string& name)
256 {
257 stringstream command;
258 command << "feedbacksettings;feedback=" << feedbackID;
259 AddUpdate(command.str(), Languages::TextFeedbackUpdated, name);
260 }
261
262 void WebServer::FeedbackDelete(const FeedbackID feedbackID, const std::string& name)
263 {
264 stringstream command;
265 command << "feedbackdelete;feedback=" << feedbackID;
266 AddUpdate(command.str(), Languages::TextFeedbackDeleted, name);
267 }
268
269 void WebServer::RouteSettings(const RouteID routeID, const std::string& name)
270 {
271 stringstream command;
272 command << "routesettings;route=" << routeID;
273 AddUpdate(command.str(), Languages::TextRouteUpdated, name);
274 }
275
276 void WebServer::RouteDelete(const RouteID routeID, const std::string& name)
277 {
278 stringstream command;
279 command << "routedelete;route=" << routeID;
280 AddUpdate(command.str(), Languages::TextRouteDeleted, name);
281 }
282
283 void WebServer::SwitchState(__attribute__((unused)) const ControlType controlType, const DataModel::Switch* mySwitch)
284 {
285 stringstream command;
286 const DataModel::AccessoryState state = mySwitch->GetAccessoryState();
287 command << "switch;switch=" << mySwitch->GetID() << ";state=";
288 Languages::TextSelector text;
289 switch (state)
290 {
291 case DataModel::AccessoryState::SwitchStateTurnout:
292 command << "turnout";
293 text = Languages::TextSwitchStateIsTurnout;
294 break;
295
296 case DataModel::AccessoryState::SwitchStateThird:
297 command << "third";
298 text = Languages::TextSwitchStateIsThird;
299 break;
300
301 case DataModel::AccessoryState::SwitchStateStraight:
302 default:
303 command << "straight";
304 text = Languages::TextSwitchStateIsStraight;
305 break;
306 }
307 AddUpdate(command.str(), text, mySwitch->GetName());
308 }
309
310 void WebServer::SwitchSettings(const SwitchID switchID,
311 const std::string& name,
312 __attribute__((unused)) const std::string& matchKey)
313 {
314 stringstream command;
315 command << "switchsettings;switch=" << switchID;
316 AddUpdate(command.str(), Languages::TextSwitchUpdated, name);
317 }
318
319 void WebServer::SwitchDelete(const SwitchID switchID,
320 const std::string& name,
321 __attribute__((unused)) const std::string& matchkey)
322 {
323 stringstream command;
324 command << "switchdelete;switch=" << switchID;
325 AddUpdate(command.str(), Languages::TextSwitchDeleted, name);
326 }
327
328 void WebServer::TrackState(const DataModel::Track* track)
329 {
330 const LocoBase* locoBase = manager.GetLocoBase(track->GetMainLocoBaseDelayed());
331 const bool reserved = locoBase != nullptr;
332 const string& trackName = track->GetMainName();
333 const string& locoName = reserved ? locoBase->GetName() : "";
334 const bool occupied = track->GetMainStateDelayed() == DataModel::Feedback::FeedbackStateOccupied;
335 const bool blocked = track->GetMainBlocked();
336 const Orientation orientation = track->GetMainLocoOrientation();
337 const string occupiedText = (occupied ? "true" : "false");
338 const string blockedText = (blocked ? "true" : "false");
339 const string reservedText = (reserved ? "true" : "false");
340 const string orientationText = (orientation ? "true" : "false");
341 string command = "trackstate;track="
342 + to_string(track->GetID())
343 + ";occupied=" + occupiedText
344 + ";reserved=" + reservedText
345 + ";blocked=" + blockedText
346 + ";orientation=" + orientationText
347 + ";loconame=" + locoName;
348
349 if (blocked)
350 {
351 if (reserved)
352 {
353 AddUpdate(command, Languages::TextTrackStatusIsBlockedAndReserved, trackName, locoName);
354 }
355 else if (occupied)
356 {
357 AddUpdate(command, Languages::TextTrackStatusIsBlockedAndOccupied, trackName);
358 }
359 else
360 {
361 AddUpdate(command, Languages::TextTrackStatusIsBlocked, trackName);
362 }
363 }
364 else
365 {
366 if (reserved)
367 {
368 AddUpdate(command, Languages::TextTrackStatusIsReserved, trackName, locoName);;
369 }
370 else if (occupied)
371 {
372 AddUpdate(command, Languages::TextTrackStatusIsOccupied, trackName);
373 }
374 else
375 {
376 AddUpdate(command, Languages::TextTrackStatusIsFree, trackName);
377 }
378 }
379 }
380
381 void WebServer::TrackSettings(const TrackID trackID, const std::string& name)
382 {
383 stringstream command;
384 command << "tracksettings;track=" << trackID;
385 AddUpdate(command.str(), Languages::TextTrackUpdated, name);
386 }
387
388 void WebServer::TrackDelete(const TrackID trackID, const std::string& name)
389 {
390 stringstream command;
391 command << "trackdelete;track=" << trackID;
392 AddUpdate(command.str(), Languages::TextTrackDeleted, name);
393 }
394
395 void WebServer::SignalState(__attribute__((unused)) const ControlType controlType, const DataModel::Signal* signal)
396 {
397 const DataModel::AccessoryState state = signal->GetAccessoryState();
398 string stateText;
399 Languages::TextSelector text;
400 switch(state)
401 {
402 case DataModel::SignalStateStop:
403 default:
404 stateText = "stop";
405 text = Languages::TextSignalStateIsStop;
406 break;
407
408 case DataModel::SignalStateClear:
409 stateText = "clear";
410 text = Languages::TextSignalStateIsClear;
411 break;
412
413 case DataModel::SignalStateAspect2:
414 stateText = "aspect2";
415 text = Languages::TextSignalStateIsAspect2;
416 break;
417
418 case DataModel::SignalStateAspect3:
419 stateText = "aspect3";
420 text = Languages::TextSignalStateIsAspect3;
421 break;
422
423 case DataModel::SignalStateAspect4:
424 stateText = "aspect4";
425 text = Languages::TextSignalStateIsAspect4;
426 break;
427
428 case DataModel::SignalStateAspect5:
429 stateText = "aspect5";
430 text = Languages::TextSignalStateIsAspect5;
431 break;
432
433 case DataModel::SignalStateAspect6:
434 stateText = "aspect6";
435 text = Languages::TextSignalStateIsAspect6;
436 break;
437
438 case DataModel::SignalStateAspect7:
439 stateText = "aspect7";
440 text = Languages::TextSignalStateIsAspect7;
441 break;
442
443 case DataModel::SignalStateAspect8:
444 stateText = "aspect8";
445 text = Languages::TextSignalStateIsAspect8;
446 break;
447
448 case DataModel::SignalStateAspect9:
449 stateText = "aspect9";
450 text = Languages::TextSignalStateIsAspect9;
451 break;
452
453 case DataModel::SignalStateAspect10:
454 stateText = "aspect10";
455 text = Languages::TextSignalStateIsAspect10;
456 break;
457
458 case DataModel::SignalStateDark:
459 stateText = "dark";
460 text = Languages::TextSignalStateIsDark;
461 break;
462
463 case DataModel::SignalStateStopExpected:
464 stateText = "stopexpected";
465 text = Languages::TextSignalStateIsStopExpected;
466 break;
467
468 case DataModel::SignalStateClearExpected:
469 stateText = "clearexpected";
470 text = Languages::TextSignalStateIsClearExpected;
471 break;
472
473 case DataModel::SignalStateAspect2Expected:
474 stateText = "aspect2expected";
475 text = Languages::TextSignalStateIsAspect2Expected;
476 break;
477
478 case DataModel::SignalStateAspect3Expected:
479 stateText = "aspect3expected";
480 text = Languages::TextSignalStateIsAspect3Expected;
481 break;
482
483 case DataModel::SignalStateAspect4Expected:
484 stateText = "aspect4expected";
485 text = Languages::TextSignalStateIsAspect4Expected;
486 break;
487
488 case DataModel::SignalStateAspect5Expected:
489 stateText = "aspect5expected";
490 text = Languages::TextSignalStateIsAspect5Expected;
491 break;
492
493 case DataModel::SignalStateAspect6Expected:
494 stateText = "aspect6expected";
495 text = Languages::TextSignalStateIsAspect6Expected;
496 break;
497
498 case DataModel::SignalStateAspect7Expected:
499 stateText = "aspect7expected";
500 text = Languages::TextSignalStateIsAspect7Expected;
501 break;
502
503 case DataModel::SignalStateAspect8Expected:
504 stateText = "aspect8expected";
505 text = Languages::TextSignalStateIsAspect8Expected;
506 break;
507
508 case DataModel::SignalStateAspect9Expected:
509 stateText = "aspect9expected";
510 text = Languages::TextSignalStateIsAspect9Expected;
511 break;
512
513 case DataModel::SignalStateAspect10Expected:
514 stateText = "aspect10expected";
515 text = Languages::TextSignalStateIsAspect10Expected;
516 break;
517 }
518 const string signalIdText(to_string(signal->GetID()));
519 const string command = "signal;signal=" + signalIdText + ";state=" + stateText;
520 AddUpdate(command, text, signal->GetName());
521 }
522
523 void WebServer::SignalSettings(const SignalID signalID,
524 const std::string& name,
525 __attribute__((unused)) const std::string& matchKey)
526 {
527 stringstream command;
528 command << "signalsettings;signal=" << signalID;
529 AddUpdate(command.str(), Languages::TextSignalUpdated, name);
530 }
531
532 void WebServer::SignalDelete(const SignalID signalID,
533 const std::string& name,
534 __attribute__((unused)) const std::string& matchkey)
535 {
536 stringstream command;
537 command << "signaldelete;signal=" << signalID;
538 AddUpdate(command.str(), Languages::TextSignalDeleted, name);
539 }
540
541 void WebServer::ClusterSettings(const ClusterID clusterID, const std::string& name)
542 {
543 stringstream command;
544 command << "clustersettings;cluster=" << clusterID;
545 AddUpdate(command.str(), Languages::TextClusterUpdated, name);
546 }
547
548 void WebServer::ClusterDelete(const ClusterID clusterID, const std::string& name)
549 {
550 stringstream command;
551 command << "clusterdelete;cluster=" << clusterID;
552 AddUpdate(command.str(), Languages::TextClusterDeleted, name);
553 }
554
555 void WebServer::TextSettings(const TextID textID, const std::string& name)
556 {
557 string command = "textsettings;text=" + to_string(textID);
558 AddUpdate(command, Languages::TextTextUpdated, name);
559 }
560
561 void WebServer::TextDelete(const TextID textID, const std::string& name)
562 {
563 string command = "textdelete;text=" + to_string(textID);
564 AddUpdate(command, Languages::TextTextDeleted, name);
565 }
566
567 void WebServer::CounterSettings(const CounterID counterID, const std::string& name)
568 {
569 const string command = "countersettings;counter=" + to_string(counterID);
570 AddUpdate(command, Languages::TextCounterUpdated, name);
571 }
572
573 void WebServer::CounterDelete(const CounterID counterID, const std::string& name)
574 {
575 const string command = "counterdelete;counter=" + to_string(counterID);
576 AddUpdate(command, Languages::TextCounterDeleted, name);
577 }
578
579 void WebServer::CounterState(const DataModel::Counter* const counter)
580 {
581 const string command ="counterstate;counter=" + to_string(counter->GetID()) + ";count=" + to_string(counter->GetCounter());
582 AddUpdate(command, Languages::TextCounterUpdated, counter->GetName());
583 }
584
585 void WebServer::LocoBaseRelease(const DataModel::LocoBase* loco)
586 {
587 string command("locorelease;loco=");
588 command += loco->GetObjectIdentifier();
589 AddUpdate(command, Languages::TextLocoIsReleased, loco->GetName());
590 }
591
592 void WebServer::RouteRelease(const RouteID routeID)
593 {
594 stringstream command;
595 command << "routeRelease;route=" << routeID;
596 AddUpdate(command.str(), Languages::TextRouteIsReleased, manager.GetRouteName(routeID));
597 }
598
599 void WebServer::LocoBaseDestinationReached(const LocoBase* loco,
600 const Route* route,
601 const Track* track)
602 {
603 string command("locoDestinationReached;loco=");
604 command += loco->GetObjectIdentifier();
605 command += ";route=";
606 command += to_string(route->GetID());
607 command += ";track=";
608 command += to_string(track->GetID());
609 AddUpdate(command, Languages::TextLocoHasReachedDestination, loco->GetName(), track->GetName(), route->GetName());
610 }
611
612 void WebServer::LocoBaseStart(const DataModel::LocoBase* loco)
613 {
614 string command("locoStart;loco=");
615 command += loco->GetObjectIdentifier();
616 AddUpdate(command, Languages::TextLocoIsInAutoMode, loco->GetName());
617 }
618
619 void WebServer::LocoBaseStop(const DataModel::LocoBase* loco)
620 {
621 string command("locoStop;loco=");
622 command += loco->GetObjectIdentifier();
623 AddUpdate(command, Languages::TextLocoIsInManualMode, loco->GetName());
624 }
625
626 void WebServer::LocoSettings(const LocoID locoID,
627 const std::string& name,
628 __attribute__((unused)) const std::string& matchKey)
629 {
630 stringstream command;
631 command << "locosettings;loco=" << locoID;
632 AddUpdate(command.str(), Languages::TextLocoUpdated, name);
633 }
634
635 void WebServer::LocoDelete(const LocoID locoID,
636 const std::string& name,
637 __attribute__((unused)) const std::string& matchkey)
638 {
639 stringstream command;
640 command << "locodelete;loco=" << locoID;
641 AddUpdate(command.str(), Languages::TextLocoDeleted, name);
642 }
643
644 void WebServer::MultipleUnitSettings(const MultipleUnitID multipleUnitID,
645 const std::string& name,
646 __attribute__((unused)) const std::string& matchKey)
647 {
648 stringstream command;
649 command << "multipleunitsettings;loco=" << multipleUnitID;
650 AddUpdate(command.str(), Languages::TextLocoUpdated, name);
651 }
652
653 void WebServer::MultipleUnitDelete(const MultipleUnitID multipleUnitID,
654 const std::string& name,
655 __attribute__((unused)) const std::string& matchkey)
656 {
657 stringstream command;
658 command << "multipleunitdelete;loco=" << multipleUnitID;
659 AddUpdate(command.str(), Languages::TextLocoDeleted, name);
660 }
661
662 void WebServer::LayerSettings(const LayerID layerID, const std::string& name)
663 {
664 stringstream command;
665 command << "layersettings;layer=" << layerID;
666 AddUpdate(command.str(), Languages::TextLayerUpdated, name);
667 }
668
669 void WebServer::LayerDelete(const LayerID layerID, const std::string& name)
670 {
671 stringstream command;
672 command << "layerdelete;layer=" << layerID;
673 AddUpdate(command.str(), Languages::TextLayerDeleted, name);
674 }
675
676 void WebServer::ProgramValue(const CvNumber cv, const CvValue value)
677 {
678 stringstream command;
679 command << "dcccvvalue;cv=" << static_cast<int>(cv) << ";value=" << static_cast<int>(value);
680 AddUpdate(command.str(), Languages::TextProgramReadValue , static_cast<int>(cv), static_cast<int>(value));
681 }
682
683 void WebServer::AddUpdate(const string& data)
684 {
685 std::lock_guard<std::mutex> lock(updateMutex);
686 updates[updateID] = data;
687 ++updateID;
688 updates.erase(updateID - MaxUpdates);
689 }
690
691 bool WebServer::NextUpdate(unsigned int& updateIDClient, string& s)
692 {
693 std::lock_guard<std::mutex> lock(updateMutex);
694
695 if (updateIDClient + MaxUpdates <= updateID)
696 {
697 updateIDClient = updateID - MaxUpdates + 1;
698 }
699
700 if (updates.count(updateIDClient) == 1)
701 {
702 s = updates.at(updateIDClient);
703 return true;
704 }
705
706 return false;
707 }
708
709 }} // namespace Server::Web