EMANE  1.2.1
eelloaderfadingselection.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2017 - 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 
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("LoaderFadingSelection expects at least 1 argument");
60  }
61  else
62  {
63  InputArguments::const_iterator iterArg = args.begin();
64 
65  // parse the individual fading selection 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  // <txModuleID>,none|nakagami ...
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 specify a tx nem and fading model
86  if(params.size() != 2)
87  {
88  throw FormatException("LoaderFadingSelection expects 2 parameters");
89  }
90  else
91  {
92  NEMId targetNEM{moduleId};
93  NEMId txNEM{};
94  Events::FadingModel model{};
95 
96  // convert the strings into the appropriate types
97  for(size_t i = 0; i < params.size(); ++i)
98  {
99  try
100  {
101  switch(i)
102  {
103  case 0:
104  // <txModuleID> which must be nem:UINT16
105  {
106  size_t pos = params[i].find(':');
107 
108  if(pos != std::string::npos)
109  {
110  if(params[i].substr(0,pos) == "nem")
111  {
112  txNEM = Utils::ParameterConvert(params[i].substr(pos + 1)).toUINT16();
113  }
114  else
115  {
116  std::stringstream sstream;
117  throw FormatException("LoaderFadingSelection only supports 'nem' module type");
118  }
119  }
120  }
121  break;
122 
123  case 1:
124  {
125  std::string sModel{params[i]};
126 
127  if(sModel == "none")
128  {
130  }
131  else if(sModel == "nakagami")
132  {
134  }
135  else
136  {
137  throw makeException<FormatException>("LoaderFadingSelection loader unknown fading model: %s",
138  sModel.c_str());
139  }
140  }
141  break;
142 
143  default:
144  throw FormatException("LoaderFadingSelection loader too many parameters");
145  break;
146  }
147  }
149  {
150  std::stringstream sstream;
151  sstream<<"LoaderFadingSelection loader: Parameter conversion error. "<<exp.what()<<std::ends;
152  throw FormatException(sstream.str());
153  }
154  }
155 
156  // load the full cache
157  loadFadingSelectionCache(targetNEM,txNEM,model,fadingSelectionEntryCache_);
158 
159  // load the delta cache
160  loadFadingSelectionCache(targetNEM,txNEM,model,fadingSelectionDeltaEntryCache_);
161  }
162  }
163  }
164  }
165 }
166 
168 {
169  EventInfoList eventInfoList;
170 
171  FadingSelectionEntryCache * pCache = 0;
172 
173  if(mode == DELTA)
174  {
175  // in DELTA mode events *only* contain entries that have
176  // changes since the last update
177  pCache = &fadingSelectionDeltaEntryCache_;
178  }
179  else
180  {
181  // in FULL mode events contain all the entries regardless
182  // of whether they contain updated data since the last
183  // getEvents() call
184  pCache = &fadingSelectionEntryCache_;
185  }
186 
187  Events::FadingSelections fadingSelections;
188 
189  FadingSelectionEntryCache::iterator iter = pCache->begin();
190 
191  for(;iter != pCache->end(); ++iter)
192  {
193  FadingSelectionEntryMap::iterator iterEntry = iter->second.begin();
194 
195  for(; iterEntry != iter->second.end(); ++iterEntry)
196  {
197  fadingSelections.push_back(iterEntry->second);
198  }
199 
200  if(!fadingSelections.empty())
201  {
202  eventInfoList.push_back({iter->first,
204  Events::FadingSelectionEvent(fadingSelections).serialize()});
205  }
206 
207  fadingSelections.clear();
208  }
209 
210  fadingSelectionDeltaEntryCache_.clear();
211 
212  return eventInfoList;
213 }
214 
215 void EMANE::Generators::EEL::LoaderFadingSelection::loadFadingSelectionCache(NEMId targetNEM,
216  NEMId txNEM,
217  Events::FadingModel model,
218  FadingSelectionEntryCache & cache)
219 {
220  Events::FadingSelection fadingSelection{txNEM,model};
221 
222  FadingSelectionEntryCache::iterator iter = cache.end();
223 
224  if((iter = cache.find(targetNEM)) != cache.end())
225  {
226  std::pair<FadingSelectionEntryMap::iterator,bool> ret =
227  iter->second.insert(std::make_pair(txNEM,fadingSelection));
228 
229  if(!ret.second)
230  {
231  ret.first->second = fadingSelection;
232  }
233  }
234  else
235  {
236  FadingSelectionEntryMap entryMap;
237  entryMap.insert(std::make_pair(txNEM,fadingSelection));
238  cache.insert(std::make_pair(targetNEM,entryMap));
239  }
240 }
241 
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
const char * what() const
Definition: exception.h:62
std::uint16_t NEMId
Definition: types.h:52
EventInfoList getEvents(EventPublishMode mode) override
std::list< FadingSelection > FadingSelections
std::list< EventInfo > EventInfoList
Parameter conversion class with range checks.
void load(const ModuleType &modelType, const ModuleId &moduleId, const EventType &eventType, const InputArguments &args) override