EMANE  1.2.1
controlmessageserializer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014,2016 - Adjacent Link LLC, Bridgewater, New
3  * Jersey
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of Adjacent Link LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
37 #include <arpa/inet.h>
38 
40  serializations_(msgs.size(),std::string()),
41  vectorIO_(msgs.size() * 2 + 1,iovec()),
42  controlMessageHeaders_(msgs.size(),ControlMessageHeader()),
43  length_(0)
44 {
45  ControlMessages::const_iterator iter = msgs.begin();
46 
47  controlMessageSerializerHeader_.u16Count_ = htons(msgs.size());
48  vectorIO_[0].iov_len = sizeof(ControlMessageSerializerHeader);
49  vectorIO_[0].iov_base = &controlMessageSerializerHeader_;
50  length_ += vectorIO_[0].iov_len;
51 
52  for(int i = 0; iter != msgs.end(); ++iter,++i)
53  {
54  (*iter)->serialize().swap(serializations_[i]);
55  controlMessageHeaders_[i].u16Id_ = htons((*iter)->getId());
56  controlMessageHeaders_[i].u16Length_ = htons(serializations_[i].length());
57 
58  vectorIO_[i*2+1].iov_len = sizeof(ControlMessageHeader);
59  vectorIO_[i*2+1].iov_base = &controlMessageHeaders_[i];
60  length_ += vectorIO_[i*2+1].iov_len;
61 
62  vectorIO_[i*2 + 2].iov_len = serializations_[i].length();
63  vectorIO_[i*2 + 2].iov_base = const_cast<char *>(serializations_[i].c_str());
64  length_ += vectorIO_[i*2+2].iov_len;
65  }
66 }
67 
69 
72 {
73  return vectorIO_;
74 }
75 
77 {
78  return length_;
79 }
80 
82 {
83  ControlMessages controlMessages;
84 
85  if(length >= sizeof(ControlMessageSerializerHeader))
86  {
87  const ControlMessageSerializerHeader * pControlMessageSerializerHeader =
88  reinterpret_cast<const ControlMessageSerializerHeader *>(pData);
89 
90  std::uint16_t u16ControlMessageCount =
91  ntohs(pControlMessageSerializerHeader->u16Count_);
92 
93  length -= sizeof(ControlMessageSerializerHeader);
94 
95  const ControlMessageHeader * pControlMessageHeader =
96  reinterpret_cast<const ControlMessageHeader *>(pControlMessageSerializerHeader->data_);
97 
98  for(int i = 0; i < u16ControlMessageCount; ++i)
99  {
100  if(length >= sizeof(ControlMessageHeader))
101  {
102  length -= sizeof(ControlMessageHeader);
103 
104  std::uint16_t u16ControlMessageId = ntohs(pControlMessageHeader->u16Id_);
105  std::uint16_t u16ControlMessageLength = ntohs(pControlMessageHeader->u16Length_);
106 
107  if(length >= u16ControlMessageLength)
108  {
109  controlMessages.push_back(Controls::SerializedControlMessage::create(u16ControlMessageId,
110  pControlMessageHeader->data_,
111  u16ControlMessageLength));
112 
113  length -= u16ControlMessageLength;
114  }
115  else
116  {
117  throw ControlMessageSerializerException("Control Message header length mismatch");
118  }
119 
120 
121  pControlMessageHeader =
122  reinterpret_cast<const ControlMessageHeader *>(pControlMessageHeader->data_ +
123  u16ControlMessageLength);
124  }
125  else
126  {
127  throw ControlMessageSerializerException("Control Message count mismatch");
128  }
129 
130 
131  }
132  }
133  else
134  {
135  throw ControlMessageSerializerException("Control message serializer header length mismatch");
136  }
137 
138  return controlMessages;
139 }
140 
143 {
144  ControlMessages controlMessages;
145 
146  if(!vectorIO.empty())
147  {
148  if(vectorIO[0].iov_len == sizeof(ControlMessageSerializerHeader))
149  {
150  const ControlMessageSerializerHeader * pControlMessageSerializerHeader =
151  reinterpret_cast<const ControlMessageSerializerHeader *>(vectorIO[0].iov_base);
152 
153  std::uint16_t u16ControlMessageCount =
154  ntohs(pControlMessageSerializerHeader->u16Count_);
155 
156 
157  if(u16ControlMessageCount <= (vectorIO.size() - 1) / 2)
158  {
159  for(int i = 0; i < u16ControlMessageCount; ++i)
160  {
161  if(vectorIO[i*2 +1].iov_len == sizeof(ControlMessageHeader))
162  {
163  const ControlMessageHeader * pControlMessageHeader =
164  reinterpret_cast<const ControlMessageHeader *>(vectorIO[i*2 +1].iov_base);
165 
166  std::uint16_t u16ControlMessageId = ntohs(pControlMessageHeader->u16Id_);
167  std::uint16_t u16ControlMessageLength = ntohs(pControlMessageHeader->u16Length_);
168 
169 
170  if(vectorIO[i*2 + 2].iov_len == u16ControlMessageLength)
171  {
172  controlMessages.push_back(Controls::SerializedControlMessage::create(u16ControlMessageId,
173  vectorIO[i*2 + 2].iov_base,
174  u16ControlMessageLength));
175  }
176  else
177  {
178  throw ControlMessageSerializerException("Control Message data length mismatch");
179  }
180  }
181  else
182  {
183  throw ControlMessageSerializerException("Control Message header length mismatch");
184  }
185  }
186  }
187  else
188  {
189  throw ControlMessageSerializerException("Control Message count mismatch");
190  }
191  }
192  else
193  {
194  throw ControlMessageSerializerException("Control message serializer header length mismatch");
195  }
196  }
197 
198  return controlMessages;
199 }
ControlMessageSerializer(const ControlMessages &msgs)
std::list< const ControlMessage * > ControlMessages
std::vector< iovec > VectorIO
Definition: vectorio.h:43
static ControlMessages create(const void *pData, size_t length)
static SerializedControlMessage * create(ControlMessageId id, const void *pData, size_t length)
const Utils::VectorIO & getVectorIO() const
Exception thrown to indicate a serialization or deserialization error.