-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathon_off.cc~
110 lines (87 loc) · 3.87 KB
/
on_off.cc~
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include"ns3/core-module.h"
#include"ns3/point-to-point-module.h"
#include"ns3/network-module.h"
#include"ns3/applications-module.h"
#include"ns3/netanim-module.h"
#include"ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include <sys/time.h>
#include <ctime>
using namespace ns3;
NS_LOG_COMPONENT_DEFINE("OnOffAppExp");
long getCurrentTime(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec*1000 + tv.tv_usec/1000;
}
int
main(int argc, char *argv[])
{
RngSeedManager::SetSeed(getCurrentTime());
RngSeedManager::SetRun(std::time(0));
Time::SetResolution (Time::NS);
LogComponentEnable("OnOffAppExp", LOG_LEVEL_INFO);
LogComponentEnable("PacketSink", LOG_LEVEL_INFO);
//LogComponentEnable("DataRate", LOG_LEVEL_INFO);
uint32_t nNodes = 2;
NodeContainer p2pNodes;
p2pNodes.Create(nNodes);
InternetStackHelper internet;
internet.Install(p2pNodes);
PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue("512Kbps"));
p2p.SetChannelAttribute("Delay", StringValue("0.5ms"));
NetDeviceContainer netdev;
netdev = p2p.Install(p2pNodes);
Ipv4AddressHelper address;
address.SetBase("192.168.1.0","255.255.255.0");
uint16_t port = 4000;
Ipv4InterfaceContainer intf;
intf = address.Assign(netdev);
MobilityHelper mobility;
//mobility.SetPositionAllocator("ns3::GridPositionAllocator",
// "MinX", DoubleValue(0.0),
// "MinY", DoubleValue(0.0),
// "DeltaX", DoubleValue(10.0),
// "DeltaY", DoubleValue(10.0),
// "GridWidth", UintegerValue(20),
// "LayoutType", StringValue("RowFirst"));
ConstantPositionMobilityModel cpmm = ConstantPositionMobilityModel();
ConstantPositionMobilityModel cpmm2 = ConstantPositionMobilityModel();
cpmm.SetPosition(Vector(0.0, 0.0, 0.0));
cpmm2.SetPosition(Vector(2.5, 2.5, 0.0));
//mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel", "Position", Vector3DValue(Vector3D(0.0, 0.0, 0)));
//mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.PushReferenceMobilityModel(&cpmm);
mobility.Install(p2pNodes.Get(0));
//mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel", "Position", Vector3DValue(Vector3D(0.0, 5.0, 0)));
//mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
//cpmm.SetPosition(Vector(2.5, 2.5, 0.0));
mobility.PushReferenceMobilityModel(&cpmm2);
mobility.Install(p2pNodes.Get(1));
OnOffHelper onOff("ns3::TcpSocketFactory", Address(InetSocketAddress(intf.GetAddress(1),port)));
onOff.SetAttribute("OnTime",StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
onOff.SetAttribute("OffTime",StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
onOff.SetAttribute("PacketSize", UintegerValue(1000));
onOff.SetAttribute("DataRate", StringValue("5kbps"));
onOff.SetAttribute("MaxBytes", UintegerValue(10000));
double simTime = 25.0;
ApplicationContainer app1 = onOff.Install(p2pNodes);
app1.Start(Seconds(1.0));
app1.Stop(Seconds(simTime));
PacketSinkHelper sinkHelper("ns3::TcpSocketFactory",Address(InetSocketAddress(Ipv4Address::GetAny(), port)));
ApplicationContainer app2 = sinkHelper.Install(p2pNodes.Get(1));
app2.Start(Seconds(0.0));
app2.Stop(Seconds(simTime));
Simulator::Stop(Seconds(simTime));
AnimationInterface anim("on_off.xml");
p2p.EnablePcapAll("./pcap/on_off");
NS_LOG_INFO("Begin simulation...");
//Ptr<Node> ptr_node = p2pNodes.Get(0);
//Ptr<PointToPointNetDevice> ptr_netdev = ptr_node->GetObject<PointToPointNetDevice>();
//Ptr<PointToPointChannel> ptr_channel = ptr_netdev->GetObject<PointToPointChannel>();
//Time delay = ptr_channel->GetDelay();
//std::cout<<"The point to point channel's delay is:"<<delay<<std::endl;
Simulator::Run();
Simulator::Destroy();
}