| helics 3.6.1

TranslatorFederate.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 "../common/JsonBuilder.hpp"
10 #include "Core.hpp"
11 #include "FederateIdExtra.hpp"
12 #include "TimeCoordinator.hpp"
13 #include "TranslatorInfo.hpp"
14 #include "gmlc/containers/AirLock.hpp"
15 #include "gmlc/containers/MappedPointerVector.hpp"
16
17 #include <any>
18 #include <deque>
19 #include <functional>
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <utility>
25 #include <vector>
26
27 namespace helics {
28 class HandleManager;
29 class ActionMessage;
30 class BasicHandleInfo;
31
32 class TranslatorFederate {
33 private:
34 GlobalFederateId mFedID;
35 GlobalBrokerId mCoreID;
36 const std::string mName;
37 // Core* mCore{nullptr};
38 TimeCoordinator mCoord;
39 HandleManager* mHandles{nullptr};
40 FederateStates current_state{FederateStates::CREATED};
42 bool usingGlobalTime{false};
43
44 std::function<void(const ActionMessage&)> mQueueMessage;
45 std::function<void(ActionMessage&&)> mQueueMessageMove;
46 std::function<void(const ActionMessage&)> mSendMessage;
47 std::function<void(ActionMessage&&)> mSendMessageMove;
48
49 std::function<void(ActionMessage&)> mDeliverMessage;
50
51 std::function<void(int, std::string_view, std::string_view)> mLogger;
52 std::function<gmlc::containers::AirLock<std::any>&(int)> mGetAirLock;
53
54 Time minReturnTime{Time::maxVal()};
55
57 gmlc::containers::MappedPointerVector<TranslatorInfo, GlobalHandle> translators;
58 // bool hasTiming{false};
59
60 public:
61 TranslatorFederate(GlobalFederateId fedID, std::string name, GlobalBrokerId coreID, Core* core);
62 ~TranslatorFederate();
64 TranslatorInfo* createTranslator(GlobalBrokerId dest,
65 InterfaceHandle handle,
66 std::string_view key,
67 std::string_view endpointType,
68 std::string_view units);
69
70 void setCallbacks(std::function<void(const ActionMessage&)> queueMessage,
71 std::function<void(ActionMessage&&)> queueMessageMove,
72 std::function<void(const ActionMessage&)> sendMessage,
73 std::function<void(ActionMessage&&)> sendMessageMove)
74 {
75 mQueueMessage = std::move(queueMessage);
76 mQueueMessageMove = std::move(queueMessageMove);
77 mSendMessage = std::move(sendMessage);
78 mSendMessageMove = std::move(sendMessageMove);
79 }
80
81 void setLogger(std::function<void(int, std::string_view, std::string_view)> logger)
82 {
83 mLogger = std::move(logger);
84 }
85
86 void setDeliver(std::function<void(ActionMessage&)> deliverMessage)
87 {
88 mDeliverMessage = std::move(deliverMessage);
89 }
90
91 void setAirLockFunction(std::function<gmlc::containers::AirLock<std::any>&(int)> getAirLock)
92 {
93 mGetAirLock = std::move(getAirLock);
94 }
95
96 void handleMessage(ActionMessage& command);
97
98 void setHandleManager(HandleManager* handles) { mHandles = handles; }
99
100 std::string query(std::string_view queryStr) const;
102 bool hasActiveTimeDependencies() const;
103
104 void useGlobalTimeCoordinator(bool value)
105 {
106 usingGlobalTime = value;
107 mCoord.globalTime = value;
108 }
109
110 private:
111 void routeMessage(const ActionMessage& msg);
112
114 TranslatorInfo* getTranslatorInfo(GlobalHandle id);
115 TranslatorInfo* getTranslatorInfo(GlobalFederateId fed, InterfaceHandle handle);
116 const TranslatorInfo* getTranslatorInfo(GlobalFederateId fed, InterfaceHandle handle) const;
117
118 void executeTranslator(ActionMessage& command, TranslatorInfo* trans);
119 };
120 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::Core
Definition: core/Core.hpp:41
helics::GlobalBrokerId
Definition: GlobalFederateId.hpp:30
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:75
helics::GlobalHandle
Definition: GlobalFederateId.hpp:147
helics::HandleManager
Definition: HandleManager.hpp:25
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
helics::TimeCoordinator
Definition: TimeCoordinator.hpp:48
helics::TimeCoordinator::globalTime
bool globalTime
true if using a global time manager parent
Definition: TimeCoordinator.hpp:94
helics::TranslatorFederate
Definition: TranslatorFederate.hpp:32
helics::TranslatorFederate::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: TranslatorFederate.cpp:475
helics::TranslatorFederate::createTranslator
TranslatorInfo * createTranslator(GlobalBrokerId dest, InterfaceHandle handle, std::string_view key, std::string_view endpointType, std::string_view units)
Definition: TranslatorFederate.cpp:291
helics::TranslatorInfo
Definition: TranslatorInfo.hpp:32
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::FederateStates
FederateStates
Definition: CoreTypes.hpp:21
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27