Service State Machine (fsm)

Implementation of the OSI Upper Layer Services DICOM, Part 8, Section 7

class pynetdicom2.fsm.PContextDef(id: int, sop_class: UID, supported_ts: UID)

Presentation Context Definition.

class pynetdicom2.fsm.States(*values)

Services states enumeration.

STA_1 = 0

Idle

STA_2 = 1

Transport Connection Open (Awaiting A-ASSOCIATE-RQ PDU)

STA_3 = 2

Awaiting Local A-ASSOCIATE response primitive (from local user)

STA_4 = 3

Awaiting transport connection opening to complete (from local transport service)

STA_5 = 4

Awaiting A-ASSOCIATE-AC or A-ASSOCIATE-RJ PDU

STA_6 = 5

Association established and ready for data transfer

STA_7 = 6

Awaiting A-RELEASE-RP PDU

STA_8 = 7

Awaiting local A-RELEASE response primitive (from local user)

STA_9 = 8

Release collision requestor side; awaiting A-RELEASE response (from local user)

STA_10 = 9

Release collision acceptor side; awaiting A-RELEASE-RP PDU

STA_11 = 10

Release collision requestor side; awaiting A-RELEASE-RP PDU

STA_12 = 11

Release collision acceptor side; awaiting A-RELEASE response primitive (from local user)

STA_13 = 12

Awaiting Transport Connection Close Indication (Association no longer exists)

class pynetdicom2.fsm.Events(*values)

Events enumeration.

EVT_1 = 0

A-ASSOCIATE request (local user)

EVT_2 = 1

Transport connect confirmation (local transport service)

EVT_3 = 2

A-ASSOCIATE-AC PDU (received on transport connection)

EVT_4 = 3

A-ASSOCIATE-RJ PDU (received on transport connection)

EVT_5 = 4

Transport connection indication (local transport service)

EVT_6 = 5

A-ASSOCIATE-RQ PDU (on transport connection)

EVT_7 = 6

A-ASSOCIATE response primitive (accept)

EVT_8 = 7

A-ASSOCIATE response primitive (reject)

EVT_9 = 8

P-DATA request primitive

EVT_10 = 9

P-DATA-TF PDU (on transport connection)

EVT_11 = 10

A-RELEASE request primitive

EVT_12 = 11

A-RELEASE-RQ PDU (on transport)

EVT_13 = 12

A-RELEASE-RP PDU (on transport)

EVT_14 = 13

A-RELEASE response primitive

EVT_15 = 14

A-ABORT request primitive

EVT_16 = 15

A-ABORT PDU (on transport)

EVT_17 = 16

Transport connection closed

EVT_18 = 17

ARTIM timer expired (rej/rel)

EVT_19 = 18

Unrecognized/invalid PDU

pynetdicom2.fsm.PEER_PDU_EVENTS = frozenset({Events.EVT_10, Events.EVT_12, Events.EVT_13, Events.EVT_16, Events.EVT_19, Events.EVT_3, Events.EVT_4, Events.EVT_6})

Events triggered by PDUs received from the peer. If one of these occurs in a state that defines no transition for it, the peer violated the protocol and the association is aborted instead of raising a local error.

class pynetdicom2.fsm.DecodablePDU(*args, **kwargs)

Structural type for PDU classes that can decode themselves.

Every concrete PDU class in pdu satisfies this protocol. It lets lookup tables such as the DUL provider’s PDU_TYPES be typed as Type[DecodablePDU] instead of an invalid Type[Union[...]].

classmethod decode(raw_bytes: bytes) Any

Decodes a PDU instance from its binary representation.

class pynetdicom2.fsm.ProviderProto(*args, **kwargs)

DUL Provider protocol

create_socket() None

Creates and connects a new TCP socket

class pynetdicom2.fsm.Timer(max_seconds: int)

A small helper timer class

start() None

Sets a timer

stop() None

Stops a timer

restart() None

Restarts a timer

check() bool

Checks if timer has expired

class pynetdicom2.fsm.StateMachine(provider: ProviderProto, timer: Timer, store_in_file: set[UID], get_file_cb: Callable[[PContextDef, Dataset], tuple[BinaryIO, int]])

Service State Machine implementation.

Variables:
  • current_state – current state

  • provider – DUL provider

  • timer

  • store_in_file – set of SOP Class UIDs, for which incoming datasets should be stored in a file, rather than in-memory

  • get_file_cb – callback for getting a file object for storage

  • accepted_contexts – accepted presentation contexts in current association

  • dimse_decoder – decoder for incoming P-DATA-TF PDUs, used to re-create incoming DIMSE message

  • transition_table – state machine transition table

