Files
unison/src/network/examples/packet-socket-apps.cc

97 lines
2.8 KiB
C++
Raw Normal View History

/*
* Copyright (c) 2014 Universita' di Firenze
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
*/
// Network topology
//
// n0 n1
// | |
// =================
// SimpleChannel
//
// - Packets flows from n0 to n1
//
// This example shows how to use the PacketSocketServer and PacketSocketClient
// to send non-IP packets over a SimpleNetDevice
#include "ns3/core-module.h"
#include "ns3/network-module.h"
using namespace ns3;
2022-10-07 20:08:35 +00:00
int
main(int argc, char* argv[])
{
2022-10-07 20:08:35 +00:00
bool verbose = false;
2022-10-07 20:08:35 +00:00
CommandLine cmd(__FILE__);
cmd.AddValue("verbose", "turn on log components", verbose);
cmd.Parse(argc, argv);
2022-10-07 20:08:35 +00:00
if (verbose)
{
2022-10-07 20:08:35 +00:00
LogComponentEnable("PacketSocketServer", LOG_LEVEL_ALL);
LogComponentEnable("PacketSocketClient", LOG_LEVEL_ALL);
LogComponentEnable("SimpleNetDevice", LOG_LEVEL_ALL);
}
2022-10-07 20:08:35 +00:00
NodeContainer nodes;
nodes.Create(2);
2022-10-07 20:08:35 +00:00
ns3::PacketMetadata::Enable();
2022-10-07 20:08:35 +00:00
PacketSocketHelper packetSocket;
2022-10-07 20:08:35 +00:00
// give packet socket powers to nodes.
packetSocket.Install(nodes);
2022-10-07 20:08:35 +00:00
Ptr<SimpleNetDevice> txDev;
txDev = CreateObject<SimpleNetDevice>();
nodes.Get(0)->AddDevice(txDev);
2022-10-07 20:08:35 +00:00
Ptr<SimpleNetDevice> rxDev;
rxDev = CreateObject<SimpleNetDevice>();
nodes.Get(1)->AddDevice(rxDev);
2022-10-07 20:08:35 +00:00
Ptr<SimpleChannel> channel = CreateObject<SimpleChannel>();
txDev->SetChannel(channel);
rxDev->SetChannel(channel);
txDev->SetNode(nodes.Get(0));
rxDev->SetNode(nodes.Get(1));
2022-10-07 20:08:35 +00:00
PacketSocketAddress socketAddr;
socketAddr.SetSingleDevice(txDev->GetIfIndex());
socketAddr.SetPhysicalAddress(rxDev->GetAddress());
// Arbitrary protocol type.
// Note: PacketSocket doesn't have any L4 multiplexing or demultiplexing
// The only mux/demux is based on the protocol field
socketAddr.SetProtocol(1);
2022-10-07 20:08:35 +00:00
Ptr<PacketSocketClient> client = CreateObject<PacketSocketClient>();
client->SetRemote(socketAddr);
nodes.Get(0)->AddApplication(client);
2022-10-07 20:08:35 +00:00
Ptr<PacketSocketServer> server = CreateObject<PacketSocketServer>();
server->SetLocal(socketAddr);
nodes.Get(1)->AddApplication(server);
2022-10-07 20:08:35 +00:00
Simulator::Run();
Simulator::Destroy();
return 0;
}