def __init__(self, connection_manager, channel, grpc_timeout): self.connection_manager = connection_manager self.channel = channel self.grpc_timeout = grpc_timeout self.local_stub = VolthaLocalServiceStub(channel) self.stopped = False self.packet_out_queue = Queue() # queue to send out PacketOut msgs self.packet_in_queue = DeferredQueue() # queue to receive PacketIn self.change_event_queue = DeferredQueue() # queue change events
class GrpcClient(object): def __init__(self, connection_manager, channel, grpc_timeout): self.log = get_logger() self.connection_manager = connection_manager self.channel = channel self.grpc_timeout = grpc_timeout self.local_stub = VolthaLocalServiceStub(channel) self.stopped = False self.packet_out_queue = Queue() # queue to send out PacketOut msgs self.packet_in_queue = DeferredQueue() # queue to receive PacketIn self.change_event_queue = DeferredQueue() # queue change events self.count_pkt_in = 0 self.count_pkt_out = 0 def start(self): self.log.debug('starting', grpc_timeout=self.grpc_timeout) self.start_packet_out_stream() self.start_packet_in_stream() self.start_change_event_in_stream() reactor.callLater(0, self.packet_in_forwarder_loop) reactor.callLater(0, self.change_event_processing_loop) self.log.info('started') return self def stop(self): self.log.debug('stopping') self.stopped = True self.log.info('stopped') def start_packet_out_stream(self): def packet_generator(): while 1: try: packet = self.packet_out_queue.get(block=True, timeout=1.0) except Empty: if self.stopped: return else: self.count_pkt_out += 1 self.log.debug('counters grpc_client OUT - {}'.format( self.count_pkt_out)) yield packet def stream_packets_out(): generator = packet_generator() try: self.local_stub.StreamPacketsOut(generator) except _Rendezvous, e: self.log.error('grpc-exception', status=e.code()) if e.code() == StatusCode.UNAVAILABLE: os.system("kill -15 {}".format(os.getpid())) reactor.callInThread(stream_packets_out)