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
mathieu@2833
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
mathieu@2833
     2
/*
mathieu@2833
     3
 * Copyright (c) 2008 INRIA
mathieu@2833
     4
 *
mathieu@2833
     5
 * This program is free software; you can redistribute it and/or modify
mathieu@2833
     6
 * it under the terms of the GNU General Public License version 2 as
mathieu@2833
     7
 * published by the Free Software Foundation;
mathieu@2833
     8
 *
mathieu@2833
     9
 * This program is distributed in the hope that it will be useful,
mathieu@2833
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
mathieu@2833
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
mathieu@2833
    12
 * GNU General Public License for more details.
mathieu@2833
    13
 *
mathieu@2833
    14
 * You should have received a copy of the GNU General Public License
mathieu@2833
    15
 * along with this program; if not, write to the Free Software
mathieu@2833
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
mathieu@2833
    17
 *
mathieu@2833
    18
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
mathieu@2833
    19
 */
mathieu@2418
    20
#include "point-to-point-helper.h"
tomh@2802
    21
#include "ns3/simulator.h"
mathieu@2603
    22
#include "ns3/point-to-point-net-device.h"
mathieu@2603
    23
#include "ns3/point-to-point-channel.h"
mathieu@2418
    24
#include "ns3/queue.h"
mathieu@2786
    25
#include "ns3/pcap-writer.h"
mathieu@2786
    26
#include "ns3/config.h"
mathieu@2786
    27
#include "ns3/packet.h"
craigdo@4147
    28
#include "ns3/names.h"
mathieu@2418
    29
mathieu@2418
    30
