EMANE  1.2.1
nemqueuedlayer.h
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 - 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 #ifndef EMANENEMQUEUEDLAYER_HEADER_
36 #define EMANENEMQUEUEDLAYER_HEADER_
37 
38 #include "emane/nemlayer.h"
43 
44 #include <deque>
45 #include <functional>
46 #include <thread>
47 #include <mutex>
48 #include <unordered_map>
49 
50 namespace EMANE
51 {
63  class NEMQueuedLayer : public NEMLayer,
65  {
66  public:
68 
69  void initialize(Registrar & registrar) override;
70 
71  void start() override;
72 
73  void stop() override;
74 
75  void processConfiguration(const ConfigurationUpdate & update) override;
76 
77  void processDownstreamControl(const ControlMessages & msgs) override;
78 
79  void processDownstreamPacket(DownstreamPacket &pkt, const ControlMessages & msgs) override;
80 
81  void processUpstreamPacket(UpstreamPacket &pkt, const ControlMessages & msgs) override;
82 
83  void processUpstreamControl(const ControlMessages & msgs) override;
84 
85  void processEvent(const EventId &eventId, const Serialization &serialization) override;
86 
87  void processTimedEvent(TimerEventId eventId,
88  const TimePoint & expireTime,
89  const TimePoint & scheduleTime,
90  const TimePoint & fireTime,
91  const void * arg) override;
92 
93 
94  template <typename Function>
95  void processTimer(Function fn,
96  const TimePoint & expireTime,
97  const TimePoint & scheduleTime,
98  const TimePoint & fireTime);
99 
100  protected:
101  NEMQueuedLayer(NEMId id, PlatformServiceProvider * pPlatformService);
102 
103  virtual void doProcessConfiguration(const ConfigurationUpdate &) = 0;
104 
105  virtual void doProcessDownstreamControl(const ControlMessages &) = 0;
106 
107  virtual void doProcessDownstreamPacket(DownstreamPacket &, const ControlMessages &) = 0;
108 
109  virtual void doProcessUpstreamPacket(UpstreamPacket &, const ControlMessages &) = 0;
110 
111  virtual void doProcessUpstreamControl(const ControlMessages &) = 0;
112 
113  virtual void doProcessEvent(const EventId &, const Serialization &) = 0;
114 
115  virtual void doProcessTimedEvent(TimerEventId eventId,
116  const TimePoint & expireTime,
117  const TimePoint & scheduleTime,
118  const TimePoint & fireTime,
119  const void * arg) = 0;
120 
121 
122  private:
123  using QCallback = std::function<void()>;
124  using MessageProcessingQueue = std::deque<QCallback>;
125  PlatformServiceProvider * pPlatformService_;
126  std::thread thread_;
127  MessageProcessingQueue queue_;
128  std::mutex mutex_;
129  int iFd_;
130  int iepollFd_;
131  bool bCancel_;
132 
133  using FileDescriptorStore = std::unordered_map<int,
134  std::pair<DescriptorType,
135  Callback>>;
136  FileDescriptorStore fileDescriptorStore_;
137 
138  StatisticNumeric<std::uint64_t> * pProcessedDownstreamPacket_;
139  StatisticNumeric<std::uint64_t> * pProcessedUpstreamPacket_;
140  StatisticNumeric<std::uint64_t> * pProcessedDownstreamControl_;
141  StatisticNumeric<std::uint64_t> * pProcessedUpstreamControl_;
142  StatisticNumeric<std::uint64_t> * pProcessedEvent_;
143  StatisticNumeric<std::uint64_t> * pProcessedTimedEvent_;
144  StatisticNumeric<std::uint64_t> * pProcessedConfiguration_;
145 
146  Utils::RunningAverage<double> avgQueueWait_;
147  Utils::RunningAverage<double> avgQueueDepth_;
148  Utils::RunningAverage<double> avgTimedEventLatency_;
149  Utils::RunningAverage<double> avgTimedEventLatencyRatio_;
150 
151  std::unique_ptr<Utils::StatisticHistogramTable<EventId>> pStatisticHistogramTable_;
152 
154 
155  void processWorkQueue();
156 
157  void handleProcessConfiguration(TimePoint enqueueTime,
158  const ConfigurationUpdate);
159 
160  void handleProcessDownstreamControl(TimePoint enqueueTime,
161  const ControlMessages);
162 
163  void handleProcessDownstreamPacket(TimePoint enqueueTime,
165  const ControlMessages);
166 
167  void handleProcessUpstreamPacket(TimePoint enqueueTime,
168  UpstreamPacket &,
169  const ControlMessages);
170 
171  void handleProcessUpstreamControl(TimePoint enqueueTime,
172  const ControlMessages);
173 
174  void handleProcessEvent(TimePoint enqueueTime,
175  const EventId,
176  const Serialization);
177 
178  void handleProcessTimedEvent(TimePoint enqueueTime,
179  TimerEventId eventId,
180  const TimePoint & expireTime,
181  const TimePoint & scheduleTime,
182  const TimePoint & fireTime,
183  const void * arg);
184 
185  void removeFileDescriptor(int iFd) override;
186 
187  void addFileDescriptor_i(int iFd,
188  DescriptorType type,
189  Callback callback) override;
190 
191  void enqueue_i(QCallback && callback);
192 
193  void processTimer_i(TimerServiceProvider::TimerCallback callback,
194  const TimePoint & expireTime,
195  const TimePoint & scheduleTime,
196  const TimePoint & fireTime);
197 
198  void updateTimerStats(TimePoint enqueueTime,
199  const TimePoint & expireTime,
200  const TimePoint & scheduleTime,
201  const TimePoint & fireTime);
202  };
203 }
204 
205 #include "nemqueuedlayer.inl"
206 
207 #endif //EMANENEMQUEUEDLAYER_HEADER_
std::string Serialization
Definition: serializable.h:42
A Packet class that allows upstream processing to strip layer headers as the packet travels up the st...
The Registrar interface provides access to all of the emulator registrars.
Definition: registrar.h:50
void processDownstreamControl(const ControlMessages &msgs) override
virtual void doProcessUpstreamPacket(UpstreamPacket &, const ControlMessages &)=0
NEMQueuedLayer(NEMId id, PlatformServiceProvider *pPlatformService)
std::list< const ControlMessage * > ControlMessages
virtual void doProcessEvent(const EventId &, const Serialization &)=0
Base class for NEMLayer containers. Builders construct NEMLayer objects to contain derived instances ...
Definition: nemlayer.h:57
void processUpstreamControl(const ControlMessages &msgs) override
std::uint16_t EventId
Definition: types.h:53
The PlatformServiceProvider interface provides access to emulator services.
void start() override
virtual void doProcessUpstreamControl(const ControlMessages &)=0
virtual void doProcessDownstreamControl(const ControlMessages &)=0
void stop() override
void processConfiguration(const ConfigurationUpdate &update) override
Specialized packet the allows downstream processing to add layer specific headers as the packet trave...
virtual void doProcessConfiguration(const ConfigurationUpdate &)=0
std::function< void(const TimePoint &, const TimePoint &, const TimePoint &)> TimerCallback
std::uint16_t NEMId
Definition: types.h:52
File Descriptor service interface allows for adding arbitrary file descriptors for read or write proc...
void processTimedEvent(TimerEventId eventId, const TimePoint &expireTime, const TimePoint &scheduleTime, const TimePoint &fireTime, const void *arg) override
void initialize(Registrar &registrar) override
std::vector< ConfigurationNameAnyValues > ConfigurationUpdate
std::size_t TimerEventId
Definition: types.h:54
Clock::time_point TimePoint
Definition: types.h:50
virtual void doProcessDownstreamPacket(DownstreamPacket &, const ControlMessages &)=0
void processEvent(const EventId &eventId, const Serialization &serialization) override
virtual void doProcessTimedEvent(TimerEventId eventId, const TimePoint &expireTime, const TimePoint &scheduleTime, const TimePoint &fireTime, const void *arg)=0
void processDownstreamPacket(DownstreamPacket &pkt, const ControlMessages &msgs) override
Definition: agent.h:43
void processUpstreamPacket(UpstreamPacket &pkt, const ControlMessages &msgs) override
void processTimer(Function fn, const TimePoint &expireTime, const TimePoint &scheduleTime, const TimePoint &fireTime)
A layer stack with a porcessing queue between each layer to decouple to intra queue processing...