• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

traintastic / traintastic / 23667775307

27 Mar 2026 09:13PM UTC coverage: 28.009% (-0.002%) from 28.011%
23667775307

push

github

reinder
[boost] fix timer cancel() error_code overload is removed in 1.87, see #220

0 of 5 new or added lines in 1 file covered. (0.0%)

220 existing lines in 10 files now uncovered.

8182 of 29212 relevant lines covered (28.01%)

194.75 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/server/src/hardware/protocol/withrottle/kernel.hpp
1
/**
2
 * server/src/hardware/protocol/withrottle/kernel.hpp
3
 *
4
 * This file is part of the traintastic source code.
5
 *
6
 * Copyright (C) 2022-2023,2025 Reinder Feenstra
7
 *
8
 * This program is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU General Public License
10
 * as published by the Free Software Foundation; either version 2
11
 * of the License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21
 */
22

23
#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_WITHROTTLE_KERNEL_HPP
24
#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_WITHROTTLE_KERNEL_HPP
25

26
#include "../kernelbase.hpp"
27
#include <unordered_map>
28
#include <boost/asio/post.hpp>
29
#include <boost/signals2/connection.hpp>
30
#include <traintastic/enum/tristate.hpp>
31
#include "config.hpp"
32
#include "iohandler/iohandler.hpp"
33

34
class Clock;
35
class Decoder;
36
class Throttle;
37
class HardwareThrottle;
38
class ThrottleController;
39

40
namespace WiThrottle {
41

42
enum class ThrottleCommand : char;
43
struct Address;
44

45
class Kernel : public ::KernelBase
46
{
47
  private:
48
    struct MultiThrottle
49
    {
50
      uint16_t address;
51
      bool isLongAddress;
52
      std::shared_ptr<HardwareThrottle> throttle;
53
    };
54

55
    struct Client
56
    {
57
      std::string id;
58
      std::string name;
59
      std::unordered_map<char, MultiThrottle> multiThrottles;
60
    };
61

62
    std::unique_ptr<IOHandler> m_ioHandler;
63

64
    TriState m_powerOn;
65

66
    std::shared_ptr<Clock> m_clock;
67
    boost::signals2::connection m_clockChangeConnection;
68

69
    ThrottleController* m_throttleController;
70
    std::unordered_map<IOHandler::ClientId, Client> m_clients;
71

72
    Config m_config;
73
    bool m_running = false;
74

75
    Kernel(std::string logId_, const Config& config);
76

77
    void setIOHandler(std::unique_ptr<IOHandler> handler);
78

UNCOV
79
    void postSendTo(std::string message, IOHandler::ClientId clientId)
×
80
    {
81
      boost::asio::post(m_ioContext, 
×
UNCOV
82
        [this, msg=std::move(message), clientId]()
×
83
        {
84
          sendTo(msg, clientId);
×
85
        });
×
UNCOV
86
    }
×
87

UNCOV
88
    void postSendToAll(std::string message)
×
89
    {
90
      boost::asio::post(m_ioContext, 
×
UNCOV
91
        [this, msg=std::move(message)]()
×
92
        {
93
          sendToAll(msg);
×
94
        });
×
UNCOV
95
    }
×
96

97
    void sendTo(std::string_view message, IOHandler::ClientId clientId);
98
    void sendToAll(std::string_view message);
99

100
    MultiThrottle* getMultiThrottle(IOHandler::ClientId clientId, char multiThrottleId);
101
    const std::shared_ptr<HardwareThrottle>& getThottle(IOHandler::ClientId clientId, char multiThrottleId = invalidMultiThrottleId);
102
    const std::shared_ptr<Decoder>& getDecoder(IOHandler::ClientId clientId, char multiThrottleId);
103

104
    void multiThrottleAction(IOHandler::ClientId clientId, char multiThrottleId, const Address& address, ThrottleCommand throttleCommand, std::string_view message);
105

106
    void throttleReleased(IOHandler::ClientId clientId, char multiThrottleId, const std::shared_ptr<Throttle>& throttle);
107

108
  public:
109
    static constexpr char invalidMultiThrottleId = '\0';
110

111
    Kernel(const Kernel&) = delete;
112
    Kernel& operator =(const Kernel&) = delete;
113

114
#ifndef NDEBUG
UNCOV
115
    bool isKernelThread() const
×
116
    {
UNCOV
117
      return std::this_thread::get_id() == m_thread.get_id();
×
118
    }
119
#endif
120

121
    /**
122
     * \brief Create kernel and IO handler
123
     *
124
     * \param[in] args IO handler arguments
125
     * \return The kernel instance
126
     */
127
    template<class IOHandlerType, class... Args>
UNCOV
128
    static std::unique_ptr<Kernel> create(std::string logId_, const Config& config, Args... args)
×
129
    {
130
      static_assert(std::is_base_of_v<IOHandler, IOHandlerType>);
131
      std::unique_ptr<Kernel> kernel{new Kernel(std::move(logId_), config)};
×
132
      kernel->setIOHandler(std::make_unique<IOHandlerType>(*kernel, std::forward<Args>(args)...));
×
133
      return kernel;
×
UNCOV
134
    }
×
135

136
    /**
137
     * \brief Set WiThrottle configuration
138
     *
139
     * \param[in] config The WiThrottle configuration
140
     */
141
    void setConfig(const Config& config);
142

143
    /**
144
     * \brief Set clock for LocoNet fast clock
145
     *
146
     * \param[in] clock The clock
147
     * \note This function may not be called when the kernel is running.
148
     */
149
    void setClock(std::shared_ptr<Clock> clock);
150

151
    /**
152
     * \brief Set the throttle controller
153
     *
154
     * \param[in] throttleController The throttle controller
155
     * \note This function may not be called when the kernel is running.
156
     */
UNCOV
157
    inline void setThrottleController(ThrottleController* throttleController)
×
158
    {
159
      assert(!m_running);
×
160
      m_throttleController = throttleController;
×
UNCOV
161
    }
×
162

163
    /**
164
     *
165
     */
166
    void start();
167

168
    /**
169
     *
170
     */
171
    void stop();
172

173
    /**
174
     *
175
     *
176
     */
177
    void setPowerOn(bool on);
178

179
    /**
180
     * \brief New client handler
181
     * \param[in] cliendId The client who connected
182
     * \note This function must run in the kernel's IO context
183
     */
184
    void newClient(IOHandler::ClientId clientId);
185

186
    /**
187
     * \brief Client gone handler
188
     * \param[in] cliendId The client who disconnected
189
     * \note This function must run in the kernel's IO context
190
     */
191
    void clientGone(IOHandler::ClientId clientId);
192

193
    /**
194
     * \brief Incoming message handler
195
     * This method must be called by the IO handler whenever a WiThrottle message is received.
196
     * \param[in] message The received WiThrottle message
197
     * \param[in] cliendId The client who sent the message
198
     * \note This function must run in the kernel's IO context
199
     */
200
    void receiveFrom(std::string_view message, IOHandler::ClientId clientId);
201
};
202

203
}
204

205
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc