2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
commeffect.py: EMANE CommEffect model for CORE
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
|
2019-02-16 17:50:19 +00:00
|
|
|
import logging
|
2018-07-06 19:41:32 +01:00
|
|
|
import os
|
2020-01-15 00:27:08 +00:00
|
|
|
from typing import Dict, List
|
2019-09-10 22:20:51 +01:00
|
|
|
|
2018-07-04 02:49:36 +01:00
|
|
|
from lxml import etree
|
|
|
|
|
2020-01-15 00:27:08 +00:00
|
|
|
from core.config import ConfigGroup, Configuration
|
2019-09-10 22:20:51 +01:00
|
|
|
from core.emane import emanemanifest, emanemodel
|
2020-01-15 00:27:08 +00:00
|
|
|
from core.nodes.interface import CoreInterface
|
2018-07-04 02:49:36 +01:00
|
|
|
from core.xml import emanexml
|
2013-08-29 15:21:13 +01:00
|
|
|
|
|
|
|
try:
|
2018-03-29 22:04:15 +01:00
|
|
|
from emane.events.commeffectevent import CommEffectEvent
|
2017-04-25 16:45:34 +01:00
|
|
|
except ImportError:
|
2018-04-21 05:22:08 +01:00
|
|
|
try:
|
|
|
|
from emanesh.events.commeffectevent import CommEffectEvent
|
|
|
|
except ImportError:
|
2019-02-16 17:50:19 +00:00
|
|
|
logging.debug("compatible emane python bindings not installed")
|
2017-04-25 16:45:34 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2020-01-15 00:27:08 +00:00
|
|
|
def convert_none(x: float) -> int:
|
2018-03-19 23:33:36 +00:00
|
|
|
"""
|
|
|
|
Helper to use 0 for None values.
|
|
|
|
"""
|
2019-10-17 22:52:31 +01:00
|
|
|
if isinstance(x, str):
|
2018-03-19 23:33:36 +00:00
|
|
|
x = float(x)
|
|
|
|
if x is None:
|
|
|
|
return 0
|
|
|
|
else:
|
|
|
|
return int(x)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-03-19 23:33:36 +00:00
|
|
|
|
2018-03-29 22:38:32 +01:00
|
|
|
class EmaneCommEffectModel(emanemodel.EmaneModel):
|
2017-04-25 16:45:34 +01:00
|
|
|
name = "emane_commeffect"
|
2018-03-28 21:58:49 +01:00
|
|
|
|
2018-03-30 20:08:33 +01:00
|
|
|
shim_library = "commeffectshim"
|
2019-04-08 17:49:37 +01:00
|
|
|
shim_xml = "commeffectshim.xml"
|
2018-03-30 20:08:33 +01:00
|
|
|
shim_defaults = {}
|
2019-04-08 17:49:37 +01:00
|
|
|
config_shim = []
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-07-11 17:19:06 +01:00
|
|
|
# comm effect does not need the default phy and external configurations
|
2019-04-08 17:49:37 +01:00
|
|
|
phy_config = []
|
|
|
|
external_config = []
|
|
|
|
|
|
|
|
@classmethod
|
2020-01-15 00:27:08 +00:00
|
|
|
def load(cls, emane_prefix: str) -> None:
|
2019-04-08 17:49:37 +01:00
|
|
|
shim_xml_path = os.path.join(emane_prefix, "share/emane/manifest", cls.shim_xml)
|
|
|
|
cls.config_shim = emanemanifest.parse(shim_xml_path, cls.shim_defaults)
|
2018-07-04 02:49:36 +01:00
|
|
|
|
2018-06-06 22:51:45 +01:00
|
|
|
@classmethod
|
2020-01-15 00:27:08 +00:00
|
|
|
def configurations(cls) -> List[Configuration]:
|
2018-06-06 22:51:45 +01:00
|
|
|
return cls.config_shim
|
|
|
|
|
|
|
|
@classmethod
|
2020-01-15 00:27:08 +00:00
|
|
|
def config_groups(cls) -> List[ConfigGroup]:
|
2019-09-10 23:10:24 +01:00
|
|
|
return [ConfigGroup("CommEffect SHIM Parameters", 1, len(cls.configurations()))]
|
2018-03-19 23:33:36 +00:00
|
|
|
|
2020-01-15 00:27:08 +00:00
|
|
|
def build_xml_files(
|
|
|
|
self, config: Dict[str, str], interface: CoreInterface = None
|
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Build the necessary nem and commeffect XMLs in the given path.
|
|
|
|
If an individual NEM has a nonstandard config, we need to build
|
|
|
|
that file also. Otherwise the WLAN-wide
|
|
|
|
nXXemane_commeffectnem.xml, nXXemane_commeffectshim.xml are used.
|
2018-03-28 21:58:49 +01:00
|
|
|
|
2020-01-16 19:00:57 +00:00
|
|
|
:param config: emane model configuration for the node and interface
|
2018-03-28 21:58:49 +01:00
|
|
|
:param interface: interface for the emane node
|
|
|
|
:return: nothing
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2018-03-28 21:58:49 +01:00
|
|
|
# retrieve xml names
|
2018-07-06 19:41:32 +01:00
|
|
|
nem_name = emanexml.nem_file_name(self, interface)
|
|
|
|
shim_name = emanexml.shim_file_name(self, interface)
|
2018-03-28 21:58:49 +01:00
|
|
|
|
2018-07-04 02:49:36 +01:00
|
|
|
# create and write nem document
|
2019-10-18 18:33:31 +01:00
|
|
|
nem_element = etree.Element("nem", name=f"{self.name} NEM", type="unstructured")
|
2018-07-04 02:49:36 +01:00
|
|
|
transport_type = "virtual"
|
|
|
|
if interface and interface.transport_type == "raw":
|
|
|
|
transport_type = "raw"
|
2019-04-30 07:31:47 +01:00
|
|
|
transport_file = emanexml.transport_file_name(self.id, transport_type)
|
2018-07-06 19:41:32 +01:00
|
|
|
etree.SubElement(nem_element, "transport", definition=transport_file)
|
2018-03-30 20:08:33 +01:00
|
|
|
|
2018-07-04 02:49:36 +01:00
|
|
|
# set shim configuration
|
|
|
|
etree.SubElement(nem_element, "shim", definition=shim_name)
|
2013-08-29 15:21:13 +01:00
|
|
|
|
2018-07-06 19:41:32 +01:00
|
|
|
nem_file = os.path.join(self.session.session_dir, nem_name)
|
|
|
|
emanexml.create_file(nem_element, "nem", nem_file)
|
2018-07-04 02:49:36 +01:00
|
|
|
|
|
|
|
# create and write shim document
|
2019-09-10 23:10:24 +01:00
|
|
|
shim_element = etree.Element(
|
2019-10-18 18:33:31 +01:00
|
|
|
"shim", name=f"{self.name} SHIM", library=self.shim_library
|
2019-09-10 23:10:24 +01:00
|
|
|
)
|
2018-03-30 20:08:33 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# append all shim options (except filterfile) to shimdoc
|
2018-06-06 22:51:45 +01:00
|
|
|
for configuration in self.config_shim:
|
|
|
|
name = configuration.id
|
|
|
|
if name == "filterfile":
|
|
|
|
continue
|
|
|
|
value = config[name]
|
2018-07-04 02:49:36 +01:00
|
|
|
emanexml.add_param(shim_element, name, value)
|
2018-03-28 21:58:49 +01:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# empty filterfile is not allowed
|
2018-06-06 22:51:45 +01:00
|
|
|
ff = config["filterfile"]
|
2017-08-04 22:34:44 +01:00
|
|
|
if ff.strip() != "":
|
2018-07-04 02:49:36 +01:00
|
|
|
emanexml.add_param(shim_element, "filterfile", ff)
|
|
|
|
|
2018-07-06 19:41:32 +01:00
|
|
|
shim_file = os.path.join(self.session.session_dir, shim_name)
|
|
|
|
emanexml.create_file(shim_element, "shim", shim_file)
|
2018-03-28 21:58:49 +01:00
|
|
|
|
2019-09-10 23:10:24 +01:00
|
|
|
def linkconfig(
|
|
|
|
self,
|
2020-01-15 00:27:08 +00:00
|
|
|
netif: CoreInterface,
|
|
|
|
bw: float = None,
|
|
|
|
delay: float = None,
|
|
|
|
loss: float = None,
|
|
|
|
duplicate: float = None,
|
|
|
|
jitter: float = None,
|
|
|
|
netif2: CoreInterface = None,
|
|
|
|
) -> None:
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
|
|
|
Generate CommEffect events when a Link Message is received having
|
2013-08-29 15:21:13 +01:00
|
|
|
link parameters.
|
2017-04-25 16:45:34 +01:00
|
|
|
"""
|
2013-08-29 15:21:13 +01:00
|
|
|
service = self.session.emane.service
|
|
|
|
if service is None:
|
2019-06-03 22:36:21 +01:00
|
|
|
logging.warning("%s: EMANE event service unavailable", self.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2018-03-19 23:33:36 +00:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
if netif is None or netif2 is None:
|
2019-06-03 22:36:21 +01:00
|
|
|
logging.warning("%s: missing NEM information", self.name)
|
2013-08-29 15:21:13 +01:00
|
|
|
return
|
2018-03-19 23:33:36 +00:00
|
|
|
|
2013-08-29 15:21:13 +01:00
|
|
|
# TODO: batch these into multiple events per transmission
|
2013-12-03 19:48:06 +00:00
|
|
|
# TODO: may want to split out seconds portion of delay and jitter
|
2018-03-19 23:33:36 +00:00
|
|
|
event = CommEffectEvent()
|
2019-04-30 07:31:47 +01:00
|
|
|
emane_node = self.session.get_node(self.id)
|
2018-03-19 23:33:36 +00:00
|
|
|
nemid = emane_node.getnemid(netif)
|
|
|
|
nemid2 = emane_node.getnemid(netif2)
|
2013-08-29 15:21:13 +01:00
|
|
|
mbw = bw
|
2019-02-16 17:50:19 +00:00
|
|
|
logging.info("sending comm effect event")
|
2018-03-19 23:33:36 +00:00
|
|
|
event.append(
|
|
|
|
nemid,
|
|
|
|
latency=convert_none(delay),
|
|
|
|
jitter=convert_none(jitter),
|
|
|
|
loss=convert_none(loss),
|
|
|
|
duplicate=convert_none(duplicate),
|
2019-09-11 05:01:51 +01:00
|
|
|
unicast=int(convert_none(bw)),
|
|
|
|
broadcast=int(convert_none(mbw)),
|
2018-03-19 23:33:36 +00:00
|
|
|
)
|
|
|
|
service.publish(nemid2, event)
|