EMANE  1.2.1
nemotaadapter.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2014,2016 - Adjacent Link LLC, Bridgewater,
3  * New Jersey
4  * Copyright (c) 2008-2009 - DRS CenGen, LLC, Columbia, Maryland
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of DRS CenGen, LLC nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "nemotaadapter.h"
36 #include "otamanager.h"
37 #include "logservice.h"
38 
41 
43  id_{id},
44  bCancel_{}
45 {}
46 
48 {
49  bCancel_ = false;
50 
52 
53  thread_ = std::thread{&NEMOTAAdapter::processPacketQueue,this};
54 
55  if(ThreadUtils::elevate(thread_))
56  {
58  ERROR_LEVEL,"NEMOTAAdapter::NEMOTAAdapter: Unable to set Priority");
59  }
60 }
61 
63 {
64  if(!bCancel_ && thread_.joinable())
65  {
66  mutex_.lock();
67  bCancel_ = true;
68  cond_.notify_one();
69  mutex_.unlock();
70  thread_.join();
71  }
72 }
73 
75 {
76  try
77  {
79  }
80  catch(...)
81  {
82  }
83 
84  if(thread_.joinable())
85  {
86  mutex_.lock();
87  bCancel_ = true;
88  cond_.notify_one();
89  mutex_.unlock();
90  thread_.join();
91  }
92 }
93 
95 {
96  sendUpstreamPacket(pkt, msgs);
97 }
98 
100  const ControlMessages & msgs)
101 {
102  std::lock_guard<std::mutex> m(mutex_);
103 
104  queue_.emplace_back(pkt, msgs);
105 
106  cond_.notify_one();
107 
108 }
109 
110 void EMANE::NEMOTAAdapter::processPacketQueue()
111 {
112  while(1)
113  {
114  std::unique_lock<std::mutex> lock(mutex_);
115 
116  while(queue_.empty() && !bCancel_)
117  {
118  cond_.wait(lock);
119  }
120 
121  if(bCancel_)
122  {
123  break;
124  }
125 
126  DownstreamPacketQueue queue{};
127 
128  queue.swap(queue_);
129 
130  lock.unlock();
131 
132  for(auto & entry : queue)
133  {
134  try
135  { // id, pkt, ctrl
136  OTAManagerSingleton::instance()->sendOTAPacket(id_, entry.first, entry.second);
137  }
138  catch(std::exception & exp)
139  {
140  // cannot really do too much at this point, so we'll log it
142  ERROR_LEVEL,
143  "NEMOTAAdapter::processPacketQueue Excepetion caught: %s",
144  exp.what());
145  }
146  catch(...)
147  {
148  // cannot really do too much at this point, so we'll log it
150  ERROR_LEVEL,
151  "NEMOTAAdapter::processPacketQueue Excepetion caught");
152  }
153  }
154 
155  queue.clear();
156  }
157 }
A Packet class that allows upstream processing to strip layer headers as the packet travels up the st...
void registerOTAUser(NEMId id, OTAUser *pOTAUser) override
Definition: otamanager.cc:409
void sendOTAPacket(NEMId id, const DownstreamPacket &pkt, const ControlMessages &msgs) const override
Definition: otamanager.cc:136
void processOTAPacket(UpstreamPacket &pkt, const ControlMessages &msgs)
std::list< const ControlMessage * > ControlMessages
Specialized packet the allows downstream processing to add layer specific headers as the packet trave...
std::uint16_t NEMId
Definition: types.h:52
void unregisterOTAUser(NEMId id) override
Definition: otamanager.cc:421
void processDownstreamPacket(DownstreamPacket &pkt, const ControlMessages &msgs)
void sendUpstreamPacket(UpstreamPacket &pkt, const ControlMessages &msgs=empty)
#define LOGGER_STANDARD_LOGGING(logger, level, fmt, args...)
static OTAManager * instance()
Definition: singleton.h:56
int elevate(std::thread &thread)
Definition: threadutils.h:44