From 4e4fcddd00322d9d0e23cd9c8d027edb0935ff6f Mon Sep 17 00:00:00 2001 From: bharnden <32446120+bharnden@users.noreply.github.com> Date: Sat, 12 Oct 2019 08:56:39 -0700 Subject: [PATCH] added grpc api for creating emane link/unlink messages, which will be sent to the gui for being added/removed --- daemon/core/api/grpc/client.py | 15 +++++++++ daemon/core/api/grpc/server.py | 46 ++++++++++++++++++++++++++- daemon/proto/core/api/grpc/core.proto | 13 ++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/daemon/core/api/grpc/client.py b/daemon/core/api/grpc/client.py index dfb96d21..2d767993 100644 --- a/daemon/core/api/grpc/client.py +++ b/daemon/core/api/grpc/client.py @@ -864,6 +864,21 @@ class CoreGrpcClient(object): request = core_pb2.OpenXmlRequest(data=data) return self.stub.OpenXml(request) + def emane_link(self, session_id, nem_one, nem_two, linked): + """ + Helps broadcast wireless link/unlink between EMANE nodes. + + :param int session_id: session id + :param int nem_one: + :param int nem_two: + :param bool linked: True to link, False to unlink + :return: core_pb2.EmaneLinkResponse + """ + request = core_pb2.EmaneLinkRequest( + session_id=session_id, nem_one=nem_one, nem_two=nem_two, linked=linked + ) + return self.stub.EmaneLink(request) + def connect(self): """ Open connection to server, must be closed manually. diff --git a/daemon/core/api/grpc/server.py b/daemon/core/api/grpc/server.py index 7fb6ed31..c438a414 100644 --- a/daemon/core/api/grpc/server.py +++ b/daemon/core/api/grpc/server.py @@ -21,7 +21,7 @@ from core.emulator.data import ( NodeData, ) from core.emulator.emudata import InterfaceData, LinkOptions, NodeOptions -from core.emulator.enumerations import EventTypes, LinkTypes, NodeTypes +from core.emulator.enumerations import EventTypes, LinkTypes, MessageFlags, NodeTypes from core.errors import CoreError from core.location.mobility import BasicRangeModel, Ns2ScriptedMobility from core.nodes.base import CoreNetworkBase @@ -1557,3 +1557,47 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer): continue interfaces.append(interface) return core_pb2.GetInterfacesResponse(interfaces=interfaces) + + def EmaneLink(self, request, context): + """ + Helps broadcast wireless link/unlink between EMANE nodes. + + :param core.api.grpc.core_pb2.EmaneLinkRequest request: get-interfaces request + :param grpc.ServicerContext context: context object + :return: emane link response with success status + :rtype: core.api.grpc.core_pb2.EmaneLinkResponse + """ + logging.debug("emane link: %s", request) + session = self.get_session(request.session_id, context) + nem_one = request.nem_one + emane_one, netif = session.emane.nemlookup(nem_one) + if not emane_one or not netif: + context.abort( + grpc.StatusCode.NOT_FOUND, "nem one {} not found".format(nem_one) + ) + node_one = netif.node + + nem_two = request.nem_two + emane_two, netif = session.emane.nemlookup(nem_two) + if not emane_two or not netif: + context.abort( + grpc.StatusCode.NOT_FOUND, "nem two {} not found".format(nem_two) + ) + node_two = netif.node + + if emane_one.id == emane_two.id: + if request.linked: + flag = MessageFlags.ADD.value + else: + flag = MessageFlags.DELETE.value + link = LinkData( + message_type=flag, + link_type=LinkTypes.WIRELESS.value, + node1_id=node_one.id, + node2_id=node_two.id, + network_id=emane_one.id, + ) + session.broadcast_link(link) + return core_pb2.EmaneLinkResponse(result=True) + else: + return core_pb2.EmaneLinkResponse(result=False) diff --git a/daemon/proto/core/api/grpc/core.proto b/daemon/proto/core/api/grpc/core.proto index 91049f40..68c83ad2 100644 --- a/daemon/proto/core/api/grpc/core.proto +++ b/daemon/proto/core/api/grpc/core.proto @@ -119,6 +119,8 @@ service CoreApi { // utilities rpc GetInterfaces (GetInterfacesRequest) returns (GetInterfacesResponse) { } + rpc EmaneLink (EmaneLinkRequest) returns (EmaneLinkResponse) { + } } // rpc request/response messages @@ -633,6 +635,17 @@ message GetInterfacesResponse { repeated string interfaces = 1; } +message EmaneLinkRequest { + int32 session_id = 1; + int32 nem_one = 2; + int32 nem_two = 3; + bool linked = 4; +} + +message EmaneLinkResponse { + bool result = 1; +} + // data structures for messages below message MessageType { enum Enum {