A Discrete-Event Network Simulator
API
packet-socket-apps.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Universita' di Firenze
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18  */
19 
20 // Network topology
21 //
22 // n0 n1
23 // | |
24 // =================
25 // SimpleChannel
26 //
27 // - Packets flows from n0 to n1
28 //
29 // This example shows how to use the PacketSocketServer and PacketSocketClient
30 // to send non-IP packets over a SimpleNetDevice
31 
32 #include "ns3/core-module.h"
33 #include "ns3/network-module.h"
34 
35 using namespace ns3;
36 
37 int
38 main(int argc, char* argv[])
39 {
40  bool verbose = false;
41 
42  CommandLine cmd(__FILE__);
43  cmd.AddValue("verbose", "turn on log components", verbose);
44  cmd.Parse(argc, argv);
45 
46  if (verbose)
47  {
48  LogComponentEnable("PacketSocketServer", LOG_LEVEL_ALL);
49  LogComponentEnable("PacketSocketClient", LOG_LEVEL_ALL);
50  LogComponentEnable("SimpleNetDevice", LOG_LEVEL_ALL);
51  }
52 
54  nodes.Create(2);
55 
57 
58  PacketSocketHelper packetSocket;
59 
60  // give packet socket powers to nodes.
61  packetSocket.Install(nodes);
62 
64  txDev = CreateObject<SimpleNetDevice>();
65  nodes.Get(0)->AddDevice(txDev);
66 
68  rxDev = CreateObject<SimpleNetDevice>();
69  nodes.Get(1)->AddDevice(rxDev);
70 
71  Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
72  txDev->SetChannel(channel);
73  rxDev->SetChannel(channel);
74  txDev->SetNode(nodes.Get(0));
75  rxDev->SetNode(nodes.Get(1));
76 
77  PacketSocketAddress socketAddr;
78  socketAddr.SetSingleDevice(txDev->GetIfIndex());
79  socketAddr.SetPhysicalAddress(rxDev->GetAddress());
80  // Arbitrary protocol type.
81  // Note: PacketSocket doesn't have any L4 multiplexing or demultiplexing
82  // The only mux/demux is based on the protocol field
83  socketAddr.SetProtocol(1);
84 
85  Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient>();
86  client->SetRemote(socketAddr);
88 
89  Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer>();
90  server->SetLocal(socketAddr);
92 
95  return 0;
96 }
Parse command-line arguments.
Definition: command-line.h:232
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddDevice(Ptr< NetDevice > device)
Associate a NetDevice to this node.
Definition: node.cc:138
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
static void Enable()
Enable the packet metadata.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:302
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
cmd
Definition: second.py:40
channel
Definition: third.py:88
bool verbose