def encode_msg(self, context_id, max_pdu_length): """Yield P-DATA primitive(s) for the current DIMSE Message. **Encoding** The encoding of the Command Set shall be Little Endian Implicit VR, while the Data Set will be encoded as per the agreed presentation context. A P-DATA request PDV List parameter shall contain one or more PDVs. Each PDV is wholly contained in a given P-DATA request and doesn't span across several P-DATA request primitives. The fragmentation of any message results in a series of PDVs that shall be sent, on a given association, by a corresponding series of P-DATA requests preserving the ordering of the fragments of any message. No fragments of any other messages shall be sent until all fragments of the current message have been sent. Parameters ---------- context_id : int The ID of the agreed presentation context. max_pdu_length : int The maximum PDV length in bytes. Yields ------ pdu_primitives.P_DATA The current DIMSE message as one or more P-DATA service primitives. References ---------- * DICOM Standard, Part 7, Section 6.3.1 * DICOM Standard, Part 8, Annex E """ self.ID = context_id # The Command Set is always Little Endian Implicit VR (PS3.7 6.3.1) # encode(dataset, is_implicit_VR, is_little_endian) encoded_command_set = encode(self.command_set, True, True) # COMMAND SET (always) # Split the command set into fragments with maximum size max_pdu_length no_fragments = ceil(len(encoded_command_set) / (max_pdu_length - 6)) cmd_fragments = self._generate_pdv_fragments(encoded_command_set, max_pdu_length) # First to (n - 1)th command data fragment - bits xxxxxx01 for ii in range(int(no_fragments - 1)): pdata = P_DATA() pdata.presentation_data_value_list.append( [context_id, b'\x01' + next(cmd_fragments)] ) yield pdata # Last command data fragment - bits xxxxxx11 pdata = P_DATA() pdata.presentation_data_value_list.append( [context_id, b'\x03' + next(cmd_fragments)] ) yield pdata # DATASET (if available) # Check that the Data Set is not empty if self.data_set is not None: encoded_data_set = self.data_set.getvalue() if encoded_data_set: # Split the data set into fragments with maximum # size max_pdu_length no_fragments = ceil( len(encoded_data_set) / (max_pdu_length - 6) ) ds_fragments = self._generate_pdv_fragments(encoded_data_set, max_pdu_length) # First to (n - 1)th dataset fragment - bits xxxxxx00 for ii in range(int(no_fragments - 1)): pdata = P_DATA() pdata.presentation_data_value_list.append( [context_id, b'\x00' + next(ds_fragments)] ) yield pdata # Last dataset fragment - bits xxxxxx10 pdata = P_DATA() pdata.presentation_data_value_list.append( [context_id, b'\x02' + next(ds_fragments)] ) yield pdata
def test_assignment(self): """ Check assignment works correctly """ primitive = P_DATA() primitive.presentation_data_value_list = [[1, b'\x00']] self.assertEqual(primitive.presentation_data_value_list, [[1, b'\x00']])