import logging
import json
import asyncio

from google.protobuf import json_format

from umbra.common.protobuf.umbra_grpc import MonitorBase
from umbra.common.protobuf.umbra_pb2 import Instruction, Snapshot

from umbra.monitor.tools import Tools

logger = logging.getLogger(__name__)
logging.getLogger("hpack").setLevel(logging.WARNING)


class Monitor(MonitorBase):
    def __init__(self, info):
        self.tools = Tools()

    async def Listen(self, stream):
        logging.debug("Instruction Received")
        instruction: Instruction = await stream.recv_message()        
        instruction_dict = json_format.MessageToDict(instruction, preserving_proto_field_name=True)
        snapshot_dict = await self.tools.handle(instruction_dict)
        snapshot = json_format.ParseDict(snapshot_dict, Snapshot())
        await stream.send_message(snapshot)

[((12, 9, 12, 36), 'logging.getLogger', 'logging.getLogger', ({(12, 27, 12, 35): '__name__'}, {}), '(__name__)', False, 'import logging\n'), ((13, 0, 13, 26), 'logging.getLogger', 'logging.getLogger', ({(13, 18, 13, 25): '"""hpack"""'}, {}), "('hpack')", False, 'import logging\n'), ((18, 21, 18, 28), 'umbra.monitor.tools.Tools', 'Tools', ({}, {}), '()', False, 'from umbra.monitor.tools import Tools\n'), ((21, 8, 21, 45), 'logging.debug', 'logging.debug', ({(21, 22, 21, 44): '"""Instruction Received"""'}, {}), "('Instruction Received')", False, 'import logging\n'), ((23, 27, 23, 99), 'google.protobuf.json_format.MessageToDict', 'json_format.MessageToDict', (), '', False, 'from google.protobuf import json_format\n'), ((25, 56, 25, 66), 'umbra.common.protobuf.umbra_pb2.Snapshot', 'Snapshot', ({}, {}), '()', False, 'from umbra.common.protobuf.umbra_pb2 import Instruction, Snapshot\n')]