core-extra/daemon/examples/python/wlan.py
2020-06-19 13:25:47 -07:00

58 lines
1.7 KiB
Python

"""
This is a standalone script to run a small WLAN based scenario and will not
interact with the GUI.
"""
import logging
from core.emulator.coreemu import CoreEmu
from core.emulator.data import IpPrefixes, NodeOptions
from core.emulator.enumerations import EventTypes
from core.location.mobility import BasicRangeModel
from core.nodes.base import CoreNode
from core.nodes.network import WlanNode
NODES = 2
def main():
# ip generator for example
prefixes = IpPrefixes("10.83.0.0/16")
# create emulator instance for creating sessions and utility methods
coreemu = CoreEmu()
session = coreemu.create_session()
# must be in configuration state for nodes to start, when using "node_add" below
session.set_state(EventTypes.CONFIGURATION_STATE)
# create wlan network node
wlan = session.add_node(WlanNode, _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(NODES):
node = session.add_node(CoreNode, options=options)
interface = prefixes.create_iface(node)
session.add_link(node.id, wlan.id, iface1_data=interface)
# instantiate session
session.instantiate()
# get nodes for example run
first_node = session.get_node(1, CoreNode)
last_node = session.get_node(NODES, CoreNode)
address = prefixes.ip4_address(first_node.id)
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()
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()