namespace ns3 {
mathieu@2418
    31
mathieu@2418
    32
mathieu@2418
    33
PointToPointHelper::PointToPointHelper ()
mathieu@2744
    34
{
mathieu@2744
    35
  m_queueFactory.SetTypeId ("ns3::DropTailQueue");
mathieu@2744
    36
  m_deviceFactory.SetTypeId ("ns3::PointToPointNetDevice");
mathieu@2744
    37
  m_channelFactory.SetTypeId ("ns3::PointToPointChannel");
mathieu@2744
    38
}
mathieu@2418
    39
mathieu@2418
    40
void 
mathieu@2418
    41
PointToPointHelper::SetQueue (std::string type,
mathieu@3786
    42
                              std::string n1, const AttributeValue &v1,
mathieu@3786
    43
                              std::string n2, const AttributeValue &v2,
mathieu@3786
    44
                              std::string n3, const AttributeValue &v3,
mathieu@3786
    45
                              std::string n4, const AttributeValue &v4)
mathieu@2418
    46
{
mathieu@2418
    47
  m_queueFactory.SetTypeId (type);
mathieu@2418
    48
  m_queueFactory.Set (n1, v1);
mathieu@2418
    49
  m_queueFactory.Set (n2, v2);
mathieu@2418
    50
  m_queueFactory.Set (n3, v3);
mathieu@2418
    51
  m_queueFactory.Set (n4, v4);
mathieu@2418
    52
}
mathieu@2418
    53
mathieu@2603
    54
void 
craigdo@3381
    55
PointToPointHelper::SetDeviceAttribute (std::string n1, const AttributeValue &v1)
mathieu@2603
    56
{
mathieu@2603
    57
  m_deviceFactory.Set (n1, v1);
mathieu@2603
    58
}
mathieu@2603
    59
mathieu@2603
    60
void 
craigdo@3381
    61
PointToPointHelper::SetChannelAttribute (std::string n1, const AttributeValue &v1)
mathieu@2603
    62
{
mathieu@2603
    63
  m_channelFactory.Set (n1, v1);
mathieu@2603
    64
}
mathieu@2603
    65
mathieu@2789
    66
void 
mathieu@2789
    67
PointToPointHelper::EnablePcap (std::string filename, uint32_t nodeid, uint32_t deviceid)
mathieu@2789
    68
{
mathieu@2789
    69
  std::ostringstream oss;
mathieu@3789
    70
  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/";
mathieu@3789
    71
  Config::MatchContainer matches = Config::LookupMatches (oss.str ());
mathieu@3789
    72
  if (matches.GetN () == 0)
mathieu@3789
    73
    {
mathieu@3789
    74
      return;
mathieu@3789
    75
    }
mathieu@3789
    76
  oss.str ("");
mathieu@2839
    77
  oss << filename << "-" << nodeid << "-" << deviceid << ".pcap";
nbaldo@4709
    78
  Ptr<PcapWriter> pcap = CreateObject<PcapWriter> ();
mathieu@2789
    79
  pcap->Open (oss.str ());
craigdo@3012
    80
  pcap->WritePppHeader ();
mathieu@2789
    81
  oss.str ("");
craigdo@4264
    82
  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid;
craigdo@4264
    83
  oss << "/$ns3::PointToPointNetDevice/PromiscSniffer";
craigdo@4263
    84
  Config::ConnectWithoutContext (oss.str (), MakeBoundCallback (&PointToPointHelper::SniffEvent, pcap));
mathieu@2789
    85
}
craigdo@4264
    86
craigdo@4264
    87
void 
craigdo@4264
    88
PointToPointHelper::EnablePcap (std::string filename, Ptr<NetDevice> nd)
craigdo@4264
    89
{
craigdo@4264
    90
  EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
craigdo@4264
    91
}
craigdo@4264
    92
craigdo@4264
    93
void 
craigdo@4264
    94
PointToPointHelper::EnablePcap (std::string filename, std::string ndName)
craigdo@4264
    95
{
craigdo@4264
    96
  Ptr<NetDevice> nd = Names::Find<NetDevice> (ndName);
craigdo@4264
    97
  EnablePcap (filename, nd->GetNode ()->GetId (), nd->GetIfIndex ());
craigdo@4264
    98
}
craigdo@4264
    99
mathieu@2789
   100
void 
mathieu@2789
   101
PointToPointHelper::EnablePcap (std::string filename, NetDeviceContainer d)
mathieu@2789
   102
{
mathieu@2789
   103
  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
mathieu@2789
   104
    {
mathieu@2789
   105
      Ptr<NetDevice> dev = *i;
mathieu@2789
   106
      EnablePcap (filename, dev->GetNode ()->GetId (), dev->GetIfIndex ());
mathieu@2789
   107
    }
mathieu@2789
   108
}
craigdo@4264
   109
mathieu@2789
   110
void
mathieu@2789
   111
PointToPointHelper::EnablePcap (std::string filename, NodeContainer n)
mathieu@2789
   112
{
mathieu@2789
   113
  NetDeviceContainer devs;
mathieu@2789
   114
  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
mathieu@2789
   115
    {
mathieu@2789
   116
      Ptr<Node> node = *i;
mathieu@2789
   117
      for (uint32_t j = 0; j < node->GetNDevices (); ++j)
mathieu@3786
   118
        {
mathieu@3786
   119
          devs.Add (node->GetDevice (j));
mathieu@3786
   120
        }
mathieu@2789
   121
    }
mathieu@2789
   122
  EnablePcap (filename, devs);
mathieu@2789
   123
}
mathieu@2603
   124
mathieu@2789
   125
void
tomh@2996
   126
PointToPointHelper::EnablePcapAll (std::string filename)
mathieu@2786
   127
{
mathieu@2789
   128
  EnablePcap (filename, NodeContainer::GetGlobal ());
mathieu@2786
   129
}
mathieu@2786
   130
mathieu@2786
   131
void 
mathieu@2789
   132
PointToPointHelper::EnableAscii (std::ostream &os, uint32_t nodeid, uint32_t deviceid)
mathieu@2786
   133
{
mathieu@3553
   134
  Packet::EnablePrinting ();
mathieu@2789
   135
  std::ostringstream oss;
craigdo@4263
   136
  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/MacRx";
tomh@2802
   137
  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiRxEvent, &os));
mathieu@2789
   138
  oss.str ("");
mathieu@2789
   139
  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Enqueue";
tomh@2802
   140
  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiEnqueueEvent, &os));
mathieu@2789
   141
  oss.str ("");
mathieu@2789
   142
  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Dequeue";
tomh@2802
   143
  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDequeueEvent, &os));
tomh@2802
   144
  oss.str ("");
tomh@2802
   145
  oss << "/NodeList/" << nodeid << "/DeviceList/" << deviceid << "/$ns3::PointToPointNetDevice/TxQueue/Drop";
