cleaned up broadcast_node to use nodes directly

This commit is contained in:
Blake Harnden 2020-03-22 16:38:15 -07:00
parent fd7db64f6d
commit 33bcc24d88
8 changed files with 39 additions and 84 deletions

View file

@ -721,8 +721,7 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
if request.source:
source = request.source
if not has_geo:
node_data = node.data(source=source)
session.broadcast_node(node_data)
session.broadcast_node(node, source=source)
except CoreError:
result = False
return core_pb2.EditNodeResponse(result=result)

View file

@ -1836,24 +1836,16 @@ class CoreHandler(socketserver.BaseRequestHandler):
Return API messages that describe the current session.
"""
# find all nodes and links
nodes_data = []
links_data = []
with self.session._nodes_lock:
for node_id in self.session.nodes:
node = self.session.nodes[node_id]
node_data = node.data(message_type=MessageFlags.ADD)
if node_data:
nodes_data.append(node_data)
self.session.broadcast_node(node, MessageFlags.ADD)
node_links = node.all_link_data(flags=MessageFlags.ADD)
for link_data in node_links:
links_data.append(link_data)
# send all nodes first, so that they will exist for any links
for node_data in nodes_data:
self.session.broadcast_node(node_data)
for link_data in links_data:
self.session.broadcast_link(link_data)
@ -1960,8 +1952,9 @@ class CoreHandler(socketserver.BaseRequestHandler):
)
self.session.broadcast_config(config_data)
node_count = self.session.get_node_count()
logging.info(
"informed GUI about %d nodes and %d links", len(nodes_data), len(links_data)
"informed GUI about %d nodes and %d links", node_count, len(links_data)
)

View file

@ -806,8 +806,7 @@ class EmaneManager(ModelManager):
# don"t use node.setposition(x,y,z) which generates an event
node.position.set(x, y, z)
node.position.set_geo(lon, lat, alt)
node_data = node.data(lat=lat, lon=lon, alt=alt)
self.session.broadcast_node(node_data)
self.session.broadcast_node(node)
return True
def emanerunning(self, node: CoreNode) -> bool:

View file

@ -17,14 +17,7 @@ from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple, Type
from core import constants, utils
from core.emane.emanemanager import EmaneManager
from core.emane.nodes import EmaneNet
from core.emulator.data import (
ConfigData,
EventData,
ExceptionData,
FileData,
LinkData,
NodeData,
)
from core.emulator.data import ConfigData, EventData, ExceptionData, FileData, LinkData
from core.emulator.distributed import DistributedController
from core.emulator.emudata import (
IdGen,
@ -34,7 +27,13 @@ from core.emulator.emudata import (
create_interface,
link_config,
)
from core.emulator.enumerations import EventTypes, ExceptionLevels, LinkTypes, NodeTypes
from core.emulator.enumerations import (
EventTypes,
ExceptionLevels,
LinkTypes,
MessageFlags,
NodeTypes,
)
from core.emulator.sessionconfig import SessionConfig
from core.errors import CoreError
from core.location.event import EventLoop
@ -805,35 +804,13 @@ class Session:
using_lat_lon_alt = has_empty_position and has_lat_lon_alt
if using_lat_lon_alt:
x, y, _ = self.location.getxyz(lat, lon, alt)
node.position.set_geo(lon, lat, alt)
# set position and broadcast
if None not in [x, y]:
node.setposition(x, y, None)
# broadcast updated location when using lat/lon/alt
if using_lat_lon_alt:
self.broadcast_node_location(node, lon, lat, alt)
def broadcast_node_location(
self, node: NodeBase, lon: float, lat: float, alt: float
) -> None:
"""
Broadcast node location to all listeners.
:param node: node to broadcast location for
:return: nothing
"""
node_data = NodeData(
message_type=0,
id=node.id,
x_position=node.position.x,
y_position=node.position.y,
latitude=lat,
longitude=lon,
altitude=alt,
)
self.broadcast_node(node_data)
node.position.set_geo(lon, lat, alt)
self.broadcast_node(node)
else:
if has_empty_position:
x, y = 0, 0
node.setposition(x, y, None)
def start_mobility(self, node_ids: List[int] = None) -> None:
"""
@ -1026,14 +1003,23 @@ class Session:
for handler in self.exception_handlers:
handler(exception_data)
def broadcast_node(self, node_data: NodeData) -> None:
def broadcast_node(
self,
node: NodeBase,
message_type: MessageFlags = MessageFlags.NONE,
source: str = None,
) -> None:
"""
Handle node data that should be provided to node handlers.
:param node_data: node data to send out
:param node: node to broadcast
:param message_type: type of message to broadcast, None by default
:param source: source of broadcast, None by default
:return: nothing
"""
node_data = node.data(message_type, source)
if not node_data:
return
for handler in self.node_handlers:
handler(node_data)

View file

@ -812,8 +812,7 @@ class WayPointMobility(WirelessModel):
:return: nothing
"""
node.position.set(x, y, z)
node_data = node.data()
self.session.broadcast_node(node_data)
self.session.broadcast_node(node)
def setendtime(self) -> None:
"""

View file

@ -192,20 +192,12 @@ class NodeBase:
return ifindex
def data(
self,
message_type: MessageFlags = MessageFlags.NONE,
lat: float = None,
lon: float = None,
alt: float = None,
source: str = None,
self, message_type: MessageFlags = MessageFlags.NONE, source: str = None
) -> NodeData:
"""
Build a data object for this node.
:param message_type: purpose for the data object we are creating
:param lat: latitude
:param lon: longitude
:param alt: altitude
:param source: source of node data
:return: node data object
"""
@ -217,12 +209,10 @@ class NodeBase:
server = None
if self.server is not None:
server = self.server.name
services = self.services
if services is not None:
services = "|".join([service.name for service in services])
node_data = NodeData(
return NodeData(
message_type=message_type,
id=self.id,
node_type=self.apitype,
@ -233,17 +223,15 @@ class NodeBase:
opaque=self.opaque,
x_position=x,
y_position=y,
latitude=lat,
longitude=lon,
altitude=alt,
latitude=self.position.lat,
longitude=self.position.lon,
altitude=self.position.alt,
model=model,
server=server,
services=services,
source=source,
)
return node_data
def all_link_data(self, flags: MessageFlags = MessageFlags.NONE) -> List[LinkData]:
"""
Build CORE Link data for this object. There is no default

View file

@ -879,21 +879,13 @@ class PtpNet(CoreNetwork):
super().attach(netif)
def data(
self,
message_type: int,
lat: float = None,
lon: float = None,
alt: float = None,
source: str = None,
self, message_type: MessageFlags = MessageFlags.NONE, source: str = None
) -> NodeData:
"""
Do not generate a Node Message for point-to-point links. They are
built using a link message instead.
:param message_type: purpose for the data object we are creating
:param lat: latitude
:param lon: longitude
:param alt: altitude
:param source: source of node data
:return: node data object
"""

View file

@ -982,7 +982,6 @@ class TestGrpc:
client = CoreGrpcClient()
session = grpc_server.coreemu.create_session()
node = session.add_node()
node_data = node.data()
queue = Queue()
def handle_event(event_data):
@ -994,7 +993,7 @@ class TestGrpc:
with client.context_connect():
client.events(session.id, handle_event)
time.sleep(0.1)
session.broadcast_node(node_data)
session.broadcast_node(node)
# then
queue.get(timeout=5)