| helics 3.6.1

ActionMessage.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 "ActionMessageDefintions.hpp"
10 #include "SmallBuffer.hpp"
11 #include "basic_CoreTypes.hpp"
12
13 #include <memory>
14 #include <string>
15 #include <utility>
16 #include <vector>
17
18 namespace helics {
19 constexpr int targetStringLoc{0};
20 constexpr int sourceStringLoc{1};
21 constexpr int unitStringLoc{1};
22 constexpr int origSourceStringLoc{2};
23 constexpr int origDestStringLoc{3};
24 constexpr int typeStringLoc{0};
25 constexpr int typeOutStringLoc{1};
26
27 constexpr int32_t cmd_info_basis{65536};
28
30 class ActionMessage {
31 // need to try to make sure this object is under 64 bytes in size to fit in cache lines NOT
32 // there yet
33 private:
34 action_message_def::action_t messageAction{CMD_IGNORE}; // 4 -- command
35 public:
36 int32_t messageID{0};
37 GlobalFederateId source_id{parent_broker_id};
38 InterfaceHandle source_handle{};
39 GlobalFederateId dest_id{parent_broker_id};
40 InterfaceHandle dest_handle{};
41 uint16_t counter{0};
42 uint16_t flags{0};
43 uint32_t sequenceID{0};
44 Time actionTime{timeZero};
45 Time Te{timeZero};
46 Time Tdemin{timeZero};
47 Time Tso{timeZero};
48 SmallBuffer payload;
49 private:
50 std::vector<std::string> stringData;
51 public:
53 ActionMessage() noexcept {}
58 /* implicit */ ActionMessage(action_message_def::action_t startingAction); // NOLINT
61 ActionMessage(action_message_def::action_t startingAction,
62 GlobalFederateId sourceId,
63 GlobalFederateId destId);
65 ActionMessage(ActionMessage&& act) noexcept;
67 explicit ActionMessage(std::unique_ptr<Message> message);
69 explicit ActionMessage(const std::string& bytes);
71 explicit ActionMessage(const std::vector<char>& bytes);
73 ActionMessage(const void* data, size_t size);
75 ~ActionMessage();
77 ActionMessage(const ActionMessage& act);
79 ActionMessage& operator=(const ActionMessage& act);
81 ActionMessage& operator=(ActionMessage&& act) noexcept;
87 ActionMessage& operator=(std::unique_ptr<Message> message) noexcept;
89 action_message_def::action_t action() const noexcept { return messageAction; }
91 void setAction(action_message_def::action_t newAction);
92
94 void setSource(GlobalHandle hand)
95 {
96 source_id = hand.fed_id;
97 source_handle = hand.handle;
98 }
100 void setDestination(GlobalHandle hand)
101 {
102 dest_id = hand.fed_id;
103 dest_handle = hand.handle;
104 }
106 const std::vector<std::string>& getStringData() const { return stringData; }
108 void name(std::string_view name) { payload = name; }
110 std::string_view name() const { return payload.to_string(); }
111 void clearStringData() { stringData.clear(); }
112 // most use cases for this involve short strings, or already have references that need to be
113 // copied so supporting move isn't going to be that useful here, the long strings are going in
114 // the payload
115 void setStringData(std::string_view string1)
116 {
117 stringData.resize(1);
118 stringData[0] = string1;
119 }
120 void setStringData(std::string_view string1, std::string_view string2)
121 {
122 stringData.resize(2);
123 stringData[0] = string1;
124 stringData[1] = string2;
125 }
126 void setStringData(std::string_view string1, std::string_view string2, std::string_view string3)
127 {
128 stringData.resize(3);
129 stringData[0] = string1;
130 stringData[1] = string2;
131 stringData[2] = string3;
132 }
133 void setStringData(std::string_view string1,
134 std::string_view string2,
135 std::string_view string3,
136 std::string_view string4)
137 {
138 stringData.resize(4);
139 stringData[0] = string1;
140 stringData[1] = string2;
141 stringData[2] = string3;
142 stringData[3] = string4;
143 }
144 const std::string& getString(int index) const;
145
146 void setString(int index, std::string_view str);
148 GlobalHandle getSource() const { return GlobalHandle{source_id, source_handle}; }
150 GlobalHandle getDest() const { return GlobalHandle{dest_id, dest_handle}; }
152 void swapSourceDest() noexcept
153 {
154 std::swap(source_id, dest_id);
155 std::swap(source_handle, dest_handle);
156 }
158 void setExtraData(int32_t data) { dest_handle = InterfaceHandle{data}; }
160 int32_t getExtraData() const { return dest_handle.baseValue(); }
162 void setExtraDestData(int32_t data) { source_handle = InterfaceHandle{data}; }
164 int32_t getExtraDestData() const { return source_handle.baseValue(); }
165 // functions that convert to and from a byte stream
166
168 int serializedByteCount() const;
174 int toByteArray(std::byte* data, std::size_t buffer_size) const;
176 void to_string(std::string& data) const;
178 std::string to_string() const;
180 std::string to_json_string() const;
183 std::string packetize() const;
184 void packetize(std::string& data) const;
187 std::string packetize_json() const;
189 void to_vector(std::vector<char>& data) const;
191 std::vector<char> to_vector() const;
193 std::size_t fromByteArray(const std::byte* data, std::size_t buffer_size);
197 std::size_t depacketize(const void* data, std::size_t buffer_size);
200 std::size_t from_string(std::string_view data);
203 bool from_json_string(std::string_view data);
205 std::size_t from_vector(const std::vector<char>& data);
206
207 friend std::unique_ptr<Message> createMessageFromCommand(const ActionMessage& cmd);
208 friend std::unique_ptr<Message> createMessageFromCommand(ActionMessage&& cmd);
209 };
210
211 inline bool operator<(const ActionMessage& cmd, const ActionMessage& cmd2)
212 {
213 return (cmd.actionTime < cmd2.actionTime);
214 }
215
219 std::unique_ptr<Message> createMessageFromCommand(const ActionMessage& cmd);
220
224 std::unique_ptr<Message> createMessageFromCommand(ActionMessage&& cmd);
225
227 inline bool isProtocolCommand(const ActionMessage& command) noexcept
228 {
229 return ((command.action() == CMD_PROTOCOL) || (command.action() == CMD_PROTOCOL_PRIORITY) ||
230 (command.action() == CMD_PROTOCOL_BIG));
231 }
233 inline bool isPriorityCommand(const ActionMessage& command) noexcept
234 {
235 return (command.action() < action_message_def::action_t::cmd_ignore);
236 }
237
238 inline bool isTimingCommand(const ActionMessage& command) noexcept
239 {
240 switch (command.action()) {
241 case CMD_DISCONNECT:
242 case CMD_BROADCAST_DISCONNECT:
243 case CMD_DISCONNECT_CORE:
244 case CMD_DISCONNECT_BROKER:
245 case CMD_DISCONNECT_FED:
246 case CMD_TIME_GRANT:
247 case CMD_TIME_REQUEST:
248 case CMD_EXEC_GRANT:
249 case CMD_EXEC_REQUEST:
250 case CMD_PRIORITY_DISCONNECT:
251 case CMD_TERMINATE_IMMEDIATELY:
252 case CMD_ERROR:
253 case CMD_LOCAL_ERROR:
254 case CMD_GLOBAL_ERROR:
255 case CMD_TIMING_INFO:
256 return true;
257 default:
258 return false;
259 }
260 }
261
262 inline bool isDependencyCommand(const ActionMessage& command) noexcept
263 {
264 switch (command.action()) {
265 case CMD_ADD_DEPENDENCY:
266 case CMD_REMOVE_DEPENDENCY:
267 case CMD_ADD_DEPENDENT:
268 case CMD_REMOVE_DEPENDENT:
269 case CMD_ADD_INTERDEPENDENCY:
270 case CMD_REMOVE_INTERDEPENDENCY:
271 return true;
272 default:
273 return false;
274 }
275 }
276
278 inline bool isDisconnectCommand(const ActionMessage& command) noexcept
279 {
280 switch (command.action()) {
281 case CMD_DISCONNECT:
282 case CMD_DISCONNECT_CHECK:
283 case CMD_DISCONNECT_NAME:
284 case CMD_USER_DISCONNECT:
285 case CMD_DISCONNECT_FED:
286 case CMD_DISCONNECT_CORE:
287 case CMD_PRIORITY_DISCONNECT:
288 case CMD_TERMINATE_IMMEDIATELY:
289 case CMD_REMOVE_FILTER:
290 case CMD_REMOVE_ENDPOINT:
291 case CMD_DISCONNECT_FED_ACK:
292 case CMD_DISCONNECT_CORE_ACK:
293 case CMD_DISCONNECT_BROKER_ACK:
294 case CMD_DISCONNECT_BROKER:
295 case CMD_BROADCAST_DISCONNECT:
296 case CMD_STOP:
297 return true;
298 case CMD_TIME_GRANT:
299 return (command.actionTime == Time::maxVal());
300 default:
301 return false;
302 }
303 }
304
306 inline bool isErrorCommand(const ActionMessage& command) noexcept
307 {
308 switch (command.action()) {
309 case CMD_ERROR:
310 case CMD_LOCAL_ERROR:
311 case CMD_GLOBAL_ERROR:
312 return true;
313 default:
314 return false;
315 }
316 }
318 inline bool isIgnoreableCommand(const ActionMessage& command) noexcept
319 {
320 if (isDisconnectCommand(command)) {
321 return true;
322 }
323 if (isErrorCommand(command)) {
324 return true;
325 }
326 switch (command.action()) {
327 case CMD_LOG:
328 case CMD_WARNING:
329 case CMD_REMOTE_LOG:
330 case CMD_TIME_REQUEST:
331 case CMD_REQUEST_CURRENT_TIME:
332 return true;
333 default:
334 return false;
335 }
336 }
337
339 inline bool isValidCommand(const ActionMessage& command) noexcept
340 {
341 return (command.action() != action_message_def::action_t::cmd_invalid);
342 }
347 std::string prettyPrintString(const ActionMessage& command);
348
351 std::ostream& operator<<(std::ostream& out, const ActionMessage& command);
352
357 int appendMessage(ActionMessage& multiMessage, const ActionMessage& newMessage);
358
363 std::string errorMessageString(const ActionMessage& command);
364
366 void setIterationFlags(ActionMessage& command, IterationRequest iterate);
367
368 } // namespace helics
helics::ActionMessage
Definition: ActionMessage.hpp:30
helics::ActionMessage::setExtraData
void setExtraData(int32_t data)
Definition: ActionMessage.hpp:158
helics::ActionMessage::actionTime
Time actionTime
40 the time an action took place or will take place //32
Definition: ActionMessage.hpp:44
helics::ActionMessage::Tso
Time Tso
64 the second order dependent time
Definition: ActionMessage.hpp:47
helics::ActionMessage::ActionMessage
ActionMessage() noexcept
Definition: ActionMessage.hpp:53
helics::ActionMessage::serializedByteCount
int serializedByteCount() const
Definition: ActionMessage.cpp:283
helics::ActionMessage::from_vector
std::size_t from_vector(const std::vector< char > &data)
Definition: ActionMessage.cpp:667
helics::ActionMessage::from_json_string
bool from_json_string(std::string_view data)
Definition: ActionMessage.cpp:624
helics::ActionMessage::counter
uint16_t counter
26 counter for filter tracking or multiMessage counter
Definition: ActionMessage.hpp:41
helics::ActionMessage::operator=
ActionMessage & operator=(const ActionMessage &act)
Definition: ActionMessage.cpp:89
helics::ActionMessage::getExtraData
int32_t getExtraData() const
Definition: ActionMessage.hpp:160
helics::ActionMessage::setSource
void setSource(GlobalHandle hand)
Definition: ActionMessage.hpp:94
helics::ActionMessage::payload
SmallBuffer payload
buffer to contain the data payload
Definition: ActionMessage.hpp:48
helics::ActionMessage::action
action_message_def::action_t action() const noexcept
Definition: ActionMessage.hpp:89
helics::ActionMessage::name
std::string_view name() const
Definition: ActionMessage.hpp:110
helics::ActionMessage::swapSourceDest
void swapSourceDest() noexcept
Definition: ActionMessage.hpp:152
helics::ActionMessage::source_handle
InterfaceHandle source_handle
16 – for local handle or local code
Definition: ActionMessage.hpp:38
helics::ActionMessage::createMessageFromCommand
friend std::unique_ptr< Message > createMessageFromCommand(const ActionMessage &cmd)
Definition: ActionMessage.cpp:679
helics::ActionMessage::depacketize
std::size_t depacketize(const void *data, std::size_t buffer_size)
Definition: ActionMessage.cpp:579
helics::ActionMessage::getStringData
const std::vector< std::string > & getStringData() const
Definition: ActionMessage.hpp:106
helics::ActionMessage::setDestination
void setDestination(GlobalHandle hand)
Definition: ActionMessage.hpp:100
helics::ActionMessage::to_vector
std::vector< char > to_vector() const
Definition: ActionMessage.cpp:402
helics::ActionMessage::messageID
int32_t messageID
8 – multiMessage ID for a variety of purposes
Definition: ActionMessage.hpp:36
helics::ActionMessage::toByteArray
int toByteArray(std::byte *data, std::size_t buffer_size) const
Definition: ActionMessage.cpp:182
helics::ActionMessage::packetize
std::string packetize() const
Definition: ActionMessage.cpp:366
helics::ActionMessage::getDest
GlobalHandle getDest() const
Definition: ActionMessage.hpp:150
helics::ActionMessage::to_json_string
std::string to_json_string() const
Definition: ActionMessage.cpp:316
helics::ActionMessage::getExtraDestData
int32_t getExtraDestData() const
Definition: ActionMessage.hpp:164
helics::ActionMessage::sequenceID
uint32_t sequenceID
32 a sequence number for ordering
Definition: ActionMessage.hpp:43
helics::ActionMessage::setAction
void setAction(action_message_def::action_t newAction)
Definition: ActionMessage.cpp:143
helics::ActionMessage::packetize_json
std::string packetize_json() const
Definition: ActionMessage.cpp:373
helics::ActionMessage::Te
Time Te
48 event time
Definition: ActionMessage.hpp:45
helics::ActionMessage::dest_id
GlobalFederateId dest_id
20 fed_id for a targeted multiMessage
Definition: ActionMessage.hpp:39
helics::ActionMessage::getSource
GlobalHandle getSource() const
Definition: ActionMessage.hpp:148
helics::ActionMessage::~ActionMessage
~ActionMessage()
helics::ActionMessage::fromByteArray
std::size_t fromByteArray(const std::byte *data, std::size_t buffer_size)
Definition: ActionMessage.cpp:433
helics::ActionMessage::source_id
GlobalFederateId source_id
12 – for federate_id or route_id
Definition: ActionMessage.hpp:37
helics::ActionMessage::name
void name(std::string_view name)
Definition: ActionMessage.hpp:108
helics::ActionMessage::to_string
std::string to_string() const
Definition: ActionMessage.cpp:303
helics::ActionMessage::dest_handle
InterfaceHandle dest_handle
24 local handle for a targeted multiMessage
Definition: ActionMessage.hpp:40
helics::ActionMessage::flags
uint16_t flags
28 set of messageFlags
Definition: ActionMessage.hpp:42
helics::ActionMessage::Tdemin
Time Tdemin
56 min dependent event time
Definition: ActionMessage.hpp:46
helics::ActionMessage::from_string
std::size_t from_string(std::string_view data)
Definition: ActionMessage.cpp:613
helics::ActionMessage::setExtraDestData
void setExtraDestData(int32_t data)
Definition: ActionMessage.hpp:162
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:75
helics::GlobalHandle
Definition: GlobalFederateId.hpp:147
helics::GlobalHandle::fed_id
GlobalFederateId fed_id
the federate id component
Definition: GlobalFederateId.hpp:149
helics::GlobalHandle::handle
InterfaceHandle handle
the interface handle component
Definition: GlobalFederateId.hpp:150
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
helics::InterfaceHandle::baseValue
constexpr BaseType baseValue() const
Definition: LocalFederateId.hpp:73
helics::SmallBuffer
Definition: SmallBuffer.hpp:25
helics::SmallBuffer::to_string
std::string_view to_string() const
Definition: SmallBuffer.hpp:238
helics::action_message_def::action_t
action_t
Definition: ActionMessageDefintions.hpp:20
helics::action_message_def::action_t::cmd_ignore
@ cmd_ignore
null command
helics::action_message_def::action_t::cmd_invalid
@ cmd_invalid
indicates that command has generated an invalid state
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::isValidCommand
bool isValidCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:339
helics::isPriorityCommand
bool isPriorityCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:233
helics::isDisconnectCommand
bool isDisconnectCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:278
helics::prettyPrintString
std::string prettyPrintString(const ActionMessage &command)
Definition: ActionMessage.cpp:892
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::isErrorCommand
bool isErrorCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:306
helics::createMessageFromCommand
std::unique_ptr< Message > createMessageFromCommand(const ActionMessage &cmd)
Definition: ActionMessage.cpp:679
helics::operator<<
std::ostream & operator<<(std::ostream &out, const ActionMessage &command)
Definition: ActionMessage.cpp:965
helics::errorMessageString
std::string errorMessageString(const ActionMessage &command)
Definition: ActionMessage.cpp:880
helics::IterationRequest
IterationRequest
Definition: CoreTypes.hpp:103
helics::appendMessage
int appendMessage(ActionMessage &multiMessage, const ActionMessage &newMessage)
Definition: ActionMessage.cpp:971
helics::parent_broker_id
constexpr GlobalBrokerId parent_broker_id
Definition: GlobalFederateId.hpp:67
helics::setIterationFlags
void setIterationFlags(ActionMessage &command, IterationRequest iterate)
Definition: ActionMessage.cpp:982
helics::isIgnoreableCommand
bool isIgnoreableCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:318
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::isProtocolCommand
bool isProtocolCommand(const ActionMessage &command) noexcept
Definition: ActionMessage.hpp:227