helics  3.3.0
BaseTimeCoordinator.hpp
1 /*
2 Copyright (c) 2017-2022,
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 "CoreFederateInfo.hpp"
11 #include "TimeDependencies.hpp"
12 
13 #include "json/forwards.h"
14 #include <atomic>
15 #include <functional>
16 #include <string>
17 #include <utility>
18 #include <vector>
19 
20 namespace helics {
21 
26  protected:
28  std::function<void(const ActionMessage&)> sendMessageFunction;
31 
33  std::int32_t sequenceCounter{0};
34  bool noParent{true};
35  bool federatesOnly{false};
36  bool checkingExec{false};
37  bool executionMode{false};
38  bool restrictive_time_policy{false};
42  bool nonGranting{false};
43  bool delayedTiming{false};
44  bool disconnected{false};
45 
46  public:
47  static constexpr std::int32_t TIME_COORDINATOR_VERSION{1};
48  BaseTimeCoordinator();
49  explicit BaseTimeCoordinator(std::function<void(const ActionMessage&)> userSendMessageFunction);
50  virtual ~BaseTimeCoordinator() = default;
52  void setMessageSender(std::function<void(const ActionMessage&)> userSendMessageFunction);
53 
54  void setRestrictivePolicy(bool policy) { restrictive_time_policy = policy; }
56  std::vector<GlobalFederateId> getDependencies() const;
58  std::vector<GlobalFederateId> getDependents() const;
59 
60  void setSourceId(GlobalFederateId sourceId) { mSourceId = sourceId; }
61  GlobalFederateId sourceId() const { return mSourceId; }
65  virtual bool updateTimeFactors() = 0;
66 
70  const DependencyInfo* getDependencyInfo(GlobalFederateId ofed) const;
72  bool isDependency(GlobalFederateId ofed) const;
74  bool empty() const { return dependencies.empty(); }
75 
76  protected:
79  GlobalFederateId fed,
80  std::int32_t responseCode) const;
82  void sendTimingInfo();
83 
84  public:
88  bool processTimeMessage(const ActionMessage& cmd);
89 
95  virtual bool addDependency(GlobalFederateId fedID);
99  virtual bool addDependent(GlobalFederateId fedID);
102  virtual void removeDependency(GlobalFederateId fedID);
105  virtual void removeDependent(GlobalFederateId fedID);
106 
107  void setAsChild(GlobalFederateId fedID);
108  void setAsParent(GlobalFederateId fedID);
109  void setVersion(GlobalFederateId fedID, std::int8_t version);
110  GlobalFederateId getParent() const;
111 
113  void disconnect();
117 
122 
124  virtual std::string printTimeStatus() const = 0;
126  virtual void generateDebuggingTimeInfo(Json::Value& base) const;
127 
129  bool hasActiveTimeDependencies() const;
130 
132  int dependencyCount() const;
134  GlobalFederateId getMinDependency() const;
135 
138  Json::Value grantTimeoutCheck(const ActionMessage& cmd);
140  virtual Time getNextTime() const = 0;
141 };
142 } // namespace helics
helics::BaseTimeCoordinator::nonGranting
bool nonGranting
Definition: BaseTimeCoordinator.hpp:42
helics::ActionMessage::setExtraDestData
void setExtraDestData(int32_t data)
Definition: ActionMessage.hpp:161
helics::GlobalFederateId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:84
helics::BaseTimeCoordinator::dependencies
TimeDependencies dependencies
Definition: BaseTimeCoordinator.hpp:27
helics::BaseTimeCoordinator::sendMessageFunction
std::function< void(const ActionMessage &)> sendMessageFunction
callback used to send the messages
Definition: BaseTimeCoordinator.hpp:29
helics::TimeData::responseSequenceCounter
std::int32_t responseSequenceCounter
Definition: TimeDependencies.hpp:74
helics::TimeDependencies::removeDependent
void removeDependent(GlobalFederateId id)
Definition: TimeDependencies.cpp:409
helics::BaseTimeCoordinator::getDependencyInfo
const DependencyInfo * getDependencyInfo(GlobalFederateId ofed) const
Definition: BaseTimeCoordinator.cpp:226
helics::BaseTimeCoordinator::mSourceId
GlobalFederateId mSourceId
the identifier for inserting into the source id field of any generated messages;
Definition: BaseTimeCoordinator.hpp:32
helics::TimeDependencies
Definition: TimeDependencies.hpp:124
helics_definitions.hpp
base helics enumerations for C++ API's, a namespace wrapper for the definitions defined in helics_enu...
helics::TimeDependencies::getMinDependency
GlobalFederateId getMinDependency() const
Definition: TimeDependencies.cpp:609
helics::ActionMessage::setAction
void setAction(action_message_def::action_t newAction)
Definition: ActionMessage.cpp:142
helics::TimeDependencies::removeDependency
void removeDependency(GlobalFederateId id)
Definition: TimeDependencies.cpp:371
helics::BaseTimeCoordinator::disconnect
void disconnect()
Definition: BaseTimeCoordinator.cpp:79
helics::TimeData
Definition: TimeDependencies.hpp:57
helics::BaseTimeCoordinator::getDependencies
std::vector< GlobalFederateId > getDependencies() const
Definition: BaseTimeCoordinator.cpp:231
helics::TimeData::interrupted
bool interrupted
indicator that the federates next event is a timing interruption
Definition: TimeDependencies.hpp:67
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::TimeData::Te
Time Te
the next currently scheduled event
Definition: TimeDependencies.hpp:60
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:72
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::parent_broker_id
constexpr GlobalBrokerId parent_broker_id
Definition: GlobalFederateId.hpp:64
helics::BaseTimeCoordinator::sequenceCounter
std::int32_t sequenceCounter
storage for sequence counter
Definition: BaseTimeCoordinator.hpp:33
helics::BaseTimeCoordinator::processTimeMessage
bool processTimeMessage(const ActionMessage &cmd)
Definition: BaseTimeCoordinator.cpp:367
helics::BaseTimeCoordinator::isDependency
bool isDependency(GlobalFederateId ofed) const
Definition: BaseTimeCoordinator.cpp:151
helicscpp::version
std::string version()
Definition: cpp98/helics.hpp:26
helics::TimeDependencies::isDependency
bool isDependency(GlobalFederateId ofed) const
Definition: TimeDependencies.cpp:308
helics::BaseTimeCoordinator::removeDependent
virtual void removeDependent(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:221
helics::appendMessage
int appendMessage(ActionMessage &m, const ActionMessage &newMessage)
Definition: ActionMessage.cpp:925
helics::ActionMessage::counter
uint16_t counter
26 counter for filter tracking or message counter
Definition: ActionMessage.hpp:41
helics::IterationRequest::FORCE_ITERATION
@ FORCE_ITERATION
force an iteration whether it is needed or not
helics::TimeDependencies::size
auto size() const
Definition: TimeDependencies.hpp:151
helics::BaseTimeCoordinator::printTimeStatus
virtual std::string printTimeStatus() const =0
helics::BaseTimeCoordinator::empty
bool empty() const
Definition: BaseTimeCoordinator.hpp:74
helics::TimeData::timingVersion
std::int8_t timingVersion
version indicator
Definition: TimeDependencies.hpp:69
helics::TimeData::next
Time next
next possible message or value
Definition: TimeDependencies.hpp:59
helics::BaseTimeCoordinator::checkExecEntry
virtual MessageProcessingResult checkExecEntry(GlobalFederateId triggerFed=GlobalFederateId{})=0
helics::TimeDependencies::activeDependencyCount
int activeDependencyCount() const
Definition: TimeDependencies.cpp:602
helics::BaseTimeCoordinator::executionMode
bool executionMode
Definition: BaseTimeCoordinator.hpp:37
helics::BaseTimeCoordinator::federatesOnly
bool federatesOnly
indicator that the forwarder only operates with federates
Definition: BaseTimeCoordinator.hpp:35
helics::BaseTimeCoordinator::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: BaseTimeCoordinator.cpp:253
helics::delayed_timing_flag
@ delayed_timing_flag
flag to indicate the request is from federate with delayed timing
Definition: flagOperations.hpp:30
helics::BaseTimeCoordinator::restrictive_time_policy
bool restrictive_time_policy
flag indicating that a restrictive time policy should be used
Definition: BaseTimeCoordinator.hpp:39
helics::IterationRequest
IterationRequest
Definition: CoreTypes.hpp:92
helics::TimeDependencies::getDependencyInfo
const DependencyInfo * getDependencyInfo(GlobalFederateId id) const
Definition: TimeDependencies.cpp:326
helics::ActionMessage::messageID
int32_t messageID
8 – message ID for a variety of purposes
Definition: ActionMessage.hpp:36
helics::ActionMessage::setExtraData
void setExtraData(int32_t data)
Definition: ActionMessage.hpp:157
helics::setActionFlag
void setActionFlag(FlagContainer &M, FlagIndex flag)
Definition: flagOperations.hpp:130
helics::TimeDependencies::begin
auto begin()
Definition: TimeDependencies.hpp:153
helics::BaseTimeCoordinator::sendTimingInfo
void sendTimingInfo()
Definition: BaseTimeCoordinator.cpp:268
helics::BaseTimeCoordinator::processDependencyUpdateMessage
void processDependencyUpdateMessage(const ActionMessage &cmd)
Definition: BaseTimeCoordinator.cpp:402
helics::BaseTimeCoordinator::setMessageSender
void setMessageSender(std::function< void(const ActionMessage &)> userSendMessageFunction)
Definition: BaseTimeCoordinator.cpp:35
helics::BaseTimeCoordinator::grantTimeoutCheck
Json::Value grantTimeoutCheck(const ActionMessage &cmd)
Definition: BaseTimeCoordinator.cpp:136
helics::IterationRequest::ITERATE_IF_NEEDED
@ ITERATE_IF_NEEDED
indicator that the iterations need to continue
helics::ActionMessage::payload
SmallBuffer payload
buffer to contain the data payload
Definition: ActionMessage.hpp:48
helics::ActionMessage::dest_id
GlobalFederateId dest_id
20 fed_id for a targeted message
Definition: ActionMessage.hpp:39
helics::ActionMessage::Te
Time Te
48 event time
Definition: ActionMessage.hpp:45
helics::TimeDependencies::addDependency
bool addDependency(GlobalFederateId id)
Definition: TimeDependencies.cpp:346
helics::BaseTimeCoordinator::noParent
bool noParent
indicator that the coordinator does not have parents
Definition: BaseTimeCoordinator.hpp:34
helics::BaseTimeCoordinator::enteringExecMode
virtual void enteringExecMode(IterationRequest mode=IterationRequest::NO_ITERATIONS)
Definition: BaseTimeCoordinator.cpp:44
helics::DependencyInfo
Definition: TimeDependencies.hpp:93
helics::BaseTimeCoordinator::dependencyCount
int dependencyCount() const
Definition: BaseTimeCoordinator.cpp:258
helics::BaseTimeCoordinator::addDependent
virtual bool addDependent(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:170
helics::BaseTimeCoordinator::updateTimeFactors
virtual bool updateTimeFactors()=0
helics::TimeDependencies::updateTime
DependencyProcessingResult updateTime(const ActionMessage &m)
Definition: TimeDependencies.cpp:432
helics::MessageProcessingResult
MessageProcessingResult
Definition: CoreTypes.hpp:64
helics::non_granting_flag
@ non_granting_flag
flag to indicate the request is from a non-granting federate
Definition: flagOperations.hpp:25
helics::ActionMessage::Tdemin
Time Tdemin
56 min dependent event time
Definition: ActionMessage.hpp:46
helics::ActionMessage::actionTime
Time actionTime
40 the time an action took place or will take place //32
Definition: ActionMessage.hpp:44
helics::parent_flag
@ parent_flag
flag indicating the message is from a parent object
Definition: flagOperations.hpp:32
helics::TimeDependencies::addDependent
bool addDependent(GlobalFederateId id)
Definition: TimeDependencies.cpp:384
helics::interrupted_flag
@ interrupted_flag
flag to mark an interrupted event
Definition: flagOperations.hpp:28
helics::BaseTimeCoordinator
Definition: BaseTimeCoordinator.hpp:25
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::BaseTimeCoordinator::getNextTime
virtual Time getNextTime() const =0
helics::TimeDependencies::hasActiveTimeDependencies
bool hasActiveTimeDependencies() const
Definition: TimeDependencies.cpp:589
helics::checkActionFlag
bool checkActionFlag(uint16_t flags, FlagIndex flag)
Definition: flagOperations.hpp:138
helics::BaseTimeCoordinator::generateTimeRequest
ActionMessage generateTimeRequest(const TimeData &dep, GlobalFederateId fed, std::int32_t responseCode) const
Definition: BaseTimeCoordinator.cpp:288
helics::BaseTimeCoordinator::generateDebuggingTimeInfo
virtual void generateDebuggingTimeInfo(Json::Value &base) const
Definition: BaseTimeCoordinator.cpp:118
helics::TimeData::minDe
Time minDe
min dependency event time
Definition: TimeDependencies.hpp:61
helics::TimeDependencies::empty
bool empty() const
Definition: TimeDependencies.hpp:166
helics::TimeData::minFed
GlobalFederateId minFed
identifier for the min dependency
Definition: TimeDependencies.hpp:63
helics::BaseTimeCoordinator::removeDependency
virtual void removeDependency(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:216
flagOperations.hpp
helics::IterationRequest::NO_ITERATIONS
@ NO_ITERATIONS
indicator that the iterations have completed
helics::BaseTimeCoordinator::getDependents
std::vector< GlobalFederateId > getDependents() const
Definition: BaseTimeCoordinator.cpp:242
helics::BaseTimeCoordinator::getMinDependency
GlobalFederateId getMinDependency() const
Definition: BaseTimeCoordinator.cpp:263
helics::ActionMessage::source_id
GlobalFederateId source_id
12 – for federate_id or route_id
Definition: ActionMessage.hpp:37
helics::TimeDependencies::checkForIssues
std::pair< int, std::string > checkForIssues(bool waiting) const
Definition: TimeDependencies.cpp:661
helics::BaseTimeCoordinator::addDependency
virtual bool addDependency(GlobalFederateId fedID)
Definition: BaseTimeCoordinator.cpp:156
helics::child_flag
@ child_flag
flag indicating a message is from a child object
Definition: flagOperations.hpp:35
helics::setIterationFlags
void setIterationFlags(ActionMessage &command, IterationRequest iterate)
Definition: ActionMessage.cpp:936