Debian Codegraph @

Server/Z21/Z21Server.cpp

raw symbols SCIP
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 "DataModel/LocoBase.h"
22 #include "DataModel/LocoFunctions.h"
23 #include "Manager.h"
24 #include "Server/Z21/Z21Client.h"
25 #include "Server/Z21/Z21Server.h"
26 #include "Utils/Network.h"
27
28 using DataModel::Loco;
29 using DataModel::LocoBase;
30 using DataModel::LocoFunctionNr;
31 using DataModel::LocoFunctionState;
32
33 namespace Network
34 {
35 class UdpClient;
36 }
37
38 namespace Server { namespace Z21
39 {
40 Z21Server::Z21Server(Manager& manager,
41 const unsigned short port)
42 : ControlInterface(ControlTypeZ21Server),
43 Network::UdpServer("0.0.0.0", port, "Z21Server"),
44 logger(Logger::Logger::GetLogger("Z21Server")),
45 manager(manager),
46 lastClientID(0)
47 {
48 }
49
50 Z21Server::~Z21Server()
51 {
52 logger->Info(Languages::TextZ21ServerStopped);
53 }
54
55 void Z21Server::Start()
56 {
57 StartUdpServer();
58 logger->Info(Languages::TextZ21ServerStarted);
59 }
60
61 void Z21Server::Stop()
62 {
63 TerminateUdpServer();
64 }
65
66 Network::UdpClient* Z21Server::UdpClientFactory(const int serverSocket,
67 const struct sockaddr_storage* clientAddress)
68 {
69 return reinterpret_cast<Network::UdpClient*>(new Z21Client(++lastClientID, manager, serverSocket, clientAddress));
70 }
71
72 void Z21Server::Booster(__attribute__((unused)) const ControlType controlType,
73 const BoosterState status)
74 {
75 if (status == BoosterStateGo)
76 {
77 for (auto client : clients)
78 {
79 reinterpret_cast<Z21Client*>(client)->SendPowerOn();
80 }
81 }
82 else
83 {
84 for (auto client : clients)
85 {
86 reinterpret_cast<Z21Client*>(client)->SendPowerOff();
87 }
88 }
89 }
90
91 void Z21Server::LocoBaseSpeed(__attribute__((unused)) const ControlType controlType,
92 const LocoBase* locoBase,
93 __attribute__((unused)) const Speed speed)
94 {
95 if (nullptr == locoBase)
96 {
97 return;
98 }
99 for (auto client : clients)
100 {
101 reinterpret_cast<Z21Client*>(client)->SendLocoInfo(locoBase);
102 }
103 }
104
105 void Z21Server::LocoBaseOrientation(__attribute__((unused)) const ControlType controlType,
106 const LocoBase* locoBase,
107 __attribute__((unused)) const Orientation orientation)
108 {
109 if (nullptr == locoBase)
110 {
111 return;
112 }
113 for (auto client : clients)
114 {
115 reinterpret_cast<Z21Client*>(client)->SendLocoInfo(locoBase);
116 }
117 }
118
119 void Z21Server::LocoBaseFunction(__attribute__((unused)) const ControlType controlType,
120 const LocoBase* locoBase,
121 __attribute__((unused)) const LocoFunctionNr function,
122 __attribute__((unused)) const LocoFunctionState state)
123 {
124 if (!locoBase)
125 {
126 return;
127 }
128 for (auto client : clients)
129 {
130 reinterpret_cast<Z21Client*>(client)->SendLocoInfo(locoBase);
131 }
132 }
133
134 void Z21Server::AccessoryState(__attribute__((unused)) const ControlType controlType,
135 const DataModel::Accessory* accessory)
136 {
137 AccessoryBaseState(accessory);
138 }
139
140 void Z21Server::SwitchState(__attribute__((unused)) const ControlType controlType,
141 const DataModel::Switch* mySwitch)
142 {
143 AccessoryBaseState(mySwitch);
144 }
145
146 void Z21Server::SignalState(__attribute__((unused)) const ControlType controlType,
147 const DataModel::Signal* signal)
148 {
149 AccessoryBaseState(signal);
150 }
151
152 void Z21Server::AccessoryBaseState(const DataModel::AccessoryBase* accessoryBase)
153 {
154 if (!accessoryBase)
155 {
156 return;
157 }
158 for (auto client : clients)
159 {
160 reinterpret_cast<Z21Client*>(client)->SendTurnoutInfo(accessoryBase);
161 }
162 }
163 }} // namespace Server::Z21