/
fsm.py
1275 lines (995 loc) · 35 KB
/
fsm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
The DUL's finite state machine representation.
"""
import logging
import queue
from typing import TYPE_CHECKING, cast
from pynetdicom import evt
from pynetdicom.pdu import (
A_ASSOCIATE_RQ,
A_ASSOCIATE_RJ,
A_ASSOCIATE_AC,
P_DATA_TF,
A_RELEASE_RQ,
A_RELEASE_RP,
A_ABORT_RQ,
)
from pynetdicom.pdu_primitives import A_P_ABORT, A_ABORT
from pynetdicom.transport import T_CONNECT
if TYPE_CHECKING: # pragma: no cover
from pynetdicom.dul import DULServiceProvider
from pynetdicom.transport import AssociationSocket
from pynetdicom.pdu_primitives import A_ASSOCIATE, P_DATA, A_RELEASE
LOGGER = logging.getLogger("pynetdicom.sm")
class InvalidEventError(Exception):
"""Exception for use when an invalid event occurs for a given state."""
pass
# pylint: disable=invalid-name
class StateMachine:
"""Implementation of the DICOM Upper Layer State Machine.
Attributes
----------
current_state : str
The current state of the state machine, ``'Sta1'`` to ``'Sta13'``.
dul : dul.DULServiceProvider
The DICOM Upper Layer service instance for the local AE
References
----------
* DICOM Standard, Part 8, :dcm:`Section 9.2<part08/sect_9.2.html>`
"""
def __init__(self, dul: "DULServiceProvider"):
"""Create a new :class:`StateMachine`.
Parameters
---------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the association.
"""
self.current_state = "Sta1"
self.dul = dul
def do_action(self, event: str) -> None:
"""Execute the action triggered by `event`.
Parameters
----------
event : str
The event to be processed, ``'Evt1'`` to ``'Evt19'``
"""
# Check (event + state) is valid
if (event, self.current_state) not in TRANSITION_TABLE:
msg = "Invalid event '{}' for the current state '{}'".format(
event, self.current_state
)
LOGGER.error(msg)
raise InvalidEventError(msg)
action_name = TRANSITION_TABLE[(event, self.current_state)]
# action is the (description, function, state) tuple
# associated with the action_name
action = ACTIONS[action_name]
# Attempt to execute the action and move the state machine to its
# next state
try:
# Execute the required action
next_state = action[1](self.dul)
# Event handler - FSM transition
evt.trigger(
self.dul.assoc,
evt.EVT_FSM_TRANSITION,
{
"action": action_name,
"current_state": self.current_state,
"fsm_event": event,
"next_state": next_state,
},
)
# print(
# "{}: {} + {} -> {} -> {}".format(
# self.dul.assoc.mode[0].upper(),
# self.current_state,
# event,
# action_name,
# next_state,
# )
# )
# Move the state machine to the next state
self.transition(next_state)
except Exception as exc:
LOGGER.error(
"State Machine received an exception attempting "
"to perform the action '%s' while in state '%s'",
action_name,
self.current_state,
)
LOGGER.exception(exc)
self.dul.kill_dul()
raise
def transition(self, state: str) -> None:
"""Transition the state machine to the next state.
Parameters
----------
state : str
The state to transition to, ``'Sta1'`` to ``'Sta13'``.
Raises
------
ValueError
If `state` is not a valid state.
"""
# Validate that state is acceptable
if state in STATES.keys():
self.current_state = state
else:
msg = f"Invalid state '{state}' for State Machine"
LOGGER.error(msg)
raise ValueError(msg)
def AE_1(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-1.
*Event*
Service user issued A-ASSOCIATE (request) to the service provider
*Action*
Issue TRANSPORT CONNECT request primitive to the transport service.
*State/Event Triggers*
- Sta1 + Evt1
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta4'``, the next state of the state machine.
"""
# A-ASSOCIATE (request) primitive received from local user
request = cast("A_ASSOCIATE", dul.to_provider_queue.get(False))
# Issue TRANSPORT CONNECT request primitive to local transport service
sock = cast("AssociationSocket", dul.socket)
sock.connect(T_CONNECT(request))
return "Sta4"
def AE_2(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-2.
On receiving connection confirmation, send A-ASSOCIATE-RQ to the peer AE
This send a byte stream with the format given by Table 9-11
State-event triggers: Sta4 + Evt2
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta5'``, the next state of the state machine.
"""
# TRANSPORT CONNECTION primitive received from transport service
primitive = cast("T_CONNECT", dul.to_provider_queue.get(False))
# Send A-ASSOCIATE-RQ PDU to the peer
dul._send(A_ASSOCIATE_RQ(primitive.request))
return "Sta5"
def AE_3(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-3.
On receiving A-ASSOCIATE-AC, issue acceptance confirmation
State-event triggers: Sta5 + Evt3
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta6'``, the next state of the state machine
"""
# Received A-ASSOCIATE-AC PDU from the peer
pdu = dul._recv_pdu.get(False)
# Issue A-ASSOCIATE confirmation (accept) primitive
dul.to_user_queue.put(cast("A_ASSOCIATE", pdu.to_primitive()))
return "Sta6"
def AE_4(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-4.
On receiving A-ASSOCIATE-RJ, issue rejection confirmation and close
connection
State-event triggers: Sta5 + Evt4
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
# Received A-ASSOCIATE-RJ PDU from the peer
pdu = dul._recv_pdu.get(False)
# Issue A-ASSOCIATE confirmation (reject) primitive and close transport
# connection
dul.to_user_queue.put(cast("A_ASSOCIATE", pdu.to_primitive()))
sock = cast("AssociationSocket", dul.socket)
sock.close()
assoc = dul.assoc
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
dul.kill_dul()
return "Sta1"
def AE_5(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-5.
From Idle state, on receiving a remote connection attempt, respond and
start ARTIM. This is the first step in associating a remote AE (requestor)
to the local AE (acceptor).
State-event triggers: Sta1 + Evt5
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta2'``, the next state of the state machine
"""
# Start ARTIM timer
dul.artim_timer.start()
return "Sta2"
def AE_6(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-6.
On receiving an A-ASSOCIATE-RQ PDU from the peer then stop the ARTIM timer
and then either
* issue an A-ASSOCIATE indication primitive if the -RQ is acceptable or
* issue an A-ASSOCIATE-RJ PDU to the peer and start the ARTIM timer
This is a lower-level DUL Service Provider initiated rejection - for
example this could be where the protocol version is checked
State-event triggers: Sta2 + Evt6
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
Either ``'Sta3'`` or ``'Sta13'``, the next state of the state machine
"""
# Stop ARTIM timer
dul.artim_timer.stop()
recv_pdu = cast(A_ASSOCIATE_RQ, dul._recv_pdu.get(False))
primitive = recv_pdu.to_primitive()
# If A-ASSOCIATE-RQ not acceptable by service dul provider
# Then set reason and send -RJ PDU back to peer
if recv_pdu.protocol_version != 0x0001:
LOGGER.error(
"A-ASSOCIATE-RQ: Unsupported protocol version "
f"'0x{recv_pdu.protocol_version:04X}'"
)
# Send A-ASSOCIATE-RJ PDU and start ARTIM timer
primitive.result = 0x01
primitive.result_source = 0x02
primitive.diagnostic = 0x02
dul._send(A_ASSOCIATE_RJ(primitive))
dul.artim_timer.start()
return "Sta13"
# If A-ASSOCIATE-RQ acceptable by service dul provider
# issue A-ASSOCIATE indication primitive and move to Sta3
dul.to_user_queue.put(primitive)
return "Sta3"
def AE_7(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-7.
On receiving association request acceptance, issue A-ASSOCIATE-AC
State-event triggers: Sta3 + Evt7
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta6'``, the next state of the state machine
"""
# Received A-ASSOCIATE (AC) primitive from local user
primitive = cast("A_ASSOCIATE", dul.to_provider_queue.get(False))
# Send A-ASSOCIATE-AC PDU
dul._send(A_ASSOCIATE_AC(primitive))
return "Sta6"
def AE_8(dul: "DULServiceProvider") -> str:
"""Association establishment action AE-8.
On receiving association request rejection, issue A-ASSOCIATE-RJ
State-event triggers: Sta3 + Evt8
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta13'``, the next state of the state machine
"""
# Received A-ASSOCIATE (RJ) primitive from local user
primitive = cast("A_ASSOCIATE", dul.to_provider_queue.get(False))
# Send A-ASSOCIATE-RJ PDU and start ARTIM timer
dul._send(A_ASSOCIATE_RJ(primitive))
dul.artim_timer.start()
return "Sta13"
def DT_1(dul: "DULServiceProvider") -> str:
"""Data transfer DT-1.
On receiving a P-DATA request, send P-DATA-TF
State-event triggers: Sta6 + Evt9
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta6'``, the next state of the state machine
"""
# P-DATA request received from local user
primitive = cast("P_DATA", dul.to_provider_queue.get(False))
# Send P-DATA-TF PDU
dul._send(P_DATA_TF(primitive))
return "Sta6"
def DT_2(dul: "DULServiceProvider") -> str:
"""Data transfer DT-2.
On receiving a P-DATA-TF request, send P-DATA indication
State-event triggers: Sta6 + Evt10
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta6'``, the next state of the state machine
"""
# P-DATA-TF PDU received from peer
pdu = dul._recv_pdu.get(False)
# Send P-DATA indication primitive directly to DIMSE for processing
dul.assoc.dimse.receive_primitive(cast("P_DATA", pdu.to_primitive()))
return "Sta6"
def AR_1(dul: "DULServiceProvider") -> str:
"""Association release AR-1.
Send Association release request
State-event triggers: Sta6 + Evt11
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta7'``, the next state of the state machine
"""
# Received A-RELEASE request from local user
primitive = cast("A_RELEASE", dul.to_provider_queue.get(False))
# Send A-RELEASE-RQ PDU
dul._send(A_RELEASE_RQ(primitive))
return "Sta7"
def AR_2(dul: "DULServiceProvider") -> str:
"""Association release AR-2.
On receiving an association release request, send release indication
State-event triggers: Sta6 + Evt12
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta8'``, the next state of the state machine
"""
# A-RELEASE-RQ PDU received from peer
pdu = dul._recv_pdu.get(False)
# Send A-RELEASE indication primitive
dul.to_user_queue.put(pdu.to_primitive())
return "Sta8"
def AR_3(dul: "DULServiceProvider") -> str:
"""Association release AR-3.
On receiving an association release response, send release confirmation,
close connection and go back to Idle state
State-event triggers: Sta7 + Evt13, Sta11 + Evt13
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
# A-RELEASE-RP PDU received from peer
pdu = dul._recv_pdu.get(False)
# Issue A-RELEASE confirmation primitive and close transport connection
dul.to_user_queue.put(pdu.to_primitive())
sock = cast("AssociationSocket", dul.socket)
sock.close()
assoc = dul.assoc
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
dul.kill_dul()
return "Sta1"
def AR_4(dul: "DULServiceProvider") -> str:
"""Association release AR-4.
On receiving an association release response, send release response
State-event triggers: Sta8 + Evt14, Sta12 + Evt14
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta13'``, the next state of the state machine
"""
# A-RELEASE (response) primitive received from local user
primitive = cast("A_RELEASE", dul.to_provider_queue.get(False))
# Issue A-RELEASE-RP PDU and start ARTIM timer
dul._send(A_RELEASE_RP(primitive))
dul.artim_timer.start()
return "Sta13"
def AR_5(dul: "DULServiceProvider") -> str:
"""Association release AR-5.
On receiving transport connection closed, stop the ARTIM timer and go back
to Idle state
State-event triggers: Sta13 + Evt17
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
assoc = dul.assoc
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
# Stop ARTIM timer
dul.artim_timer.stop()
dul.kill_dul()
return "Sta1"
def AR_6(dul: "DULServiceProvider") -> str:
"""Association release AR-6.
On receiving P-DATA-TF during attempted association release request
send P-DATA indication
State-event triggers: Sta7 + Evt10
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta7'``, the next state of the state machine
"""
# P-DATA-TF PDU received from peer
pdu = cast("P_DATA_TF", dul._recv_pdu.get(False))
# Issue P-DATA indication
dul.to_user_queue.put(pdu.to_primitive())
return "Sta7"
def AR_7(dul: "DULServiceProvider") -> str:
"""Association release AR-7.
On receiving P-DATA request during attempted association release request
send P-DATA-TF
State-event triggers: Sta8 + Evt9
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta8'``, the next state of the state machine
"""
# P-DATA primitive received from local user
primitive = cast("P_DATA", dul.to_provider_queue.get(False))
# Issue P-DATA-TF PDU
dul._send(P_DATA_TF(primitive))
return "Sta8"
def AR_8(dul: "DULServiceProvider") -> str:
"""Association release AR-8.
On receiving association release request while local is requesting release
then issue release collision indication
State-event triggers: Sta7 + Evt12
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
Either ``'Sta9'`` or ``'Sta10'``, the next state of the state machine
"""
# A-RELEASE-RQ PDU received from peer
pdu = cast("A_RELEASE_RQ", dul._recv_pdu.get(False))
# Issue A-RELEASE indication (release collision)
dul.to_user_queue.put(pdu.to_primitive())
if dul.assoc.is_requestor:
return "Sta9"
return "Sta10"
def AR_9(dul: "DULServiceProvider") -> str:
"""Association release AR-9.
On receiving A-RELEASE primitive, send release response
State-event triggers: Sta9 + Evt14
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta11'``, the next state of the state machine
"""
# A-RELEASE (response) primitive received from local user
primitive = cast("A_RELEASE", dul.to_provider_queue.get(False))
# Send A-RELEASE-RP PDU
dul._send(A_RELEASE_RP(primitive))
return "Sta11"
def AR_10(dul: "DULServiceProvider") -> str:
"""Association release AR-10.
On receiving A-RELEASE-RP, issue release confirmation
State-event triggers: Sta10 + Evt13
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta12'``, the next state of the state machine
"""
# A-RELEASE-RP PDU received from peer
pdu = cast("A_RELEASE_RP", dul._recv_pdu.get(False))
# Issue A-RELEASE confirmation primitive
dul.to_user_queue.put(pdu.to_primitive())
return "Sta12"
def AA_1(dul: "DULServiceProvider") -> str:
"""Association abort AA-1.
If on sending A-ASSOCIATE-RQ we receive an invalid reply, or an abort
request then abort
State-event triggers: Sta2 + Evt3/Evt4/Evt10/Evt12/Evt13/Evt19,
Sta3/Sta5/Sta6/Sta7/Sta8/Sta9/Sta10/Sta11/Sta12 + Evt15
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta13'``, the next state of the state machine
"""
# Received invalid PDU from peer or an A-ABORT primitive from local user
try:
primitive = dul.to_provider_queue.queue[0]
if isinstance(primitive, (A_ABORT, A_P_ABORT)):
primitive = dul.to_provider_queue.get(False)
except (queue.Empty, IndexError):
primitive = None
# Send A-ABORT PDU (service-user source) and start (or restart
# if already started) ARTIM timer.
pdu = A_ABORT_RQ()
if primitive is not None:
pdu.from_primitive(primitive)
else:
# Reason not specified
pdu.source = 0x00
pdu.reason_diagnostic = 0x00
dul._send(pdu)
dul.artim_timer.restart()
return "Sta13"
def AA_2(dul: "DULServiceProvider") -> str:
"""Association abort AA-2.
On receiving an A-ABORT or if the ARTIM timer expires, close connection and
return to Idle
State-event triggers: Sta2 + Evt16/Evt18, Sta4 + Evt15, Sta13 + Evt16/Evt18
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
# Stop ARTIM timer if running. Close transport connection.
dul.artim_timer.stop()
sock = cast("AssociationSocket", dul.socket)
sock.close()
assoc = dul.assoc
assoc.dimse.msg_queue.put((None, None))
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
dul.kill_dul()
return "Sta1"
def AA_3(dul: "DULServiceProvider") -> str:
"""Association abort AA-3.
On receiving A-ABORT, issue abort indication, close connection and
return to Idle
State-event triggers: Sta3/Sta5/Sta6/Sta7/Sta8/Sta9/Sta10/Sta11/Sta12 +
Evt16
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
# A-ABORT PDU received from peer
pdu = cast("A_ABORT_RQ", dul._recv_pdu.get(False))
# If (service-user initiated abort):
# - Issue A-ABORT indication and close transport connection.
# Otherwise (service-dul initiated abort):
# - Issue A-P-ABORT indication and close transport connection.
# This action is triggered by the reception of an A-ABORT PDU
dul.to_user_queue.put(pdu.to_primitive())
sock = cast("AssociationSocket", dul.socket)
sock.close()
assoc = dul.assoc
assoc.dimse.msg_queue.put((None, None))
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
dul.kill_dul()
return "Sta1"
def AA_4(dul: "DULServiceProvider") -> str:
"""Association abort AA-4.
If connection closed, issue A-P-ABORT and return to Idle
State-event triggers: Sta3/Sta4/Sta5/Sta6/Sta7/Sta8/Sta9/Sta10/Sta11/Sta12
+ Evt17
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
assoc = dul.assoc
assoc.dimse.msg_queue.put((None, None))
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
# Issue A-P-ABORT indication primitive.
primitive = A_P_ABORT()
primitive.provider_reason = 0x00
dul.to_user_queue.put(primitive)
dul.kill_dul()
return "Sta1"
def AA_5(dul: "DULServiceProvider") -> str:
"""Association abort AA-5.
If connection closed during association request, stop ARTIM timer and
return to Idle
State-event triggers: Sta2 + Evt17
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta1'``, the next state of the state machine
"""
assoc = dul.assoc
remote = assoc.acceptor if assoc.is_requestor else assoc.requestor
address = (remote.address, remote.port)
evt.trigger(dul.assoc, evt.EVT_CONN_CLOSE, {"address": address})
# Stop ARTIM timer.
dul.artim_timer.stop()
dul.kill_dul()
return "Sta1"
def AA_6(dul: "DULServiceProvider") -> str:
"""Association abort AA-6.
If receive a PDU while waiting for connection to close, ignore it
State-event triggers: Sta13 + Evt3/Evt4/Evt10/Evt12/Evt13
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta13'``, the next state of the state machine
"""
# Ignore PDU
try:
dul._recv_pdu.get(False)
except queue.Empty:
pass
return "Sta13"
def AA_7(dul: "DULServiceProvider") -> str:
"""Association abort AA-7.
If receive a association request or invalid PDU while waiting for
connection to close, send A-ABORT PDU
State-event triggers: Sta13 + Evt6/Evt19
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str
``'Sta13'``, the next state of the state machine
"""
primitive = A_P_ABORT()
primitive.provider_reason = 0x02
# Send A-ABORT PDU
dul._send(A_ABORT_RQ(primitive))
return "Sta13"
def AA_8(dul: "DULServiceProvider") -> str:
"""Association abort AA-8.
If receive invalid event, send A-ABORT PDU, issue A-P-ABORT indication and
start ARTIM timer
State-event triggers: Evt3 + Sta3/6/7/8/9/10/11/12,
Evt4 + Sta3/5/6/7/8/9/10/11/12, Evt6 + Sta3/5/6/7/8/9/10/11/12,
Evt10 + Sta3/5/8/9/10/11/12, Evt12 + Sta3/5/8/9/10/11/12,
Evt13 + Sta3/5/6/8/9/12, Evt19 + Sta3/5/6/7/8/9/10/11/12
Parameters
----------
dul : dul.DULServiceProvider
The DICOM Upper Layer Service instance for the local AE
Returns
-------
str