1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3 * Copyright (c) 2008 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19 */
20
21 #include "ns3/node.h"
22 #include "ns3/node-list.h"
23 #include "ns3/simulator.h"
24 #include "ns3/ipv6-routing-protocol.h"
25 #include "ns3/ipv6-list-routing.h"
26 #include "ns3/ipv6-l3-protocol.h"
27 #include "ns3/ipv6-interface.h"
28 #include "ns3/ndisc-cache.h"
29 #include "ns3/names.h"
30 #include "ipv6-routing-helper.h"
31
32 namespace ns3 {
33
~Ipv6RoutingHelper()34 Ipv6RoutingHelper::~Ipv6RoutingHelper ()
35 {
36 }
37
38 void
PrintRoutingTableAllAt(Time printTime,Ptr<OutputStreamWrapper> stream,Time::Unit unit)39 Ipv6RoutingHelper::PrintRoutingTableAllAt (Time printTime, Ptr<OutputStreamWrapper> stream, Time::Unit unit)
40 {
41 for (uint32_t i = 0; i < NodeList::GetNNodes (); i++)
42 {
43 Ptr<Node> node = NodeList::GetNode (i);
44 Simulator::Schedule (printTime, &Ipv6RoutingHelper::Print, node, stream, unit);
45 }
46 }
47
48 void
PrintRoutingTableAllEvery(Time printInterval,Ptr<OutputStreamWrapper> stream,Time::Unit unit)49 Ipv6RoutingHelper::PrintRoutingTableAllEvery (Time printInterval, Ptr<OutputStreamWrapper> stream, Time::Unit unit)
50 {
51 for (uint32_t i = 0; i < NodeList::GetNNodes (); i++)
52 {
53 Ptr<Node> node = NodeList::GetNode (i);
54 Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintEvery, printInterval, node, stream, unit);
55 }
56 }
57
58 void
PrintRoutingTableAt(Time printTime,Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)59 Ipv6RoutingHelper::PrintRoutingTableAt (Time printTime, Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit)
60 {
61 Simulator::Schedule (printTime, &Ipv6RoutingHelper::Print, node, stream, unit);
62 }
63
64 void
PrintRoutingTableEvery(Time printInterval,Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)65 Ipv6RoutingHelper::PrintRoutingTableEvery (Time printInterval,Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit)
66 {
67 Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintEvery, printInterval, node, stream, unit);
68 }
69
70 void
Print(Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)71 Ipv6RoutingHelper::Print (Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit)
72 {
73 Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
74 if (ipv6)
75 {
76 Ptr<Ipv6RoutingProtocol> rp = ipv6->GetRoutingProtocol ();
77 NS_ASSERT (rp);
78 rp->PrintRoutingTable (stream, unit);
79 }
80 }
81
82 void
PrintEvery(Time printInterval,Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)83 Ipv6RoutingHelper::PrintEvery (Time printInterval, Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit)
84 {
85 Ptr<Ipv6> ipv6 = node->GetObject<Ipv6> ();
86 if (ipv6)
87 {
88 Ptr<Ipv6RoutingProtocol> rp = ipv6->GetRoutingProtocol ();
89 NS_ASSERT (rp);
90 rp->PrintRoutingTable (stream, unit);
91 Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintEvery, printInterval, node, stream, unit);
92 }
93 }
94
95 void
PrintNeighborCacheAllAt(Time printTime,Ptr<OutputStreamWrapper> stream,Time::Unit unit)96 Ipv6RoutingHelper::PrintNeighborCacheAllAt (Time printTime, Ptr<OutputStreamWrapper> stream, Time::Unit unit /* = Time::S */)
97 {
98 for (uint32_t i = 0; i < NodeList::GetNNodes (); i++)
99 {
100 Ptr<Node> node = NodeList::GetNode (i);
101 Simulator::Schedule (printTime, &Ipv6RoutingHelper::PrintNdiscCache, node, stream, unit);
102 }
103 }
104
105 void
PrintNeighborCacheAllEvery(Time printInterval,Ptr<OutputStreamWrapper> stream,Time::Unit unit)106 Ipv6RoutingHelper::PrintNeighborCacheAllEvery (Time printInterval, Ptr<OutputStreamWrapper> stream, Time::Unit unit /* = Time::S */)
107 {
108 for (uint32_t i = 0; i < NodeList::GetNNodes (); i++)
109 {
110 Ptr<Node> node = NodeList::GetNode (i);
111 Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintNdiscCacheEvery, printInterval, node, stream, unit);
112 }
113 }
114
115 void
PrintNeighborCacheAt(Time printTime,Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)116 Ipv6RoutingHelper::PrintNeighborCacheAt (Time printTime, Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit /* = Time::S */)
117 {
118 Simulator::Schedule (printTime, &Ipv6RoutingHelper::PrintNdiscCache, node, stream, unit);
119 }
120
121 void
PrintNeighborCacheEvery(Time printInterval,Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)122 Ipv6RoutingHelper::PrintNeighborCacheEvery (Time printInterval,Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit /* = Time::S */)
123 {
124 Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintNdiscCacheEvery, printInterval, node, stream, unit);
125 }
126
127 void
PrintNdiscCache(Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)128 Ipv6RoutingHelper::PrintNdiscCache (Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit /* = Time::S */)
129 {
130 Ptr<Ipv6L3Protocol> ipv6 = node->GetObject<Ipv6L3Protocol> ();
131 if (ipv6)
132 {
133 std::ostream* os = stream->GetStream ();
134
135 *os << "NDISC Cache of node ";
136 std::string found = Names::FindName (node);
137 if (Names::FindName (node) != "")
138 {
139 *os << found;
140 }
141 else
142 {
143 *os << static_cast<int> (node->GetId ());
144 }
145 *os << " at time " << Simulator::Now ().As (unit) << "\n";
146
147 for (uint32_t i=0; i<ipv6->GetNInterfaces(); i++)
148 {
149 Ptr<NdiscCache> ndiscCache = ipv6->GetInterface (i)->GetNdiscCache ();
150 if (ndiscCache)
151 {
152 ndiscCache->PrintNdiscCache (stream);
153 }
154 }
155 }
156 }
157
158 void
PrintNdiscCacheEvery(Time printInterval,Ptr<Node> node,Ptr<OutputStreamWrapper> stream,Time::Unit unit)159 Ipv6RoutingHelper::PrintNdiscCacheEvery (Time printInterval, Ptr<Node> node, Ptr<OutputStreamWrapper> stream, Time::Unit unit /* = Time::S */)
160 {
161 Ptr<Ipv6L3Protocol> ipv6 = node->GetObject<Ipv6L3Protocol> ();
162 if (ipv6)
163 {
164 std::ostream* os = stream->GetStream ();
165
166 *os << "NDISC Cache of node ";
167 std::string found = Names::FindName (node);
168 if (Names::FindName (node) != "")
169 {
170 *os << found;
171 }
172 else
173 {
174 *os << static_cast<int> (node->GetId ());
175 }
176 *os << " at time " << Simulator::Now ().As (unit) << "\n";
177
178 for (uint32_t i=0; i<ipv6->GetNInterfaces(); i++)
179 {
180 Ptr<NdiscCache> ndiscCache = ipv6->GetInterface (i)->GetNdiscCache ();
181 if (ndiscCache)
182 {
183 ndiscCache->PrintNdiscCache (stream);
184 }
185 }
186 Simulator::Schedule (printInterval, &Ipv6RoutingHelper::PrintNdiscCacheEvery, printInterval, node, stream, unit);
187 }
188 }
189
190 } // namespace ns3
191