def _AVP__encode_data(self): '''生成子avp的实例,递归编码''' self.avp['AVP_BUF'] = '' # 遍历传入的列表,生成实例,进行编码 for sub_avp_obj_ in self.avp['AVP_DATA']: avp_instance = avp_factory.create_avp(self.my_cmd_code, sub_avp_obj_.keys()[0], sub_avp_obj_.values()[0], None, self.dcc) avp_instance.encode() self.avp['SUB_AVP'].append(avp_instance) self.avp['AVP_BUF'] += avp_instance.avp['AVP_BUF'] return self.avp['AVP_BUF']
def unpack(self, pack_buf): '''解包DCC消息,返回解包后的一个字典 首先解析包头,再解析具体包体 解码BUF,输出解码后的实例 ''' self.dmsg['DCC_STAT'] = self.dcc.dcc_def.const.DECODE_DCC_AVP_BEGIN self.dmsg['DCC_BUF'] = pack_buf avp_pack_buf = "" # 解析包头 offset = self.__unpack_head(self.dmsg['DCC_BUF']) # 根据 cmd_code 获取 DCC_NAME _cmd_code = (int(self.dmsg['DCC_CODE']), int(self.dmsg['DCC_REQUEST'])) self.dmsg['DCC_NAME'] = self.dcc.dcc_cfg.Code2Cmd[_cmd_code][2] self.dmsg['DCC_STAT'] = self.dcc.dcc_def.const.DECODE_DCC_MSG_BODY_BEGIN while offset != self.dmsg['DCC_LENGTH']: # 确定具体需要解包的AVP BUF avp_pack_buf = self.dmsg['DCC_BUF'][offset:] try: # 返回需要解析AVP的AVP_INSTANCE avp_instance = create_avp(cmd_code = _cmd_code, avp_code = None, avp_data = None, decode_buf = avp_pack_buf, dcc_instance = self.dcc ) except Exception, e: raise self.dcc.dcc_err.DccE_CreateAvpError, \ "Create Avp Instance Error: %s" % e # AVP解包,并且将解包后结果添加到self.dmsg['DCC_AVP_LIST'] avp_instance.decode() self.dmsg['DCC_AVP_LIST'].append(avp_instance) # avp['AVP_LENGTH'] 因为是标明的长度,但是dcc要求是4的倍数,所以在这里进行修正 lengthset = (avp_instance.avp['AVP_LENGTH'] + 3) // 4 * 4 offset += lengthset
def _AVP__decode_data(self, offset): '''重载,将AVP_BUF多次进行解析''' offset_ = offset self.avp['AVP_DATA'] = [] while offset_ < self.avp['AVP_LENGTH']: sub_buf_ = self.avp['AVP_BUF'][offset_:] avp_instance = avp_factory.create_avp(self.my_cmd_code, None, None, sub_buf_, self.dcc) avp_instance.decode() self.avp['SUB_AVP'].append(avp_instance) self.avp['AVP_DATA'].append(repr({repr(avp_instance.avp['AVP_CODE']):avp_instance.avp['AVP_DATA']})) # avp['AVP_LENGTH'] 因为是标明的长度,但是dcc要求是4的倍数,所以在这里进行修正 lengthset = (avp_instance.avp['AVP_LENGTH'] + 3) // 4 * 4 offset_ += lengthset
def __pack_AVP(self, cmd_code, avp_list): '''将AVP都编码打包到一起''' self.dmsg['DCC_STAT'] = self.dcc.dcc_def.const.ENCODE_DCC_MSG_BODY_BEGIN pack_buf = "" for decode_avp_dict in avp_list: avp_instance = create_avp(cmd_code, decode_avp_dict.keys()[0], decode_avp_dict.values()[0], None, self.dcc ) avp_instance.encode() # 将创建的AVP添加入DCC_AVP_LIST self.dmsg['DCC_AVP_LIST'].append(avp_instance) # 将所有编码后的数据整合到一起 pack_buf += avp_instance.avp['AVP_BUF'] self.dmsg['DCC_STAT'] = self.dcc.dcc_def.const.ENCODE_DCC_MSG_BODY_END return pack_buf