grpc added client disconnect detection for streams

This commit is contained in:
bharnden 2019-03-18 22:32:01 -07:00
parent e282b3b8f8
commit 84ff1f4275

View file

@ -2,7 +2,7 @@ import logging
import os import os
import tempfile import tempfile
import time import time
from Queue import Queue from Queue import Queue, Empty
from itertools import repeat from itertools import repeat
import grpc import grpc
@ -377,175 +377,193 @@ class CoreApiServer(core_pb2_grpc.CoreApiServicer):
queue = Queue() queue = Queue()
session.node_handlers.append(lambda x: queue.put(x)) session.node_handlers.append(lambda x: queue.put(x))
while True: while context.is_active():
node = queue.get() try:
node_event = core_pb2.NodeEvent() node = queue.get(timeout=1)
update_proto( node_event = core_pb2.NodeEvent()
node_event.node, update_proto(
id=node.id, node_event.node,
name=node.name, id=node.id,
model=node.model name=node.name,
) model=node.model
update_proto( )
node_event.node.position, update_proto(
x=node.x_position, node_event.node.position,
y=node.y_position x=node.x_position,
) y=node.y_position
services = node.services or "" )
node_event.node.services.extend(services.split("|")) services = node.services or ""
yield node_event node_event.node.services.extend(services.split("|"))
yield node_event
except Empty:
continue
def LinkEvents(self, request, context): def LinkEvents(self, request, context):
session = self.get_session(request.id, context) session = self.get_session(request.id, context)
queue = Queue() queue = Queue()
session.link_handlers.append(lambda x: queue.put(x)) session.link_handlers.append(lambda x: queue.put(x))
while True: while context.is_active():
event = queue.get() try:
link_event = core_pb2.LinkEvent() event = queue.get(timeout=1)
if event.interface1_id is not None: link_event = core_pb2.LinkEvent()
interface_one = link_event.link.interface_one if event.interface1_id is not None:
update_proto( interface_one = link_event.link.interface_one
interface_one, update_proto(
id=event.interface1_id, interface_one,
name=event.interface1_name, id=event.interface1_id,
mac=convert_value(event.interface1_mac), name=event.interface1_name,
ip4=convert_value(event.interface1_ip4), mac=convert_value(event.interface1_mac),
ip4mask=event.interface1_ip4_mask, ip4=convert_value(event.interface1_ip4),
ip6=convert_value(event.interface1_ip6), ip4mask=event.interface1_ip4_mask,
ip6mask=event.interface1_ip6_mask, ip6=convert_value(event.interface1_ip6),
) ip6mask=event.interface1_ip6_mask,
)
if event.interface2_id is not None: if event.interface2_id is not None:
interface_two = link_event.link.interface_two interface_two = link_event.link.interface_two
update_proto( update_proto(
interface_two, interface_two,
id=event.interface2_id, id=event.interface2_id,
name=event.interface2_name, name=event.interface2_name,
mac=convert_value(event.interface2_mac), mac=convert_value(event.interface2_mac),
ip4=convert_value(event.interface2_ip4), ip4=convert_value(event.interface2_ip4),
ip4mask=event.interface2_ip4_mask, ip4mask=event.interface2_ip4_mask,
ip6=convert_value(event.interface2_ip6), ip6=convert_value(event.interface2_ip6),
ip6mask=event.interface2_ip6_mask, ip6mask=event.interface2_ip6_mask,
) )
link_event.message_type = event.message_type link_event.message_type = event.message_type
update_proto( update_proto(
link_event.link, link_event.link,
type=event.link_type, type=event.link_type,
node_one=event.node1_id, node_one=event.node1_id,
node_two=event.node2_id node_two=event.node2_id
) )
update_proto( update_proto(
link_event.link.options, link_event.link.options,
opaque=event.opaque, opaque=event.opaque,
jitter=event.jitter, jitter=event.jitter,
key=event.key, key=event.key,
mburst=event.mburst, mburst=event.mburst,
mer=event.mer, mer=event.mer,
per=event.per, per=event.per,
bandwidth=event.bandwidth, bandwidth=event.bandwidth,
burst=event.burst, burst=event.burst,
delay=event.delay, delay=event.delay,
dup=event.dup, dup=event.dup,
unidirectional=event.unidirectional unidirectional=event.unidirectional
) )
yield link_event yield link_event
except Empty:
continue
def SessionEvents(self, request, context): def SessionEvents(self, request, context):
session = self.get_session(request.id, context) session = self.get_session(request.id, context)
queue = Queue() queue = Queue()
session.event_handlers.append(lambda x: queue.put(x)) session.event_handlers.append(lambda x: queue.put(x))
while True: while context.is_active():
event = queue.get() try:
session_event = core_pb2.SessionEvent() event = queue.get(timeout=1)
event_time = event.time session_event = core_pb2.SessionEvent()
if event_time is not None: event_time = event.time
event_time = float(event_time) if event_time is not None:
update_proto( event_time = float(event_time)
session_event, update_proto(
node=event.node, session_event,
event=event.event_type, node=event.node,
name=event.name, event=event.event_type,
data=event.data, name=event.name,
time=event_time, data=event.data,
session=session.session_id time=event_time,
) session=session.session_id
yield session_event )
yield session_event
except Empty:
continue
def ConfigEvents(self, request, context): def ConfigEvents(self, request, context):
session = self.get_session(request.id, context) session = self.get_session(request.id, context)
queue = Queue() queue = Queue()
session.config_handlers.append(lambda x: queue.put(x)) session.config_handlers.append(lambda x: queue.put(x))
while True: while context.is_active():
event = queue.get() try:
config_event = core_pb2.ConfigEvent() event = queue.get(timeout=1)
update_proto( config_event = core_pb2.ConfigEvent()
config_event, update_proto(
message_type=event.message_type, config_event,
node=event.node, message_type=event.message_type,
object=event.object, node=event.node,
type=event.type, object=event.object,
captions=event.captions, type=event.type,
bitmap=event.bitmap, captions=event.captions,
data_values=event.data_values, bitmap=event.bitmap,
possible_values=event.possible_values, data_values=event.data_values,
groups=event.groups, possible_values=event.possible_values,
session=event.session, groups=event.groups,
interface=event.interface_number, session=event.session,
network_id=event.network_id, interface=event.interface_number,
opaque=event.opaque network_id=event.network_id,
) opaque=event.opaque
config_event.data_types.extend(event.data_types) )
yield config_event config_event.data_types.extend(event.data_types)
yield config_event
except Empty:
continue
def ExceptionEvents(self, request, context): def ExceptionEvents(self, request, context):
session = self.get_session(request.id, context) session = self.get_session(request.id, context)
queue = Queue() queue = Queue()
session.exception_handlers.append(lambda x: queue.put(x)) session.exception_handlers.append(lambda x: queue.put(x))
while True: while context.is_active():
event = queue.get() try:
exception_event = core_pb2.ExceptionEvent() event = queue.get(timeout=1)
event_time = event.date exception_event = core_pb2.ExceptionEvent()
if event_time is not None: event_time = event.date
event_time = float(event_time) if event_time is not None:
update_proto( event_time = float(event_time)
exception_event, update_proto(
node=event.node, exception_event,
session=event.session, node=event.node,
level=event.level, session=event.session,
source=event.source, level=event.level,
date=event_time, source=event.source,
text=event.text, date=event_time,
opaque=event.opaque text=event.text,
) opaque=event.opaque
yield exception_event )
yield exception_event
except Empty:
continue
def FileEvents(self, request, context): def FileEvents(self, request, context):
session = self.get_session(request.id, context) session = self.get_session(request.id, context)
queue = Queue() queue = Queue()
session.file_handlers.append(lambda x: queue.put(x)) session.file_handlers.append(lambda x: queue.put(x))
while True: while context.is_active():
event = queue.get() try:
file_event = core_pb2.FileEvent() event = queue.get(timeout=1)
update_proto( file_event = core_pb2.FileEvent()
file_event, update_proto(
message_type=event.message_type, file_event,
node=event.node, message_type=event.message_type,
name=event.name, node=event.node,
mode=event.mode, name=event.name,
number=event.number, mode=event.mode,
type=event.type, number=event.number,
source=event.source, type=event.type,
session=event.session, source=event.source,
data=event.data, session=event.session,
compressed_data=event.compressed_data data=event.data,
) compressed_data=event.compressed_data
yield file_event )
yield file_event
except Empty:
continue
def CreateNode(self, request, context): def CreateNode(self, request, context):
session = self.get_session(request.session, context) session = self.get_session(request.session, context)