EMANE  1.2.1
eelloaderpathloss.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2010 - DRS CenGen, LLC, Columbia, Maryland
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 DRS CenGen, 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  */
34 
35 #include "eelloaderpathloss.h"
39 
40 #include <vector>
41 #include <sstream>
42 #include <cstring>
43 
45 {}
46 
48 {}
49 
51  const ModuleId & moduleId,
52  const EventType & ,
53  const InputArguments & args)
54 {
55  if(moduleType == "nem")
56  {
57  if(args.size() < 1)
58  {
59  throw FormatException("LoaderPathloss expects at least 1 argument");
60  }
61  else
62  {
63  InputArguments::const_iterator iterArg = args.begin();
64 
65  // parse the individual pathloss entry params
66  for(; iterArg != args.end(); ++iterArg)
67  {
68  InputArguments params;
69 
70  size_t posStart = 0;
71  size_t posEnd = 0;
72 
73  // build a parameters vector holding all the
74  // comma separeted elements
75  // <dstModuleID>,<dB>[,dBrev] ...
76  while(posEnd != std::string::npos)
77  {
78  posEnd = iterArg->find_first_of(",",posStart);
79 
80  params.push_back( iterArg->substr(posStart, posEnd - posStart));
81 
82  posStart = posEnd + 1;
83  }
84 
85  // you must at least specify a destination and pathloss
86  if(params.size() < 2)
87  {
88  throw FormatException("LoaderPathloss expects at least 1 parameter");
89  }
90  else
91  {
92  NEMId srcNEM{moduleId};
93  NEMId dstNEM{};
94  float fReversePathlossdb{};
95  float fForwardPathlossdb{};
96 
97  // convert the strings into the appropriate types
98  for(size_t i = 0; i < params.size(); ++i)
99  {
100  try
101  {
102  switch(i)
103  {
104  case 0:
105  // <dstModuleID> which must be nem:UINT16
106  {
107  size_t pos = params[i].find(':');
108 
109  if(pos != std::string::npos)
110  {
111  if(params[i].substr(0,pos) == "nem")
112  {
113  dstNEM = Utils::ParameterConvert(params[i].substr(pos + 1)).toUINT16();
114  }
115  else
116  {
117  std::stringstream sstream;
118  throw FormatException("LoaderPathloss only supports 'nem' module type");
119  }
120  }
121  }
122  break;
123 
124  case 1:
125  fForwardPathlossdb = Utils::ParameterConvert(params[i]).toFloat();
126 
127  // store the value as the reverse pathloss as well
128  // if reverse pathloss is given it will ovewrite this value
129  fReversePathlossdb = fForwardPathlossdb;
130  break;
131 
132  case 2:
133  fReversePathlossdb = Utils::ParameterConvert(params[i]).toFloat();
134  break;
135 
136  default:
137  throw FormatException("LoaderPathloss loader too many parameters");
138  break;
139  }
140  }
142  {
143  std::stringstream sstream;
144  sstream<<"LoaderPathloss loader: Parameter conversion error. "<<exp.what()<<std::ends;
145  throw FormatException(sstream.str());
146  }
147  }
148 
149  // load the full pathloss cache
150  loadPathlossCache(dstNEM,srcNEM,fForwardPathlossdb,fReversePathlossdb,pathlossEntryCache_);
151 
152  // load the delta update cache
153  loadPathlossCache(dstNEM,srcNEM,fForwardPathlossdb,fReversePathlossdb,pathlossDeltaEntryCache_);
154  }
155  }
156  }
157  }
158 }
159 
161 {
162  EventInfoList eventInfoList;
163 
164  PathlossEntryCache * pCache = 0;
165 
166  if(mode == DELTA)
167  {
168  // in DELTA mode events *only* contain entries that have
169  // changes since the last update
170  pCache = &pathlossDeltaEntryCache_;
171  }
172  else
173  {
174  // in FULL mode events contain all the entries regardless
175  // of whether they contain updated data since the last
176  // getEvents() call
177  pCache = &pathlossEntryCache_;
178  }
179 
180  Events::Pathlosses pathlosses;
181 
182  PathlossEntryCache::iterator iter = pCache->begin();
183 
184  for(;iter != pCache->end(); ++iter)
185  {
186  PathlossEntryMap::iterator iterEntry = iter->second.begin();
187 
188  for(; iterEntry != iter->second.end(); ++iterEntry)
189  {
190  pathlosses.push_back(iterEntry->second);
191  }
192 
193  if(!pathlosses.empty())
194  {
195  eventInfoList.push_back({iter->first,
197  Events::PathlossEvent(pathlosses).serialize()});
198  }
199 
200  pathlosses.clear();
201  }
202 
203  pathlossDeltaEntryCache_.clear();
204 
205  return eventInfoList;
206 }
207 
208 void EMANE::Generators::EEL::LoaderPathloss::loadPathlossCache(NEMId dstNEM,
209  NEMId srcNEM,
210  float fForwardPathloss,
211  float fReversePathloss,
212  PathlossEntryCache & cache)
213 {
214  // swap entry info to create a reverse pathloss entry
215  Events::Pathloss pathlossForward{srcNEM,fForwardPathloss,fReversePathloss};
216  Events::Pathloss pathlossReverse{dstNEM,fReversePathloss,fForwardPathloss};
217 
218  PathlossEntryCache::iterator iter = cache.end();
219 
220  if((iter = cache.find(dstNEM)) != cache.end())
221  {
222  std::pair<PathlossEntryMap::iterator,bool> ret =
223  iter->second.insert(std::make_pair(pathlossForward.getNEMId(),pathlossForward));
224 
225  if(!ret.second)
226  {
227  ret.first->second = pathlossForward;
228  }
229  }
230  else
231  {
232  PathlossEntryMap entryMap;
233  entryMap.insert(std::make_pair(pathlossForward.getNEMId(),pathlossForward));
234  cache.insert(std::make_pair(dstNEM,entryMap));
235  }
236 
237 
238  if((iter = cache.find(srcNEM)) != cache.end())
239  {
240  std::pair<PathlossEntryMap::iterator,bool> ret =
241  iter->second.insert(std::make_pair(pathlossReverse.getNEMId(),pathlossReverse));
242 
243  if(!ret.second)
244  {
245  ret.first->second = pathlossReverse;
246  }
247  }
248  else
249  {
250  PathlossEntryMap entryMap;
251  entryMap.insert(std::make_pair(pathlossReverse.getNEMId(),pathlossReverse));
252  cache.insert(std::make_pair(srcNEM,entryMap));
253  }
254 }
255 
std::vector< std::string > InputArguments
#define DECLARE_EEL_LOADER_PLUGIN(X)
Definition: loaderplugin.h:91
Parameter conversion exception class.
std::uint16_t toUINT16(std::uint16_t u16Min=std::numeric_limits< std::uint16_t >::min(), std::uint16_t u16Max=std::numeric_limits< std::uint16_t >::max()) const
float toFloat(float fMin=std::numeric_limits< float >::lowest(), float fMax=std::numeric_limits< float >::max()) const
void load(const ModuleType &modelType, const ModuleId &moduleId, const EventType &eventType, const InputArguments &args) override
A pathloss entry holds the source NEM Id and the forward and reverse pathloss to apply to received tr...
Definition: pathloss.h:54
std::list< Pathloss > Pathlosses
Definition: pathloss.h:95
const char * what() const
Definition: exception.h:62
std::uint16_t NEMId
Definition: types.h:52
EventInfoList getEvents(EventPublishMode mode) override
std::list< EventInfo > EventInfoList
Parameter conversion class with range checks.