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 
23 import sys
24 
25 import ns.applications
26 import ns.core
27 import ns.internet
28 import ns.mobility
29 import ns.network
30 import ns.point_to_point
31 import 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 
79 def SetPosition(node, position):
80  mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId())
81  mobility.SetPosition(position)
82 
83 
84 def GetPosition(node):
85  mobility = node.GetObject(ns.mobility.MobilityModel.GetTypeId())
86  return mobility.GetPosition()
87 
88 def AdvancePosition(node):
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 
97 def main(argv):
98  ns.core.CommandLine().Parse(argv)
99 
100  ns.network.Packet.EnablePrinting();
101 
102  # enable rts cts all the time.
103  ns.core.Config.SetDefault("ns3::WifiRemoteStationManager::RtsCtsThreshold", ns.core.StringValue("0"))
104  # disable fragmentation
105  ns.core.Config.SetDefault("ns3::WifiRemoteStationManager::FragmentationThreshold", ns.core.StringValue("2200"))
106 
107  wifi = ns.wifi.WifiHelper()
108  mobility = ns.mobility.MobilityHelper()
109  stas = ns.network.NodeContainer()
110  ap = ns.network.NodeContainer()
111  #NetDeviceContainer staDevs;
112  packetSocket = ns.network.PacketSocketHelper()
113 
114  stas.Create(2)
115  ap.Create(1)
116 
117  # give packet socket powers to nodes.
118  packetSocket.Install(stas)
119  packetSocket.Install(ap)
120 
121  wifiPhy = ns.wifi.YansWifiPhyHelper.Default()
122  wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
123  wifiPhy.SetChannel(wifiChannel.Create())
124 
125  ssid = ns.wifi.Ssid("wifi-default")
126  wifi.SetRemoteStationManager("ns3::ArfWifiManager")
127  wifiMac = ns.wifi.WifiMacHelper()
128 
129  # setup stas.
130  wifiMac.SetType("ns3::StaWifiMac",
131  "Ssid", ns.wifi.SsidValue(ssid))
132  staDevs = wifi.Install(wifiPhy, wifiMac, stas)
133  # setup ap.
134  wifiMac.SetType("ns3::ApWifiMac",
135  "Ssid", ns.wifi.SsidValue(ssid),
136  "BeaconInterval", ns.core.TimeValue(ns.core.Seconds(2.5)))
137  wifi.Install(wifiPhy, wifiMac, ap)
138 
139  # mobility.
140  mobility.Install(stas)
141  mobility.Install(ap)
142 
143  ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, ap.Get(0))
144 
145  socket = ns.network.PacketSocketAddress()
146  socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
147  socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
148  socket.SetProtocol(1)
149 
150  onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", ns.network.Address(socket))
151  onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
152 
153  apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
154  apps.Start(ns.core.Seconds(0.5))
155  apps.Stop(ns.core.Seconds(43.0))
156 
157  ns.core.Simulator.Stop(ns.core.Seconds(44.0))
158 
159  # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
160  # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
161  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
162  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
163  # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
164  # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
165 
166 
167  ns.core.Simulator.Run()
168  ns.core.Simulator.Destroy()
169 
170  return 0
171 
172 
173 if __name__ == '__main__':
174  sys.exit(main(sys.argv))
175 
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