1 // -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*-
2 //
3 // Copyright (c) 2009 INESC Porto
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: Gustavo J. A. M. Carneiro  <gjc@inescporto.pt> <gjcarneiro@gmail.com>
19 //
20 
21 #include "flow-monitor-helper.h"
22 
23 #include "ns3/flow-monitor.h"
24 #include "ns3/ipv4-flow-classifier.h"
25 #include "ns3/ipv4-flow-probe.h"
26 #include "ns3/ipv4-l3-protocol.h"
27 #include "ns3/ipv6-flow-classifier.h"
28 #include "ns3/ipv6-flow-probe.h"
29 #include "ns3/ipv6-l3-protocol.h"
30 #include "ns3/node.h"
31 #include "ns3/node-list.h"
32 
33 
34 namespace ns3 {
35 
FlowMonitorHelper()36 FlowMonitorHelper::FlowMonitorHelper ()
37 {
38   m_monitorFactory.SetTypeId ("ns3::FlowMonitor");
39 }
40 
~FlowMonitorHelper()41 FlowMonitorHelper::~FlowMonitorHelper ()
42 {
43   if (m_flowMonitor)
44     {
45       m_flowMonitor->Dispose ();
46       m_flowMonitor = 0;
47       m_flowClassifier4 = 0;
48       m_flowClassifier6 = 0;
49     }
50 }
51 
52 void
SetMonitorAttribute(std::string n1,const AttributeValue & v1)53 FlowMonitorHelper::SetMonitorAttribute (std::string n1, const AttributeValue &v1)
54 {
55   m_monitorFactory.Set (n1, v1);
56 }
57 
58 
59 Ptr<FlowMonitor>
GetMonitor()60 FlowMonitorHelper::GetMonitor ()
61 {
62   if (!m_flowMonitor)
63     {
64       m_flowMonitor = m_monitorFactory.Create<FlowMonitor> ();
65       m_flowClassifier4 = Create<Ipv4FlowClassifier> ();
66       m_flowMonitor->AddFlowClassifier (m_flowClassifier4);
67       m_flowClassifier6 = Create<Ipv6FlowClassifier> ();
68       m_flowMonitor->AddFlowClassifier (m_flowClassifier6);
69     }
70   return m_flowMonitor;
71 }
72 
73 
74 Ptr<FlowClassifier>
GetClassifier()75 FlowMonitorHelper::GetClassifier ()
76 {
77   if (!m_flowClassifier4)
78     {
79       m_flowClassifier4 = Create<Ipv4FlowClassifier> ();
80     }
81   return m_flowClassifier4;
82 }
83 
84 
85 Ptr<FlowClassifier>
GetClassifier6()86 FlowMonitorHelper::GetClassifier6 ()
87 {
88   if (!m_flowClassifier6)
89     {
90       m_flowClassifier6 = Create<Ipv6FlowClassifier> ();
91     }
92   return m_flowClassifier6;
93 }
94 
95 
96 Ptr<FlowMonitor>
Install(Ptr<Node> node)97 FlowMonitorHelper::Install (Ptr<Node> node)
98 {
99   Ptr<FlowMonitor> monitor = GetMonitor ();
100   Ptr<FlowClassifier> classifier = GetClassifier ();
101   Ptr<Ipv4L3Protocol> ipv4 = node->GetObject<Ipv4L3Protocol> ();
102   if (ipv4)
103     {
104       Ptr<Ipv4FlowProbe> probe = Create<Ipv4FlowProbe> (monitor,
105                                                         DynamicCast<Ipv4FlowClassifier> (classifier),
106                                                         node);
107     }
108   Ptr<FlowClassifier> classifier6 = GetClassifier6 ();
109   Ptr<Ipv6L3Protocol> ipv6 = node->GetObject<Ipv6L3Protocol> ();
110   if (ipv6)
111     {
112       Ptr<Ipv6FlowProbe> probe6 = Create<Ipv6FlowProbe> (monitor,
113                                                          DynamicCast<Ipv6FlowClassifier> (classifier6),
114                                                          node);
115     }
116   return m_flowMonitor;
117 }
118 
119 
120 Ptr<FlowMonitor>
Install(NodeContainer nodes)121 FlowMonitorHelper::Install (NodeContainer nodes)
122 {
123   for (NodeContainer::Iterator i = nodes.Begin (); i != nodes.End (); ++i)
124     {
125       Ptr<Node> node = *i;
126       if (node->GetObject<Ipv4L3Protocol> () || node->GetObject<Ipv6L3Protocol> ())
127         {
128           Install (node);
129         }
130     }
131   return m_flowMonitor;
132 }
133 
134 Ptr<FlowMonitor>
InstallAll()135 FlowMonitorHelper::InstallAll ()
136 {
137   for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
138     {
139       Ptr<Node> node = *i;
140       if (node->GetObject<Ipv4L3Protocol> () || node->GetObject<Ipv6L3Protocol> ())
141         {
142           Install (node);
143         }
144     }
145   return m_flowMonitor;
146 }
147 
148 void
SerializeToXmlStream(std::ostream & os,uint16_t indent,bool enableHistograms,bool enableProbes)149 FlowMonitorHelper::SerializeToXmlStream (std::ostream &os, uint16_t indent, bool enableHistograms, bool enableProbes)
150 {
151   if (m_flowMonitor)
152     {
153       m_flowMonitor->SerializeToXmlStream (os, indent, enableHistograms, enableProbes);
154     }
155 }
156 
157 std::string
SerializeToXmlString(uint16_t indent,bool enableHistograms,bool enableProbes)158 FlowMonitorHelper::SerializeToXmlString (uint16_t indent, bool enableHistograms, bool enableProbes)
159 {
160   std::ostringstream os;
161   if (m_flowMonitor)
162     {
163       m_flowMonitor->SerializeToXmlStream (os, indent, enableHistograms, enableProbes);
164     }
165   return os.str ();
166 }
167 
168 void
SerializeToXmlFile(std::string fileName,bool enableHistograms,bool enableProbes)169 FlowMonitorHelper::SerializeToXmlFile (std::string fileName, bool enableHistograms, bool enableProbes)
170 {
171   if (m_flowMonitor)
172     {
173       m_flowMonitor->SerializeToXmlFile (fileName, enableHistograms, enableProbes);
174     }
175 }
176 
177 
178 } // namespace ns3
179