def _dispatch_message()

in ossdbtoolsservice/hosting/json_rpc_server.py [0:0]


    def _dispatch_message(self, message):
        """
        Dispatches a message that was received to the necessary handler
        :param message: The message that was received
        """
        if message.message_type in [JSONRPCMessageType.ResponseSuccess, JSONRPCMessageType.ResponseError]:
            # Responses need to be routed to the handler that requested them
            # TODO: Route to the handler or send error message
            return

        # Figure out which handler will execute the request/notification
        if message.message_type is JSONRPCMessageType.Request:
            if self._logger is not None:
                self._logger.info('Received request id=%s method=%s', message.message_id, message.message_method)
            handler = self._request_handlers.get(message.message_method)
            request_context = RequestContext(message, self._output_queue)

            # Make sure we got a handler for the request
            if handler is None:
                # TODO: Localize?
                request_context.send_error(f'Requested method is unsupported: {message.message_method}')
                if self._logger is not None:
                    self._logger.warn('Requested method is unsupported: %s', message.message_method)
                return

            # Call the handler with a request context and the deserialized parameter object
            if handler.class_ is None:
                # Don't attempt to do complex deserialization
                deserialized_object = message.message_params
            else:
                # Use the complex deserializer
                deserialized_object = handler.class_.from_dict(message.message_params)
            try:
                handler.handler(request_context, deserialized_object)
            except Exception as e:
                error_message = f'Unhandled exception while handling request method {message.message_method}: "{e}"'  # TODO: Localize
                if self._logger is not None:
                    self._logger.exception(error_message)
                request_context.send_error(error_message, code=-32603)
        elif message.message_type is JSONRPCMessageType.Notification:
            if self._logger is not None:
                self._logger.info('Received notification method=%s', message.message_method)
            handler = self._notification_handlers.get(message.message_method)

            if handler is None:
                # Ignore the notification
                if self._logger is not None:
                    self._logger.warn('Notification method %s is unsupported', message.message_method)
                return

            # Call the handler with a notification context
            notification_context = NotificationContext(self._output_queue)
            deserialized_object = None
            if handler.class_ is None:
                # Don't attempt to do complex deserialization
                deserialized_object = message.message_params
            else:
                # Use the complex deserializer
                deserialized_object = handler.class_.from_dict(message.message_params)
            try:
                handler.handler(notification_context, deserialized_object)
            except Exception:
                error_message = f'Unhandled exception while handling notification method {message.message_method}'
                if self._logger is not None:
                    self._logger.exception(error_message)
        else:
            # If this happens we have a serious issue with the JSON RPC reader
            if self._logger is not None:
                self._logger.warn('Received unsupported message type %s', message.message_type)
            return