SOLA
Loading...
Searching...
No Matches
message.h
1// Copyright The SOLA Contributors
2//
3// Licensed under the MIT License.
4// For details on the licensing terms, see the LICENSE file.
5// SPDX-License-Identifier: MIT
6
7#ifndef MINHTON_MESSAGE_MESSAGE_H_
8#define MINHTON_MESSAGE_MESSAGE_H_
9
10#include <cstdint>
11#include <map>
12#include <memory>
13#include <stdexcept>
14#include <string>
15#include <variant>
16
17#include "minhton/core/node_info.h"
18#include "minhton/message/message_header.h"
19#include "minhton/message/message_logging.h"
20#include "minhton/message/types.h"
21
22namespace minhton {
23
28template <typename T> class MinhtonMessage {
29public:
31
35
40
45
49 void setTarget(NodeInfo new_target);
50
51 T getFullMessage() const;
52
53private:
54 friend T;
55
58 void validate() const;
59
60 bool validateHeader(const MinhtonMessageHeader &header) const;
61};
62
63template <typename T> MinhtonMessage<T>::MinhtonMessage() = default;
64
66 return static_cast<const T *>(this)->header_;
67}
68
69template <typename T> NodeInfo MinhtonMessage<T>::getSender() const {
70 return static_cast<const T *>(this)->header_.getSender();
71}
72
73template <typename T> NodeInfo MinhtonMessage<T>::getTarget() const {
74 return static_cast<const T *>(this)->header_.getTarget();
75}
76
77template <typename T> T MinhtonMessage<T>::getFullMessage() const {
78 return *static_cast<const T *>(this);
79}
80
81template <typename T>
82bool MinhtonMessage<T>::validateHeader(const MinhtonMessageHeader &header) const {
83 return header.getSender().getPhysicalNodeInfo().isInitialized() &&
84 (header.getTarget().getPhysicalNodeInfo().isInitialized() ||
85 header.getTarget().getLogicalNodeInfo().isInitialized());
86}
87
88template <typename T> void MinhtonMessage<T>::validate() const {
89 if (!(validateHeader(static_cast<const T *>(this)->header_))) {
90 throw std::invalid_argument("Invalid MINHTON message header");
91 }
92 if (!(static_cast<const T *>(this)->validateImpl())) {
93 throw std::invalid_argument("Invalid MINHTON message payload");
94 }
95}
96
97template <typename T> void MinhtonMessage<T>::setTarget(NodeInfo new_target) {
98 static_cast<T *>(this)->header_.setTarget(new_target);
99 validate();
100}
101
102} // namespace minhton
103
104#endif
Definition message_header.h:24
Definition message.h:28
NodeInfo getTarget() const
Definition message.h:73
NodeInfo getSender() const
Definition message.h:69
void setTarget(NodeInfo new_target)
Definition message.h:97
MinhtonMessageHeader getHeader() const
Definition message.h:65
Definition node_info.h:24
minhton::PhysicalNodeInfo getPhysicalNodeInfo() const
Definition node_info.cpp:32
bool isInitialized() const
Definition physical_node_info.cpp:73
Definition minhton_watchdog_ns3.cpp:24