tomh@2802
   146
  Config::Connect (oss.str (), MakeBoundCallback (&PointToPointHelper::AsciiDropEvent, &os));
mathieu@2786
   147
}
craigdo@4264
   148
mathieu@2786
   149
void 
mathieu@2789
   150
PointToPointHelper::EnableAscii (std::ostream &os, NetDeviceContainer d)
mathieu@2786
   151
{
mathieu@2789
   152
  for (NetDeviceContainer::Iterator i = d.Begin (); i != d.End (); ++i)
mathieu@2789
   153
    {
mathieu@2789
   154
      Ptr<NetDevice> dev = *i;
mathieu@2789
   155
      EnableAscii (os, dev->GetNode ()->GetId (), dev->GetIfIndex ());
mathieu@2789
   156
    }
mathieu@2789
   157
}
craigdo@4264
   158
mathieu@2789
   159
void
mathieu@2789
   160
PointToPointHelper::EnableAscii (std::ostream &os, NodeContainer n)
mathieu@2789
   161
{
mathieu@2789
   162
  NetDeviceContainer devs;
mathieu@2789
   163
  for (NodeContainer::Iterator i = n.Begin (); i != n.End (); ++i)
mathieu@2789
   164
    {
mathieu@2789
   165
      Ptr<Node> node = *i;
mathieu@2789
   166
      for (uint32_t j = 0; j < node->GetNDevices (); ++j)
mathieu@3786
   167
        {
mathieu@3786
   168
          devs.Add (node->GetDevice (j));
mathieu@3786
   169
        }
mathieu@2789
   170
    }
mathieu@2789
   171
  EnableAscii (os, devs);
mathieu@2786
   172
}
mathieu@2786
   173
mathieu@2786
   174
void
tomh@2996
   175
PointToPointHelper::EnableAsciiAll (std::ostream &os)
mathieu@2786
   176
{
mathieu@2789
   177
  EnableAscii (os, NodeContainer::GetGlobal ());
mathieu@2786
   178
}
mathieu@2786
   179
mathieu@2418
   180
NetDeviceContainer 
mathieu@2887
   181
PointToPointHelper::Install (NodeContainer c)
mathieu@2418
   182
{
mathieu@2418
   183
  NS_ASSERT (c.GetN () == 2);
mathieu@2887
   184
  return Install (c.Get (0), c.Get (1));
mathieu@2418
   185
}
craigdo@3793
   186
mathieu@2418
   187
NetDeviceContainer 
mathieu@2887
   188
PointToPointHelper::Install (Ptr<Node> a, Ptr<Node> b)
mathieu@2418
   189
{
mathieu@2418
   190
  NetDeviceContainer container;
mathieu@2418
   191
mathieu@2745
   192
  Ptr<PointToPointNetDevice> devA = m_deviceFactory.Create<PointToPointNetDevice> ();
mathieu@2656
   193
  devA->SetAddress (Mac48Address::Allocate ());
mathieu@2603
   194
  a->AddDevice (devA);
mathieu@2603
   195
  Ptr<Queue> queueA = m_queueFactory.Create<Queue> ();
craigdo@3010
   196
  devA->SetQueue (queueA);
mathieu@2745
   197
  Ptr<PointToPointNetDevice> devB = m_deviceFactory.Create<PointToPointNetDevice> ();
mathieu@2656
   198
  devB->SetAddress (Mac48Address::Allocate ());
mathieu@2603
   199
  b->AddDevice (devB);
mathieu@2603
   200
  Ptr<Queue> queueB = m_queueFactory.Create<Queue> ();
craigdo@3010
   201
  devB->SetQueue (queueB);
mathieu@2745
   202
  Ptr<PointToPointChannel> channel = m_channelFactory.Create<PointToPointChannel> ();
mathieu@2418
   203
  devA->Attach (channel);
mathieu@2418
   204
  devB->Attach (channel);
mathieu@2418
   205
  container.Add (devA);
mathieu@2418
   206
  container.Add (devB);
mathieu@2418
   207
mathieu@2418
   208
  return container;
mathieu@2418
   209
}
mathieu@2418
   210
craigdo@4140
   211
NetDeviceContainer 
craigdo@4140
   212
