/* -*- 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
*
* This program conducts a simple experiment: It builds up a topology based on
* either Inet or Orbis trace files. A random node is then chosen, and all the
* other nodes will send a packet to it. The TTL is measured and reported as an histogram.
*
*/
#include <ctime>
#include <sstream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-nix-vector-helper.h"
#include "ns3/topology-read-module.h"
#include <list>
#include "wifi-example-apps.h"
#include "ns3/netanim-module.h"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("TopologyCreationExperiment");
const long blocksize=1024;
const long totalsize=256*1024; //must be multiply of 512
long count;
TimestampTag prefix;
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 dstSocketRecv (Ptr<Socket> socket);
void HandleAccept (Ptr<Socket> s, const Address& from);
std::map<uint32_t,std::vector<Ipv4Address>> ipv4neigh_mp;
std::map<uint32_t,std::vector<TimestampTag>> tagmp;
int main (int argc, char *argv[])
{
count=totalsize/blocksize-1;
prefix.SetTimestamp (Simulator::Now ());
std::string format ("Inet");
// std::string input ("src/topology-read/examples/double.txt"); //2
std::string input ("src/topology-read/examples/tree.txt"); //10
// std::string input ("src/topology-read/examples/output.txt"); //1000
// std::string input ("src/topology-read/examples/Inet_toposample.txt"); //4000
// Set up command line parameters used to control the experiment.
CommandLine cmd;
cmd.AddValue ("format", "Format to use for data input [Orbis|Inet|Rocketfuel].",
format);
cmd.AddValue ("input", "Name of the input file.",
input);
cmd.Parse (argc, argv);
// ------------------------------------------------------------
// -- Read topology data.
// --------------------------------------------
// Pick a topology reader based in the requested format.
TopologyReaderHelper topoHelp;
topoHelp.SetFileName (input);
topoHelp.SetFileType (format);
Ptr<TopologyReader> inFile = topoHelp.GetTopologyReader ();
NodeContainer nodes;
if (inFile != 0)
{
nodes = inFile->Read ();
}
if (inFile->LinksSize () == 0)
{
NS_LOG_ERROR ("Problems reading the topology file. Failing.");
return -1;
}
// ------------------------------------------------------------
// -- Create nodes and network stacks
// --------------------------------------------
NS_LOG_INFO ("creating internet stack");
InternetStackHelper stack;
// Setup NixVector Routing
Ipv4NixVectorHelper nixRouting;
stack.SetRoutingHelper (nixRouting); // has effect on the next Install ()
stack.Install (nodes);
NS_LOG_INFO ("creating ip4 addresses");
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.255.255.252");
int totlinks = inFile->LinksSize ();
NS_LOG_INFO ("creating node containers");
NodeContainer* nc = new NodeContainer[totlinks];
TopologyReader::ConstLinksIterator iter;
int i = 0;
for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
{
nc[i] = NodeContainer (iter->GetFromNode (), iter->GetToNode ());
}
NS_LOG_INFO ("creating net device containers");
NetDeviceContainer* ndc = new NetDeviceContainer[totlinks];
PointToPointHelper p2p;
for (int i = 0; i < totlinks; i++)
{
// p2p.SetChannelAttribute ("Delay", TimeValue(MilliSeconds(weight[i])));
p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
// p2p.SetDeviceAttribute ("Mtu", UintegerValue (65536));
ndc[i] = p2p.Install (nc[i]);
}
// it crates little subnets, one for each couple of nodes.
NS_LOG_INFO ("creating ipv4 interfaces");
Ipv4InterfaceContainer* ipic = new Ipv4InterfaceContainer[totlinks];
for (int i = 0; i < totlinks; i++)
{
ipic[i] = address.Assign (ndc[i]);
address.NewNetwork ();
}
NS_LOG_INFO ("creating ipv4neigh_mp");
i=0;
for ( iter = inFile->LinksBegin (); iter != inFile->LinksEnd (); iter++, i++ )
{
uint32_t from=iter->GetFromNode()->GetId();
uint32_t to=iter->GetToNode()->GetId();
std::map<uint32_t,std::vector<Ipv4Address>>::iterator it= ipv4neigh_mp.find(from);
if (it==ipv4neigh_mp.end()){
// std::cout<<"creating "<<from<<std::endl;
std::vector<Ipv4Address> v;
v.push_back(ipic[i].GetAddress(1));
// std::cout<<"length "<<v.size()<<std::endl;
ipv4neigh_mp.insert(std::make_pair(from,v));
}else{
// std::cout<<"appending "<<from<<std::endl;
ipv4neigh_mp[from].push_back(ipic[i].GetAddress(1));
}
std::map<uint32_t,std::vector<Ipv4Address>>::iterator it1= ipv4neigh_mp.find(to);
if (it1==ipv4neigh_mp.end()){
// std::cout<<"creating "<<to<<std::endl;
std::vector<Ipv4Address> v;
v.push_back(ipic[i].GetAddress(0));
// std::cout<<"length "<<v.size()<<std::endl;
ipv4neigh_mp.insert(std::make_pair(to,v));
}else{
// std::cout<<"appending "<<to<<std::endl;
ipv4neigh_mp[to].push_back(ipic[i].GetAddress(0));
}
}
// std::map<uint32_t,std::vector<Ipv4Address>>::iterator it;
// for(it=ipv4neigh_mp.begin();it!=ipv4neigh_mp.end();it++){
// std::cout<<it->second.size()<<std::endl;
// }
Ptr<Socket> srcSocket = Socket::CreateSocket (nodes.Get(0), TypeId::LookupByName ("ns3::TcpSocketFactory"));
// srcSocket->Bind ();
// srcSocket->SetRecvCallback (MakeCallback (&dstSocketRecv));
uint16_t dstport = 12345;
InetSocketAddress dst = InetSocketAddress (Ipv4Address::GetAny(), dstport);
for ( unsigned int i = 1; i < nodes.GetN (); i++ )
{
Ptr<Socket> dstSocket = Socket::CreateSocket (nodes.Get(i), TypeId::LookupByName ("ns3::TcpSocketFactory"));
dstSocket->Bind (dst);
dstSocket->Listen();
dstSocket->SetAcceptCallback (
MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
MakeCallback (&HandleAccept)
);
}
// AnimationInterface anim ("animation.xml");
p2p.EnablePcapAll ("socket-bound-static-routing");
Simulator::Schedule (Seconds (0.1),&SendStuffAddNewTag, srcSocket,ipic[0].GetAddress(1), dstport);
NS_LOG_INFO ("Run Simulation.");
Simulator::Run ();
Simulator::Destroy ();
delete[] ipic;
delete[] ndc;
delete[] nc;
NS_LOG_INFO ("Done.");
return 0;
}
void HandleAccept (Ptr<Socket> s, const Address& from)
{
// NS_LOG_INFO("HandleAccept");
s->SetRecvCallback (MakeCallback (&dstSocketRecv));
}
void SendStuffAddNewTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
{
for(int i=0;i<count;i++){
NS_LOG_INFO("Send");
Ptr<Packet> p = Create<Packet> ();
p->AddPaddingAtEnd (blocksize);
p->AddByteTag (prefix);
sock->Connect (InetSocketAddress (dstaddr,port));
std::cout<<"GetTxAvailable: "<<sock->GetTxAvailable()<<std::endl;
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
}
Ptr<Packet> p = Create<Packet> ();
p->AddPaddingAtEnd (blocksize);
TimestampTag timestamp;
timestamp.SetTimestamp (Simulator::Now ());
p->AddByteTag (timestamp);
sock->Connect (InetSocketAddress (dstaddr,port));
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
sock->Close();
return;
}
void SendStuffWithOldTag(Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port,TimestampTag tag)
{
for(int i=0;i<count;i++){
Ptr<Packet> p = Create<Packet> ();
p->AddPaddingAtEnd (blocksize);
p->AddByteTag (prefix);
sock->Connect (InetSocketAddress (dstaddr,port));
std::cout<<"GetTxAvailable: "<<sock->GetTxAvailable()<<std::endl;
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
}
Ptr<Packet> p = Create<Packet> ();
p->AddPaddingAtEnd (blocksize);
p->AddByteTag (tag);
sock->Connect (InetSocketAddress (dstaddr,port));
sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
sock->Close();
return;
}
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev)
{
sock->BindToNetDevice (netdev);
return;
}
void dstSocketRecv (Ptr<Socket> socket)
{
// NS_LOG_INFO("dstSocketRecv");
Address from;
Ptr<Packet> packet = socket->RecvFrom (from);
ByteTagIterator it=packet-> GetByteTagIterator() ;
TimestampTag tag;
it.Next().GetTag(tag);
if(tag.Compare(prefix)==0){
NS_LOG_INFO("The node receive prefix");
return;
}
uint32_t id=socket->GetNode()->GetId();
Ipv4Address ipv4from=InetSocketAddress::ConvertFrom (from).GetIpv4 ();
std::map<uint32_t,std::vector<TimestampTag>>::iterator tagit= tagmp.find(id);
if(tagit!=tagmp.end()){
std::vector<TimestampTag> tagvector= tagit->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(Simulator::Now()<<": The "<<id<<" node receive a packet with a NEW timestamptag");
tagmp[id].push_back(tag);
std::vector<Ipv4Address> v=ipv4neigh_mp.find(id)->second;
Ptr<Node> n=socket->GetNode();
for(unsigned int i=0;i<v.size();i++){
if(v[i].IsEqual(ipv4from)){
NS_LOG_INFO("Dont send back");
continue;
}
NS_LOG_INFO ("The "<<id<<" node send the "<<i<<" th");
Ptr<Socket> socket1 = Socket::CreateSocket (n, TypeId::LookupByName ("ns3::TcpSocketFactory"));
SendStuffWithOldTag (socket1,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;
}
}