python examples, removed params common local module to avoid confusion, clean things up a bit and added a module doc to help explain the file

This commit is contained in:
Blake Harnden 2020-04-18 08:11:12 -07:00
parent 7da7ea5d62
commit d659a5c139
5 changed files with 57 additions and 93 deletions

View file

@ -1,14 +1,20 @@
import datetime
"""
This is a standalone script to run a small EMANE scenario and will not interact
with the GUI. You also must have installed OSPF MDR as noted in the documentation
installation page.
"""
import logging
import parser
from core.emane.ieee80211abg import EmaneIeee80211abgModel
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import EventTypes, NodeTypes
NODES = 2
def example(args):
def main():
# ip generator for example
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
@ -19,7 +25,8 @@ def example(args):
# must be in configuration state for nodes to start, when using "node_add" below
session.set_state(EventTypes.CONFIGURATION_STATE)
# create emane network node
# create emane network node, emane determines connectivity based on
# location, so the session and nodes must be configured to provide one
session.set_location(47.57917, -122.13232, 2.00000, 1.0)
options = NodeOptions()
options.set_position(80, 50)
@ -28,7 +35,7 @@ def example(args):
# create nodes
options = NodeOptions(model="mdr")
for i in range(args.nodes):
for i in range(NODES):
node = session.add_node(options=options)
node.setposition(x=150 * (i + 1), y=150)
interface = prefixes.create_interface(node)
@ -42,16 +49,6 @@ def example(args):
coreemu.shutdown()
def main():
logging.basicConfig(level=logging.INFO)
args = parser.parse("emane80211")
start = datetime.datetime.now()
logging.info(
"running emane 80211 example: nodes(%s) time(%s)", args.nodes, args.time
)
example(args)
logging.info("elapsed time: %s", datetime.datetime.now() - start)
if __name__ == "__main__" or __name__ == "__builtin__":
logging.basicConfig(level=logging.INFO)
main()

View file

@ -1,32 +0,0 @@
import argparse
DEFAULT_NODES = 2
DEFAULT_TIME = 10
DEFAULT_STEP = 1
def parse(name):
parser = argparse.ArgumentParser(description=f"Run {name} example")
parser.add_argument(
"-n",
"--nodes",
type=int,
default=DEFAULT_NODES,
help="number of nodes to create in this example",
)
parser.add_argument(
"-c",
"--count",
type=int,
default=DEFAULT_TIME,
help="number of time to ping node",
)
args = parser.parse_args()
if args.nodes < 2:
parser.error(f"invalid min number of nodes: {args.nodes}")
if args.count < 1:
parser.error(f"invalid ping count({args.count}), count must be greater than 0")
return args

View file

@ -1,13 +1,18 @@
import logging
import time
"""
This is a standalone script to run a small switch based scenario and will not
interact with the GUI.
"""
import logging
import params
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes
from core.emulator.enumerations import EventTypes, NodeTypes
NODES = 2
def example(args):
def main():
# ip generator for example
prefixes = IpPrefixes(ip4_prefix="10.83.0.0/16")
@ -19,10 +24,10 @@ def example(args):
session.set_state(EventTypes.CONFIGURATION_STATE)
# create switch network node
switch = session.add_node(_type=NodeTypes.SWITCH)
switch = session.add_node(_type=NodeTypes.SWITCH, _id=100)
# create nodes
for _ in range(args.nodes):
for _ in range(NODES):
node = session.add_node()
interface = prefixes.create_interface(node)
session.add_link(node.id, switch.id, interface_one=interface)
@ -31,27 +36,17 @@ def example(args):
session.instantiate()
# get nodes to run example
first_node = session.get_node(2)
last_node = session.get_node(args.nodes + 1)
first_node_address = prefixes.ip4_address(first_node)
logging.info("node %s pinging %s", last_node.name, first_node_address)
output = last_node.cmd(f"ping -c {args.count} {first_node_address}")
first_node = session.get_node(1)
last_node = session.get_node(NODES)
address = prefixes.ip4_address(first_node)
logging.info("node %s pinging %s", last_node.name, address)
output = last_node.cmd(f"ping -c 3 {address}")
logging.info(output)
# shutdown session
coreemu.shutdown()
def main():
logging.basicConfig(level=logging.INFO)
args = params.parse("switch")
start = time.perf_counter()
logging.info(
"running switch example: nodes(%s) ping count(%s)", args.nodes, args.count
)
example(args)
logging.info("elapsed time: %s", time.perf_counter() - start)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()

View file

@ -1,10 +1,19 @@
"""
This is a script to run a small switch based scenario and depends on
the user running this script through the "Execute Python Script" option
in the GUI. The usage of globals() below allows this script to leverage the
same CoreEmu instance the GUI is using.
"""
import logging
from core.emulator.emudata import IpPrefixes
from core.emulator.enumerations import EventTypes, NodeTypes
NODES = 2
def example(nodes):
def main():
# ip generator for example
prefixes = IpPrefixes("10.83.0.0/16")
@ -19,7 +28,7 @@ def example(nodes):
switch = session.add_node(_type=NodeTypes.SWITCH)
# create nodes
for _ in range(nodes):
for _ in range(NODES):
node = session.add_node()
interface = prefixes.create_interface(node)
session.add_link(node.id, switch.id, interface_one=interface)
@ -30,4 +39,4 @@ def example(nodes):
if __name__ in {"__main__", "__builtin__"}:
logging.basicConfig(level=logging.INFO)
example(2)
main()

View file

@ -1,14 +1,19 @@
import logging
import time
"""
This is a standalone script to run a small WLAN based scenario and will not
interact with the GUI.
"""
import logging
import params
from core.emulator.coreemu import CoreEmu
from core.emulator.emudata import IpPrefixes, NodeOptions
from core.emulator.enumerations import EventTypes, NodeTypes
from core.location.mobility import BasicRangeModel
NODES = 2
def example(args):
def main():
# ip generator for example
prefixes = IpPrefixes("10.83.0.0/16")
@ -20,13 +25,13 @@ def example(args):
session.set_state(EventTypes.CONFIGURATION_STATE)
# create wlan network node
wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN)
wlan = session.add_node(_type=NodeTypes.WIRELESS_LAN, _id=100)
session.mobility.set_model(wlan, BasicRangeModel)
# create nodes, must set a position for wlan basic range model
options = NodeOptions(model="mdr")
options.set_position(0, 0)
for _ in range(args.nodes):
for _ in range(NODES):
node = session.add_node(options=options)
interface = prefixes.create_interface(node)
session.add_link(node.id, wlan.id, interface_one=interface)
@ -35,27 +40,17 @@ def example(args):
session.instantiate()
# get nodes for example run
first_node = session.get_node(2)
last_node = session.get_node(args.nodes + 1)
first_node = session.get_node(1)
last_node = session.get_node(NODES)
address = prefixes.ip4_address(first_node)
logging.info("node %s pinging %s", last_node.name, address)
output = last_node.cmd(f"ping -c {args.count} {address}")
output = last_node.cmd(f"ping -c 3 {address}")
logging.info(output)
# shutdown session
coreemu.shutdown()
def main():
logging.basicConfig(level=logging.INFO)
args = params.parse("wlan")
start = time.perf_counter()
logging.info(
"running wlan example: nodes(%s) ping count(%s)", args.nodes, args.count
)
example(args)
logging.info("elapsed time: %s", time.perf_counter() - start)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()