2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
mobility.py: mobility helpers for moving nodes and calculating wireless range.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
import heapq
|
2017-04-25 16:45:34 +01:00
|
|
|
import math
|
|
|
|
import os
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2017-08-07 19:58:51 +01:00
|
|
|
from core import logger
|
2017-04-25 16:45:34 +01:00
|
|
|
from core.conf import Configurable
|
|
|
|
from core.conf import ConfigurableManager
|
2013-08-29 15:21:13 +01:00
|
|
|
from core.coreobj import PyCoreNode
|
2017-04-25 16:45:34 +01:00
|
|
|
from core.data import EventData, LinkData
|
|
|
|
from core.enumerations import ConfigDataTypes
|
|
|
|
from core.enumerations import EventTypes
|
|
|
|
from core.enumerations import LinkTypes
|
|
|
|
from core.enumerations import MessageFlags
|
|
|
|
from core.enumerations import MessageTypes
|
|
|
|
from core.enumerations import NodeTlvs
|
|
|
|
from core.enumerations import RegisterTlvs
|
2018-03-02 00:23:58 +00:00
|
|
|
from core.misc import utils
|
2017-04-25 16:45:34 +01:00
|
|
|
from core.misc.ipaddress import IpAddress
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
class MobilityManager(ConfigurableManager):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Member of session class for handling configuration data for mobility and
|
2013-08-29 15:21:13 +01:00
|
|
|
range models.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
name = "MobilityManager"
|
|
|
|
config_type = RegisterTlvs.WIRELESS.value
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def __init__(self, session):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Creates a MobilityManager instance.
|
|
|
|
|
|
|
|
:param core.session.Session session: session this manager is tied to
|
|
|
|
"""
|
|
|
|
ConfigurableManager.__init__(self)
|
|
|
|
self.session = session
|
2013-08-29 15:21:13 +01:00
|
|
|
# configurations for basic range, indexed by WLAN node number, are
|
|
|
|
# stored in self.configs
|
|
|
|
# mapping from model names to their classes
|
2017-04-25 16:45:34 +01:00
|
|
|
self._modelclsmap = {
|
|
|
|
BasicRangeModel.name: BasicRangeModel,
|
|
|
|
Ns2ScriptedMobility.name: Ns2ScriptedMobility
|
|
|
|
}
|
2013-08-29 15:21:13 +01:00
|
|
|
# dummy node objects for tracking position of nodes on other servers
|
|
|
|
self.phys = {}
|
|
|
|
self.physnets = {}
|
2017-04-25 19:38:53 +01:00
|
|
|
self.session.broker.handlers.add(self.physnodehandlelink)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
def startup(self, node_ids=None):
|
|
|
|
"""
|
|
|
|
Session is transitioning from instantiation to runtime state.
|
2013-08-29 15:21:13 +01:00
|
|
|
Instantiate any mobility models that have been configured for a WLAN.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param list node_ids: node ids to startup
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
if node_ids is None:
|
|
|
|
node_ids = self.configs.keys()
|
|
|
|
|
|
|
|
for node_id in node_ids:
|
|
|
|
logger.info("checking mobility startup for node: %s", node_id)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-04-25 16:45:34 +01:00
|
|
|
node = self.session.get_object(node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.warn("skipping mobility configuration for unknown node %d." % node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
if node_id not in self.configs:
|
|
|
|
logger.warn("missing mobility configuration for node %d." % node_id)
|
2013-12-05 23:44:25 +00:00
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
v = self.configs[node_id]
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
for model in v:
|
|
|
|
try:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("setting mobility model to node: %s", model)
|
2013-08-29 15:21:13 +01:00
|
|
|
cls = self._modelclsmap[model[0]]
|
2017-04-25 16:45:34 +01:00
|
|
|
node.setmodel(cls, model[1])
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.warn("skipping mobility configuration for unknown model '%s'" % model[0])
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if self.session.master:
|
2017-04-25 16:45:34 +01:00
|
|
|
self.installphysnodes(node)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
if node.mobility:
|
|
|
|
self.session.event_loop.add_event(0.0, node.mobility.startup)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def reset(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Reset all configs.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.clearconfig(nodenum=None)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
def setconfig(self, node_id, config_type, values):
|
|
|
|
"""
|
|
|
|
Normal setconfig() with check for run-time updates for WLANs.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param int node_id: node id
|
|
|
|
:param config_type: configuration type
|
|
|
|
:param values: configuration value
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
super(MobilityManager, self).setconfig(node_id, config_type, values)
|
2013-12-09 17:56:06 +00:00
|
|
|
if self.session is None:
|
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
if self.session.state == EventTypes.RUNTIME_STATE.value:
|
2013-12-09 17:56:06 +00:00
|
|
|
try:
|
2017-04-25 16:45:34 +01:00
|
|
|
node = self.session.get_object(node_id)
|
|
|
|
node.updatemodel(config_type, values)
|
2013-12-09 17:56:06 +00:00
|
|
|
except KeyError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.exception("Skipping mobility configuration for unknown node %d.", node_id)
|
|
|
|
|
|
|
|
def handleevent(self, event_data):
|
|
|
|
"""
|
|
|
|
Handle an Event Message used to start, stop, or pause
|
|
|
|
mobility scripts for a given WlanNode.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param EventData event_data: event data to handle
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
event_type = event_data.event_type
|
|
|
|
node_id = event_data.node
|
|
|
|
name = event_data.name
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-04-25 16:45:34 +01:00
|
|
|
node = self.session.get_object(node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.exception("Ignoring event for model '%s', unknown node '%s'", name, node_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# name is e.g. "mobility:ns2script"
|
|
|
|
models = name[9:].split(',')
|
2017-04-25 16:45:34 +01:00
|
|
|
for model in models:
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-04-25 16:45:34 +01:00
|
|
|
cls = self._modelclsmap[model]
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.warn("Ignoring event for unknown model '%s'", model)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
if cls.config_type in [RegisterTlvs.WIRELESS.value, RegisterTlvs.MOBILITY.value]:
|
2013-08-29 15:21:13 +01:00
|
|
|
model = node.mobility
|
|
|
|
else:
|
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if model is None:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.warn("Ignoring event, %s has no model", node.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
if cls.name != model.name:
|
|
|
|
logger.warn("Ignoring event for %s wrong model %s,%s", node.name, cls.name, model.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
if event_type == EventTypes.STOP.value or event_type == EventTypes.RESTART.value:
|
2013-08-29 15:21:13 +01:00
|
|
|
model.stop(move_initial=True)
|
2017-04-25 16:45:34 +01:00
|
|
|
if event_type == EventTypes.START.value or event_type == EventTypes.RESTART.value:
|
2013-08-29 15:21:13 +01:00
|
|
|
model.start()
|
2017-04-25 16:45:34 +01:00
|
|
|
if event_type == EventTypes.PAUSE.value:
|
2013-08-29 15:21:13 +01:00
|
|
|
model.pause()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def sendevent(self, model):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Send an event message on behalf of a mobility model.
|
|
|
|
This communicates the current and end (max) times to the GUI.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param WayPointMobility model: mobility model to send event for
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
event_type = EventTypes.NONE.value
|
2013-08-29 15:21:13 +01:00
|
|
|
if model.state == model.STATE_STOPPED:
|
2017-04-25 16:45:34 +01:00
|
|
|
event_type = EventTypes.STOP.value
|
2013-08-29 15:21:13 +01:00
|
|
|
elif model.state == model.STATE_RUNNING:
|
2017-04-25 16:45:34 +01:00
|
|
|
event_type = EventTypes.START.value
|
2013-08-29 15:21:13 +01:00
|
|
|
elif model.state == model.STATE_PAUSED:
|
2017-04-25 16:45:34 +01:00
|
|
|
event_type = EventTypes.PAUSE.value
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
data = "start=%d" % int(model.lasttime - model.timezero)
|
|
|
|
data += " end=%d" % int(model.endtime)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
event_data = EventData(
|
2017-05-03 17:30:49 +01:00
|
|
|
node=model.object_id,
|
2017-04-25 16:45:34 +01:00
|
|
|
event_type=event_type,
|
|
|
|
name="mobility:%s" % model.name,
|
|
|
|
data=data,
|
|
|
|
time="%s" % time.time()
|
|
|
|
)
|
|
|
|
|
|
|
|
self.session.broadcast_event(event_data)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def updatewlans(self, moved, moved_netifs):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
A mobility script has caused nodes in the 'moved' list to move.
|
|
|
|
Update every WlanNode. This saves range calculations if the model
|
|
|
|
were to recalculate for each individual node movement.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param list moved: moved nodes
|
|
|
|
:param list moved_netifs: moved network interfaces
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
for nodenum in self.configs:
|
|
|
|
try:
|
2017-04-25 16:45:34 +01:00
|
|
|
n = self.session.get_object(nodenum)
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
|
|
|
continue
|
|
|
|
if n.model:
|
|
|
|
n.model.update(moved, moved_netifs)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def addphys(self, netnum, node):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Keep track of PhysicalNodes and which network they belong to.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param int netnum: network number
|
|
|
|
:param core.coreobj.PyCoreNode node: node to add physical network to
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
nodenum = node.objid
|
|
|
|
self.phys[nodenum] = node
|
|
|
|
if netnum not in self.physnets:
|
2017-04-25 16:45:34 +01:00
|
|
|
self.physnets[netnum] = [nodenum, ]
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
|
|
|
self.physnets[netnum].append(nodenum)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
# TODO: remove need for handling old style message
|
2017-04-25 16:45:34 +01:00
|
|
|
def physnodehandlelink(self, message):
|
|
|
|
"""
|
|
|
|
Broker handler. Snoop Link add messages to get
|
|
|
|
node numbers of PhyiscalNodes and their nets.
|
|
|
|
Physical nodes exist only on other servers, but a shadow object is
|
|
|
|
created here for tracking node position.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param message: link message to handle
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
if message.message_type == MessageTypes.LINK.value and message.flags & MessageFlags.ADD.value:
|
|
|
|
nn = message.node_numbers()
|
2013-08-29 15:21:13 +01:00
|
|
|
# first node is always link layer node in Link add message
|
2017-04-25 16:45:34 +01:00
|
|
|
if nn[0] not in self.session.broker.network_nodes:
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
if nn[1] in self.session.broker.physical_nodes:
|
2013-08-29 15:21:13 +01:00
|
|
|
# record the fact that this PhysicalNode is linked to a net
|
|
|
|
dummy = PyCoreNode(session=self.session, objid=nn[1],
|
2017-04-25 16:45:34 +01:00
|
|
|
name="n%d" % nn[1], start=False)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.addphys(nn[0], dummy)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
# TODO: remove need to handling old style messages
|
2017-04-25 16:45:34 +01:00
|
|
|
def physnodeupdateposition(self, message):
|
|
|
|
"""
|
|
|
|
Snoop node messages belonging to physical nodes. The dummy object
|
2013-08-29 15:21:13 +01:00
|
|
|
in self.phys[] records the node position.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param message: message to handle
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
nodenum = message.node_numbers()[0]
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
|
|
|
dummy = self.phys[nodenum]
|
2017-04-25 16:45:34 +01:00
|
|
|
nodexpos = message.get_tlv(NodeTlvs.X_POSITION.value)
|
|
|
|
nodeypos = message.get_tlv(NodeTlvs.Y_POSITION.value)
|
2013-08-29 15:21:13 +01:00
|
|
|
dummy.setposition(nodexpos, nodeypos, None)
|
|
|
|
except KeyError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.exception("error retrieving physical node: %s", nodenum)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def installphysnodes(self, net):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
After installing a mobility model on a net, include any physical
|
2013-08-29 15:21:13 +01:00
|
|
|
nodes that we have recorded. Use the GreTap tunnel to the physical node
|
|
|
|
as the node's interface.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param net: network to install
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-07-11 16:55:15 +01:00
|
|
|
nodenums = self.physnets.get(net.objid, [])
|
2013-08-29 15:21:13 +01:00
|
|
|
for nodenum in nodenums:
|
|
|
|
node = self.phys[nodenum]
|
2017-04-25 19:38:53 +01:00
|
|
|
# TODO: fix this bad logic, relating to depending on a break to get a valid server
|
|
|
|
for server in self.session.broker.getserversbynode(nodenum):
|
|
|
|
break
|
|
|
|
netif = self.session.broker.gettunnel(net.objid, IpAddress.to_int(server.host))
|
2013-08-29 15:21:13 +01:00
|
|
|
node.addnetif(netif, 0)
|
|
|
|
netif.node = node
|
2017-04-25 19:38:53 +01:00
|
|
|
x, y, z = netif.node.position.get()
|
2013-08-29 15:21:13 +01:00
|
|
|
netif.poshook(netif, x, y, z)
|
|
|
|
|
|
|
|
|
|
|
|
class WirelessModel(Configurable):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Base class used by EMANE models and the basic range model.
|
2013-08-29 15:21:13 +01:00
|
|
|
Used for managing arbitrary configuration parameters.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
config_type = RegisterTlvs.WIRELESS.value
|
|
|
|
bitmap = None
|
|
|
|
position_callback = None
|
|
|
|
|
|
|
|
def __init__(self, session, object_id, values=None):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Create a WirelessModel instance.
|
|
|
|
|
|
|
|
:param core.session.Session session: core session we are tied to
|
|
|
|
:param int object_id: object id
|
|
|
|
:param values: values
|
|
|
|
"""
|
2017-04-25 16:45:34 +01:00
|
|
|
Configurable.__init__(self, session, object_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
# 'values' can be retrieved from a ConfigurableManager, or used here
|
|
|
|
# during initialization, depending on the model.
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
def all_link_data(self, flags):
|
|
|
|
"""
|
|
|
|
May be used if the model can populate the GUI with wireless (green)
|
|
|
|
link lines.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param flags: link data flags
|
|
|
|
:return: link data
|
|
|
|
:rtype: list
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
return []
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def update(self, moved, moved_netifs):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Update this wireless model.
|
|
|
|
|
|
|
|
:param bool moved: flag is it was moved
|
|
|
|
:param list moved_netifs: moved network interfaces
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
raise NotImplementedError
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-12-09 17:56:06 +00:00
|
|
|
def updateconfig(self, values):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-05-03 17:30:49 +01:00
|
|
|
For run-time updates of model config. Returns True when position callback and set link
|
|
|
|
parameters should be invoked.
|
|
|
|
|
|
|
|
:param values: value to update
|
|
|
|
:return: False
|
|
|
|
:rtype: bool
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-12-09 17:56:06 +00:00
|
|
|
return False
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BasicRangeModel(WirelessModel):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Basic Range wireless model, calculates range between nodes and links
|
2013-08-29 15:21:13 +01:00
|
|
|
and unlinks nodes based on this distance. This was formerly done from
|
|
|
|
the GUI.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
name = "basic_range"
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
# configuration parameters are
|
|
|
|
# ( 'name', 'type', 'default', 'possible-value-list', 'caption')
|
2017-04-25 16:45:34 +01:00
|
|
|
config_matrix = [
|
|
|
|
("range", ConfigDataTypes.UINT32.value, '275',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'wireless range (pixels)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("bandwidth", ConfigDataTypes.UINT32.value, '54000',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'bandwidth (bps)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("jitter", ConfigDataTypes.FLOAT.value, '0.0',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'transmission jitter (usec)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("delay", ConfigDataTypes.FLOAT.value, '5000.0',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'transmission delay (usec)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("error", ConfigDataTypes.FLOAT.value, '0.0',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'error rate (%)'),
|
|
|
|
]
|
|
|
|
|
|
|
|
# value groupings
|
2017-04-25 16:45:34 +01:00
|
|
|
config_groups = "Basic Range Parameters:1-%d" % len(config_matrix)
|
|
|
|
|
|
|
|
def __init__(self, session, object_id, values=None):
|
|
|
|
"""
|
2017-05-03 17:30:49 +01:00
|
|
|
Create a BasicRangeModel instance.
|
|
|
|
|
|
|
|
:param core.session.Session session: related core session
|
|
|
|
:param int object_id: object id
|
|
|
|
:param values: values
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
super(BasicRangeModel, self).__init__(session=session, object_id=object_id)
|
|
|
|
self.wlan = session.get_object(object_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
self._netifs = {}
|
|
|
|
self._netifslock = threading.Lock()
|
|
|
|
if values is None:
|
2017-04-25 16:45:34 +01:00
|
|
|
values = session.mobility.getconfig(object_id, self.name, self.getdefaultvalues())[1]
|
|
|
|
self.range = float(self.valueof("range", values))
|
|
|
|
logger.info("Basic range model configured for WLAN %d using range %d", object_id, self.range)
|
2013-12-09 17:56:06 +00:00
|
|
|
self.valuestolinkparams(values)
|
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
# link parameters
|
|
|
|
self.bw = None
|
|
|
|
self.delay = None
|
|
|
|
self.loss = None
|
|
|
|
self.jitter = None
|
|
|
|
|
2013-12-09 17:56:06 +00:00
|
|
|
def valuestolinkparams(self, values):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Values to convert to link parameters.
|
|
|
|
|
|
|
|
:param values: values to convert
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.bw = int(self.valueof("bandwidth", values))
|
|
|
|
if self.bw == 0.0:
|
|
|
|
self.bw = None
|
|
|
|
self.delay = float(self.valueof("delay", values))
|
|
|
|
if self.delay == 0.0:
|
|
|
|
self.delay = None
|
|
|
|
self.loss = float(self.valueof("error", values))
|
|
|
|
if self.loss == 0.0:
|
|
|
|
self.loss = None
|
|
|
|
self.jitter = float(self.valueof("jitter", values))
|
|
|
|
if self.jitter == 0.0:
|
|
|
|
self.jitter = None
|
|
|
|
|
|
|
|
@classmethod
|
2017-04-25 16:45:34 +01:00
|
|
|
def configure_mob(cls, session, config_data):
|
|
|
|
"""
|
|
|
|
Handle configuration messages for setting up a model.
|
2013-08-29 15:21:13 +01:00
|
|
|
Pass the MobilityManager object as the manager object.
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
:param core.session.Session session: current session calling function
|
2017-04-25 16:45:34 +01:00
|
|
|
:param core.conf.ConfigData config_data: configuration data for carrying out a configuration
|
2017-05-03 17:30:49 +01:00
|
|
|
:return: configuration data
|
|
|
|
:rtype: core.data.ConfigData
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
return cls.configure(session.mobility, config_data)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def setlinkparams(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Apply link parameters to all interfaces. This is invoked from
|
2013-08-29 15:21:13 +01:00
|
|
|
WlanNode.setmodel() after the position callback has been set.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
with self._netifslock:
|
|
|
|
for netif in self._netifs:
|
|
|
|
self.wlan.linkconfig(netif, bw=self.bw, delay=self.delay,
|
2017-04-25 16:45:34 +01:00
|
|
|
loss=self.loss, duplicate=None,
|
|
|
|
jitter=self.jitter)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def get_position(self, netif):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Retrieve network interface position.
|
|
|
|
|
|
|
|
:param netif: network interface position to retrieve
|
|
|
|
:return: network interface position
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
with self._netifslock:
|
|
|
|
return self._netifs[netif]
|
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
def set_position(self, netif, x=None, y=None, z=None):
|
|
|
|
"""
|
|
|
|
A node has moved; given an interface, a new (x,y,z) position has
|
2013-08-29 15:21:13 +01:00
|
|
|
been set; calculate the new distance between other nodes and link or
|
|
|
|
unlink node pairs based on the configured range.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param netif: network interface to set position for
|
|
|
|
:param x: x position
|
|
|
|
:param y: y position
|
|
|
|
:param z: z position
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
# print "set_position(%s, x=%s, y=%s, z=%s)" % (netif.localname, x, y, z)
|
2013-08-29 15:21:13 +01:00
|
|
|
self._netifslock.acquire()
|
|
|
|
self._netifs[netif] = (x, y, z)
|
|
|
|
if x is None or y is None:
|
|
|
|
self._netifslock.release()
|
|
|
|
return
|
|
|
|
for netif2 in self._netifs:
|
|
|
|
self.calclink(netif, netif2)
|
|
|
|
self._netifslock.release()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
position_callback = set_position
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def update(self, moved, moved_netifs):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Node positions have changed without recalc. Update positions from
|
2013-08-29 15:21:13 +01:00
|
|
|
node.position, then re-calculate links for those that have moved.
|
|
|
|
Assumes bidirectional links, with one calculation per node pair, where
|
|
|
|
one of the nodes has moved.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param bool moved: flag is it was moved
|
|
|
|
:param list moved_netifs: moved network interfaces
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
with self._netifslock:
|
|
|
|
while len(moved_netifs):
|
|
|
|
netif = moved_netifs.pop()
|
|
|
|
(nx, ny, nz) = netif.node.getposition()
|
|
|
|
if netif in self._netifs:
|
|
|
|
self._netifs[netif] = (nx, ny, nz)
|
|
|
|
for netif2 in self._netifs:
|
|
|
|
if netif2 in moved_netifs:
|
|
|
|
continue
|
|
|
|
self.calclink(netif, netif2)
|
|
|
|
|
|
|
|
def calclink(self, netif, netif2):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Helper used by set_position() and update() to
|
|
|
|
calculate distance between two interfaces and perform
|
|
|
|
linking/unlinking. Sends link/unlink messages and updates the
|
|
|
|
WlanNode's linked dict.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param netif: interface one
|
|
|
|
:param netif2: interface two
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if netif == netif2:
|
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
2017-04-27 21:34:23 +01:00
|
|
|
x, y, z = self._netifs[netif]
|
|
|
|
x2, y2, z2 = self._netifs[netif2]
|
|
|
|
|
|
|
|
if x2 is None or y2 is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
d = self.calcdistance((x, y, z), (x2, y2, z2))
|
|
|
|
|
|
|
|
# ordering is important, to keep the wlan._linked dict organized
|
|
|
|
a = min(netif, netif2)
|
|
|
|
b = max(netif, netif2)
|
|
|
|
|
|
|
|
with self.wlan._linked_lock:
|
|
|
|
linked = self.wlan.linked(a, b)
|
|
|
|
|
2017-07-25 18:16:18 +01:00
|
|
|
logger.info("checking range netif1(%s) netif2(%s): linked(%s) actual(%s) > config(%s)",
|
|
|
|
a.name, b.name, linked, d, self.range)
|
2017-04-27 21:34:23 +01:00
|
|
|
if d > self.range:
|
|
|
|
if linked:
|
2017-07-25 18:16:18 +01:00
|
|
|
logger.info("was linked, unlinking")
|
2017-04-27 21:34:23 +01:00
|
|
|
self.wlan.unlink(a, b)
|
|
|
|
self.sendlinkmsg(a, b, unlink=True)
|
|
|
|
else:
|
|
|
|
if not linked:
|
2017-07-25 18:16:18 +01:00
|
|
|
logger.info("was not linked, linking")
|
2017-04-27 21:34:23 +01:00
|
|
|
self.wlan.link(a, b)
|
|
|
|
self.sendlinkmsg(a, b)
|
2013-08-29 15:21:13 +01:00
|
|
|
except KeyError:
|
2017-04-27 21:34:23 +01:00
|
|
|
logger.exception("error getting interfaces during calclinkS")
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2014-10-31 16:46:43 +00:00
|
|
|
@staticmethod
|
|
|
|
def calcdistance(p1, p2):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Calculate the distance between two three-dimensional points.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param tuple p1: point one
|
|
|
|
:param tuple p2: point two
|
|
|
|
:return: distance petween the points
|
|
|
|
:rtype: float
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
a = p1[0] - p2[0]
|
|
|
|
b = p1[1] - p2[1]
|
|
|
|
c = 0
|
|
|
|
if p1[2] is not None and p2[2] is not None:
|
|
|
|
c = p1[2] - p2[2]
|
|
|
|
return math.hypot(math.hypot(a, b), c)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-12-09 17:56:06 +00:00
|
|
|
def updateconfig(self, values):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Configuration has changed during runtime.
|
|
|
|
MobilityManager.setconfig() -> WlanNode.updatemodel() ->
|
2013-12-09 17:56:06 +00:00
|
|
|
WirelessModel.updateconfig()
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param values: values to update configuration
|
|
|
|
:return: was update successful
|
|
|
|
:rtype: bool
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-12-09 17:56:06 +00:00
|
|
|
self.valuestolinkparams(values)
|
2017-04-25 16:45:34 +01:00
|
|
|
self.range = float(self.valueof("range", values))
|
2013-12-09 17:56:06 +00:00
|
|
|
return True
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
def create_link_data(self, interface1, interface2, message_type):
|
|
|
|
"""
|
|
|
|
Create a wireless link/unlink data message.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param core.coreobj.PyCoreNetIf interface1: interface one
|
|
|
|
:param core.coreobj.PyCoreNetIf interface2: interface two
|
|
|
|
:param message_type: link message type
|
|
|
|
:return: link data
|
|
|
|
:rtype: LinkData
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
|
|
|
|
return LinkData(
|
|
|
|
message_type=message_type,
|
|
|
|
node1_id=interface1.node.objid,
|
|
|
|
node2_id=interface2.node.objid,
|
|
|
|
network_id=self.wlan.objid,
|
|
|
|
link_type=LinkTypes.WIRELESS.value
|
|
|
|
)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def sendlinkmsg(self, netif, netif2, unlink=False):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Send a wireless link/unlink API message to the GUI.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param core.coreobj.PyCoreNetIf netif: interface one
|
|
|
|
:param core.coreobj.PyCoreNetIf netif2: interface two
|
|
|
|
:param bool unlink: unlink or not
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if unlink:
|
2017-04-25 16:45:34 +01:00
|
|
|
message_type = MessageFlags.DELETE.value
|
2013-08-29 15:21:13 +01:00
|
|
|
else:
|
2017-04-25 16:45:34 +01:00
|
|
|
message_type = MessageFlags.ADD.value
|
|
|
|
|
|
|
|
link_data = self.create_link_data(netif, netif2, message_type)
|
|
|
|
self.session.broadcast_link(link_data)
|
|
|
|
|
|
|
|
def all_link_data(self, flags):
|
|
|
|
"""
|
|
|
|
Return a list of wireless link messages for when the GUI reconnects.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param flags: link flags
|
|
|
|
:return: all link data
|
|
|
|
:rtype: list
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
all_links = []
|
2013-08-29 15:21:13 +01:00
|
|
|
with self.wlan._linked_lock:
|
|
|
|
for a in self.wlan._linked:
|
|
|
|
for b in self.wlan._linked[a]:
|
|
|
|
if self.wlan._linked[a][b]:
|
2017-04-25 16:45:34 +01:00
|
|
|
all_links.append(self.create_link_data(a, b, flags))
|
|
|
|
return all_links
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
class WayPoint(object):
|
|
|
|
"""
|
|
|
|
Maintains information regarding waypoints.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, time, nodenum, coords, speed):
|
|
|
|
"""
|
|
|
|
Creates a WayPoint instance.
|
|
|
|
|
|
|
|
:param time: waypoint time
|
|
|
|
:param int nodenum: node id
|
|
|
|
:param coords: waypoint coordinates
|
|
|
|
:param speed: waypoint speed
|
|
|
|
"""
|
|
|
|
self.time = time
|
|
|
|
self.nodenum = nodenum
|
|
|
|
self.coords = coords
|
|
|
|
self.speed = speed
|
|
|
|
|
|
|
|
def __cmp__(self, other):
|
|
|
|
"""
|
|
|
|
Custom comparison method for waypoints.
|
|
|
|
|
|
|
|
:param WayPoint other: waypoint to compare to
|
|
|
|
:return: the comparison result against the other waypoint
|
|
|
|
:rtype: int
|
|
|
|
"""
|
|
|
|
tmp = cmp(self.time, other.time)
|
|
|
|
if tmp == 0:
|
|
|
|
tmp = cmp(self.nodenum, other.nodenum)
|
|
|
|
return tmp
|
|
|
|
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
class WayPointMobility(WirelessModel):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Abstract class for mobility models that set node waypoints.
|
|
|
|
"""
|
|
|
|
name = "waypoint"
|
|
|
|
config_type = RegisterTlvs.MOBILITY.value
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
STATE_STOPPED = 0
|
|
|
|
STATE_RUNNING = 1
|
|
|
|
STATE_PAUSED = 2
|
2017-04-25 16:45:34 +01:00
|
|
|
|
|
|
|
def __init__(self, session, object_id, values=None):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Create a WayPointMobility instance.
|
|
|
|
|
|
|
|
:param core.session.Session session: CORE session instance
|
|
|
|
:param int object_id: object id
|
|
|
|
:param values: values for this model
|
|
|
|
:return:
|
|
|
|
"""
|
2017-04-25 16:45:34 +01:00
|
|
|
super(WayPointMobility, self).__init__(session=session, object_id=object_id, values=values)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.state = self.STATE_STOPPED
|
|
|
|
self.queue = []
|
|
|
|
self.queue_copy = []
|
|
|
|
self.points = {}
|
|
|
|
self.initial = {}
|
|
|
|
self.lasttime = None
|
|
|
|
self.endtime = None
|
2017-04-25 16:45:34 +01:00
|
|
|
self.wlan = session.get_object(object_id)
|
2013-08-29 15:21:13 +01:00
|
|
|
# these are really set in child class via confmatrix
|
|
|
|
self.loop = False
|
|
|
|
self.refresh_ms = 50
|
|
|
|
# flag whether to stop scheduling when queue is empty
|
|
|
|
# (ns-3 sets this to False as new waypoints may be added from trace)
|
|
|
|
self.empty_queue_stop = True
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def runround(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Advance script time and move nodes.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if self.state != self.STATE_RUNNING:
|
2017-04-25 16:45:34 +01:00
|
|
|
return
|
2013-08-29 15:21:13 +01:00
|
|
|
t = self.lasttime
|
|
|
|
self.lasttime = time.time()
|
|
|
|
now = self.lasttime - self.timezero
|
|
|
|
dt = self.lasttime - t
|
2017-04-25 16:45:34 +01:00
|
|
|
# print "runround(now=%.2f, dt=%.2f)" % (now, dt)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# keep current waypoints up-to-date
|
|
|
|
self.updatepoints(now)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if not len(self.points):
|
|
|
|
if len(self.queue):
|
|
|
|
# more future waypoints, allow time for self.lasttime update
|
|
|
|
nexttime = self.queue[0].time - now
|
|
|
|
if nexttime > (0.001 * self.refresh_ms):
|
2017-04-25 16:45:34 +01:00
|
|
|
nexttime -= 0.001 * self.refresh_ms
|
2017-08-02 22:07:56 +01:00
|
|
|
self.session.event_loop.add_event(nexttime, self.runround)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
# no more waypoints or queued items, loop?
|
|
|
|
if not self.empty_queue_stop:
|
|
|
|
# keep running every refresh_ms, even with empty queue
|
2017-08-02 22:07:56 +01:00
|
|
|
self.session.event_loop.add_event(0.001 * self.refresh_ms, self.runround)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
if not self.loopwaypoints():
|
|
|
|
return self.stop(move_initial=False)
|
|
|
|
if not len(self.queue):
|
|
|
|
# prevent busy loop
|
|
|
|
return
|
|
|
|
return self.run()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# only move netifs attached to self.wlan, or all nodenum in script?
|
|
|
|
moved = []
|
|
|
|
moved_netifs = []
|
|
|
|
for netif in self.wlan.netifs():
|
|
|
|
node = netif.node
|
|
|
|
if self.movenode(node, dt):
|
|
|
|
moved.append(node)
|
|
|
|
moved_netifs.append(netif)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# calculate all ranges after moving nodes; this saves calculations
|
2017-04-25 16:45:34 +01:00
|
|
|
# self.wlan.model.update(moved)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.session.mobility.updatewlans(moved, moved_netifs)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# TODO: check session state
|
2017-08-02 22:07:56 +01:00
|
|
|
self.session.event_loop.add_event(0.001 * self.refresh_ms, self.runround)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def run(self):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Run the waypoint mobility scenario.
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2017-08-03 00:39:38 +01:00
|
|
|
logger.info("running mobility scenario")
|
2013-08-29 15:21:13 +01:00
|
|
|
self.timezero = time.time()
|
|
|
|
self.lasttime = self.timezero - (0.001 * self.refresh_ms)
|
|
|
|
self.movenodesinitial()
|
|
|
|
self.runround()
|
|
|
|
self.session.mobility.sendevent(self)
|
|
|
|
|
|
|
|
def movenode(self, node, dt):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Calculate next node location and update its coordinates.
|
|
|
|
Returns True if the node's position has changed.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to move
|
|
|
|
:param dt: move factor
|
|
|
|
:return: True if node was moved, False otherwise
|
|
|
|
:rtype: bool
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if node.objid not in self.points:
|
|
|
|
return False
|
|
|
|
x1, y1, z1 = node.getposition()
|
|
|
|
x2, y2, z2 = self.points[node.objid].coords
|
|
|
|
speed = self.points[node.objid].speed
|
|
|
|
# instantaneous move (prevents dx/dy == 0.0 below)
|
|
|
|
if speed == 0:
|
|
|
|
self.setnodeposition(node, x2, y2, z2)
|
|
|
|
del self.points[node.objid]
|
|
|
|
return True
|
|
|
|
# speed can be a velocity vector (ns3 mobility) or speed value
|
|
|
|
if isinstance(speed, (float, int)):
|
|
|
|
# linear speed value
|
|
|
|
alpha = math.atan2(y2 - y1, x2 - x1)
|
|
|
|
sx = speed * math.cos(alpha)
|
|
|
|
sy = speed * math.sin(alpha)
|
|
|
|
else:
|
|
|
|
# velocity vector
|
|
|
|
sx = speed[0]
|
|
|
|
sy = speed[1]
|
|
|
|
|
|
|
|
# calculate dt * speed = distance moved
|
|
|
|
dx = sx * dt
|
|
|
|
dy = sy * dt
|
|
|
|
# prevent overshoot
|
|
|
|
if abs(dx) > abs(x2 - x1):
|
|
|
|
dx = x2 - x1
|
|
|
|
if abs(dy) > abs(y2 - y1):
|
|
|
|
dy = y2 - y1
|
|
|
|
if dx == 0.0 and dy == 0.0:
|
|
|
|
if self.endtime < (self.lasttime - self.timezero):
|
|
|
|
# the last node to reach the last waypoint determines this
|
|
|
|
# script's endtime
|
|
|
|
self.endtime = self.lasttime - self.timezero
|
|
|
|
del self.points[node.objid]
|
|
|
|
return False
|
2017-04-25 16:45:34 +01:00
|
|
|
# print "node %s dx,dy= <%s, %d>" % (node.name, dx, dy)
|
2013-08-29 15:21:13 +01:00
|
|
|
if (x1 + dx) < 0.0:
|
|
|
|
dx = 0.0 - x1
|
|
|
|
if (y1 + dy) < 0.0:
|
|
|
|
dy = 0.0 - y1
|
|
|
|
self.setnodeposition(node, x1 + dx, y1 + dy, z1)
|
|
|
|
return True
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def movenodesinitial(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Move nodes to their initial positions. Then calculate the ranges.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
moved = []
|
|
|
|
moved_netifs = []
|
|
|
|
for netif in self.wlan.netifs():
|
|
|
|
node = netif.node
|
|
|
|
if node.objid not in self.initial:
|
|
|
|
continue
|
|
|
|
(x, y, z) = self.initial[node.objid].coords
|
|
|
|
self.setnodeposition(node, x, y, z)
|
|
|
|
moved.append(node)
|
|
|
|
moved_netifs.append(netif)
|
2017-04-25 16:45:34 +01:00
|
|
|
# self.wlan.model.update(moved)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.session.mobility.updatewlans(moved, moved_netifs)
|
|
|
|
|
|
|
|
def addwaypoint(self, time, nodenum, x, y, z, speed):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Waypoints are pushed to a heapq, sorted by time.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param time: waypoint time
|
|
|
|
:param int nodenum: node id
|
|
|
|
:param x: x position
|
|
|
|
:param y: y position
|
|
|
|
:param z: z position
|
|
|
|
:param speed: speed
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
# print "addwaypoint: %s %s %s,%s,%s %s" % (time, nodenum, x, y, z, speed)
|
2017-05-03 17:30:49 +01:00
|
|
|
wp = WayPoint(time, nodenum, coords=(x, y, z), speed=speed)
|
2013-08-29 15:21:13 +01:00
|
|
|
heapq.heappush(self.queue, wp)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def addinitial(self, nodenum, x, y, z):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Record initial position in a dict.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param int nodenum: node id
|
|
|
|
:param x: x position
|
|
|
|
:param y: y position
|
|
|
|
:param z: z position
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-05-03 17:30:49 +01:00
|
|
|
wp = WayPoint(0, nodenum, coords=(x, y, z), speed=0)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.initial[nodenum] = wp
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def updatepoints(self, now):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Move items from self.queue to self.points when their time has come.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param int now: current timestamp
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
while len(self.queue):
|
|
|
|
if self.queue[0].time > now:
|
2017-04-25 16:45:34 +01:00
|
|
|
break
|
2013-08-29 15:21:13 +01:00
|
|
|
wp = heapq.heappop(self.queue)
|
|
|
|
self.points[wp.nodenum] = wp
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def copywaypoints(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Store backup copy of waypoints for looping and stopping.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.queue_copy = list(self.queue)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def loopwaypoints(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Restore backup copy of waypoints when looping.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.queue = list(self.queue_copy)
|
|
|
|
return self.loop
|
|
|
|
|
|
|
|
def setnodeposition(self, node, x, y, z):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Helper to move a node, notify any GUI (connected session handlers),
|
|
|
|
without invoking the interface poshook callback that may perform
|
|
|
|
range calculation.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param core.netns.nodes.CoreNode node: node to set position for
|
|
|
|
:param x: x position
|
|
|
|
:param y: y position
|
|
|
|
:param z: z position
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
# this would cause PyCoreNetIf.poshook() callback (range calculation)
|
2017-04-25 16:45:34 +01:00
|
|
|
# node.setposition(x, y, z)
|
2013-08-29 15:21:13 +01:00
|
|
|
node.position.set(x, y, z)
|
2017-04-25 16:45:34 +01:00
|
|
|
node_data = node.data(message_type=0)
|
|
|
|
self.session.broadcast_node(node_data)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def setendtime(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Set self.endtime to the time of the last waypoint in the queue of
|
|
|
|
waypoints. This is just an estimate. The endtime will later be
|
|
|
|
adjusted, after one round of the script has run, to be the time
|
|
|
|
that the last moving node has reached its final waypoint.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
|
|
|
self.endtime = self.queue[-1].time
|
|
|
|
except IndexError:
|
|
|
|
self.endtime = 0
|
|
|
|
|
|
|
|
def start(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Run the script from the beginning or unpause from where it
|
|
|
|
was before.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
laststate = self.state
|
|
|
|
self.state = self.STATE_RUNNING
|
|
|
|
if laststate == self.STATE_STOPPED or laststate == self.STATE_RUNNING:
|
|
|
|
self.loopwaypoints()
|
|
|
|
self.timezero = 0
|
|
|
|
self.lasttime = 0
|
|
|
|
self.run()
|
|
|
|
elif laststate == self.STATE_PAUSED:
|
|
|
|
now = time.time()
|
|
|
|
self.timezero += now - self.lasttime
|
|
|
|
self.lasttime = now - (0.001 * self.refresh_ms)
|
|
|
|
self.runround()
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def stop(self, move_initial=True):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Stop the script and move nodes to initial positions.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param bool move_initial: flag to check if we should move nodes to initial position
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.state = self.STATE_STOPPED
|
|
|
|
self.loopwaypoints()
|
|
|
|
self.timezero = 0
|
|
|
|
self.lasttime = 0
|
|
|
|
if move_initial:
|
|
|
|
self.movenodesinitial()
|
|
|
|
self.session.mobility.sendevent(self)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def pause(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Pause the script; pause time is stored to self.lasttime.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.state = self.STATE_PAUSED
|
|
|
|
self.lasttime = time.time()
|
|
|
|
|
|
|
|
|
|
|
|
class Ns2ScriptedMobility(WayPointMobility):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handles the ns-2 script format, generated by scengen/setdest or
|
|
|
|
BonnMotion.
|
|
|
|
"""
|
|
|
|
name = "ns2script"
|
|
|
|
|
|
|
|
config_matrix = [
|
|
|
|
("file", ConfigDataTypes.STRING.value, '',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'mobility script file'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("refresh_ms", ConfigDataTypes.UINT32.value, '50',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'refresh time (ms)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("loop", ConfigDataTypes.BOOL.value, '1',
|
2013-08-29 15:21:13 +01:00
|
|
|
'On,Off', 'loop'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("autostart", ConfigDataTypes.STRING.value, '',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'auto-start seconds (0.0 for runtime)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("map", ConfigDataTypes.STRING.value, '',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'node mapping (optional, e.g. 0:1,1:2,2:3)'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("script_start", ConfigDataTypes.STRING.value, '',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'script file to run upon start'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("script_pause", ConfigDataTypes.STRING.value, '',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'script file to run upon pause'),
|
2017-04-25 16:45:34 +01:00
|
|
|
("script_stop", ConfigDataTypes.STRING.value, '',
|
2013-08-29 15:21:13 +01:00
|
|
|
'', 'script file to run upon stop'),
|
|
|
|
]
|
2017-04-25 16:45:34 +01:00
|
|
|
config_groups = "ns-2 Mobility Script Parameters:1-%d" % len(config_matrix)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2017-04-25 16:45:34 +01:00
|
|
|
def __init__(self, session, object_id, values=None):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Creates a Ns2ScriptedMobility instance.
|
|
|
|
|
|
|
|
:param core.session.Session session: CORE session instance
|
|
|
|
:param int object_id: object id
|
|
|
|
:param values: values
|
|
|
|
"""
|
2017-04-25 16:45:34 +01:00
|
|
|
super(Ns2ScriptedMobility, self).__init__(session=session, object_id=object_id, values=values)
|
2013-08-29 15:21:13 +01:00
|
|
|
self._netifs = {}
|
|
|
|
self._netifslock = threading.Lock()
|
|
|
|
if values is None:
|
2017-04-25 16:45:34 +01:00
|
|
|
values = session.mobility.getconfig(object_id, self.name, self.getdefaultvalues())[1]
|
|
|
|
self.file = self.valueof("file", values)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.refresh_ms = int(self.valueof("refresh_ms", values))
|
2017-04-25 16:45:34 +01:00
|
|
|
self.loop = self.valueof("loop", values).lower() == "on"
|
2013-08-29 15:21:13 +01:00
|
|
|
self.autostart = self.valueof("autostart", values)
|
|
|
|
self.parsemap(self.valueof("map", values))
|
|
|
|
self.script_start = self.valueof("script_start", values)
|
|
|
|
self.script_pause = self.valueof("script_pause", values)
|
|
|
|
self.script_stop = self.valueof("script_stop", values)
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("ns-2 scripted mobility configured for WLAN %d using file: %s", object_id, self.file)
|
2013-08-29 15:21:13 +01:00
|
|
|
self.readscriptfile()
|
|
|
|
self.copywaypoints()
|
|
|
|
self.setendtime()
|
|
|
|
|
|
|
|
@classmethod
|
2017-04-25 16:45:34 +01:00
|
|
|
def configure_mob(cls, session, config_data):
|
|
|
|
"""
|
|
|
|
Handle configuration messages for setting up a model.
|
2013-08-29 15:21:13 +01:00
|
|
|
Pass the MobilityManager object as the manager object.
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
:param core.session.Session session: current session calling function
|
2017-04-25 16:45:34 +01:00
|
|
|
:param core.conf.ConfigData config_data: configuration data for carrying out a configuration
|
|
|
|
"""
|
|
|
|
return cls.configure(session.mobility, config_data)
|
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def readscriptfile(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Read in mobility script from a file. This adds waypoints to a
|
|
|
|
priority queue, sorted by waypoint time. Initial waypoints are
|
|
|
|
stored in a separate dict.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
filename = self.findfile(self.file)
|
|
|
|
try:
|
|
|
|
f = open(filename, 'r')
|
2017-04-25 16:45:34 +01:00
|
|
|
except IOError:
|
|
|
|
logger.exception("ns-2 scripted mobility failed to load file '%s'", self.file)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("reading ns-2 script file: %s" % filename)
|
2013-08-29 15:21:13 +01:00
|
|
|
ln = 0
|
|
|
|
ix = iy = iz = None
|
|
|
|
inodenum = None
|
|
|
|
for line in f:
|
|
|
|
ln += 1
|
|
|
|
if line[:2] != '$n':
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
if line[:8] == "$ns_ at ":
|
|
|
|
if ix is not None and iy is not None:
|
|
|
|
self.addinitial(self.map(inodenum), ix, iy, iz)
|
|
|
|
ix = iy = iz = None
|
|
|
|
# waypoints:
|
|
|
|
# $ns_ at 1.00 "$node_(6) setdest 500.0 178.0 25.0"
|
|
|
|
parts = line.split()
|
|
|
|
time = float(parts[2])
|
2017-04-25 16:45:34 +01:00
|
|
|
nodenum = parts[3][1 + parts[3].index('('):parts[3].index(')')]
|
2013-08-29 15:21:13 +01:00
|
|
|
x = float(parts[5])
|
|
|
|
y = float(parts[6])
|
|
|
|
z = None
|
|
|
|
speed = float(parts[7].strip('"'))
|
|
|
|
self.addwaypoint(time, self.map(nodenum), x, y, z, speed)
|
|
|
|
elif line[:7] == "$node_(":
|
|
|
|
# initial position (time=0, speed=0):
|
|
|
|
# $node_(6) set X_ 780.0
|
|
|
|
parts = line.split()
|
|
|
|
time = 0.0
|
2017-04-25 16:45:34 +01:00
|
|
|
nodenum = parts[0][1 + parts[0].index('('):parts[0].index(')')]
|
2013-08-29 15:21:13 +01:00
|
|
|
if parts[2] == 'X_':
|
|
|
|
if ix is not None and iy is not None:
|
|
|
|
self.addinitial(self.map(inodenum), ix, iy, iz)
|
|
|
|
ix = iy = iz = None
|
|
|
|
ix = float(parts[3])
|
|
|
|
elif parts[2] == 'Y_':
|
|
|
|
iy = float(parts[3])
|
|
|
|
elif parts[2] == 'Z_':
|
|
|
|
iz = float(parts[3])
|
|
|
|
self.addinitial(self.map(nodenum), ix, iy, iz)
|
|
|
|
ix = iy = iz = None
|
|
|
|
inodenum = nodenum
|
|
|
|
else:
|
|
|
|
raise ValueError
|
2017-04-25 16:45:34 +01:00
|
|
|
except ValueError:
|
|
|
|
logger.exception("skipping line %d of file %s '%s'", ln, self.file, line)
|
2013-08-29 15:21:13 +01:00
|
|
|
continue
|
|
|
|
if ix is not None and iy is not None:
|
|
|
|
self.addinitial(self.map(inodenum), ix, iy, iz)
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2017-05-03 17:30:49 +01:00
|
|
|
def findfile(self, file_name):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Locate a script file. If the specified file doesn't exist, look in the
|
2017-08-02 22:07:56 +01:00
|
|
|
same directory as the scenario file, or in the default
|
2017-04-25 16:45:34 +01:00
|
|
|
configs directory (~/.core/configs). This allows for sample files without
|
2017-08-02 22:07:56 +01:00
|
|
|
absolute path names.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param str file_name: file name to find
|
|
|
|
:return: absolute path to the file
|
|
|
|
:rtype: str
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-05-03 17:30:49 +01:00
|
|
|
if os.path.exists(file_name):
|
|
|
|
return file_name
|
|
|
|
|
2017-08-02 22:07:56 +01:00
|
|
|
if self.session.file_name is not None:
|
|
|
|
d = os.path.dirname(self.session.file_name)
|
2017-05-03 17:30:49 +01:00
|
|
|
sessfn = os.path.join(d, file_name)
|
2017-04-25 16:45:34 +01:00
|
|
|
if os.path.exists(sessfn):
|
2013-08-29 15:21:13 +01:00
|
|
|
return sessfn
|
2017-05-03 17:30:49 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if self.session.user is not None:
|
2017-05-03 17:30:49 +01:00
|
|
|
userfn = os.path.join('/home', self.session.user, '.core', 'configs', file_name)
|
2017-04-25 16:45:34 +01:00
|
|
|
if os.path.exists(userfn):
|
2013-08-29 15:21:13 +01:00
|
|
|
return userfn
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
return file_name
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def parsemap(self, mapstr):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Parse a node mapping string, given as a configuration parameter.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param str mapstr: mapping string to parse
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
self.nodemap = {}
|
2017-05-03 17:30:49 +01:00
|
|
|
if mapstr.strip() == "":
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
for pair in mapstr.split(","):
|
|
|
|
parts = pair.split(":")
|
2013-08-29 15:21:13 +01:00
|
|
|
try:
|
|
|
|
if len(parts) != 2:
|
|
|
|
raise ValueError
|
|
|
|
self.nodemap[int(parts[0])] = int(parts[1])
|
|
|
|
except ValueError:
|
2017-05-03 17:30:49 +01:00
|
|
|
logger.exception("ns-2 mobility node map error")
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def map(self, nodenum):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Map one node number (from a script file) to another.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:param str nodenum: node id to map
|
|
|
|
:return: mapped value or the node id itself
|
|
|
|
:rtype: int
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
nodenum = int(nodenum)
|
|
|
|
try:
|
|
|
|
return self.nodemap[nodenum]
|
|
|
|
except KeyError:
|
2017-08-02 22:07:56 +01:00
|
|
|
logger.exception("error finding value in node map, ignored and returns node id")
|
2013-08-29 15:21:13 +01:00
|
|
|
return nodenum
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def startup(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Start running the script if autostart is enabled.
|
|
|
|
Move node to initial positions when any autostart time is specified.
|
|
|
|
Ignore the script if autostart is an empty string (can still be
|
|
|
|
started via GUI controls).
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
if self.autostart == '':
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("not auto-starting ns-2 script for %s" % self.wlan.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
try:
|
|
|
|
t = float(self.autostart)
|
|
|
|
except ValueError:
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.exception("Invalid auto-start seconds specified '%s' for %s", self.autostart, self.wlan.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
|
|
|
self.movenodesinitial()
|
2017-04-25 16:45:34 +01:00
|
|
|
logger.info("scheduling ns-2 script for %s autostart at %s" % (self.wlan.name, t))
|
2013-08-29 15:21:13 +01:00
|
|
|
self.state = self.STATE_RUNNING
|
2017-08-02 22:07:56 +01:00
|
|
|
self.session.event_loop.add_event(t, self.run)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
def start(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Handle the case when un-paused.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2017-08-03 00:39:38 +01:00
|
|
|
logger.info("starting script")
|
2013-08-29 15:21:13 +01:00
|
|
|
laststate = self.state
|
|
|
|
super(Ns2ScriptedMobility, self).start()
|
|
|
|
if laststate == self.STATE_PAUSED:
|
|
|
|
self.statescript("unpause")
|
|
|
|
|
|
|
|
def run(self):
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Start is pressed or autostart is triggered.
|
2017-05-03 17:30:49 +01:00
|
|
|
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
super(Ns2ScriptedMobility, self).run()
|
|
|
|
self.statescript("run")
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def pause(self):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Pause the mobility script.
|
|
|
|
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
super(Ns2ScriptedMobility, self).pause()
|
|
|
|
self.statescript("pause")
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def stop(self, move_initial=True):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
Stop the mobility script.
|
|
|
|
|
|
|
|
:param bool move_initial: flag to check if we should move node to initial position
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
super(Ns2ScriptedMobility, self).stop(move_initial=move_initial)
|
|
|
|
self.statescript("stop")
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
def statescript(self, typestr):
|
2017-05-03 17:30:49 +01:00
|
|
|
"""
|
|
|
|
State of the mobility script.
|
|
|
|
|
|
|
|
:param str typestr: state type string
|
|
|
|
:return: nothing
|
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
filename = None
|
|
|
|
if typestr == "run" or typestr == "unpause":
|
|
|
|
filename = self.script_start
|
|
|
|
elif typestr == "pause":
|
|
|
|
filename = self.script_pause
|
|
|
|
elif typestr == "stop":
|
|
|
|
filename = self.script_stop
|
|
|
|
if filename is None or filename == '':
|
|
|
|
return
|
|
|
|
filename = self.findfile(filename)
|
2018-03-02 23:54:27 +00:00
|
|
|
args = ["/bin/sh", filename, typestr]
|
|
|
|
utils.check_cmd(args, cwd=self.session.sessiondir, env=self.session.get_environment())
|