A Discrete-Event Network Simulator
API
qkd-kms-queue-logic.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2022 DOTFEESA www.tk.etf.unsa.ba
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Miralem Mehic <miralem.mehic@ieee.org>
19  * Emir Dervisevic <emir.dervisevic@etf.unsa.ba>
20  */
21 
22 #include "ns3/log.h"
23 #include "ns3/pointer.h"
24 #include "ns3/object-factory.h"
25 #include "ns3/drop-tail-queue.h"
26 #include "ns3/qkd-kms-queue-logic.h"
27 
28 namespace ns3 {
29 
30 NS_LOG_COMPONENT_DEFINE ("QKDKMSQueueLogic");
31 
32 NS_OBJECT_ENSURE_REGISTERED (QKDKMSQueueLogic);
33 
35 {
36  static TypeId tid = TypeId ("ns3::QKDKMSQueueLogic")
37  .SetParent<Object> ()
38  .AddConstructor<QKDKMSQueueLogic> ()
39  .AddAttribute ("MaxSize", "The maximum number of packets accepted by this queue disc.",
40  UintegerValue (10),
42  MakeUintegerChecker<uint32_t> ())
43  .AddAttribute ("NumberOfQueues", "The number of priority queues used.",
44  UintegerValue (3),
46  MakeUintegerChecker<uint32_t> ())
47  .AddTraceSource ("EnqueueKMS", "Enqueue a packet in the queue disc",
49  "ns3::QueueItem::TracedCallback")
50  .AddTraceSource ("DequeueKMS", "Dequeue a packet from the queue disc",
52  "ns3::QueueItem::TracedCallback")
53  .AddTraceSource ("DropKMS", "Drop a packet from the queue disc",
55  "ns3::QueueItem::TracedCallback")
56  .AddTraceSource ("PacketsInQueue", "Number of packets currently stored in the queue disc",
58  "ns3::TracedValueCallback::Uint32")
59  ;
60  return tid;
61 }
62 
64 {
65  NS_LOG_FUNCTION (this);
66 
67  for (uint32_t i = 0; i < m_numberOfQueues; i++){
68  std::vector<QKDKMSQueueEntry> nv;
69  m_queues.push_back(nv);
70  }
71 }
72 
74 {
75  NS_LOG_FUNCTION (this);
76 }
77 
78 bool
80 {
82 
83  if (m_nPackets >= m_maxSize)
84  {
86  NS_LOG_LOGIC ("Queue disc limit exceeded -- dropping packet");
87  return false;
88  }
89 
90  std::string payload = item.httpMessage.GetMessageBodyString();
91  nlohmann::json jOpenConnectRequest;
92 
93  if(payload.length() > 0){
94  try{
95  jOpenConnectRequest = nlohmann::json::parse(payload);
96  }catch(...) {
97  NS_FATAL_ERROR( this << "JSON parse error!" << payload );
98  }
99  }
100 
101  uint32_t priority = 0;
102  if (jOpenConnectRequest.contains("QoS")) {
103  if (jOpenConnectRequest["QoS"].contains("priority")){
104  priority = jOpenConnectRequest["QoS"]["priority"];
105  }
106  }
107 
108  if(priority > m_numberOfQueues || priority < 0){ //Primitive validation of priority value
109  priority = 0;
110  }
111 
112  m_queues[priority].push_back(item);
114  m_nPackets++;
115 
116  for (uint32_t i = 0; i < m_numberOfQueues; i++){
117  NS_LOG_LOGIC ("Number of packets in queue " << i << ": " << m_queues[i].size() );
118  }
119 
120  return true;
121 }
122 
125 {
126  NS_LOG_FUNCTION (this << m_nPackets << m_maxSize);
127 
128  QKDKMSQueueEntry item;
129  for (uint32_t i = 0; i < m_numberOfQueues; i++)
130  {
131  if (m_queues[i].size() > 0)
132  {
133  item = m_queues[i].back();
134  m_queues[i].pop_back();
136  m_nPackets--;
137  NS_LOG_LOGIC ("Popped from queue " << i);
138  NS_LOG_LOGIC ("Number of packets in queue " << i << ": " << m_queues[i].size());
139  return item;
140  }
141  }
142 
143  NS_LOG_LOGIC ("Queue empty");
144  return item;
145 }
146 
147 } // namespace ns3
std::string GetMessageBodyString()
Definition: http.h:714
A base class which provides memory management and object aggregation.
Definition: object.h:88
TracedCallback< const HTTPMessage > m_traceDequeue
A trace for the dequeue packet.
TracedValue< uint32_t > m_nPackets
The number of packets in the queue.
TracedCallback< const HTTPMessage > m_traceDroped
A trace for the dropped packet.
bool Enqueue(QKDKMSQueueEntry item)
Add the element to the queue.
uint32_t m_maxSize
The queue size.
std::vector< std::vector< QKDKMSQueueEntry > > m_queues
A list of queues.
QKDKMSQueueLogic::QKDKMSQueueEntry Dequeue(void)
Pop the element from the queue.
uint32_t m_numberOfQueues
The number of queues.
TracedCallback< const HTTPMessage > m_traceEnqueue
A trace for the enqueued packet.
static TypeId GetTypeId(void)
Get the type ID.
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
Hold an unsigned integer type.
Definition: uinteger.h:44
Ptr< const AttributeAccessor > MakeUintegerAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: uinteger.h:45
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Ptr< const TraceSourceAccessor > MakeTraceSourceAccessor(T a)
Create a TraceSourceAccessor which will control access to the underlying trace source.
basic_json<> json
default JSON class
Definition: json.h:3000
Every class exported by the ns3 library is enclosed in the ns3 namespace.
The KMS Queue elements.
HTTPMessage httpMessage