A Discrete-Event Network Simulator
API
wifi-ap.py
Go to the documentation of this file.
1# -*- Mode: Python; -*-
2# /*
3# * Copyright (c) 2005,2006,2007 INRIA
4# * Copyright (c) 2009 INESC Porto
5# *
6# * This program is free software; you can redistribute it and/or modify
7# * it under the terms of the GNU General Public License version 2 as
8# * published by the Free Software Foundation;
9# *
10# * This program is distributed in the hope that it will be useful,
11# * but WITHOUT ANY WARRANTY; without even the implied warranty of
12# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# * GNU General Public License for more details.
14# *
15# * You should have received a copy of the GNU General Public License
16# * along with this program; if not, write to the Free Software
17# * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18# *
19# * Authors: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
20# * Gustavo Carneiro <gjc@inescporto.pt>
21# */
22
23import sys
24
25import ns.applications
26import ns.core
27import ns.internet
28import ns.mobility
29import ns.network
30import ns.point_to_point
31import ns.wifi
32
33# void
34# DevTxTrace (std::string context, Ptr<const Packet> p, Mac48Address address)
35# {
36# std::cout << " TX to=" << address << " p: " << *p << std::endl;
37# }
38# void
39# DevRxTrace(std::string context, Ptr<const Packet> p, Mac48Address address)
40# {
41# std::cout << " RX from=" << address << " p: " << *p << std::endl;
42# }
43# void
44# PhyRxOkTrace(std::string context, Ptr<const Packet> packet, double snr, WifiMode mode, enum WifiPreamble preamble)
45# {
46# std::cout << "PHYRXOK mode=" << mode << " snr=" << snr << " " << *packet << std::endl;
47# }
48# void
49# PhyRxErrorTrace(std::string context, Ptr<const Packet> packet, double snr)
50# {
51# std::cout << "PHYRXERROR snr=" << snr << " " << *packet << std::endl;
52# }
53# void
54# PhyTxTrace(std::string context, Ptr<const Packet> packet, WifiMode mode, WifiPreamble preamble, uint8_t txPower)
55# {
56# std::cout << "PHYTX mode=" << mode << " " << *packet << std::endl;
57# }
58# void
59# PhyStateTrace(std::string context, Time start, Time duration, enum WifiPhy::State state)
60# {
61# std::cout << " state=";
62# switch(state) {
63# case WifiPhy::TX:
64# std::cout << "tx ";
65# break;
66# case WifiPhy::SYNC:
67# std::cout << "sync ";
68# break;
69# case WifiPhy::CCA_BUSY:
70# std::cout << "cca-busy";
71# break;
72# case WifiPhy::IDLE:
73# std::cout << "idle ";
74# break;
75# }
76# std::cout << " start="<<start<<" duration="<<duration<<std::endl;
77# }
78
79def SetPosition(node, position):
80 mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId())
81 mobility.SetPosition(position)
82
83
84def GetPosition(node):
85 mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId())
86 return mobility.GetPosition()
87
89 pos = GetPosition(node);
90 pos.x += 5.0
91 if pos.x >= 210.0:
92 return
93 SetPosition(node, pos)
94 ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, node)
95
96
97def main(argv):
98 ns.core.CommandLine().Parse(argv)
99
100 ns.network.Packet.EnablePrinting();
101
102 wifi = ns.wifi.WifiHelper()
103 mobility = ns.mobility.MobilityHelper()
104 stas = ns.network.NodeContainer()
105 ap = ns.network.NodeContainer()
106 #NetDeviceContainer staDevs;
107 packetSocket = ns.network.PacketSocketHelper()
108
109 stas.Create(2)
110 ap.Create(1)
111
112 # give packet socket powers to nodes.
113 packetSocket.Install(stas)
114 packetSocket.Install(ap)
115
116 wifiPhy = ns.wifi.YansWifiPhyHelper()
117 wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
118 wifiPhy.SetChannel(wifiChannel.Create())
119
120 ssid = ns.wifi.Ssid("wifi-default")
121 wifiMac = ns.wifi.WifiMacHelper()
122
123 # setup stas.
124 wifiMac.SetType("ns3::StaWifiMac",
125 "Ssid", ns.wifi.SsidValue(ssid))
126 staDevs = wifi.Install(wifiPhy, wifiMac, stas)
127 # setup ap.
128 wifiMac.SetType("ns3::ApWifiMac",
129 "Ssid", ns.wifi.SsidValue(ssid))
130 wifi.Install(wifiPhy, wifiMac, ap)
131
132 # mobility.
133 mobility.Install(stas)
134 mobility.Install(ap)
135
136 ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, ap.Get(0))
137
138 socket = ns.network.PacketSocketAddress()
139 socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
140 socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
141 socket.SetProtocol(1)
142
143 onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", ns.network.Address(socket))
144 onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
145
146 apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
147 apps.Start(ns.core.Seconds(0.5))
148 apps.Stop(ns.core.Seconds(43.0))
149
150 ns.core.Simulator.Stop(ns.core.Seconds(44.0))
151
152 # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
153 # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
154 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
155 # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
156 # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
157 # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
158
159
160 ns.core.Simulator.Run()
161 ns.core.Simulator.Destroy()
162
163 return 0
164
165
166if __name__ == '__main__':
167 sys.exit(main(sys.argv))
168
def GetPosition(node)
Definition: wifi-ap.py:84
def AdvancePosition(node)
Definition: wifi-ap.py:88
def SetPosition(node, position)
Definition: wifi-ap.py:79