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  wifi.Install(wifiPhy, wifiMac, ap)
132 
133  # mobility.
134  mobility.Install(stas)
135  mobility.Install(ap)
136 
137  ns.core.Simulator.Schedule(ns.core.Seconds(1.0), AdvancePosition, ap.Get(0))
138 
139  socket = ns.network.PacketSocketAddress()
140  socket.SetSingleDevice(staDevs.Get(0).GetIfIndex())
141  socket.SetPhysicalAddress(staDevs.Get(1).GetAddress())
142  socket.SetProtocol(1)
143 
144  onoff = ns.applications.OnOffHelper("ns3::PacketSocketFactory", ns.network.Address(socket))
145  onoff.SetConstantRate (ns.network.DataRate ("500kb/s"))
146 
147  apps = onoff.Install(ns.network.NodeContainer(stas.Get(0)))
148  apps.Start(ns.core.Seconds(0.5))
149  apps.Stop(ns.core.Seconds(43.0))
150 
151  ns.core.Simulator.Stop(ns.core.Seconds(44.0))
152 
153  # Config::Connect("/NodeList/*/DeviceList/*/Tx", MakeCallback(&DevTxTrace));
154  # Config::Connect("/NodeList/*/DeviceList/*/Rx", MakeCallback(&DevRxTrace));
155  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxOk", MakeCallback(&PhyRxOkTrace));
156  # Config::Connect("/NodeList/*/DeviceList/*/Phy/RxError", MakeCallback(&PhyRxErrorTrace));
157  # Config::Connect("/NodeList/*/DeviceList/*/Phy/Tx", MakeCallback(&PhyTxTrace));
158  # Config::Connect("/NodeList/*/DeviceList/*/Phy/State", MakeCallback(&PhyStateTrace));
159 
160 
161  ns.core.Simulator.Run()
162  ns.core.Simulator.Destroy()
163 
164  return 0
165 
166 
167 if __name__ == '__main__':
168  sys.exit(main(sys.argv))
169 
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