grpc: removed add session server, achieved with start session providing servers for session

This commit is contained in:
Blake Harnden 2021-05-06 10:56:51 -07:00
parent 7e6b87101b
commit d40435fa68
6 changed files with 42 additions and 54 deletions

View file

@ -307,6 +307,7 @@ class CoreGrpcClient:
)
config_service_configs.append(config_service_config)
options = {k: v.value for k, v in session.options.items()}
servers = [x.to_proto() for x in session.servers]
request = core_pb2.StartSessionRequest(
session_id=session.id,
nodes=nodes,
@ -325,6 +326,7 @@ class CoreGrpcClient:
user=session.user,
definition=definition,
metadata=session.metadata,
servers=servers,
)
response = self.stub.StartSession(request)
return response.result, list(response.exceptions)
@ -421,22 +423,6 @@ class CoreGrpcClient:
response = self.stub.SetSessionState(request)
return response.result
def add_session_server(self, session_id: int, name: str, host: str) -> bool:
"""
Add distributed session server.
:param session_id: id of session
:param name: name of server to add
:param host: host address to connect to
:return: True for success, False otherwise
:raises grpc.RpcError: when session doesn't exist
"""
request = core_pb2.AddSessionServerRequest(
session_id=session_id, name=name, host=host
)
response = self.stub.AddSessionServer(request)
return response.result
def alert(
self,
session_id: int,

View file

@ -244,6 +244,10 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
session.options.set_config(key, value)
session.metadata = dict(request.metadata)
# add servers
for server in request.servers:
session.distributed.add_server(server.name, server.host)
# location
if request.HasField("location"):
grpcutils.session_location(session, request.location)
@ -477,6 +481,10 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
config_service_configs = grpcutils.get_node_config_service_configs(session)
session_file = str(session.file_path) if session.file_path else None
options = get_config_options(session.options.get_configs(), session.options)
servers = [
core_pb2.Server(name=x.name, host=x.host)
for x in session.distributed.servers.values()
]
session_proto = core_pb2.Session(
id=session.id,
state=session.state.value,
@ -497,24 +505,10 @@ class CoreGrpcServer(core_pb2_grpc.CoreApiServicer):
metadata=session.metadata,
file=session_file,
options=options,
servers=servers,
)
return core_pb2.GetSessionResponse(session=session_proto)
def AddSessionServer(
self, request: core_pb2.AddSessionServerRequest, context: ServicerContext
) -> core_pb2.AddSessionServerResponse:
"""
Add distributed server to a session.
:param request: get-session
request
:param context: context object
:return: add session server response
"""
session = self.get_session(request.session_id, context)
session.distributed.add_server(request.name, request.host)
return core_pb2.AddSessionServerResponse(result=True)
def SessionAlert(
self, request: core_pb2.SessionAlertRequest, context: ServicerContext
) -> core_pb2.SessionAlertResponse:

View file

@ -184,6 +184,19 @@ class ConfigServiceDefaults:
)
@dataclass
class Server:
name: str
host: str
@classmethod
def from_proto(cls, proto: core_pb2.Server) -> "Server":
return Server(name=proto.name, host=proto.host)
def to_proto(self) -> core_pb2.Server:
return core_pb2.Server(name=self.name, host=self.host)
@dataclass
class Service:
group: str
@ -775,6 +788,7 @@ class Session:
metadata: Dict[str, str] = field(default_factory=dict)
file: Path = None
options: Dict[str, ConfigOption] = field(default_factory=dict)
servers: List[Server] = field(default_factory=list)
@classmethod
def from_proto(cls, proto: core_pb2.Session) -> "Session":
@ -812,6 +826,7 @@ class Session:
node.mobility_config = ConfigOption.from_dict(mapped_config.config)
file_path = Path(proto.file) if proto.file else None
options = ConfigOption.from_dict(proto.options)
servers = [Server.from_proto(x) for x in proto.servers]
return Session(
id=proto.id,
state=SessionState(proto.state),
@ -827,6 +842,7 @@ class Session:
metadata=dict(proto.metadata),
file=file_path,
options=options,
servers=servers,
)
def add_node(

View file

@ -28,6 +28,7 @@ from core.api.grpc.wrappers import (
NodeServiceData,
NodeType,
Position,
Server,
ServiceConfig,
ServiceFileConfig,
Session,
@ -433,10 +434,6 @@ class CoreClient:
except grpc.RpcError as e:
self.app.show_grpc_exception("Edit Node Error", e)
def send_servers(self) -> None:
for server in self.servers.values():
self.client.add_session_server(self.session.id, server.name, server.address)
def get_links(self, definition: bool = False) -> Tuple[List[Link], List[Link]]:
if not definition:
self.ifaces_manager.set_macs([x.link for x in self.links.values()])
@ -457,10 +454,12 @@ class CoreClient:
links, asym_links = self.get_links(definition)
self.session.links = links
self.session.metadata = self.get_metadata()
self.session.servers.clear()
for server in self.servers.values():
self.session.servers.append(Server(name=server.name, host=server.address))
result = False
exceptions = []
try:
self.send_servers()
result, exceptions = self.client.start_session(
self.session, asym_links, definition
)

View file

@ -2,7 +2,7 @@ import argparse
import logging
from core.api.grpc import client
from core.api.grpc.wrappers import NodeType, Position
from core.api.grpc.wrappers import NodeType, Position, Server
def log_event(event):
@ -19,12 +19,10 @@ def main(args):
# create session
session = core.add_session()
logging.info("created session: %s", session.id)
# add distributed server
server_name = "core2"
result = core.add_session_server(session.id, server_name, args.server)
logging.info("added session server: %s", result)
server = Server(name="core2", host=args.server)
session.servers.append(server)
# handle events session may broadcast
core.events(session.id, log_event)
@ -35,7 +33,7 @@ def main(args):
position = Position(x=100, y=50)
node1 = session.add_node(2, position=position)
position = Position(x=200, y=50)
node2 = session.add_node(3, position=position, server=server_name)
node2 = session.add_node(3, position=position, server=server.name)
# create links
iface1 = interface_helper.create_iface(node1.id, 0)

View file

@ -27,8 +27,6 @@ service CoreApi {
}
rpc SetSessionState (SetSessionStateRequest) returns (SetSessionStateResponse) {
}
rpc AddSessionServer (AddSessionServerRequest) returns (AddSessionServerResponse) {
}
rpc SessionAlert (SessionAlertRequest) returns (SessionAlertResponse) {
}
@ -162,6 +160,7 @@ message StartSessionRequest {
string user = 15;
bool definition = 16;
map<string, string> metadata = 17;
repeated Server servers = 18;
}
message StartSessionResponse {
@ -226,16 +225,6 @@ message SetSessionStateResponse {
bool result = 1;
}
message AddSessionServerRequest {
int32 session_id = 1;
string name = 2;
string host = 3;
}
message AddSessionServerResponse {
bool result = 1;
}
message SessionAlertRequest {
int32 session_id = 1;
ExceptionLevel.Enum level = 2;
@ -617,6 +606,7 @@ message Session {
map<string, string> metadata = 17;
string file = 18;
map<string, common.ConfigOption> options = 19;
repeated Server servers = 20;
}
message SessionSummary {
@ -707,3 +697,8 @@ message Geo {
float lon = 2;
float alt = 3;
}
message Server {
string name = 1;
string host = 2;
}