helics 3.7.0
Loading...
Searching...
No Matches
ActionMessage.hpp
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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
18namespace helics {
19constexpr int targetStringLoc{0};
20constexpr int sourceStringLoc{1};
21constexpr int unitStringLoc{1};
22constexpr int origSourceStringLoc{2};
23constexpr int origDestStringLoc{3};
24constexpr int typeStringLoc{0};
25constexpr int typeOutStringLoc{1};
26
27constexpr int32_t cmd_info_basis{65536};
28
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};
41 uint16_t counter{0};
42 uint16_t flags{0};
43 uint32_t sequenceID{0};
49 private:
50 std::vector<std::string> stringData;
51 public:
53 ActionMessage() noexcept {}
58 /* implicit */ ActionMessage(action_message_def::action_t startingAction); // NOLINT
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);
77 ActionMessage(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; }
92
95 {
96 source_id = hand.fed_id;
97 source_handle = hand.handle;
98 }
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);
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
211inline bool operator<(const ActionMessage& cmd, const ActionMessage& cmd2)
212{
213 return (cmd.actionTime < cmd2.actionTime);
214}
215
219std::unique_ptr<Message> createMessageFromCommand(const ActionMessage& cmd);
220
224std::unique_ptr<Message> createMessageFromCommand(ActionMessage&& cmd);
225
227inline 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}
233inline bool isPriorityCommand(const ActionMessage& command) noexcept
234{
235 return (command.action() < action_message_def::action_t::cmd_ignore);
236}
237
238inline 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
262inline 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
278inline 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
306inline 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}
318inline 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
339inline bool isValidCommand(const ActionMessage& command) noexcept
340{
341 return (command.action() != action_message_def::action_t::cmd_invalid);
342}
347std::string prettyPrintString(const ActionMessage& command);
348
351std::ostream& operator<<(std::ostream& out, const ActionMessage& command);
352
357int appendMessage(ActionMessage& multiMessage, const ActionMessage& newMessage);
358
363std::string errorMessageString(const ActionMessage& command);
364
366void setIterationFlags(ActionMessage& command, IterationRequest iterate);
367
368} // namespace helics
Definition ActionMessage.hpp:30
void setExtraData(int32_t data)
Definition ActionMessage.hpp:158
Time actionTime
40 the time an action took place or will take place //32
Definition ActionMessage.hpp:44
const std::vector< std::string > & getStringData() const
Definition ActionMessage.hpp:106
Time Tso
64 the second order dependent time
Definition ActionMessage.hpp:47
ActionMessage() noexcept
Definition ActionMessage.hpp:53
int serializedByteCount() const
Definition ActionMessage.cpp:283
std::size_t from_vector(const std::vector< char > &data)
Definition ActionMessage.cpp:667
bool from_json_string(std::string_view data)
Definition ActionMessage.cpp:624
uint16_t counter
26 counter for filter tracking or multiMessage counter
Definition ActionMessage.hpp:41
ActionMessage & operator=(const ActionMessage &act)
Definition ActionMessage.cpp:89
int32_t getExtraData() const
Definition ActionMessage.hpp:160
void setSource(GlobalHandle hand)
Definition ActionMessage.hpp:94
SmallBuffer payload
buffer to contain the data payload
Definition ActionMessage.hpp:48
action_message_def::action_t action() const noexcept
Definition ActionMessage.hpp:89
std::string_view name() const
Definition ActionMessage.hpp:110
void swapSourceDest() noexcept
Definition ActionMessage.hpp:152
InterfaceHandle source_handle
16 – for local handle or local code
Definition ActionMessage.hpp:38
std::size_t depacketize(const void *data, std::size_t buffer_size)
Definition ActionMessage.cpp:579
void setDestination(GlobalHandle hand)
Definition ActionMessage.hpp:100
std::vector< char > to_vector() const
Definition ActionMessage.cpp:402
int32_t messageID
8 – multiMessage ID for a variety of purposes
Definition ActionMessage.hpp:36
int toByteArray(std::byte *data, std::size_t buffer_size) const
Definition ActionMessage.cpp:182
std::string packetize() const
Definition ActionMessage.cpp:366
GlobalHandle getDest() const
Definition ActionMessage.hpp:150
std::string to_json_string() const
Definition ActionMessage.cpp:316
int32_t getExtraDestData() const
Definition ActionMessage.hpp:164
uint32_t sequenceID
32 a sequence number for ordering
Definition ActionMessage.hpp:43
void setAction(action_message_def::action_t newAction)
Definition ActionMessage.cpp:143
std::string packetize_json() const
Definition ActionMessage.cpp:373
Time Te
48 event time
Definition ActionMessage.hpp:45
GlobalFederateId dest_id
20 fed_id for a targeted multiMessage
Definition ActionMessage.hpp:39
GlobalHandle getSource() const
Definition ActionMessage.hpp:148
std::size_t fromByteArray(const std::byte *data, std::size_t buffer_size)
Definition ActionMessage.cpp:433
GlobalFederateId source_id
12 – for federate_id or route_id
Definition ActionMessage.hpp:37
friend std::unique_ptr< Message > createMessageFromCommand(const ActionMessage &cmd)
Definition ActionMessage.cpp:679
void name(std::string_view name)
Definition ActionMessage.hpp:108
std::string to_string() const
Definition ActionMessage.cpp:303
InterfaceHandle dest_handle
24 local handle for a targeted multiMessage
Definition ActionMessage.hpp:40
uint16_t flags
28 set of messageFlags
Definition ActionMessage.hpp:42
Time Tdemin
56 min dependent event time
Definition ActionMessage.hpp:46
std::size_t from_string(std::string_view data)
Definition ActionMessage.cpp:613
void setExtraDestData(int32_t data)
Definition ActionMessage.hpp:162
Definition GlobalFederateId.hpp:75
Definition GlobalFederateId.hpp:147
GlobalFederateId fed_id
the federate id component
Definition GlobalFederateId.hpp:149
InterfaceHandle handle
the interface handle component
Definition GlobalFederateId.hpp:150
Definition LocalFederateId.hpp:65
constexpr BaseType baseValue() const
Definition LocalFederateId.hpp:73
Definition SmallBuffer.hpp:25
std::string_view to_string() const
Definition SmallBuffer.hpp:238
action_t
Definition ActionMessageDefintions.hpp:20
@ cmd_invalid
indicates that command has generated an invalid state
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition AsyncFedCallInfo.hpp:14
bool isValidCommand(const ActionMessage &command) noexcept
Definition ActionMessage.hpp:339
bool isPriorityCommand(const ActionMessage &command) noexcept
Definition ActionMessage.hpp:233
bool isDisconnectCommand(const ActionMessage &command) noexcept
Definition ActionMessage.hpp:278
std::string prettyPrintString(const ActionMessage &command)
Definition ActionMessage.cpp:892
constexpr Time timeZero
Definition helicsTime.hpp:31
bool isErrorCommand(const ActionMessage &command) noexcept
Definition ActionMessage.hpp:306
std::unique_ptr< Message > createMessageFromCommand(const ActionMessage &cmd)
Definition ActionMessage.cpp:679
std::ostream & operator<<(std::ostream &out, const ActionMessage &command)
Definition ActionMessage.cpp:965
std::string errorMessageString(const ActionMessage &command)
Definition ActionMessage.cpp:880
IterationRequest
Definition CoreTypes.hpp:103
int appendMessage(ActionMessage &multiMessage, const ActionMessage &newMessage)
Definition ActionMessage.cpp:971
constexpr GlobalBrokerId parent_broker_id
Definition GlobalFederateId.hpp:67
void setIterationFlags(ActionMessage &command, IterationRequest iterate)
Definition ActionMessage.cpp:982
bool isIgnoreableCommand(const ActionMessage &command) noexcept
Definition ActionMessage.hpp:318
TimeRepresentation< count_time< 9 > > Time
Definition helicsTime.hpp:27
bool isProtocolCommand(const ActionMessage &command) noexcept
Definition ActionMessage.hpp:227