Skip to content

Instantly share code, notes, and snippets.

@SteveRuben
Created July 30, 2016 14:42
Show Gist options
  • Save SteveRuben/87a23536756a82dcecb04fb7469981d9 to your computer and use it in GitHub Desktop.
Save SteveRuben/87a23536756a82dcecb04fb7469981d9 to your computer and use it in GitHub Desktop.
Timer based coordination in ns3
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/ipv4-address.h"
#include "ns3/netanim-module.h"
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("Routing-Experience");
class MyTag : public Tag
{
public:
static TypeId GetTypeId (void);
virtual TypeId GetInstanceTypeId (void) const;
virtual uint32_t GetSerializedSize (void) const;
virtual void Serialize (TagBuffer i) const;
virtual void Deserialize (TagBuffer i);
virtual void Print (std::ostream &os) const;
// these are our accessors to our tag structure
void SetSimpleValue (uint8_t value);
uint8_t GetSimpleValue (void) const;
private:
uint8_t m_simpleValue;
};
TypeId
MyTag::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::MyTag")
.SetParent<Tag> ()
.AddConstructor<MyTag> ()
.AddAttribute ("SimpleValue",
"A simple value",
EmptyAttributeValue (),
MakeUintegerAccessor (&MyTag::GetSimpleValue),
MakeUintegerChecker<uint8_t> ())
;
return tid;
}
TypeId
MyTag::GetInstanceTypeId (void) const
{
return GetTypeId ();
}
uint32_t
MyTag::GetSerializedSize (void) const
{
return 1;
}
void
MyTag::Serialize (TagBuffer i) const
{
i.WriteU8 (m_simpleValue);
}
void
MyTag::Deserialize (TagBuffer i)
{
m_simpleValue = i.ReadU8 ();
}
void
MyTag::Print (std::ostream &os) const
{
os << "tagValue =" << (uint32_t)m_simpleValue;
}
void
MyTag::SetSimpleValue (uint8_t value)
{
m_simpleValue = value;
}
uint8_t
MyTag::GetSimpleValue (void) const
{
return m_simpleValue;
}
static inline std::string
PrintReceivedPacket (Ptr<Socket> socket, Ptr<Packet> packet, Address senderAddress)
{
std::ostringstream oss;
MyTag tagCopy;
packet->PeekPacketTag (tagCopy);
oss << Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId ();
//oss << Simulator::Now().GetMilliSeconds() << " " << socket->GetNode ()->GetId ();
if (InetSocketAddress::IsMatchingType (senderAddress))
{
InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
oss << " received one packet from " << addr.GetIpv4 () << " with tag equal to "<< (int)tagCopy.GetSimpleValue();
}
else
{
oss << " received one packet!";
}
return oss.str ();
}
static inline
void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
if (pktCount > 0)
{
Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("hello"), 5);
MyTag tag;
tag.SetSimpleValue(0x0);
pkt1->AddPacketTag(tag);
socket->Send (pkt1);
Simulator::Schedule (pktInterval, &GenerateTraffic,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
static inline
void GenerateTraffic0 (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
if (pktCount > 0)
{
Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("DATA"), 4);
MyTag tag;
tag.SetSimpleValue(0x10);
pkt1->AddPacketTag(tag);
socket->Send (pkt1);
Simulator::Schedule (pktInterval, &GenerateTraffic,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
static inline
void GenerateTraffic1 (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
if (pktCount > 0)
{
Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("hello"), 5);
MyTag tag;
tag.SetSimpleValue(0x56);
pkt1->AddPacketTag(tag);
Ptr<Packet> pkt2 = pkt1->Copy();
socket->Send (pkt2);
Simulator::Schedule (pktInterval, &GenerateTraffic1,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
static inline
void GenerateTraffic2 (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
if (pktCount > 0)
{
Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("hello"), 5);
MyTag tag;
tag.SetSimpleValue(0x86);
pkt1->AddPacketTag(tag);
Ptr<Packet> pkt2 = pkt1->Copy();
socket->Send (pkt2);
Simulator::Schedule (pktInterval, &GenerateTraffic1,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
static inline
void GenerateTraffic3 (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
if (pktCount > 0)
{
Ptr<Packet> pkt1 = Create<Packet> (reinterpret_cast<const uint8_t*> ("hello"), 5);
MyTag tag;
tag.SetSimpleValue(0x96);
pkt1->AddPacketTag(tag);
Ptr<Packet> pkt2 = pkt1->Copy();
socket->Send (pkt2);
Simulator::Schedule (pktInterval, &GenerateTraffic1,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
class RoutingExperiment
{
public :
RoutingExperiment();
void run();
private :
Ptr<Socket> SetupPacketReceive (Ipv4Address addr, Ptr<Node> node);
void SendMultiDestinations (Ptr<Node> sender, NodeContainer c);
NodeContainer GenerateNeighbors (NodeContainer c, uint32_t senderId);
void ReceivePacket (Ptr<Socket> socket); // node
void ReceivePacket1 (Ptr<Socket> socket);
void ReceivePacket2 (Ptr<Socket> socket);
void ReceivePacket3 (Ptr<Socket> socket);
void ReceivePacket4 (Ptr<Socket> socket);
void ReceivePacket5 (Ptr<Socket> socket);
void ReceivePacket6 (Ptr<Socket> socket);
void ReceivePacket7 (Ptr<Socket> socket);
int nNodes;
// static bool verbose = true;
NodeContainer nodes;
NodeContainer set1;
NodeContainer set2;
NodeContainer sink;
std::vector<uint32_t> tab1;
std::vector<uint32_t> tab0;
std::vector<uint32_t> tab2;
std::vector<uint32_t> tab3;
std::vector<uint32_t> tab4;
std::vector<uint32_t> tab5;
std::vector<uint32_t> tab6;
std::vector<uint32_t> tab7;
};
RoutingExperiment::RoutingExperiment(){};
void
RoutingExperiment::ReceivePacket(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab0.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
}
void
RoutingExperiment::ReceivePacket1(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab1.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
// node 1
Ptr<Socket> interm1 = Socket::CreateSocket (nodes.Get (4), tid);
InetSocketAddress local1 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm1->Bind (local1);
interm1->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket4,this));
// node 2
Ptr<Socket> interm2 = Socket::CreateSocket (nodes.Get (5), tid);
InetSocketAddress local2 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm2->Bind (local2);
interm2->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket5,this));
//node 3
Ptr<Socket> interm3 = Socket::CreateSocket (nodes.Get (6), tid);
InetSocketAddress local3 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm3->Bind (local3);
interm3->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket6,this));
std::cout << "/* message */" << std::endl;
InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetBroadcast(), 80);
Ptr<Socket> socket1 = Socket::CreateSocket (nodes.Get (1), tid);
socket1->SetAllowBroadcast(true);
socket1->Connect (remote);
Simulator::ScheduleWithContext (socket1->GetNode ()->GetId (),
Seconds (0.17), &GenerateTraffic1,
socket1, 1024, 1, Seconds (0.19));
}
void
RoutingExperiment::ReceivePacket2(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab2.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
Ipv4Address node1 ("10.2.1.2");
InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
if(addr.GetIpv4()==node1 && tab2.size()==2){
}else{
if(tab2.size()==2){
std::cout << "/* message */" << std::endl;
}
}
}
void
RoutingExperiment::ReceivePacket3(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab3.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
Ipv4Address node1 ("10.2.1.2");
Ipv4Address node2 ("10.2.1.3");
InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
if((addr.GetIpv4()==node1 || addr.GetIpv4()==node2)&&(tab3.size()==3)){
std::cout << "/* message */" << std::endl;
}
}
void
RoutingExperiment::ReceivePacket4(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab7.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
/*TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (7), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket7,this));
Ptr<Socket> socket1 = Socket::CreateSocket (nodes.Get (4), tid);
Ipv4Address dest = Ipv4Address("10.2.1.8");
InetSocketAddress remote = InetSocketAddress (dest, 80);
socket1->SetAllowBroadcast(true);
socket1->Connect (remote);
Simulator::ScheduleWithContext (socket1->GetNode ()->GetId (),
Seconds (0.39), &GenerateTraffic0,
socket1, 1024, 1, Seconds (0.41));*/
}
void
RoutingExperiment::ReceivePacket5(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab7.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
// node 1
Ptr<Socket> interm1 = Socket::CreateSocket (nodes.Get (4), tid);
InetSocketAddress local1 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm1->Bind (local1);
interm1->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket4,this));
// node 2
/* Ptr<Socket> interm2 = Socket::CreateSocket (nodes.Get (5), tid);
InetSocketAddress local2 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm2->Bind (local2);
interm2->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket5,this));*/
//node 3
Ptr<Socket> interm3 = Socket::CreateSocket (nodes.Get (6), tid);
InetSocketAddress local3 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm3->Bind (local3);
interm3->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket6,this));
Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (7), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket7,this));
Ptr<Socket> socket1 = Socket::CreateSocket (nodes.Get (5), tid);
Ipv4Address dest = Ipv4Address("10.2.1.8");
InetSocketAddress remote = InetSocketAddress (dest, 80);
socket1->SetAllowBroadcast(true);
socket1->Connect (remote);
Simulator::ScheduleWithContext (socket1->GetNode ()->GetId (),
Seconds (0.19), &GenerateTraffic0,
socket1, 1024, 1, Seconds (0.21));
}
void
RoutingExperiment::ReceivePacket6(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab7.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
}
void
RoutingExperiment::ReceivePacket7(Ptr<Socket> socket){
Ptr<Packet> packet; int bytesTotal=0,packetsReceived=0;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
MyTag tag;
packet->PeekPacketTag (tag);
tab7.push_back(tag.GetSimpleValue());
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
}
void
RoutingExperiment::run(/* arguments */) {
string phyMode("DsssRate1Mbps");
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("220"));
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("220")); // turn off RTS/CTS for frames below 2200 bytes
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode)); // Fix non-unicast data rate to be the same as that of unicast
double rss = -10; // -dBm
double interval = 1.0; // seconds
bool verbose=false;
uint32_t packetSize = 1024; // bytes
uint32_t numPackets = 1;
Time interPacketInterval = Seconds (interval);
NS_LOG_UNCOND ("Create nodes.");
nodes.Create(8);
/*set1 = NodeContainer (nodes.Get(0),nodes.Get(1),nodes.Get(2),nodes.Get(3));
set2 = NodeContainer (nodes.Get(1),nodes.Get(4),nodes.Get(5),nodes.Get(6));
sink = NodeContainer (nodes.Get(4),nodes.Get(7));*/
NS_LOG_UNCOND ("Setup wireless");
WifiHelper wifi;
if (verbose) {
wifi.EnableLogComponents();
}
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
// This is one parameter that matters when using FixedRssLossModel
// set it to zero; otherwise, gain will be added
wifiPhy.Set ("RxGain", DoubleValue (-10));
wifiPhy.Set ("TxGain", DoubleValue (10));
// ns-3 supports RadioTap and Prism tracing extensions for 802.11b
wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
// The below FixedRssLossModel will cause the rss to be fixed regardless
// of the distance between the two stations, and the transmit power
//wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
wifiChannel.AddPropagationLoss("ns3::RangePropagationLossModel", "MaxRange", DoubleValue(11.5));
wifiPhy.SetChannel (wifiChannel.Create ());
// Add a non-QoS upper mac, and disable rate control
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue (phyMode),
"ControlMode",StringValue (phyMode));
// Set it to adhoc mode
wifiMac.SetType ("ns3::AdhocWifiMac");
NS_LOG_UNCOND ("Setup devices");
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);/*
NetDeviceContainer devices2 = wifi.Install (wifiPhy, wifiMac, set2);
NetDeviceContainer devices3 = wifi.Install (wifiPhy, wifiMac, sink);*/
NS_LOG_UNCOND ("Setup mobility");
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
positionAlloc->Add (Vector (0.0, 0.0, 0.0));
positionAlloc->Add (Vector (0.0, 5.0, 0.0));
positionAlloc->Add (Vector (5.0, 0.0, 0.0));
positionAlloc->Add (Vector (5.0, 5.0, 0.0));
positionAlloc->Add (Vector (10.0, 5.0, 0.0));
positionAlloc->Add (Vector (5.0, 10.0, 0.0));
positionAlloc->Add (Vector (10.0, 10.0, 0.0));
positionAlloc->Add (Vector (15.0, 15.0, 0.0));
mobility.SetPositionAllocator (positionAlloc);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);
NS_LOG_UNCOND ("Setup Internet Stack");
InternetStackHelper internet;
internet.Install (nodes);
Ipv4AddressHelper ipv4;
NS_LOG_INFO ("Assign IP Addresses.");
ipv4.SetBase ("10.2.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (devices);/*
ipv4.SetBase ("10.2.2.0","255.255.255.0");
Ipv4InterfaceContainer ii = ipv4.Assign(devices2);
ipv4.SetBase ("10.2.3.0","255.255.255.0");
Ipv4InterfaceContainer iii = ipv4.Assign(devices3);*/
NS_LOG_UNCOND ("Begin Test: Testing " << numPackets << " packets sent with receiver rss " << rss );
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
NS_LOG_UNCOND ("Setup socket");
// node 1
Ptr<Socket> interm1 = Socket::CreateSocket (nodes.Get (1), tid);
InetSocketAddress local1 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm1->Bind (local1);
interm1->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket1,this));
// node 2
Ptr<Socket> interm2 = Socket::CreateSocket (nodes.Get (2), tid);
InetSocketAddress local2 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm2->Bind (local2);
interm2->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket2,this));
//node 3
Ptr<Socket> interm3 = Socket::CreateSocket (nodes.Get (3), tid);
InetSocketAddress local3 = InetSocketAddress (Ipv4Address::GetAny (), 80);
interm3->Bind (local3);
interm3->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket3,this));
Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), tid);
InetSocketAddress remote = InetSocketAddress (Ipv4Address("10.2.1.255"), 80);
source->SetAllowBroadcast (true);
source->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket,this));
source->Connect (remote);
Simulator::ScheduleWithContext (source->GetNode ()->GetId (),
Seconds (1.0), &GenerateTraffic,
source, packetSize, numPackets, interPacketInterval);
AnimationInterface anim ("timer.xml");
anim.SetConstantPosition (nodes.Get(0), 0, 0);
anim.SetConstantPosition (nodes.Get(0), 0, 0);
anim.UpdateNodeColor (nodes.Get(1),112,112,0);
anim.UpdateNodeColor (nodes.Get(2),112,112,0);
anim.UpdateNodeColor (nodes.Get(3),112,112,0);
anim.UpdateNodeColor (nodes.Get(7),0,0,0);
Simulator::Run ();
Simulator::Destroy ();
}
int
main(int argc, char *argv[]) {
CommandLine cmd;
cmd.Parse (argc, argv);
RoutingExperiment routing;//(5);
routing.run();
return 0;
}
@abuaseel
Copy link

well done job
as iam new to ns3
i have a question regarding network setup and routing used in this
appreciate if you can share that as to follow what have been done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment