src/helper/point-to-point-helper.cc
author Nicola Baldo <nbaldo@cttc.es>
Thu Aug 13 09:36:53 2009 +0200 (2009-08-13)
changeset 4709 b0743dbc4e55
parent 4448 641b88d1e131
child 4712 74ed62336c36
permissions -rw-r--r--
bug 639: add configurable capture size to pcap
     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 #include "point-to-point-helper.h"
    21 #include "ns3/simulator.h"
    22 #include "ns3/point-to-point-net-device.h"
    23 #include "ns3/point-to-point-channel.h"
    24 #include "ns3/queue.h"
    25 #include "ns3/pcap-writer.h"
    26 #include "ns3/config.h"
    27 #include "ns3/packet.h"
    28 #include "ns3/names.h"
    29 
    30 namespace ns3 {
    31 
    32 
    33 PointToPointHelper::PointToPointHelper ()
    34 {
    35   m_queueFactory.SetTypeId ("ns3::DropTailQueue");
    36   m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
    37   m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
    38 }
    39 
    40 void 
    41 PointToPointHelper::SetQueue (std::string type,
    42                               std::string n1, const AttributeValue &v1,
    43                               std::string n2, const AttributeValue &v2,
    44                               std::string n3, const AttributeValue &v3,
    45                               std::string n4, const AttributeValue &v4)
    46 {
    47   m_queueFactory.SetTypeId (type);
    48   m_queueFactory.Set (n1, v1);
    49   m_queueFactory.Set (n2, v2);
    50   m_queueFactory.Set (n3, v3);
    51   m_queueFactory.Set (n4, v4);
    52 }
    53 
    54 void 
    55 PointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
    56 {
    57   m_deviceFactory.Set (n1, v1);
    58 }
    59 
    60 void 
    61 PointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
    62 {
    63   m_channelFactory.Set (n1, v1);
    64 }
    65 
    66 void 
    67 PointToPointHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid)
    68 {
    69   std::ostringstream oss;
    70   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/";
    71   Config::MatchContainer matches = Config::LookupMatches (oss.str ());
    72   if (matches.GetN () == 0)
    73     {
    74       return;
    75     }
    76   oss.str ("");
    77   oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
    78   Ptr<PcapWriter> pcap = CreateObject<PcapWriter> ();
    79   pcap->Open (oss.str ());
    80   pcap->WritePppHeader ();
    81   oss.str ("");
    82   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
    83   oss << "/$ns3::PointToPointNetDevice/PromiscSniffer";
    84   Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::SniffEvent, pcap));
    85 }
    86 
    87 void 
    88 PointToPointHelper::EnablePcap (std::string filename, Ptr<NetDevice> nd)
    89 {
    90   EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
    91 }
    92 
    93 void 
    94 PointToPointHelper::EnablePcap (std::string filename, std::string ndName)
    95 {
    96   Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
    97   EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
    98 }
    99 
   100 void 
   101 PointToPointHelper::EnablePcap (std::string filename, NetDeviceContainer d)
   102 {
   103   for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
   104     {
   105       Ptr<NetDevice> dev = *i;
   106       EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
   107     }
   108 }
   109 
   110 void
   111 PointToPointHelper::EnablePcap (std::string filename, NodeContainer n)
   112 {
   113   NetDeviceContainer devs;
   114   for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
   115     {
   116       Ptr<Node> node = *i;
   117       for (uint32_t j = 0; j < node->GetNDevices (); ++j)
   118         {
   119           devs.Add (node->GetDevice (j));
   120         }
   121     }
   122   EnablePcap (filename, devs);
   123 }
   124 
   125 void
   126 PointToPointHelper::EnablePcapAll (std::string filename)
   127 {
   128   EnablePcap (filename, NodeContainer::GetGlobal ());
   129 }
   130 
   131 void 
   132 PointToPointHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
   133 {
   134   Packet::EnablePrinting ();
   135   std::ostringstream oss;
   136   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
   137   Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiRxEvent, &os));
   138   oss.str ("");
   139   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
   140   Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEnqueueEvent, &os));
   141   oss.str ("");
   142   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
   143   Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDequeueEvent, &os));
   144   oss.str ("");
   145   oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
   146   Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDropEvent, &os));
   147 }
   148 
   149 void 
   150 PointToPointHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
   151 {
   152   for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
   153     {
   154       Ptr<NetDevice> dev = *i;
   155       EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
   156     }
   157 }
   158 
   159 void
   160 PointToPointHelper::EnableAscii (std::ostream &os, NodeContainer n)
   161 {
   162   NetDeviceContainer devs;
   163   for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
   164     {
   165       Ptr<Node> node = *i;
   166       for (uint32_t j = 0; j < node->GetNDevices (); ++j)
   167         {
   168           devs.Add (node->GetDevice (j));
   169         }
   170     }
   171   EnableAscii (os, devs);
   172 }
   173 
   174 void
   175 PointToPointHelper::EnableAsciiAll (std::ostream &os)
   176 {
   177   EnableAscii (os, NodeContainer::GetGlobal ());
   178 }
   179 
   180 NetDeviceContainer 
   181 PointToPointHelper::Install (NodeContainer c)
   182 {
   183   NS_ASSERT (c.GetN () == 2);
   184   return Install (c.Get (0), c.Get (1));
   185 }
   186 
   187 NetDeviceContainer 
   188 PointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
   189 {
   190   NetDeviceContainer container;
   191 
   192   Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
   193   devA->SetAddress (Mac48Address::Allocate ());
   194   a->AddDevice (devA);
   195   Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
   196   devA->SetQueue (queueA);
   197   Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
   198   devB->SetAddress (Mac48Address::Allocate ());
   199   b->AddDevice (devB);
   200   Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
   201   devB->SetQueue (queueB);
   202   Ptr<PointToPointChannel> channel = m_channelFactory.Create<PointToPointChannel> ();
   203   devA->Attach (channel);
   204   devB->Attach (channel);
   205   container.Add (devA);
   206   container.Add (devB);
   207 
   208   return container;
   209 }
   210 
   211 NetDeviceContainer 
   212 PointToPointHelper::Install (Ptr<Node> a, std::string bName)
   213 {
   214   Ptr<Node> b = Names::Find<Node> (bName);
   215   return Install (a, b);
   216 }
   217 
   218 NetDeviceContainer 
   219 PointToPointHelper::Install (std::string aName, Ptr<Node> b)
   220 {
   221   Ptr<Node> a = Names::Find<Node> (aName);
   222   return Install (a, b);
   223 }
   224 
   225 NetDeviceContainer 
   226 PointToPointHelper::Install (std::string aName, std::string bName)
   227 {
   228   Ptr<Node> a = Names::Find<Node> (aName);
   229   Ptr<Node> b = Names::Find<Node> (bName);
   230   return Install (a, b);
   231 }
   232 
   233 void 
   234 PointToPointHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes, 
   235                                  NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
   236 {
   237   for (uint32_t i = 0; i < spokes.GetN (); ++i)
   238     {
   239       NetDeviceContainer nd = Install (hub, spokes.Get (i));
   240       hubDevices.Add (nd.Get (0));
   241       spokeDevices.Add (nd.Get (1));
   242     }
   243 }
   244 
   245 void 
   246 PointToPointHelper::InstallStar (std::string hubName, NodeContainer spokes, 
   247                                  NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
   248 {
   249   Ptr<Node> hub = Names::Find<Node> (hubName);
   250   InstallStar (hub, spokes, hubDevices, spokeDevices);
   251 }
   252 
   253 void 
   254 PointToPointHelper::SniffEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
   255 {
   256   writer->WritePacket (packet);
   257 }
   258 
   259 void 
   260 PointToPointHelper::AsciiEnqueueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
   261 {
   262   *os << "+ " << Simulator::Now ().GetSeconds () << " ";
   263   *os << path << " " << *packet << std::endl;
   264 }
   265 
   266 void 
   267 PointToPointHelper::AsciiDequeueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
   268 {
   269   *os << "- " << Simulator::Now ().GetSeconds () << " ";
   270   *os << path << " " << *packet << std::endl;
   271 }
   272 
   273 void 
   274 PointToPointHelper::AsciiDropEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
   275 {
   276   *os << "d " << Simulator::Now ().GetSeconds () << " ";
   277   *os << path << " " << *packet << std::endl;
   278 }
   279 
   280 void 
   281 PointToPointHelper::AsciiRxEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
   282 {
   283   *os << "r " << Simulator::Now ().GetSeconds () << " ";
   284   *os << path << " " << *packet << std::endl;
   285 }
   286 
   287 } // namespace ns3