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  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.Default()
117  wifiChannel = ns.wifi.YansWifiChannelHelper.Default()
118  wifiPhy.SetChannel(wifiChannel.Create())
119 
120  ssid = ns.wifi.Ssid("wifi-default")
121  wifi.SetRemoteStationManager("ns3::ArfWifiManager")
122  wifiMac = ns.wifi.WifiMacHelper()
123 
124  # setup stas.
125  wifiMac.SetType("ns3::StaWifiMac",
126  "Ssid", ns.wifi.SsidValue(ssid))
127  staDevs = wifi.Install(wifiPhy, wifiMac, stas)
128  # setup ap.
129  wifiMac.SetType("ns3::ApWifiMac",
130  "Ssid", ns.wifi.SsidValue(ssid),
131  "BeaconInterval", ns.core.TimeValue(ns.core.Seconds(2.5)))
132  wifi.Install(wifiPhy, wifiMac, ap)
133 
134  # mobility.
135  mobility.Install(stas)
136  mobility.Install(ap)
137 
138  ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, ap.Get(0))
139 
140  socket = ns.network.PacketSocketAddress()
141  socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
142  socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
143  socket.SetProtocol(1)
144 
145  onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", ns.network.Address(socket))
146  onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
147 
148  apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
149  apps.Start(ns.core.Seconds(0.5))
150  apps.Stop(ns.core.Seconds(43.0))
151 
152  ns.core.Simulator.Stop(ns.core.Seconds(44.0))
153 
154  # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
155  # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
156  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
157  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
158  # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
159  # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
160 
161 
162  ns.core.Simulator.Run()
163  ns.core.Simulator.Destroy()
164 
165  return 0
166 
167 
168 if __name__ == '__main__':
169  sys.exit(main(sys.argv))
170 
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