/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * see http://www.nsnam.org/bugzilla/show_bug.cgi?id=755 */ #include #include #include #include #include #include "ns3/core-module.h" #include "ns3/simulator-module.h" #include "ns3/node-module.h" #include "ns3/helper-module.h" #include "ns3/mobility-model.h" #include "ns3/v4ping.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("ARP_Assertion"); class TestTcp : public Object { public: static TypeId GetTypeId (void); TestTcp (); virtual ~TestTcp (); void Start (Ptr src, Ptr dst); void Stop (void); protected: virtual void DoDispose (void); private: void Send (void); void HandleRead (Ptr socket); void HandleAccept (Ptr s, const Address& from); void HandleConnectionSucceeded(Ptr s); void HandleConnectionFailed(Ptr s); void HandleConnectionCloseNormal(Ptr s); void HandleConnectionCloseError(Ptr s); void HandleDataSent(Ptr s, uint32_t i); Ptr m_socket_src; Ptr m_socket_dst; uint32_t m_seq; EventId m_sendEvent; }; NS_OBJECT_ENSURE_REGISTERED (TestTcp); TypeId TestTcp::GetTypeId (void) { static TypeId tid = TypeId ("ns3::TestTcp") .SetParent () .AddConstructor (); return tid; } TestTcp::TestTcp () { NS_LOG_FUNCTION_NOARGS (); } TestTcp::~TestTcp() { NS_LOG_FUNCTION_NOARGS (); } void TestTcp::DoDispose (void) { NS_LOG_FUNCTION_NOARGS (); Object::DoDispose (); } void TestTcp::Start (Ptr src, Ptr dst) { NS_LOG_FUNCTION_NOARGS (); if (m_socket_src == 0 && m_socket_dst == 0) { m_seq = 0; TypeId tid = TypeId::LookupByName ("ns3::TcpSocketFactory"); m_socket_src = Socket::CreateSocket (src, tid); m_socket_src->Bind (); Ipv4InterfaceAddress m_peerAddress = dst->GetObject () -> GetAddress(1, 0); NS_LOG_DEBUG("Starting sending from " << src << " to " << dst << "(" << m_peerAddress << ")"); m_socket_src->SetConnectCallback(MakeCallback(&TestTcp::HandleConnectionSucceeded, this), MakeCallback(&TestTcp::HandleConnectionFailed, this)); m_socket_src->SetCloseCallbacks(MakeCallback(&TestTcp::HandleConnectionCloseNormal, this), MakeCallback(&TestTcp::HandleConnectionCloseError, this)); m_socket_src->SetDataSentCallback(MakeCallback(&TestTcp::HandleDataSent, this)); // dst m_socket_dst = Socket::CreateSocket (dst, tid); m_socket_dst->Bind ( InetSocketAddress (Ipv4Address::GetAny (), 8888) ); m_socket_dst->SetRecvCallback(MakeCallback(&TestTcp::HandleRead, this)); m_socket_dst->Listen(); m_socket_dst->SetAcceptCallback ( MakeNullCallback, const Address &> (), MakeCallback(&TestTcp::HandleAccept, this)); m_socket_dst->SetCloseCallbacks(MakeCallback(&TestTcp::HandleConnectionCloseNormal, this), MakeCallback(&TestTcp::HandleConnectionCloseError, this)); int connectresult = m_socket_src->Connect (InetSocketAddress (m_peerAddress.GetLocal(), 8888)); NS_LOG_DEBUG("Connect() = " << connectresult << " errno="<< m_socket_src->GetErrno()); m_sendEvent = Simulator::Schedule(Seconds(1), &TestTcp::Send, this); } } void TestTcp::Stop () { NS_LOG_FUNCTION_NOARGS (); if(m_socket_src != 0 && m_socket_dst != 0){ Simulator::Cancel(m_sendEvent); m_socket_dst->SetRecvCallback(MakeNullCallback > ()); m_socket_src->Close(); m_socket_dst->Close(); m_socket_src = 0; m_socket_dst = 0; NS_LOG_DEBUG("TestTcp stopped"); } } void TestTcp::Send (void) { NS_LOG_FUNCTION_NOARGS (); NS_ASSERT (m_sendEvent.IsExpired ()); Ptr p = Create ((uint8_t*) &m_seq, 4); m_socket_src->Send (p); NS_LOG_INFO ("Sent data " << " t=" << Simulator::Now() << " m_seq=" << m_seq ); m_seq++; m_sendEvent = Simulator::Schedule(Seconds(10), &TestTcp::Send, this); } void TestTcp::HandleRead (Ptr socket) { Ptr packet; Address from; while (packet = socket->RecvFrom (from)) { if (InetSocketAddress::IsMatchingType (from)) { InetSocketAddress address = InetSocketAddress::ConvertFrom (from); uint32_t received_seq; packet->CopyData((uint8_t*) &received_seq, 4); NS_LOG_INFO ("Received " << packet->GetSize() << " bytes from " << address.GetIpv4() << " t=" << Simulator::Now() << " seq=" << received_seq); } } } void TestTcp::HandleAccept (Ptr s, const Address& from) { NS_LOG_DEBUG ("HANDLE_ACCEPT " << this << s << from); s->SetRecvCallback (MakeCallback(&TestTcp::HandleRead, this)); } void TestTcp::HandleConnectionSucceeded(Ptr s){ NS_LOG_DEBUG ("HANDLE_CONNECTION_SUCCEEDED"); } void TestTcp::HandleConnectionFailed(Ptr s){ NS_LOG_DEBUG ("HANDLE_CONNECTION_FAILED"); } void TestTcp::HandleConnectionCloseNormal(Ptr s){ NS_LOG_DEBUG ("HANDLE_CONNECTION_NORMAL"); } void TestTcp::HandleConnectionCloseError(Ptr s){ NS_LOG_DEBUG ("HANDLE_CONNECTION_CLOSE_ERROR"); } void TestTcp::HandleDataSent(Ptr s, uint32_t i){ NS_LOG_DEBUG ("HANDLE_DATA_SENT"); } /* topology */ typedef struct { const char* addr; int x; int y; } node_config; node_config node_configs[] = { { "10.12.133.225", 185, 0 }, //0 { "10.12.133.186", 500, 628 }, { "10.12.116.8", 498, 728 }, { "10.12.134.68", 197, 255 }, { "10.12.133.168", 114, 598 }, { "10.12.116.10", 306, 691 }, { "10.12.133.196", 37, 641 }, { "10.12.133.170", 138, 254 }, { "10.12.116.9", 335, 503 }, { "10.12.133.214", 639, 820 }, //9 { "10.12.133.185", 245, 390 }, { "10.12.133.156", 133, 498 }, { "10.12.133.241", 446, 816 }, { "10.12.133.208", 300, 546 }, { "10.12.116.27", 285, 497 }, { "10.12.133.199", 0, 407 }, { "10.12.116.19", 178, 355 }, { "10.12.133.222", 623, 768 }, //17 { "10.12.133.175", 199, 544 }, { "10.12.116.32", 414, 749 } }; int main (int argc, char *argv[]) { SeedManager::SetRun (3); std::string phyMode ("wifib-1mbs"); //std::string phyMode ("wifia-1mbs"); LogComponentEnable("ARP_Assertion", LOG_LEVEL_ALL ); //LogComponentEnable("ArpL3Protocol", LogLevel(LOG_LEVEL_ALL | LOG_PREFIX_TIME)); //LogComponentEnable("OlsrAgent", LogLevel(LOG_LEVEL_ALL|LOG_PREFIX_FUNC|LOG_PREFIX_TIME) ); //LogComponentEnable("OlsrAgent", LogLevel(LOG_LEVEL_INFO|LOG_PREFIX_TIME)); //LogComponentEnableAll( LogLevel(LOG_LEVEL_ALL|LOG_PREFIX_FUNC|LOG_PREFIX_TIME) ); // Set up some default values for the simulation. Use the Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200")); Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200")); Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode)); // 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); NS_LOG_INFO ("Create nodes."); NodeContainer nodes; nodes.Create (20); // Create channel netdevices phy WifiHelper wifi; YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); wifiPhy.Set("TxPowerStart", DoubleValue(24.0)); wifiPhy.Set("TxPowerEnd", DoubleValue(24.0)); wifiPhy.Set("RxGain", DoubleValue(4.0)); wifiPhy.SetPcapFormat (YansWifiPhyHelper::PCAP_FORMAT_80211_RADIOTAP); // CHANNEL YansWifiChannelHelper wifiChannel = YansWifiChannelHelper(); wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); wifiChannel.AddPropagationLoss ("ns3::LogDistancePropagationLossModel"); wifiChannel.AddPropagationLoss ("ns3::JakesPropagationLossModel", "DopplerFreq", DoubleValue(477.9)); wifiPhy.SetChannel (wifiChannel.Create ()); // MAC //wifi.SetStandard (WIFI_PHY_STANDARD_80211a); NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",StringValue(phyMode), "ControlMode",StringValue(phyMode)); wifiMac.SetType ("ns3::AdhocWifiMac"); // install on nodes NetDeviceContainer nodeDevices = wifi.Install (wifiPhy, wifiMac, nodes); //// MobilityHelper mobility; mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel"); Ptr positionAlloc = CreateObject(); for(int i=0; i<20; i++){ positionAlloc->Add(Vector(node_configs[i].x, node_configs[i].y, 0.0)); } mobility.SetPositionAllocator(positionAlloc); mobility.Install (nodes); //// NS_LOG_INFO ("Create stacks."); InternetStackHelper internet; OlsrHelper olsr; //Ipv4StaticRoutingHelper staticRouting; Ipv4ListRoutingHelper list; //list.Add (staticRouting, 0); list.Add (olsr, 10); internet.SetRoutingHelper (list); internet.Install (nodes); NS_LOG_INFO ("Set addresses"); for(int i=0; i<20; i++){ Ptr ptr_device = nodeDevices.Get(i); Ptr ptr_node = ptr_device->GetNode (); Ptr ptr_ipv4 = ptr_node->GetObject (); int32_t ifIndex = ptr_ipv4->AddInterface (ptr_device); ptr_ipv4->AddAddress(ifIndex, Ipv4InterfaceAddress(Ipv4Address(node_configs[i].addr), Ipv4Mask("255.0.0.0"))); ptr_ipv4->SetMetric (ifIndex, 1); ptr_ipv4->SetUp (ifIndex); //NS_LOG_INFO("i: "<< i << " ifIndex: " << ifIndex); } // Printing setup... for(int i=0; i<20; i++){ Ptr ptr_node = nodes.Get(i); Ptr ptr_ipv4 = ptr_node->GetObject (); Ptr ptr_mob = ptr_node->GetObject (); Ipv4InterfaceAddress adr = ptr_ipv4->GetAddress(1, 0); // ?? check params Vector pos = ptr_mob->GetPosition(); NS_LOG_INFO ( "Node " << i << ": " << adr << " : " << pos); } // Set up tcp from node17 (10.12.133.222) to node3 (10.12.134.68) Ptr testTcp = CreateObject(); Simulator::Schedule(Seconds(20), &TestTcp::Start, PeekPointer(testTcp), nodes.Get(17), nodes.Get(3)); Simulator::Schedule(Seconds(140), &TestTcp::Stop, PeekPointer(testTcp)); Simulator::Stop (Seconds (150)); NS_LOG_INFO ("Run Simulation."); Simulator::Run (); Simulator::Destroy (); NS_LOG_INFO ("Done."); return 0; }