c_cpp fourth.cc
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp fourth.cc相关的知识,希望对你有一定的参考价值。
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* 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
*
*/
/* Test program for multi-interface host, static routing
n0 n2-----n3
\ /
\ /
n1
*/
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "ns3/netanim-module.h"
#include "wifi-example-apps.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("SocketBoundRoutingExample");
void SendStuffAddNewTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
void SendStuffWithOldTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port,TimestampTag tag);
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
void srcSocketRecv (Ptr<Socket> socket);
void dstSocketRecv (Ptr<Socket> socket);
std::map<uint32_t,std::vector<Ipv4Address>> mp;
std::map<uint32_t,std::vector<TimestampTag>> tagmp;
int main (int argc, char *argv[])
{
// Allow the user to override any of the defaults and the above
// DefaultValue::Bind ()s at run-time, via command-line arguments
CommandLine cmd;
cmd.Parse (argc, argv);
Ptr<Node> n0 = CreateObject<Node> ();
Ptr<Node> n1= CreateObject<Node> ();
Ptr<Node> n2= CreateObject<Node> ();
Ptr<Node> n3= CreateObject<Node> ();
NodeContainer c = NodeContainer (n0,n1,n2,n3);
InternetStackHelper internet;
internet.Install (c);
// Point-to-point links
NodeContainer x0 = NodeContainer (n0,n1);
NodeContainer x1 = NodeContainer (n1,n2);
NodeContainer x2 = NodeContainer (n2,n3);
// We create the channels first without any IP addressing information
PointToPointHelper p2p;
p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
NetDeviceContainer d0= p2p.Install (x0);
NetDeviceContainer d1= p2p.Install (x1);
NetDeviceContainer d2= p2p.Install (x2);
// Ptr<NetDevice> SrcToRtr1=dSrcdRtr1.Get (0);
// Ptr<NetDevice> SrcToRtr2=dSrcdRtr2.Get (0);
// Later, we add IP addresses.
Ipv4AddressHelper ipv4;
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer interface0= ipv4.Assign (d0);
ipv4.NewNetwork ();
Ipv4InterfaceContainer interface1= ipv4.Assign (d1);
ipv4.NewNetwork ();
Ipv4InterfaceContainer interface2= ipv4.Assign (d2);
uint32_t id0=n0->GetId();
uint32_t id1=n1->GetId();
uint32_t id2=n2->GetId();
uint32_t id3=n3->GetId();
mp.insert(std::make_pair(id0,std::vector<Ipv4Address>{interface0.GetAddress(1)}));
mp.insert(std::make_pair(id1,std::vector<Ipv4Address>{interface0.GetAddress(0),interface1.GetAddress(1)}));
mp.insert(std::make_pair(id2,std::vector<Ipv4Address>{interface1.GetAddress(0),interface2.GetAddress(1)}));
mp.insert(std::make_pair(id3,std::vector<Ipv4Address>{interface2.GetAddress(0)}));
// Ptr<Ipv4> ipv4_0 =n0->GetObject<Ipv4> ();
// Ptr<Ipv4> ipv4_1=n1->GetObject<Ipv4> ();
// Ptr<Ipv4> ipv4_2=n2->GetObject<Ipv4> ();
// Ptr<Ipv4> ipv4_3=n3->GetObject<Ipv4> ();
// Ptr<Ipv4> ipv4_4=n4->GetObject<Ipv4> ();
// Ipv4StaticRoutingHelper ipv4RoutingHelper;
// Ptr<Ipv4StaticRouting> staticRoutingSrc = ipv4RoutingHelper.GetStaticRouting (ipv4_0);
// Ptr<Ipv4StaticRouting> staticRoutingRtr1 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr1);
// Ptr<Ipv4StaticRouting> staticRoutingRtr2 = ipv4RoutingHelper.GetStaticRouting (ipv4Rtr2);
// Ptr<Ipv4StaticRouting> staticRoutingDstRtr = ipv4RoutingHelper.GetStaticRouting (ipv4DstRtr);
// Ptr<Ipv4StaticRouting> staticRoutingDst = ipv4RoutingHelper.GetStaticRouting (ipv4Dst);
// Create static routes from Src to Dst
// staticRoutingRtr1->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.1.2"), 2);
// staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.10.2.2"), 2);
//
// // Two routes to same destination - setting separate metrics.
// // You can switch these to see how traffic gets diverted via different routes
// staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.1.2"), 1,5);
// staticRoutingSrc->AddHostRouteTo (Ipv4Address ("10.20.1.2"), Ipv4Address ("10.1.2.2"), 2,10);
// Creating static routes from DST to Source pointing to Rtr1 VIA Rtr2(!)
// staticRoutingDst->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.20.1.1"), 1);
// staticRoutingDstRtr->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.10.2.1"), 2);
// staticRoutingRtr2->AddHostRouteTo (Ipv4Address ("10.1.1.1"), Ipv4Address ("10.1.2.1"), 1);
//
// // There are no apps that can utilize the Socket Option so doing the work directly..
// // Taken from tcp-large-transfer example
Ptr<Socket> srcSocket = Socket::CreateSocket (n0, TypeId::LookupByName ("ns3::UdpSocketFactory"));
srcSocket->Bind ();
srcSocket->SetRecvCallback (MakeCallback (&dstSocketRecv));
Ptr<Socket> midSocket = Socket::CreateSocket (n1, TypeId::LookupByName ("ns3::UdpSocketFactory"));
uint16_t dstport = 12345;
Ipv4Address dstaddr ("0.0.0.0");
InetSocketAddress dst = InetSocketAddress (dstaddr, dstport);
midSocket->Bind (dst);
midSocket->SetRecvCallback (MakeCallback (&dstSocketRecv));
Ptr<Socket> dstSocket = Socket::CreateSocket (n2, TypeId::LookupByName ("ns3::UdpSocketFactory"));
dstSocket->Bind (dst);
dstSocket->SetRecvCallback (MakeCallback (&dstSocketRecv));
Ptr<Socket> dstSocket4 = Socket::CreateSocket (n3, TypeId::LookupByName ("ns3::UdpSocketFactory"));
dstSocket4->Bind (dst);
dstSocket4->SetRecvCallback (MakeCallback (&dstSocketRecv));
AsciiTraceHelper ascii;
p2p.EnableAsciiAll (ascii.CreateFileStream ("socket-bound-static-routing.tr"));
p2p.EnablePcapAll ("socket-bound-static-routing");
LogComponentEnableAll (LOG_PREFIX_TIME);
LogComponentEnable ("SocketBoundRoutingExample", LOG_LEVEL_INFO);
AnimationInterface anim ("bound.xml");
// First packet as normal (goes via Rtr1)
// todo
Simulator::Schedule (Seconds (0.1),&SendStuffAddNewTag, srcSocket,interface0.GetAddress(1), dstport);
// Second via Rtr1 explicitly
// Simulator::Schedule (Seconds (1.0),&BindSock, srcSocket, SrcToRtr1);
// Simulator::Schedule (Seconds ( 1.1),&SendStuff, srcSocket, dstaddr, dstport);
// Third via Rtr2 explicitly
// Simulator::Schedule (Seconds (2.0),&BindSock, srcSocket, SrcToRtr2);
// Simulator::Schedule (Seconds (2.1),&SendStuff, srcSocket, dstaddr, dstport);
// Fourth again as normal (goes via Rtr1)
// Simulator::Schedule (Seconds (3.0),&BindSock, srcSocket, Ptr<NetDevice>(0));
// Simulator::Schedule (Seconds (3.1),&SendStuff, srcSocket, dstaddr, dstport);
// If you uncomment what's below, it results in ASSERT failing since you can't
// bind to a socket not existing on a node
// Simulator::Schedule(Seconds(4.0),&BindSock, srcSocket, dDstRtrdDst.Get(0));
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
void SendStuffAddNewTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
{
Ptr<Packet> p = Create<Packet> ();
p->AddPaddingAtEnd (100);
TimestampTag timestamp;
timestamp.SetTimestamp (Simulator::Now ());
p->AddByteTag (timestamp);
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
return;
}
void SendStuffWithOldTag(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port,TimestampTag tag)
{
Ptr<Packet> p = Create<Packet> ();
p->AddPaddingAtEnd (100);
p->AddByteTag (tag);
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
return;
}
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev)
{
sock->BindToNetDevice (netdev);
return;
}
void
dstSocketRecv (Ptr<Socket> socket)
{
Address from;
Ptr<Packet> packet = socket->RecvFrom (from);
uint32_t id=socket->GetNode()->GetId();
std::vector<Ipv4Address> v=mp.find(id)->second;
ByteTagIterator it=packet-> GetByteTagIterator() ;
TimestampTag tag;
it.Next().GetTag(tag);
std::vector<TimestampTag> tagvector=tagmp.find(id)->second;
for(unsigned int i=0;i<tagvector.size();i++){
if(tag.Compare(tagvector[i])==0){
NS_LOG_INFO("The "<<id<<" node receive a packet with an old timestamptag");
return;
}
}
NS_LOG_INFO("The "<<id<<" node receive a packet with a NEW timestamptag");
tagvector.push_back(tag);
tagmp.insert(std::make_pair(id,tagvector));
for(unsigned int i=0;i<v.size();i++){
NS_LOG_INFO ("The "<<id<<" node send the "<<i<<" th"<<std::endl);
SendStuffWithOldTag (socket,v[i] ,12345, tag);
}
}
//----------------------------------------------------------------------
//-- TimestampTag
//------------------------------------------------------
TypeId
TimestampTag::GetTypeId (void)
{
static TypeId tid = TypeId ("TimestampTag")
.SetParent<Tag> ()
.AddConstructor<TimestampTag> ()
.AddAttribute ("Timestamp",
"Some momentous point in time!",
EmptyAttributeValue (),
MakeTimeAccessor (&TimestampTag::GetTimestamp),
MakeTimeChecker ())
;
return tid;
}
TypeId
TimestampTag::GetInstanceTypeId (void) const
{
return GetTypeId ();
}
uint32_t
TimestampTag::GetSerializedSize (void) const
{
return 8;
}
void
TimestampTag::Serialize (TagBuffer i) const
{
int64_t t = m_timestamp.GetNanoSeconds ();
i.Write ((const uint8_t *)&t, 8);
}
void
TimestampTag::Deserialize (TagBuffer i)
{
int64_t t;
i.Read ((uint8_t *)&t, 8);
m_timestamp = NanoSeconds (t);
}
void
TimestampTag::SetTimestamp (Time time)
{
m_timestamp = time;
}
Time
TimestampTag::GetTimestamp (void) const
{
return m_timestamp;
}
void
TimestampTag::Print (std::ostream &os) const
{
os << "t=" << m_timestamp;
}
int TimestampTag::Compare(TimestampTag& t)const
{
int x=m_timestamp.Compare(t.GetTimestamp());
if(x==0){
return 0;
}else{
return 1;
}
}
以上是关于c_cpp fourth.cc的主要内容,如果未能解决你的问题,请参考以下文章