EMANE  1.2.1
r2riqueuemetriccontrolmessage.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014,2016 - Adjacent Link LLC, Bridgewater,
3  * New 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 
35 #include "radiotorouter.pb.h"
36 
37 class EMANE::Controls::R2RIQueueMetricControlMessage::Implementation
38 {
39 public:
40  Implementation(){}
41 
42  Implementation(const R2RIQueueMetrics & queueMetrics):
43  queueMetrics_{queueMetrics}{}
44 
45  const R2RIQueueMetrics & getQueueMetrics() const
46  {
47  return queueMetrics_;
48  }
49 
50 private:
51  const R2RIQueueMetrics queueMetrics_;
52 };
53 
54 EMANE::Controls::R2RIQueueMetricControlMessage::
55 R2RIQueueMetricControlMessage(const R2RIQueueMetricControlMessage & msg):
56  ControlMessage{IDENTIFIER},
57  pImpl_{new Implementation{*msg.pImpl_}}
58 {}
59 
61 
62 EMANE::Controls::R2RIQueueMetricControlMessage::R2RIQueueMetricControlMessage(const R2RIQueueMetrics & queueMetrics):
63  ControlMessage{IDENTIFIER},
64  pImpl_{new Implementation{queueMetrics}}{}
65 
68 {
69  return pImpl_->getQueueMetrics();
70 }
71 
74 {
75  return new R2RIQueueMetricControlMessage{queueMetrics};
76 }
77 
79 {
80  Serialization serialization;
81 
82  EMANEMessage::RadioToRouterQueueMetrics msg;
83 
84  const R2RIQueueMetrics & metrics = pImpl_->getQueueMetrics();
85 
86  R2RIQueueMetrics::const_iterator iter = metrics.begin();
87 
88  for(;iter != metrics.end(); ++iter)
89  {
90  auto pQueueMetric = msg.add_metrics();
91 
92  pQueueMetric->set_queueid(iter->getQueueId());
93  pQueueMetric->set_maxsize(iter->getMaxSize());
94  pQueueMetric->set_currentdepth(iter->getCurrentDepth());
95  pQueueMetric->set_numdiscards(iter->getNumDiscards());
96  pQueueMetric->set_avgdelay(std::chrono::duration_cast<DoubleSeconds>(iter->getAvgDelay()).count());
97  }
98 
99  if(!msg.SerializeToString(&serialization))
100  {
101  throw SerializationException("unable to serialize R2RIQueueMetricControlMessage");
102  }
103 
104  return serialization;
105 }
106 
109 {
110  EMANEMessage::RadioToRouterQueueMetrics msg;
111 
112  if(!msg.ParseFromString(serialization))
113  {
114  throw SerializationException("unable to deserialize : R2RIQueueMetricControlMessage");
115  }
116 
117  R2RIQueueMetrics metrics;
118 
119  for(const auto & metric : msg.metrics())
120  {
121  metrics.push_back(R2RIQueueMetric{metric.queueid(),
122  metric.maxsize(),
123  metric.currentdepth(),
124  metric.numdiscards(),
125  std::chrono::duration_cast<Microseconds>(DoubleSeconds{metric.avgdelay()})});
126 
127  }
128 
129  return new R2RIQueueMetricControlMessage{metrics};
130 }
131 
132 
135 {
136  return new R2RIQueueMetricControlMessage{*this};
137 }
std::string Serialization
Definition: serializable.h:42
R2RI Queue Metric Control Message is sent to an NEM&#39;s transport layer to convey information about MAC...
SerializationException is thrown when an exception occurs during serialization or deserialization of ...
R2RIQueueMetricControlMessage * clone() const override
std::chrono::microseconds Microseconds
Definition: types.h:45
std::chrono::duration< double > DoubleSeconds
Definition: types.h:47
ControlMessage interface is the base for all control messages.
std::list< R2RIQueueMetric > R2RIQueueMetrics
R2RI queue metrics are used in conjunction with the R2RIQueueMetricControlMessage to inform an NEM&#39;s ...
ControlMessage(ControlMessageId id)
static R2RIQueueMetricControlMessage * create(const Serialization &serialization)