EMANE  1.2.1
basemodelmessage.inl
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2016 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include "tdmabasemodelmessage.pb.h"
34 
35 inline
37  u64AbsoluteSlotIndex_{},
38  u64DataRatebps_{}{}
39 
40 inline
42  std::uint64_t u64DataRatebps,
43  MessageComponents && messages):
44  u64AbsoluteSlotIndex_{u64AbsoluteSlotIndex},
45  u64DataRatebps_{u64DataRatebps},
46  messages_{std::move(messages)}{}
47 
48 inline
50 {
51  EMANEMessage::TDMABaseModelMessage message;
52 
53  if(!message.ParseFromArray(p, len))
54  {
55  throw SerializationException("unable to deserialize TDMABaseModelMessage");
56  }
57 
58  u64AbsoluteSlotIndex_ = message.absslotindex();
59  u64DataRatebps_ = message.dataratebps();
60 
61  for(const auto & msg : message.messages())
62  {
64 
65  switch(msg.type())
66  {
67  case EMANEMessage::TDMABaseModelMessage::Message::DATA:
69  break;
70  case EMANEMessage::TDMABaseModelMessage::Message::CONTROL:
72  break;
73  default:
74  throw SerializationException("TDMABaseModelMessage unkown type");
75  }
76 
77  if(msg.has_fragment())
78  {
79  const auto & fragment = msg.fragment();
80 
81  messages_.push_back({type,
82  static_cast<NEMId>(msg.destination()),
83  static_cast<Priority>(msg.priority()),
84  {Utils::make_iovec(const_cast<char *>(msg.data().c_str()),
85  msg.data().size())},
86  fragment.index(),
87  fragment.offset(),
88  fragment.sequence(),
89  fragment.more()});
90  }
91  else
92  {
93  messages_.push_back({type,
94  static_cast<NEMId>(msg.destination()),
95  static_cast<Priority>(msg.priority()),
96  {Utils::make_iovec(const_cast<char *>(msg.data().c_str()),
97  msg.data().size())}});
98  }
99  }
100 }
101 
102 inline
105 {
106  return messages_;
107 }
108 
109 inline
111 {
112  return u64AbsoluteSlotIndex_;
113 }
114 
115 inline
117 {
118  return u64DataRatebps_;
119 }
120 
121 inline
123 {
124  Serialization serialization{};
125 
126  EMANEMessage::TDMABaseModelMessage baseModelMessage{};
127 
128  baseModelMessage.set_absslotindex(u64AbsoluteSlotIndex_);
129  baseModelMessage.set_dataratebps(u64DataRatebps_);
130 
131  for(const auto & message : messages_)
132  {
133  auto pMessage = baseModelMessage.add_messages();
134 
135  pMessage->set_destination(message.getDestination());
136  pMessage->set_priority(message.getPriority());
137 
138  switch(message.getType())
139  {
141  pMessage->set_type(EMANEMessage::TDMABaseModelMessage::Message::DATA);
142  break;
144  pMessage->set_type(EMANEMessage::TDMABaseModelMessage::Message::CONTROL);
145  break;
146  default:
147  throw SerializationException("TDMABaseModelMessage unkown type");
148  }
149 
150  auto const & data = message.getData();
151 
152  pMessage->set_data(std::string(reinterpret_cast<const char *>(&data[0]),data.size()));
153 
154  if(message.isFragment())
155  {
156  auto pFragement = pMessage->mutable_fragment();
157 
158  pFragement->set_index(message.getFragmentIndex());
159  pFragement->set_offset(message.getFragmentOffset());
160  pFragement->set_more(message.isMoreFragments());
161  pFragement->set_sequence(message.getFragmentSequence());
162  }
163  }
164 
165  if(!baseModelMessage.SerializeToString(&serialization))
166  {
167  throw SerializationException("unable to serialize TDMABaseModelMessage");
168  }
169 
170 
171  return serialization;
172 }
std::string Serialization
Definition: serializable.h:42
SerializationException is thrown when an exception occurs during serialization or deserialization of ...
iovec make_iovec(void *base, std::size_t len)
Definition: vectorio.h:46
std::list< MessageComponent > MessageComponents
const MessageComponents & getMessages() const
std::uint16_t NEMId
Definition: types.h:52
Serialization serialize() const override