00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "point-to-point-helper.h"
00021 #include "ns3/simulator.h"
00022 #include "ns3/point-to-point-net-device.h"
00023 #include "ns3/point-to-point-channel.h"
00024 #include "ns3/queue.h"
00025 #include "ns3/pcap-writer.h"
00026 #include "ns3/config.h"
00027 #include "ns3/packet.h"
00028
00029
00030 namespace ns3 {
00031
00032
00033 PointToPointHelper::PointToPointHelper ()
00034 {
00035 m_queueFactory.SetTypeId ("ns3::DropTailQueue");
00036 m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
00037 m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
00038 }
00039
00040 void
00041 PointToPointHelper::SetQueue (std::string type,
00042 std::string n1, const AttributeValue &v1,
00043 std::string n2, const AttributeValue &v2,
00044 std::string n3, const AttributeValue &v3,
00045 std::string n4, const AttributeValue &v4)
00046 {
00047 m_queueFactory.SetTypeId (type);
00048 m_queueFactory.Set (n1, v1);
00049 m_queueFactory.Set (n2, v2);
00050 m_queueFactory.Set (n3, v3);
00051 m_queueFactory.Set (n4, v4);
00052 }
00053
00054 void
00055 PointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
00056 {
00057 m_deviceFactory.Set (n1, v1);
00058 }
00059
00060 void
00061 PointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
00062 {
00063 m_channelFactory.Set (n1, v1);
00064 }
00065
00066 void
00067 PointToPointHelper::SetDeviceParameter (std::string name, const AttributeValue &value)
00068 {
00069 SetDeviceAttribute (name, value);
00070 }
00071 void
00072 PointToPointHelper::SetChannelParameter (std::string name, const AttributeValue &value)
00073 {
00074 SetChannelAttribute (name, value);
00075 }
00076
00077 void
00078 PointToPointHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid)
00079 {
00080 std::ostringstream oss;
00081 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/";
00082 Config::MatchContainer matches = Config::LookupMatches (oss.str ());
00083 if (matches.GetN () == 0)
00084 {
00085 return;
00086 }
00087 oss.str ("");
00088 oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
00089 Ptr<PcapWriter> pcap = Create<PcapWriter> ();
00090 pcap->Open (oss.str ());
00091 pcap->WritePppHeader ();
00092 oss.str ("");
00093 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/Rx";
00094 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::RxEvent, pcap));
00095 oss.str ("");
00096 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
00097 Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::EnqueueEvent, pcap));
00098 }
00099 void
00100 PointToPointHelper::EnablePcap (std::string filename, NetDeviceContainer d)
00101 {
00102 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
00103 {
00104 Ptr<NetDevice> dev = *i;
00105 EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
00106 }
00107 }
00108 void
00109 PointToPointHelper::EnablePcap (std::string filename, NodeContainer n)
00110 {
00111 NetDeviceContainer devs;
00112 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
00113 {
00114 Ptr<Node> node = *i;
00115 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
00116 {
00117 devs.Add (node->GetDevice (j));
00118 }
00119 }
00120 EnablePcap (filename, devs);
00121 }
00122
00123 void
00124 PointToPointHelper::EnablePcapAll (std::string filename)
00125 {
00126 EnablePcap (filename, NodeContainer::GetGlobal ());
00127 }
00128
00129 void
00130 PointToPointHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
00131 {
00132 Packet::EnablePrinting ();
00133 std::ostringstream oss;
00134 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/Rx";
00135 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiRxEvent, &os));
00136 oss.str ("");
00137 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
00138 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEnqueueEvent, &os));
00139 oss.str ("");
00140 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
00141 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDequeueEvent, &os));
00142 oss.str ("");
00143 oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
00144 Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDropEvent, &os));
00145 }
00146 void
00147 PointToPointHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
00148 {
00149 for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
00150 {
00151 Ptr<NetDevice> dev = *i;
00152 EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
00153 }
00154 }
00155 void
00156 PointToPointHelper::EnableAscii (std::ostream &os, NodeContainer n)
00157 {
00158 NetDeviceContainer devs;
00159 for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
00160 {
00161 Ptr<Node> node = *i;
00162 for (uint32_t j = 0; j < node->GetNDevices (); ++j)
00163 {
00164 devs.Add (node->GetDevice (j));
00165 }
00166 }
00167 EnableAscii (os, devs);
00168 }
00169
00170 void
00171 PointToPointHelper::EnableAsciiAll (std::ostream &os)
00172 {
00173 EnableAscii (os, NodeContainer::GetGlobal ());
00174 }
00175
00176 NetDeviceContainer
00177 PointToPointHelper::Install (NodeContainer c)
00178 {
00179 NS_ASSERT (c.GetN () == 2);
00180 return Install (c.Get (0), c.Get (1));
00181 }
00182
00183 NetDeviceContainer
00184 PointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
00185 {
00186 NetDeviceContainer container;
00187
00188 Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
00189 devA->SetAddress (Mac48Address::Allocate ());
00190 a->AddDevice (devA);
00191 Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
00192 devA->SetQueue (queueA);
00193 Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
00194 devB->SetAddress (Mac48Address::Allocate ());
00195 b->AddDevice (devB);
00196 Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
00197 devB->SetQueue (queueB);
00198 Ptr<PointToPointChannel> channel = m_channelFactory.Create<PointToPointChannel> ();
00199 devA->Attach (channel);
00200 devB->Attach (channel);
00201 container.Add (devA);
00202 container.Add (devB);
00203
00204 return container;
00205 }
00206
00207 void
00208 PointToPointHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes,
00209 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
00210 {
00211 for (uint32_t i = 0; i < spokes.GetN (); ++i)
00212 {
00213 NetDeviceContainer nd = Install (hub, spokes.Get (i));
00214 hubDevices.Add (nd.Get (0));
00215 spokeDevices.Add (nd.Get (1));
00216 }
00217 }
00218
00219 void
00220 PointToPointHelper::EnqueueEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
00221 {
00222 writer->WritePacket (packet);
00223 }
00224 void
00225 PointToPointHelper::RxEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
00226 {
00227 writer->WritePacket (packet);
00228 }
00229 void
00230 PointToPointHelper::AsciiEnqueueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00231 {
00232 *os << "+ " << Simulator::Now ().GetSeconds () << " ";
00233 *os << path << " " << *packet << std::endl;
00234 }
00235 void
00236 PointToPointHelper::AsciiDequeueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00237 {
00238 *os << "- " << Simulator::Now ().GetSeconds () << " ";
00239 *os << path << " " << *packet << std::endl;
00240 }
00241 void
00242 PointToPointHelper::AsciiDropEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00243 {
00244 *os << "d " << Simulator::Now ().GetSeconds () << " ";
00245 *os << path << " " << *packet << std::endl;
00246 }
00247 void
00248 PointToPointHelper::AsciiRxEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
00249 {
00250 *os << "r " << Simulator::Now ().GetSeconds () << " ";
00251 *os << path << " " << *packet << std::endl;
00252 }
00253
00254
00255 }