A Discrete-Event Network Simulator
API
qkd-graph.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 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  */
20 
21 #include "qkd-graph.h"
22 
23 namespace ns3 {
24 
25 NS_LOG_COMPONENT_DEFINE ("QKDGraph");
26 
28 
30 {
31  static TypeId tid = TypeId ("ns3::QKDGraph")
32  .SetParent<Object> ()
33  ;
34  return tid;
35 }
36 
38 
40  Ptr<QKDControl> control,
41  Ptr<Node> src,
42  Ptr<Node> dst,
43  uint32_t bufferPosition,
44  std::string graphTitle,
45  std::string graphType
46 )
47 {
48  NS_LOG_FUNCTION (this << src->GetId() << dst->GetId() << bufferPosition << graphTitle);
49 
50  Ptr<Node> node = src;
51  m_buffer = control->GetBufferByDestinationNode (dst);
52  m_src = src;
53  m_dst = dst;
54 
55  m_keymCurrent = m_buffer->GetKeyCountBit();
56  m_keymMin = m_buffer->GetMinKeySizeBit();
57  m_keymMax = m_buffer->GetMaxKeySizeBit();
58  m_keymThreshold = m_buffer->GetMthr();
59  m_tempMax = m_keymMax * 1.1; //just to plot arrows on the graph
60  m_graphStatusEntry = 0;
61 
62  m_plotFileName = (graphTitle.empty()) ? "QKD_key_material" : graphTitle;
63  m_plotFileType = (graphType.empty()) ? "png" : graphType;;
64 
65  if(graphTitle.empty()){
66  std::ostringstream temp1;
67  temp1 << m_src->GetId() << m_dst->GetId();
68  m_plotFileName = m_plotFileName + "_" + temp1.str();
69  }
70 
72 
73  m_dataset.SetTitle ("Amount of key material");
75  m_dataset.SetExtra(" ls 1");
77 
78  m_datasetThreshold.SetTitle ("Threshold");
80  m_datasetThreshold.SetExtra(" ls 4");
82 
83  m_datasetMinimum.SetTitle ("Minimum");
85  m_datasetMinimum.SetExtra(" ls 5");
87 
88  m_datasetMaximum.SetTitle ("Maximum");
90  m_datasetMaximum.SetExtra(" ls 6");
92 
94  m_datasetWorkingState_0.SetTitle ("Status: READY");
95  m_datasetWorkingState_0.SetExtra (" y1=0 lc rgb '#b0fbb4'");
96 
98  m_datasetWorkingState_1.SetTitle ("Status: WARNING");
99  m_datasetWorkingState_1.SetExtra (" y1=0 lc rgb '#f7ea50'");
100 
102  m_datasetWorkingState_2.SetTitle ("Status: CHARGING");
103  m_datasetWorkingState_2.SetExtra (" y1=0 lc rgb '#fbc4ca'");
104 
106  m_datasetWorkingState_3.SetTitle ("Status: EMPTY");
107  m_datasetWorkingState_3.SetExtra (" y1=0 lc rgb '#e6e4e4'");
108 
109  m_buffer->CheckState();
110  ProcessMStatusHelpFunction(0.0, m_buffer->FetchState());
111 
112  std::string outputTerminalCommand;
113 
114  if (m_plotFileType=="png") {
115  outputTerminalCommand = "pngcair";
116  } else if (m_plotFileType=="tex") {
117  outputTerminalCommand = "epslatex";
118  } else {
119  outputTerminalCommand = m_plotFileType;
120  }
121 
122  std::ostringstream yrange;
123  yrange << "set yrange[1:" << m_tempMax << "];";
124 
125  m_gnuplot.AppendExtra ("set border linewidth 2");
126 
127  if (m_plotFileType=="tex") {
128  m_gnuplot.AppendExtra ("set terminal " + outputTerminalCommand);
129  } else {
130  m_gnuplot.AppendExtra ("set terminal " + outputTerminalCommand + " size 1524,768 enhanced font 'Helvetica,18'");
131  }
132 
133  m_gnuplot.AppendExtra (yrange.str());
134 
135  m_gnuplot.AppendExtra ("set style line 1 linecolor rgb 'red' linetype 1 linewidth 1");
136  m_gnuplot.AppendExtra ("set style line 3 linecolor rgb 'black' linetype 1 linewidth 1");
137 
138  m_gnuplot.AppendExtra ("set style line 4 linecolor rgb \"#8A2BE2\" linetype 1 linewidth 1");
139  m_gnuplot.AppendExtra ("set style line 5 linecolor rgb 'blue' linetype 8 linewidth 1");
140  m_gnuplot.AppendExtra ("set style line 6 linecolor rgb 'blue' linetype 10 linewidth 1");
141  m_gnuplot.AppendExtra ("set style line 7 linecolor rgb 'gray' linetype 0 linewidth 1");
142  m_gnuplot.AppendExtra ("set style line 8 linecolor rgb 'gray' linetype 0 linewidth 1 ");
143 
144  m_gnuplot.AppendExtra ("set grid ytics lt 0 lw 1 lc rgb \"#ccc\"");
145  m_gnuplot.AppendExtra ("set grid xtics lt 0 lw 1 lc rgb \"#ccc\"");
146 
147  m_gnuplot.AppendExtra ("set mxtics 5");
148  m_gnuplot.AppendExtra ("set grid mxtics xtics ls 7, ls 8");
149 
150  m_gnuplot.AppendExtra ("set arrow from graph 1,0 to graph 1.03,0 size screen 0.025,15,60 filled ls 3");
151  m_gnuplot.AppendExtra ("set arrow from graph 0,1 to graph 0,1.03 size screen 0.025,15,60 filled ls 3");
152 
153  m_gnuplot.AppendExtra ("set style fill transparent solid 0.4 noborder");
154  //m_gnuplot.AppendExtra ("set logscale y 2");
155 
156  if (m_plotFileType=="tex") {
157  m_gnuplot.AppendExtra ("set key outside");
158  m_gnuplot.AppendExtra ("set key center top");
159  }else{
160  m_gnuplot.AppendExtra ("set key outside");
161  m_gnuplot.AppendExtra ("set key right bottom");
162  }
163 
164  std::string plotTitle;
165 
166  if(graphTitle.empty()){
167  plotTitle = "QKD Key Buffer on node ";
168  if(m_src && m_dst){
169 
170  std::ostringstream tempSource;
171  tempSource << m_src->GetId();
172 
173  std::ostringstream tempDestination;
174  tempDestination << m_dst->GetId();
175 
176  plotTitle = plotTitle + tempSource.str() + "\\n QKD link between nodes " + tempSource.str() + " and " + tempDestination.str();
177  }
178  }else{
179  plotTitle = graphTitle;
180  }
181 
182  m_gnuplot.SetTitle (plotTitle);
183  //m_gnuplot.SetTerminal ("png");
184  // Set the labels for each axis.
185  m_gnuplot.SetLegend ("Time (seconds)", "Key material (bits)");
186 
187 }
188 
189 
190 void
192 
193  NS_LOG_FUNCTION (this << m_src->GetId() << m_dst->GetId() << m_plotFileName);
194 
195  std::string tempPlotFileName1 = m_plotFileName;
196  tempPlotFileName1 += "_data.dat";
197  std::ofstream tempPlotFile1 (tempPlotFileName1.c_str());
198 
199  for(uint i=1;i<=30;i++){
202  }
204 
205  m_buffer->CheckState();
207 
216 
217  m_plotFileName += ".plt";
218  // Open the plot file.
219  std::ofstream plotFile (m_plotFileName.c_str());
220 
221  // Write the plot file.
222  m_gnuplot.GenerateOutput (plotFile, tempPlotFile1, tempPlotFileName1);
223 
224  // Close the plot file.
225  plotFile.close ();
226 }
227 
228 void
230  m_buffer->InitTotalGraph();
231 }
232 
233 
234 void
235 QKDGraph::ProcessMCurrent(uint32_t newValue){
236 
237  NS_LOG_FUNCTION (this << newValue << m_src->GetId() << m_dst->GetId() );
239  m_dataset.Add( m_simulationTime, newValue);
240 }
241 
242 void
243 QKDGraph::ProcessMThrStatus(uint32_t newValue){
244 
245  NS_LOG_FUNCTION (this << newValue << m_src->GetId() << m_dst->GetId() );
248 }
249 
250 void
251 QKDGraph::ProcessMStatusHelpFunction(double time, uint32_t newValue){
252 
253  NS_LOG_FUNCTION (this << time << newValue << m_src->GetId() << m_dst->GetId() );
254 
255  switch(newValue){
256  case 0:
258  m_datasetWorkingState_1.Add( time, 0);
259  m_datasetWorkingState_2.Add( time, 0);
260  m_datasetWorkingState_3.Add( time, 0);
261  break;
262  case 1:
263  m_datasetWorkingState_0.Add( time, 0);
265  m_datasetWorkingState_2.Add( time, 0);
266  m_datasetWorkingState_3.Add( time, 0);
267 
268  break;
269  case 2:
270  m_datasetWorkingState_0.Add( time, 0);
271  m_datasetWorkingState_1.Add( time, 0);
273  m_datasetWorkingState_3.Add( time, 0);
274  break;
275  case 3:
276  m_datasetWorkingState_0.Add( time, 0);
277  m_datasetWorkingState_1.Add( time, 0);
278  m_datasetWorkingState_2.Add( time, 0);
280  break;
281  }
283 }
284 
285 
286 void
287 QKDGraph::ProcessMStatus(uint32_t newValue){
288 
289  NS_LOG_FUNCTION (this << newValue);
290 
293 }
294 
295 }
void SetStyle(enum Style style)
Definition: gnuplot.cc:345
void Add(double x, double y)
Definition: gnuplot.cc:362
void SetExtra(const std::string &extra)
Add extra formatting parameters to this dataset.
Definition: gnuplot.cc:152
void SetTitle(const std::string &title)
Change line title.
Definition: gnuplot.cc:141
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:759
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:739
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:752
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:765
void SetTitle(const std::string &title)
Definition: gnuplot.cc:733
void SetOutputFilename(const std::string &outputFilename)
Definition: gnuplot.cc:706
uint32_t GetId() const
Definition: node.cc:117
A base class which provides memory management and object aggregation.
Definition: object.h:89
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
std::string m_plotFileType
Definition: qkd-graph.h:122
Gnuplot2dDataset m_datasetWorkingState_3
Definition: qkd-graph.h:132
Ptr< QKDBuffer > m_buffer
QKDBuffer associated with the QKDGraph.
Definition: qkd-graph.h:111
uint32_t m_keymMax
get some boundaries for the graph
Definition: qkd-graph.h:117
std::string m_plotFileName
output filename
Definition: qkd-graph.h:121
virtual ~QKDGraph()
Destructor.
Definition: qkd-graph.cc:37
Ptr< Node > m_src
source node, info required for graph title
Definition: qkd-graph.h:112
Gnuplot2dDataset m_datasetMaximum
Definition: qkd-graph.h:135
QKDGraph(Ptr< QKDControl > control, Ptr< Node > src, Ptr< Node > dst, uint32_t bufferID, std::string graphTitle, std::string graphType)
Constructor.
Definition: qkd-graph.cc:39
Gnuplot m_gnuplot
Gluplot object settings.
Definition: qkd-graph.h:126
void ProcessMStatus(uint32_t value)
The status of the QKDBuffer changed, so plot it on the graph.
Definition: qkd-graph.cc:287
void ProcessMStatusHelpFunction(double time, uint32_t newValue)
Help function for detection of status change value.
Definition: qkd-graph.cc:251
uint32_t m_keymCurrent
get some boundaries for the graph
Definition: qkd-graph.h:116
Gnuplot2dDataset m_datasetThreshold
Definition: qkd-graph.h:133
uint32_t m_keymMin
get some boundaries for the graph
Definition: qkd-graph.h:115
Gnuplot2dDataset m_datasetWorkingState_0
Definition: qkd-graph.h:129
void InitTotalGraph() const
Initialized function for total graph.
Definition: qkd-graph.cc:229
uint32_t m_keymThreshold
get some boundaries for the graph
Definition: qkd-graph.h:119
static TypeId GetTypeId(void)
Get the type ID.
Definition: qkd-graph.cc:29
Ptr< Node > m_dst
destination node, info required for graph title
Definition: qkd-graph.h:113
void PrintGraph()
Print the graph.
Definition: qkd-graph.cc:191
uint32_t m_graphStatusEntry
temp variable
Definition: qkd-graph.h:124
Gnuplot2dDataset m_datasetWorkingState_1
Definition: qkd-graph.h:130
void ProcessMCurrent(uint32_t value)
MCurrent value of the QKDBuffer changed, so plot it on the graph.
Definition: qkd-graph.cc:235
double m_simulationTime
time value, x-axis
Definition: qkd-graph.h:123
void ProcessMThrStatus(uint32_t value)
The Mthr value of the QKDBuffer changed, so plot it on the graph.
Definition: qkd-graph.cc:243
Gnuplot2dDataset m_dataset
Definition: qkd-graph.h:127
uint32_t m_tempMax
get some boundaries for the graph
Definition: qkd-graph.h:118
Gnuplot2dDataset m_datasetWorkingState_2
Definition: qkd-graph.h:131
Gnuplot2dDataset m_datasetMinimum
Definition: qkd-graph.h:134
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#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:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.