| helics 3.6.1

MessageTimer.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8
9 #include "ActionMessage.hpp"
10 #include "gmlc/networking/AsioContextManager.h"
11
12 #include <asio/steady_timer.hpp>
13 #include <memory>
14 #include <mutex>
15 #include <vector>
16
17 namespace helics {
20 class MessageTimer: public std::enable_shared_from_this<MessageTimer> {
21 public:
22 using time_type = decltype(std::chrono::steady_clock::now());
23 explicit MessageTimer(std::function<void(ActionMessage&&)> sFunction);
26 int32_t addTimerFromNow(std::chrono::nanoseconds time, ActionMessage mess);
29 int32_t addTimer(time_type expirationTime, ActionMessage mess);
31 void cancelTimer(int32_t index);
33 void cancelAll();
35 void updateTimer(int32_t timerIndex, time_type expirationTime, ActionMessage mess);
37 void updateTimerFromNow(int32_t timerIndex, std::chrono::nanoseconds time, ActionMessage mess);
41 bool updateTimer(int32_t timerIndex, time_type expirationTime);
45 bool addTimeToTimer(int32_t timerIndex, std::chrono::nanoseconds time);
47 void updateMessage(int32_t timerIndex, ActionMessage mess);
49 void sendMessage(int32_t timerIndex);
50
51 private:
52 std::mutex timerLock;
53 std::vector<ActionMessage> buffers;
54 std::vector<time_type> expirationTimes;
56 const std::function<void(ActionMessage&&)> sendFunction;
57 std::vector<std::shared_ptr<asio::steady_timer>> timers;
59 std::shared_ptr<gmlc::networking::AsioContextManager> contextPtr;
61 decltype(contextPtr->startContextLoop()) loopHandle;
62 };
63 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::MessageTimer
Definition: MessageTimer.hpp:20
helics::MessageTimer::addTimer
int32_t addTimer(time_type expirationTime, ActionMessage mess)
Definition: MessageTimer.cpp:41
helics::MessageTimer::updateMessage
void updateMessage(int32_t timerIndex, ActionMessage mess)
Definition: MessageTimer.cpp:143
helics::MessageTimer::updateTimerFromNow
void updateTimerFromNow(int32_t timerIndex, std::chrono::nanoseconds time, ActionMessage mess)
Definition: MessageTimer.cpp:102
helics::MessageTimer::cancelTimer
void cancelTimer(int32_t index)
Definition: MessageTimer.cpp:66
helics::MessageTimer::addTimeToTimer
bool addTimeToTimer(int32_t timerIndex, std::chrono::nanoseconds time)
Definition: MessageTimer.cpp:109
helics::MessageTimer::sendMessage
void sendMessage(int32_t timerIndex)
Definition: MessageTimer.cpp:151
helics::MessageTimer::updateTimer
void updateTimer(int32_t timerIndex, time_type expirationTime, ActionMessage mess)
Definition: MessageTimer.cpp:86
helics::MessageTimer::addTimerFromNow
int32_t addTimerFromNow(std::chrono::nanoseconds time, ActionMessage mess)
Definition: MessageTimer.cpp:36
helics::MessageTimer::cancelAll
void cancelAll()
Definition: MessageTimer.cpp:75
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14