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 
43  CommandLine cmd (__FILE__);
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 }
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::LOG_LEVEL_ALL
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
third.channel
channel
Definition: third.py:92
ns3::LogComponentEnable
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
ns3::SimpleNetDevice::SetChannel
void SetChannel(Ptr< SimpleChannel > channel)
Attach a channel to this net device.
Definition: simple-net-device.cc:274
ns3::PacketSocketAddress
an address for a packet socket
Definition: packet-socket-address.h:39
ns3::PacketSocketClient::SetRemote
void SetRemote(PacketSocketAddress addr)
set the remote address and protocol to be used
Definition: packet-socket-client.cc:91
first.nodes
nodes
Definition: first.py:32
ns3::PacketSocketHelper::Install
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Definition: packet-socket-helper.cc:37
ns3::Ptr
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:74
ns3::SimpleNetDevice::GetAddress
virtual Address GetAddress(void) const
Definition: simple-net-device.cc:329
verbose
bool verbose
Definition: openflow-switch.cc:50
second.cmd
cmd
Definition: second.py:35
ns3::PacketSocketAddress::SetSingleDevice
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Definition: packet-socket-address.cc:46
ns3::PacketSocketHelper
Give ns3::PacketSocket powers to ns3::Node.
Definition: packet-socket-helper.h:32
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
ns3::SimpleNetDevice::SetNode
virtual void SetNode(Ptr< Node > node)
Definition: simple-net-device.cc:513
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::PacketSocketAddress::SetPhysicalAddress
void SetPhysicalAddress(const Address address)
Set the destination address.
Definition: packet-socket-address.cc:53
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::PacketSocketAddress::SetProtocol
void SetProtocol(uint16_t protocol)
Set the protocol.
Definition: packet-socket-address.cc:33
ns3::PacketMetadata::Enable
static void Enable(void)
Enable the packet metadata.
Definition: packet-metadata.cc:52
ns3::PacketSocketServer::SetLocal
void SetLocal(PacketSocketAddress addr)
set the local address and protocol to be used
Definition: packet-socket-server.cc:102
ns3::SimpleNetDevice::GetIfIndex
virtual uint32_t GetIfIndex(void) const
Definition: simple-net-device.cc:311