A Discrete-Event Network Simulator
API
packet-socket-apps.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014 Universita' di Firenze
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 // Network topology
22 //
23 // n0 n1
24 // | |
25 // =================
26 // SimpleChannel
27 //
28 // - Packets flows from n0 to n1
29 //
30 // This example shows how to use the PacketSocketServer and PacketSocketClient
31 // to send non-IP packets over a SimpleNetDevice
32 
33 #include "ns3/core-module.h"
34 #include "ns3/network-module.h"
35 
36 using namespace ns3;
37 
38 
39 int main (int argc, char *argv[])
40 {
41  bool verbose = false;
42 
44  cmd.AddValue ("verbose", "turn on log components", verbose);
45  cmd.Parse(argc, argv);
46 
47  if (verbose)
48  {
49  LogComponentEnable ("PacketSocketServer", LOG_LEVEL_ALL);
50  LogComponentEnable ("PacketSocketClient", LOG_LEVEL_ALL);
51  LogComponentEnable ("SimpleNetDevice", LOG_LEVEL_ALL);
52  }
53 
55  nodes.Create (2);
56 
58 
59  PacketSocketHelper packetSocket;
60 
61  // give packet socket powers to nodes.
62  packetSocket.Install (nodes);
63 
65  txDev = CreateObject<SimpleNetDevice> ();
66  nodes.Get (0)->AddDevice (txDev);
67 
69  rxDev = CreateObject<SimpleNetDevice> ();
70  nodes.Get (1)->AddDevice (rxDev);
71 
72  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel> ();
73  txDev->SetChannel (channel);
74  rxDev->SetChannel (channel);
75  txDev->SetNode (nodes.Get (0));
76  rxDev->SetNode (nodes.Get (1));
77 
78 
79  PacketSocketAddress socketAddr;
80  socketAddr.SetSingleDevice (txDev->GetIfIndex ());
81  socketAddr.SetPhysicalAddress (rxDev->GetAddress ());
82  // Arbitrary protocol type.
83  // Note: PacketSocket doesn't have any L4 multiplexing or demultiplexing
84  // The only mux/demux is based on the protocol field
85  socketAddr.SetProtocol (1);
86 
87  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient> ();
88  client->SetRemote (socketAddr);
89  nodes.Get (0)->AddApplication (client);
90 
91  Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer> ();
92  server->SetLocal (socketAddr);
93  nodes.Get (1)->AddApplication (server);
94 
95  Simulator::Run ();
97  return 0;
98 }
virtual Address GetAddress(void) const
tuple channel
Definition: third.py:85
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:157
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:73
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
an address for a packet socket
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
Give ns3::PacketSocket powers to ns3::Node.
tuple cmd
Definition: second.py:35
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
tuple nodes
Definition: first.py:25
virtual void SetNode(Ptr< Node > node)
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
Parse command-line arguments.
Definition: command-line.h:205
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
static void Enable(void)
Enable the packet metadata.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:128
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:498
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
void SetProtocol(uint16_t protocol)
Set the protocol.
Print everything.
Definition: log.h:112
void Parse(int argc, char *argv[])
Parse the program arguments.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
virtual uint32_t GetIfIndex(void) const
bool verbose