blob: 6ed3e877779bcc5e8e6ce07924e623a7ec732931 [file] [log] [blame]
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2013 University of California, Los Angeles
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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ndn-app-delay-tracer.h"
22#include "ns3/node.h"
23#include "ns3/packet.h"
24#include "ns3/config.h"
25#include "ns3/names.h"
26#include "ns3/callback.h"
27
28#include "ns3/ndn-app.h"
29#include "ns3/ndn-interest.h"
30#include "ns3/ndn-content-object.h"
31#include "ns3/simulator.h"
32#include "ns3/node-list.h"
33#include "ns3/log.h"
34
35#include <boost/lexical_cast.hpp>
36#include <boost/make_shared.hpp>
37
38#include <fstream>
39
40NS_LOG_COMPONENT_DEFINE ("ndn.AppDelayTracer");
41
42using namespace std;
43
44namespace ns3 {
45namespace ndn {
46
47
48boost::tuple< boost::shared_ptr<std::ostream>, std::list<Ptr<AppDelayTracer> > >
49AppDelayTracer::InstallAll (const std::string &file)
50{
51 using namespace boost;
52 using namespace std;
53
54 std::list<Ptr<AppDelayTracer> > tracers;
55 boost::shared_ptr<std::ofstream> outputStream = make_shared<std::ofstream> ();
56
57 outputStream->open (file.c_str (), std::ios_base::out | std::ios_base::trunc);
58 if (!outputStream->is_open ())
59 return boost::make_tuple (outputStream, tracers);
60
61 for (NodeList::Iterator node = NodeList::Begin ();
62 node != NodeList::End ();
63 node++)
64 {
65 NS_LOG_DEBUG ("Node: " << (*node)->GetId ());
66
67 Ptr<AppDelayTracer> trace = Create<AppDelayTracer> (outputStream, *node);
68 tracers.push_back (trace);
69 }
70
71 if (tracers.size () > 0)
72 {
73 // *m_l3RateTrace << "# "; // not necessary for R's read.table
74 tracers.front ()->PrintHeader (*outputStream);
75 *outputStream << "\n";
76 }
77
78 return boost::make_tuple (outputStream, tracers);
79}
80
81//////////////////////////////////////////////////////////////////////////////
82//////////////////////////////////////////////////////////////////////////////
83//////////////////////////////////////////////////////////////////////////////
84
85AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, Ptr<Node> node)
86: m_nodePtr (node)
87, m_os (os)
88{
89 m_node = boost::lexical_cast<string> (m_nodePtr->GetId ());
90
91 Connect ();
92
93 string name = Names::FindName (node);
94 if (!name.empty ())
95 {
96 m_node = name;
97 }
98}
99
100AppDelayTracer::AppDelayTracer (boost::shared_ptr<std::ostream> os, const std::string &node)
101: m_node (node)
102, m_os (os)
103{
104 Connect ();
105}
106
107AppDelayTracer::~AppDelayTracer ()
108{
109};
110
111
112void
113AppDelayTracer::Connect ()
114{
115 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/LastRetransmittedInterestDataDelay",
116 MakeCallback (&AppDelayTracer::LastRetransmittedInterestDataDelay, this));
117
Alexander Afanasyevb9faa442013-01-18 19:53:43 -0800118 Config::ConnectWithoutContext ("/NodeList/"+m_node+"/ApplicationList/*/FirstInterestDataDelay",
Alexander Afanasyevdb64ff12013-01-18 16:37:31 -0800119 MakeCallback (&AppDelayTracer::FirstInterestDataDelay, this));
120}
121
122void
123AppDelayTracer::PrintHeader (std::ostream &os) const
124{
125 os << "Node" << "\t"
126 << "AppId" << "\t"
127 << "SeqNo" << "\t"
128
129 << "Type" << "\t"
130 << "DelayS" << "\t"
131 << "DelayUS" << "\n";
132}
133
134void
135AppDelayTracer::LastRetransmittedInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay)
136{
137 *m_os << m_node << "\t"
138 << app->GetId () << "\t"
139 << seqno << "\t"
140 << "LastDelay" << "\t"
141 << delay.ToDouble (Time::S) << "\t"
142 << delay.ToDouble (Time::US) << "\n";
143}
144
145void
146AppDelayTracer::FirstInterestDataDelay (Ptr<App> app, uint32_t seqno, Time delay)
147{
148 *m_os << m_node << "\t"
149 << app->GetId () << "\t"
150 << seqno << "\t"
151 << "FullDelay" << "\t"
152 << delay.ToDouble (Time::S) << "\t"
153 << delay.ToDouble (Time::US) << "\n";
154}
155
156
157} // namespace ndn
158} // namespace ns3