lengyijun
1/1/2019 - 2:52 PM

triple_tcp.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------------n1-----------------n2
*/

#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/applications-module.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-list-routing-helper.h"
#include "wifi-example-apps.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("TripleTcp");

void SendStuff (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev);
void srcSocketRecv (Ptr<Socket> socket);
void dstSocketRecv (Ptr<Socket> socket);
void HandleAccept (Ptr<Socket> s, const Address& from);
void SendStuffAddNewTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port);
void SendStuffWithOldTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port,TimestampTag tag);
std::map<uint32_t,std::vector<TimestampTag>> tagmp;
std::map<uint32_t,std::vector<Ipv4Address>> ipv4neigh_mp;

const long blocksize=100;

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> ();

  // Point-to-point links
  NodeContainer nc= NodeContainer (n0,n1,n2);

  InternetStackHelper internet;
  internet.Install (nc);

  NodeContainer nc1= NodeContainer (n0,n1);
  NodeContainer nc2= NodeContainer (n1,n2);

  // We create the channels first without any IP addressing information
  PointToPointHelper p2p;
  p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  p2p.SetChannelAttribute ("Delay", StringValue ("2ms"));
  NetDeviceContainer nd1= p2p.Install (nc1);
  NetDeviceContainer nd2= p2p.Install (nc2);

  // Later, we add IP addresses.
  Ipv4AddressHelper ipv4;
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  Ipv4InterfaceContainer interface1= ipv4.Assign (nd1);
  ipv4.NewNetwork();
  Ipv4InterfaceContainer interface2= ipv4.Assign (nd2);

  uint16_t dstport = 12345;
  Ipv4Address dstaddr ("10.1.1.2");

  ipv4neigh_mp[n1->GetId()].push_back(Ipv4Address("10.1.2.2"));

  Ptr<Socket> srcSocket1 = Socket::CreateSocket (n0, TypeId::LookupByName ("ns3::TcpSocketFactory"));
  // srcSocket1->Bind();

   InetSocketAddress dst = InetSocketAddress (Ipv4Address::GetAny(), dstport);

   Ptr<Socket> midSocket = Socket::CreateSocket (n1, TypeId::LookupByName ("ns3::TcpSocketFactory"));
   midSocket->Bind (dst);
   midSocket->Listen();
   midSocket->SetAcceptCallback (
       MakeNullCallback<bool, Ptr<Socket>, const Address &> (),
       MakeCallback (&HandleAccept));

   Ptr<Socket> finSocket = Socket::CreateSocket (n2, TypeId::LookupByName ("ns3::TcpSocketFactory"));
   finSocket->Bind (dst);
   finSocket->Listen();

  p2p.EnablePcapAll ("socket-bound-tcp-static-routing");

  LogComponentEnableAll (LOG_PREFIX_TIME);
  LogComponentEnable ("TripleTcp", LOG_LEVEL_INFO);

  // First packet as normal (goes via Rtr1)
  NS_LOG_INFO ("Simulation start");
  Simulator::Schedule (Seconds (0.1),&SendStuffAddNewTag, srcSocket1, dstaddr, dstport);
  Simulator::Run ();
  Simulator::Destroy ();

  return 0;
} 

void HandleAccept (Ptr<Socket> s, const Address& from)
 {
    NS_LOG_INFO("HandleAccept");
    s->SetRecvCallback (MakeCallback (&dstSocketRecv));
  }

void BindSock (Ptr<Socket> sock, Ptr<NetDevice> netdev)
{
  sock->BindToNetDevice (netdev);
  return;
}

void SendStuffAddNewTag (Ptr<Socket> sock, Ipv4Address dstaddr, uint16_t port)
{
  NS_LOG_INFO ("SendStuffAddNewTag");
  Ptr<Packet> p = Create<Packet> ();
  p->AddPaddingAtEnd (100);
  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)
{
  Ptr<Packet> p = Create<Packet> ();
  p->AddPaddingAtEnd (blocksize);

  p->AddByteTag (tag);

  sock->Connect (InetSocketAddress (dstaddr,port)); 
  sock->SendTo (p, 0, InetSocketAddress (dstaddr,port));
  return;
}

void dstSocketRecv (Ptr<Socket> socket)
{
  Address from;
  Ptr<Packet> packet = socket->RecvFrom (from);
  uint32_t id=socket->GetNode()->GetId();

  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");
  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++){
    NS_LOG_INFO ("The "<<id<<" node send the "<<i<<" th"<<std::endl);
    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;
  }
}