property primitive: AAssociatePDUBase | AAssociateRqPDU | AAssociateAcPDU | AAssociateRjPDU | PDataTfPDU | AReleasePDUBase | AReleaseRqPDU | AReleaseRpPDU | AAbortPDU | None

Current PDU.

property dul_socket: socket

TCP Socket

property to_service_user: Queue[tuple[DIMSEMessage, int] | AAssociatePDUBase | AAssociateRqPDU | AAssociateAcPDU | AAssociateRjPDU | PDataTfPDU | AReleasePDUBase | AReleaseRqPDU | AReleaseRpPDU | AAbortPDU]

Outgoing PDU/DIMSE message queue

action(event: Events) None

Execute the action triggered by event.

If the event has no defined transition for the current state, an unexpected PDU received from the peer results in an abort being sent (per PS3.8), while any other event raises a descriptive error.

Parameters:

event – event to be handled

Raises:

exceptions.NetDICOMError – if the event is not a peer PDU and has no defined transition for the current state

ae_1() States

Issue TransportConnect request primitive to local transport service.

ae_2() States

Send A_ASSOCIATE-RQ PDU.

ae_3() States

Issue A-ASSOCIATE confirmation (accept) primitive.

ae_4() States

Issue A-ASSOCIATE confirmation (reject) primitive and close transport connection.

ae_5() States

Issue transport connection response primitive; start ARTIM timer.

ae_6() States

Check A-ASSOCIATE-RQ.

Stop ARTIM timer and issue A-ASSOCIATE indication primitive; per PS3.8 Table 9-6 (AE-6) the next state is always Sta3. Refusing a request is initiated by the local user through reject(), not by this action, so there is no path from here to Sta13.

ae_7() States

Send A-ASSOCIATE-AC PDU.

ae_8() States

Send A-ASSOCIATE-RJ PDU.

Per PS3.8 Table 9-6 (AE-8): send the A-ASSOCIATE-RJ PDU and move to Sta13. The ARTIM timer is started so the wait for the transport connection to close is bounded even if the peer never closes it.

dt_1() States

Send P-DATA-TF PDU.

dt_2() States

Send P-DATA indication primitive.

ar_1() States

Send A-RELEASE-RQ PDU.

ar_2() States

Send A-RELEASE indication primitive.

ar_3() States

Issue A-RELEASE confirmation primitive and close transport connection.

ar_4() States

Issue A-RELEASE-RP PDU and start ARTIM timer.

ar_5() States

Stop ARTIM timer.

ar_6() States

Issue P-DATA indication.

ar_7() States

Issue P-DATA-TF PDU.

ar_8() States

Issue A-RELEASE indication (release collision).

ar_9() States

Send A-RELEASE-RP PDU.

ar_10() States

Issue A-RELEASE confirmation primitive.

aa_1() States

Send A-ABORT PDU (service-user source) and start (or restart) ARTIM timer.

aa_2() States

Stop ARTIM timer if running. Close transport connection.

aa_3() States

Issue A-ABORT or A-P-ABORT indication and close transport connection.

If (service-user initiated abort):

  • Issue A-ABORT indication and close transport connection.

Otherwise (service-provider initiated abort):

  • Issue A-P-ABORT indication and close transport connection.

This action is triggered by the reception of an A-ABORT PDU.

aa_4() States

Issue A-P-ABORT indication primitive.

This action is triggered when the transport connection is closed unexpectedly: the abort is initiated by the DICOM UL service provider, not by the service user (PS3.8 9.3.8).

aa_5() States

Stop ARTIM timer.

aa_6() States

Ignore PDU.

aa_7() States

Send A-ABORT PDU.

aa_8() States

Send A-ABORT PDU, issue an A-P-ABORT indication and start ARTIM timer.

class pynetdicom2.fsm.DIMSEDecoder(accepted_contexts: dict[int, PContextDef], store_in_file: set[UID], get_file_cb: Callable[[PContextDef, Dataset], tuple[BinaryIO, int]])

DIMSE Message decoder.

Decodes incoming P-DATA-TF PDUs into DIMSE message instance.

Variables:
  • accepted_contexts – accepted presentation contexts in current association

  • store_in_file – set of SOP Class UIDs, for which incoming datasets should be stored in a file, rather than in-memory

  • get_file_cb – callback for getting a file object for storage

  • receivingTrue if DIMSEDecoder instance has not received all P-DATA-TF PDUs for the current DIMSE message

  • command_set_receivedTrue if Command Set for DIMSE message is received

  • data_set_receivedTrue if Dataset for DIMSE message is received

  • pc_id – Presentation Context ID

  • msg – decoded DIMSE message

process(p_data: PDataTfPDU) None

Processes new incoming P-DATA-TF PDU

Parameters:

p_data – incoming P-DATA-TF PDU

Raises:

exceptions.DIMSEProcessingError – raised if unknown PDV type is encountered