PointToPointHelper::Install (Ptr<Node> a, std::string bName)
craigdo@4140
   213
{
craigdo@4140
   214
  Ptr<Node> b = Names::Find<Node> (bName);
craigdo@4140
   215
  return Install (a, b);
craigdo@4140
   216
}
craigdo@4140
   217
craigdo@4140
   218
NetDeviceContainer 
craigdo@4140
   219
PointToPointHelper::Install (std::string aName, Ptr<Node> b)
craigdo@4140
   220
{
craigdo@4140
   221
  Ptr<Node> a = Names::Find<Node> (aName);
craigdo@4140
   222
  return Install (a, b);
craigdo@4140
   223
}
craigdo@4140
   224
craigdo@4140
   225
NetDeviceContainer 
craigdo@4140
   226
PointToPointHelper::Install (std::string aName, std::string bName)
craigdo@4140
   227
{
craigdo@4140
   228
  Ptr<Node> a = Names::Find<Node> (aName);
craigdo@4140
   229
  Ptr<Node> b = Names::Find<Node> (bName);
craigdo@4140
   230
  return Install (a, b);
craigdo@4140
   231
}
craigdo@4140
   232
mathieu@2786
   233
void 
craigdo@3793
   234
PointToPointHelper::InstallStar (Ptr<Node> hub, NodeContainer spokes, 
craigdo@3793
   235
                                 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
craigdo@3793
   236
{
craigdo@3793
   237
  for (uint32_t i = 0; i < spokes.GetN (); ++i)
craigdo@3793
   238
    {
craigdo@3793
   239
      NetDeviceContainer nd = Install (hub, spokes.Get (i));
craigdo@3793
   240
      hubDevices.Add (nd.Get (0));
craigdo@3793
   241
      spokeDevices.Add (nd.Get (1));
craigdo@3793
   242
    }
craigdo@3793
   243
}
craigdo@3793
   244
craigdo@3793
   245
void 
craigdo@4140
   246
PointToPointHelper::InstallStar (std::string hubName, NodeContainer spokes, 
craigdo@4140
   247
                                 NetDeviceContainer& hubDevices, NetDeviceContainer& spokeDevices)
craigdo@4140
   248
{
craigdo@4140
   249
  Ptr<Node> hub = Names::Find<Node> (hubName);
craigdo@4140
   250
  InstallStar (hub, spokes, hubDevices, spokeDevices);
craigdo@4140
   251
}
craigdo@4140
   252
craigdo@4140
   253
void 
craigdo@4263
   254
PointToPointHelper::SniffEvent (Ptr<PcapWriter> writer, Ptr<const Packet> packet)
mathieu@2786
   255
{
mathieu@2786
   256
  writer->WritePacket (packet);
mathieu@2786
   257
}
craigdo@4264
   258
mathieu@2786
   259
void 
tomh@2802
   260
PointToPointHelper::AsciiEnqueueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
mathieu@2786
   261
{
tomh@2802
   262
  *os << "+ " << Simulator::Now ().GetSeconds () << " ";
tomh@2802
   263
  *os << path << " " << *packet << std::endl;
tomh@2802
   264
}
craigdo@4264
   265
tomh@2802
   266
void 
tomh@2802
   267
PointToPointHelper::AsciiDequeueEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
tomh@2802
   268
{
tomh@2802
   269
  *os << "- " << Simulator::Now ().GetSeconds () << " ";
tomh@2802
   270
  *os << path << " " << *packet << std::endl;
tomh@2802
   271
}
craigdo@4264
   272
tomh@2802
   273
void 
tomh@2802
   274
PointToPointHelper::AsciiDropEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
tomh@2802
   275
{
tomh@2802
   276
  *os << "d " << Simulator::Now ().GetSeconds () << " ";
tomh@2802
   277
  *os << path << " " << *packet << std::endl;
tomh@2802
   278
}
craigdo@4264
   279
tomh@2802
   280
void 
tomh@2802
   281
PointToPointHelper::AsciiRxEvent (std::ostream *os, std::string path, Ptr<const Packet> packet)
tomh@2802
   282
{
tomh@2802
   283
  *os << "r " << Simulator::Now ().GetSeconds () << " ";
mathieu@2786
   284
  *os << path << " " << *packet << std::endl;
mathieu@2786
   285
}
mathieu@2786
   286
mathieu@2418
   287
} // namespace ns3