2017-08-17 21:29:19 +01:00
|
|
|
"""
|
|
|
|
ns3wifirandomwalk.py - This script demonstrates using CORE with the ns-3 Wifi
|
2013-08-29 15:21:13 +01:00
|
|
|
model and random walk mobility.
|
|
|
|
Patterned after the ns-3 example 'main-random-walk.cc'.
|
|
|
|
|
|
|
|
How to run this:
|
|
|
|
|
|
|
|
pushd ~/ns-allinone-3.16/ns-3.16
|
|
|
|
sudo ./waf shell
|
|
|
|
popd
|
|
|
|
python -i ns3wifirandomwalk.py
|
2017-08-17 21:29:19 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
import optparse
|
|
|
|
import sys
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
import ns.core
|
|
|
|
import ns.network
|
2017-08-17 21:29:19 +01:00
|
|
|
|
|
|
|
from core import logger
|
|
|
|
from core.misc import ipaddress
|
|
|
|
from corens3.obj import Ns3Session
|
|
|
|
from corens3.obj import Ns3WifiNet
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
def add_to_server(session):
|
2017-08-17 21:29:19 +01:00
|
|
|
"""
|
|
|
|
Add this session to the server's list if this script is executed from
|
2013-08-29 15:21:13 +01:00
|
|
|
the core-daemon server.
|
2017-08-17 21:29:19 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
global server
|
|
|
|
try:
|
2017-08-17 23:25:12 +01:00
|
|
|
server.add_session(session)
|
2013-08-29 15:21:13 +01:00
|
|
|
return True
|
|
|
|
except NameError:
|
|
|
|
return False
|
2017-08-17 21:29:19 +01:00
|
|
|
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def wifisession(opt):
|
2017-08-17 21:29:19 +01:00
|
|
|
"""
|
|
|
|
Run a random walk wifi session.
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
session = Ns3Session(persistent=True, duration=opt.duration)
|
|
|
|
session.name = "ns3wifirandomwalk"
|
|
|
|
session.filename = session.name + ".py"
|
|
|
|
session.node_count = str(opt.numnodes + 1)
|
|
|
|
add_to_server(session)
|
2017-08-17 21:29:19 +01:00
|
|
|
wifi = session.add_object(cls=Ns3WifiNet, name="wlan1", rate="OfdmRate12Mbps")
|
2013-08-29 15:21:13 +01:00
|
|
|
wifi.setposition(30, 30, 0)
|
|
|
|
# for improved connectivity
|
|
|
|
wifi.phy.Set("RxGain", ns.core.DoubleValue(18.0))
|
|
|
|
|
2017-08-17 21:29:19 +01:00
|
|
|
prefix = ipaddress.Ipv4Prefix("10.0.0.0/16")
|
2017-01-13 14:33:48 +00:00
|
|
|
services_str = "zebra|OSPFv3MDR|IPForward"
|
2013-08-29 15:21:13 +01:00
|
|
|
nodes = []
|
|
|
|
for i in xrange(1, opt.numnodes + 1):
|
2017-08-17 21:29:19 +01:00
|
|
|
node = session.addnode(name="n%d" % i)
|
2013-08-29 15:21:13 +01:00
|
|
|
node.newnetif(wifi, ["%s/%s" % (prefix.addr(i), prefix.prefixlen)])
|
|
|
|
nodes.append(node)
|
2017-08-17 21:29:19 +01:00
|
|
|
session.services.addservicestonode(node, "router", services_str)
|
2013-08-29 15:21:13 +01:00
|
|
|
session.services.bootnodeservices(node)
|
|
|
|
session.setuprandomwalkmobility(bounds=(1000.0, 750.0, 0))
|
|
|
|
|
|
|
|
# PHY tracing
|
2017-08-17 21:29:19 +01:00
|
|
|
# wifi.phy.EnableAsciiAll("ns3wifirandomwalk")
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
# mobility tracing
|
2017-08-17 21:29:19 +01:00
|
|
|
# session.setupmobilitytracing(wifi, "ns3wifirandomwalk.mob.tr",
|
2013-08-29 15:21:13 +01:00
|
|
|
# nodes, verbose=True)
|
|
|
|
session.startns3mobility(refresh_ms=150)
|
|
|
|
|
|
|
|
# start simulation
|
|
|
|
# session.instantiate() ?
|
|
|
|
session.thread = session.run(vis=opt.viz)
|
|
|
|
return session
|
|
|
|
|
|
|
|
|
2017-08-17 21:29:19 +01:00
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Main routine when running from command-line.
|
|
|
|
"""
|
|
|
|
usagestr = "usage: %prog [-h] [options] [args]"
|
|
|
|
parser = optparse.OptionParser(usage=usagestr)
|
|
|
|
parser.set_defaults(numnodes=5, duration=600, verbose=False, viz=False)
|
|
|
|
opt = {'numnodes': 5, 'duration': 600, 'verbose': False, 'viz': False}
|
|
|
|
|
|
|
|
parser.add_option("-d", "--duration", dest="duration", type=int,
|
|
|
|
help="number of seconds to run the simulation")
|
|
|
|
parser.add_option("-n", "--numnodes", dest="numnodes", type=int,
|
|
|
|
help="number of nodes")
|
|
|
|
parser.add_option("-v", "--verbose", dest="verbose",
|
|
|
|
action="store_true", help="be more verbose")
|
|
|
|
parser.add_option("-V", "--visualize", dest="viz",
|
|
|
|
action="store_true", help="enable PyViz ns-3 visualizer")
|
|
|
|
|
|
|
|
def usage(msg=None, err=0):
|
2013-08-29 15:21:13 +01:00
|
|
|
sys.stdout.write("\n")
|
|
|
|
if msg:
|
|
|
|
sys.stdout.write(msg + "\n\n")
|
|
|
|
parser.print_help()
|
|
|
|
sys.exit(err)
|
|
|
|
|
2017-08-17 21:29:19 +01:00
|
|
|
opt, args = parser.parse_args()
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
if opt.numnodes < 2:
|
|
|
|
usage("invalid numnodes: %s" % opt.numnodes)
|
|
|
|
|
|
|
|
for a in args:
|
2017-08-17 21:29:19 +01:00
|
|
|
logger.warn("ignoring command line argument: '%s'", a)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
return wifisession(opt)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__" or __name__ == "__builtin__":
|
|
|
|
session = main()
|
2017-08-17 21:29:19 +01:00
|
|
|
logger.info("\nsession =%s", session)
|