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 }
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:170
cmd
Definition: second.py:35
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
channel
Definition: third.py:85
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
nodes
Definition: first.py:25
virtual Address GetAddress(void) const
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:134
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 SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
void SetProtocol(uint16_t protocol)
Set the protocol.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Print everything.
Definition: log.h:115
virtual uint32_t GetIfIndex(void) const
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
bool verbose