示例#1
0
    def _process_open_channel(self, conn, msg_data):
        """
        Description: process open channel message
        Input:
            conn: a socket connection
            msg_data: msg body, serialized by protobuf
        Returns: True or False
        """
        request = presenter_message_pb2.OpenChannelRequest()
        response = presenter_message_pb2.OpenChannelResponse()
        if not self._parse_protobuf(request, msg_data):
            channel_name = "unknown channel"
            err_code = presenter_message_pb2.kOpenChannelErrorOther
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)
        channel_name = request.channel_name

        # check channel name if exist
        if not self.channel_manager.is_channel_exist(channel_name):
            logging.error("channel name %s is not exist.", channel_name)
            err_code = presenter_message_pb2.kOpenChannelErrorNoSuchChannel
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)
            #ret = self.channel_manager.register_one_channel(channel_name)
            #if ret != ChannelManager.err_code_ok:
            #    logging.error("Create the channel %s failed!, and ret is %d", channel_name, ret)
            #    err_code =  pb2.kOpenChannelErrorOther
            #    self._response_open_channel(conn, channel_name, response, err_code)

        # check channel path if busy
        if self.channel_manager.is_channel_busy(channel_name):
            logging.error("channel path %s is busy.", channel_name)
            err = presenter_message_pb2.kOpenChannelErrorChannelAlreadyOpened
            return self._response_open_channel(conn, channel_name, response,
                                               err)

        content_type = presenter_message_pb2.kChannelContentTypeVideo
        if request.content_type == content_type:
            media_type = "video"
        else:
            logging.error("media type %s is not recognized.",
                          request.content_type)
            err_code = presenter_message_pb2.kOpenChannelErrorOther
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)

        handler = FacialRecognitionHandler(channel_name, media_type)
        sock = conn.fileno()
        self.channel_manager.create_channel_resource(channel_name, sock,
                                                     media_type, handler)
        err_code = presenter_message_pb2.kOpenChannelErrorNone
        return self._response_open_channel(conn, channel_name, response,
                                           err_code)
def response_open_channel(client):
    """func"""
    try:
        message_head = client.recv(5)
        s = struct.Struct('IB')
        (message_len, messagename_len) = s.unpack(message_head)
        message_len = socket.ntohl(message_len)
        client.recv(messagename_len)
        message_body = client.recv(message_len - 5 - messagename_len)
        open_channel_response = pb.OpenChannelResponse()
        open_channel_response.ParseFromString(message_body)
        if open_channel_response.error_code == PB_OPEN_CHANNEL_ERROR_NONE:
            return True
        return open_channel_response.error_code

    except socket.error:
        return False
    def _process_open_channel(self, conn, msg_data):
        """
        Deserialization protobuf and process open_channel request
        Args:
            conn: a socket connection
            msg_data: a protobuf struct, include open channel request.

        Returns:

        protobuf structure like this:
         ----------------------------------------------
        |channel_name        |    string               |
        |----------------------------------------------
        |content_type        |    ChannelContentType   |
        |----------------------------------------------

        enum ChannelContentType {
            kChannelContentTypeImage = 0;
            kChannelContentTypeVideo = 1;
        }
        """
        request = pb2.OpenChannelRequest()
        response = pb2.OpenChannelResponse()

        try:
            request.ParseFromString(msg_data)
        except DecodeError:
            logging.error("ParseFromString exception: Error parsing message")
            channel_name = "unknown channel"
            return self._response_open_channel(conn, channel_name, response,
                                               pb2.kOpenChannelErrorOther)

        channel_name = request.channel_name

        # check channel name if exist
        if not self.channel_manager.is_channel_exist(channel_name):
            logging.error("rrr channel name %s is not exist.", channel_name)
            # if channel is not exist, need to create the channel
            ret = self.channel_manager.register_one_channel(channel_name)
            if ret != ChannelManager.err_code_ok:
                logging.error("Create the channel %s failed!, and ret is %d",
                              channel_name, ret)
                err_code = pb2.kOpenChannelErrorOther
                self._response_open_channel(conn, channel_name, response,
                                            err_code)

        # check channel path if busy
        if self.channel_manager.is_channel_busy(channel_name):
            logging.error("channel path %s is busy.", channel_name)
            err_code = pb2.kOpenChannelErrorChannelAlreadyOpened
            return self._response_open_channel(conn, channel_name, response,
                                               err_code)

        # if channel type is image, need clean image if exist
        self.channel_manager.clean_channel_image(channel_name)

        if request.content_type == pb2.kChannelContentTypeImage:
            media_type = "image"
        elif request.content_type == pb2.kChannelContentTypeVideo:
            media_type = "video"
        else:
            logging.error("media type %s is not recognized.",
                          request.content_type)
            return self._response_open_channel(conn, channel_name, response,
                                               pb2.kOpenChannelErrorOther)

        handler = ChannelHandler(channel_name, media_type)
        self.channel_manager.create_channel_resource(channel_name,
                                                     conn.fileno(), media_type,
                                                     handler)

        return self._response_open_channel(conn, channel_name, response,
                                           pb2.kOpenChannelErrorNone)