/
pdu.cxx
893 lines (778 loc) · 24.1 KB
/
pdu.cxx
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
/*
19980322 mvh blocked one remaining printf statement "comparing ..."
19990202 ljz Added PDU memberfunction 'IsAbstractSyntaxAccepted'
19990415 mvh Merged both versions
20000322 ljz Fix in search for PresentationContext when identical
AbstractSyntaxes occur (e.g. GECT-scanner).
Do not break out of the inner loop in
PDU_Service::InterogateAAssociateAC.
20000322 ljz+mvh Fix of PresentationContextID in PDV: in routines
PDU_Service::Write(DICOMObject*, UID*), set
PresentationContextID to TempByte, not to TempByte1.
20000911 ljz Removed two warnings
20020316 mvh Added GetLocalAddress and GetRemoteAddress
20030306 ljz Fix: Huge bug in 'InterogateAAssociateRQ'
20030424 ljz Blocked out obsolete PDU.Write(&DDO)
Some work on offering other TransferSyntaxes than
ImplicitLittleEndian when running in SCU mode
(Note: when running in SCP mode, 'dgatesop.lst' is used)
Fix: CommandObjects are always ImplicitLittleEndian
20030627 mvh Fix for MSC4.2; re-enabled obsolete write calls for test.cxx
20030710 ljz Adjusted test.cxx, and disabled obsolete calls again
20080816 mvh ValidPresContexts is now a global: how many incoming connection can handle
*/
/****************************************************************************
Copyright (C) 1995, University of California, Davis
THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND THE UNIVERSITY
OF CALIFORNIA DOES NOT MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS
PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY
SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF
THE SOFTWARE IS WITH THE USER.
Copyright of the software and supporting documentation is
owned by the University of California, and free access
is hereby granted as a license to use this software, copy this
software and prepare derivative works based upon this software.
However, any distribution of this software source code or
supporting documentation or derivative works (source code and
supporting documentation) must include this copyright notice.
****************************************************************************/
/***************************************************************************
*
* University of California, Davis
* UCDMC DICOM Network Transport Libraries
* Version 0.1 Beta
*
* Technical Contact: mhoskin@ucdavis.edu
*
***************************************************************************/
# include "dicom.hpp"
PDU_Service :: PDU_Service()
{
// PDU Level services are always big endian architecture
Buffer :: SetIncomingEndian(BIG_ENDIAN);
Buffer :: SetOutgoingEndian(BIG_ENDIAN);
AttachedRTC = NULL;
DestructAttachedRTC = FALSE;
}
PDU_Service :: ~PDU_Service()
{
if(Link.Connected)
Close();
ClearAbstractSyntaxs();
while ( AcceptedPresentationContexts.GetSize())
AcceptedPresentationContexts.RemoveAt ( 0 );
if ( AttachedRTC )
if ( DestructAttachedRTC )
delete AttachedRTC;
}
BOOL
PDU_Service :: AttachRTC (
RTC *vRTC,
BOOL DestructFlag)
{
DetachRTC();
AttachedRTC = vRTC;
DestructAttachedRTC = DestructFlag;
return ( TRUE );
}
RTC *
PDU_Service :: DetachRTC()
{
RTC *vRTC;
vRTC = AttachedRTC;
if ( AttachedRTC )
if ( DestructAttachedRTC )
delete AttachedRTC;
AttachedRTC = NULL;
return ( vRTC );
}
BOOL PDU_Service :: Read(DICOMObject *DCMObject)
{
BYTE ItemType;
if (!Link.Connected)
return ( FALSE );
PDataTF :: MsgStatus = 0;
if((PDataTF :: Length)!=0) // for bad GCC v2.6.0
{
PDataTF :: ReadDynamic( *this );
if (PDataTF :: MsgStatus > 0)
{
SizeCap = 0;
if(!ParseRawVRIntoDCM(PDataTF :: PresentationContextID,
PDataTF::VRBuffer, DCMObject))
{
AAbortRQ :: Write(*this);
Link.Close();
return ( FALSE );
}
//DumpDDO((DICOMDataObject*)DCMObject);
return(TRUE);
}
}
while ( TRUE )
{
(*this) >> ItemType;
switch ( ItemType )
{
case 0x01: // A-ASSOCIATE-RQ Should not get this here
AAssociateRQ :: Read(*this);
AAbortRQ :: Write ( *this );
Link.Close();
return ( FALSE );
case 0x02: // A-ASSOCIATE-AC Should not get this here
AAssociateAC :: Read(*this);
AAbortRQ :: Write ( *this );
Link.Close();
return ( FALSE );
case 0x03: // A-ASSOCIATE-RJ Should not get this here
AAbortRQ :: Write ( *this );
Link.Close();
return ( FALSE );
case 0x04: // P-DATA-TF
// fprintf(stderr, "start: P-DATA-TF Read\n");
PDataTF :: ReadDynamic( *this );
// fprintf(stderr, "end: P-DATA-TF Read : %d, Buffer Size %d\n",
// PDataTF :: MsgStatus, Buffer :: GetSize());
if (PDataTF :: MsgStatus > 0)
{
SizeCap = 0;
if(!ParseRawVRIntoDCM(PDataTF :: PresentationContextID,
PDataTF::VRBuffer, DCMObject))
{
AAbortRQ :: Write(*this);
Link.Close();
return ( FALSE );
}
//DumpDDO((DICOMDataObject*)DCMObject);
return(TRUE);
}
break;
case 0x05: // A-RELEASE-RQ
AReleaseRQ :: ReadDynamic(*this);
// also drop
AReleaseRP :: Write(*this);
return ( FALSE );
case 0x06: // A-RELEASE-RP
Link.Close();
return ( FALSE );
case 0x07: // A-ABORT-RQ
Link.Close();
return ( FALSE );
default:
// Protocol Error
AAbortRQ :: Write ( *this );
Link.Close();
return ( FALSE );
}
}
return ( FALSE );
}
// These routines are being de-emphazised
#if THESE_ARE_OBSOLETE
BOOL PDU_Service :: Write(DICOMCommandObject *DCMObject)
{
BYTE TempByte;
TempByte = ParseDCMIntoRawVR(DCMObject, PDataTF :: VRBuffer);
if ( ! TempByte )
{
//printf("Failure to find TempByte\n");
return ( FALSE );
}
PDataTF :: PresentationContextID = TempByte;
PDataTF :: MsgHeader = 0x01;
return(PDataTF :: Write ( *this ));
}
BOOL PDU_Service :: Write(DICOMDataObject *DCMObject)
{
BYTE TempByte;
TempByte = ParseDCMIntoRawVR(DCMObject, PDataTF :: VRBuffer);
if ( ! TempByte )
return ( FALSE );
PDataTF :: PresentationContextID = TempByte;
PDataTF :: MsgHeader = 0x00;
return(PDataTF :: Write ( *this ));
}
#endif
// These routines are the recommended method of sending a DICOM Object
BOOL PDU_Service :: Write(DICOMCommandObject *DCMObject, UID &uid)
{
BYTE TempByte, TempByte1;
//DumpDDO((DICOMDataObject*)DCMObject);
TempByte1 = GetPresentationContextID(uid);
// printf("PDU_Service :: Write(DCMObject:%x, %s) PID = %d\n",
// DCMObject, uid.GetBuffer(1), TempByte1);
if(!TempByte1)
return ( FALSE );
TempByte = ParseDCMIntoRawVR(DCMObject, PDataTF :: VRBuffer, uid, FALSE);
// printf("PDU_Service :: Write(DCMObject:%x, %s) RetParseDCMIntoRawVR = %d\n",
// DCMObject, uid.GetBuffer(1), TempByte);
if ( ! TempByte )
return ( FALSE );
PDataTF :: PresentationContextID = TempByte;
PDataTF :: MsgHeader = 0x01;
return(PDataTF :: Write ( *this ));
}
BOOL PDU_Service :: Write(DICOMDataObject *DCMObject, UID &uid)
{
BYTE TempByte, TempByte1;
//DumpDDO((DICOMDataObject*)DCMObject);
TempByte1 = GetPresentationContextID(uid);
if(!TempByte1)
return ( FALSE );
TempByte = ParseDCMIntoRawVR(DCMObject, PDataTF :: VRBuffer, uid, TRUE);
if ( ! TempByte )
return ( FALSE );
PDataTF :: PresentationContextID = TempByte;
PDataTF :: MsgHeader = 0x00;
return(PDataTF :: Write ( *this ));
}
BOOL PDU_Service :: InterogateAAssociateRQ()
{
UINT Index, IndexTrn, ValidP;
TransferSyntax TrnSyntax;
PresentationContext PresContext;
PresentationContextAccept PresContextAccept;
UserInformation UserInfo;
MaximumSubLength MaxSubLength;
Array<PresentationContext> PCArray ( FALSE );
if(!ShouldIAcceptRemoteApTitle(AAssociateRQ :: CallingApTitle))
{
AAssociateRJ :: Reason = 3; // Calling party not rec.
AAssociateRJ :: Source = 1; // PDU
AAssociateRJ :: Result = 1; // Permanent
AAssociateRJ :: Write ( *this );
return ( FALSE );
}
if(!ShouldIAcceptLocalApTitle(AAssociateRQ :: CalledApTitle))
{
AAssociateRJ :: Reason = 7; // Called Party Not Rec.
AAssociateRJ :: Source = 1; // PDU
AAssociateRJ :: Result = 1; // Permanent
AAssociateRJ :: Write ( *this );
return ( FALSE );
}
if(!ShouldIAcceptApplicationContext(AAssociateRQ :: AppContext))
{
AAssociateRJ :: Reason = 2; // AppContext Not-Supported
AAssociateRJ :: Source = 1; // PDU
AAssociateRJ :: Result = 1; // Permanent
AAssociateRJ :: Write ( *this );
return ( FALSE );
}
// Transfer the information over to the A-ASSOCIATE-AC Class
AAssociateAC :: SetCalledApTitle(AAssociateRQ :: CalledApTitle);
AAssociateAC :: SetCallingApTitle(AAssociateRQ :: CallingApTitle);
AAssociateAC :: AppContext = AAssociateRQ :: AppContext;
AAssociateAC :: UserInfo = AAssociateRQ :: UserInfo;
// Ok, now, we need to find an acceptable presentation context
Index = 0;
ValidPresContexts = ValidP = 0;
PCArray = (AAssociateRQ :: PresContexts);
while ( AcceptedPresentationContexts.GetSize())
AcceptedPresentationContexts.RemoveAt ( 0 );
/* Check all Presentation Contexts proposed by the other side, e.g.:
CTStorage 1.2.840.10008.5.1.4.1.1.2 sop
USMultiframeStorage 1.2.840.10008.5.1.4.1.1.3.1 sop
MRStorage 1.2.840.10008.5.1.4.1.1.4 sop
USStorage 1.2.840.10008.5.1.4.1.1.6.1 sop
SCStorage 1.2.840.10008.5.1.4.1.1.7 sop
*/
while ( Index < PCArray.GetSize())
{
PresContext = PCArray.Get ( Index );
PresContext.TrnSyntax.ClearType = FALSE;
IndexTrn = 0;
PresContextAccept.PresentationContextID =
PresContext.PresentationContextID;
/* Fix: default to (Transfer Syntaxes Not Supported) */
PresContextAccept.Result = 4;
/* For each Presentation Context, multiple TransferTyntaxes may be proposed, e.g:
LittleEndianImplicit 1.2.840.10008.1.2
LittleEndianExplicit 1.2.840.10008.1.2.1
BigEndianExplicit 1.2.840.10008.1.2.2
JPEGLossless 1.2.840.10008.1.2.4.70
RLELossless 1.2.840.10008.1.2.5
JPEG2000 1.2.840.10008.1.2.4.91
*/
while ( IndexTrn < PresContext.TrnSyntax.GetSize())
{
TrnSyntax = PresContext.TrnSyntax.Get(IndexTrn);
/* Following routine could better be named 'CanI...' */
if(CanYouHandleTransferSyntax(TrnSyntax))
{
PresContextAccept.TrnSyntax.TransferSyntaxName =
TrnSyntax.TransferSyntaxName;
/* OK: Accept the Transfer Syntax */
PresContextAccept.Result = 0;
AcceptedPresentationContexts.Add ( PresContextAccept );
++ValidPresContexts; ++ValidP;
break;
}
++IndexTrn;
}
/* Do we support the offered Presentation Context? */
if(!ShouldIAcceptAbstractSyntax(PresContext.AbsSyntax))
{
PresContextAccept.Result = 3;
ValidPresContexts--;
}
AAssociateAC :: AddPresentationContextAccept(PresContextAccept);
++Index;
}
if(!ValidP)
{
AAssociateAC :: Write ( *this );
return ( FALSE );
}
MaxSubLength.Set(16384); // we can do all DICOM can handle
GetImplementationClass(UserInfo.ImpClass);
GetImplementationVersion(UserInfo.ImpVersion);
UserInfo.SetMax(MaxSubLength);
AAssociateAC :: SetUserInformation ( UserInfo );
AAssociateAC :: Write ( *this );
return ( TRUE ); // yes, the communication should work out
}
BOOL PDU_Service :: InterogateAAssociateAC()
{
UINT Index, IndexTrn,ValidPresContexts;
// BOOL Good;
// TransferSyntax TrnSyntax;
// PresentationContext PresContext;
PresentationContextAccept PresContextAccept;
Array<PresentationContextAccept> PCArray ( FALSE );
Index = 0;
ValidPresContexts = 0;
PCArray = (AAssociateAC :: PresContextAccepts);
//printf("InterogateAAssociateAC : PCArray.GetSize() = %d\n",
// PCArray.GetSize());
while ( AcceptedPresentationContexts.GetSize())
AcceptedPresentationContexts.RemoveAt ( 0 );
while ( Index < PCArray.GetSize())
{
PresContextAccept = PCArray.Get ( Index );
if(!PresContextAccept.Result)
{
//printf("Remote Accepting Abstract Class\n");
AcceptedPresentationContexts.Add ( PresContextAccept );
}
// else
// printf("Remote Rejecting Abstract Class [%d]\n",
// PresContextAccept.Result);
++Index;
}
if(!AcceptedPresentationContexts.GetSize())
return ( FALSE ); // no, this association should be terminated
return ( TRUE ); // yes, the communication should work out
}
// Checks the Known Class list for the UID and further verifies that
// it's a big/little Endian. If it is, then it sets the Transfer type
// in the TransferSyntax for further reference
BOOL PDU_Service :: CanYouHandleTransferSyntax(TransferSyntax &TrnSyntax)
{
/*
KnownClass KC;
if(KnownUID(TrnSyntax.TransferSyntaxName, KC))
return ( FALSE );
if(KC.Type != "transfer")
return ( FALSE );
if(KC.Comment != "LittleEndian")
{
if(KC.Command != "BigEndian")
return ( FALSE );
else
TrnSyntax.SetType ( BIG_ENDIAN );
}
else
TrnSyntax.SetType ( LITTLE_ENDIAN );
*/
if(!strcmp((char*) TrnSyntax.TransferSyntaxName.GetBuffer(1), "1.2.840.10008.1.2"))
{
TrnSyntax.SetType ( LITTLE_ENDIAN );
return ( TRUE );
}
return ( FALSE );
}
BOOL PDU_Service :: Listen(BYTE *port)
{
PDataTF :: Length = 0;
if (Link.Connected)
Link.Close();
if(!Link.Listen((char *) port))
return ( FALSE );
if(!Link.Accept())
return ( FALSE );
// fprintf(stderr, "Waiting for AAssociateRQ\n");
AAssociateRQ :: Read( *this );
// fprintf(stderr, "Interogating AAssociateRQ\n");
if(!InterogateAAssociateRQ())
{
// fprintf(stderr, "failed to connect\n");
Link.Close();
return ( FALSE );
}
// fprintf(stderr, "connected, but not disconnecting (missing P-DATA service)\n");
// AAbortRQ :: Write ( *this );
// return ( FALSE );
return ( TRUE );
}
int PDU_Service :: Multiplex(BYTE *port)
{
int TempSocket;
PDataTF :: Length = 0;
if(!Link.Listen((char*)port))
return(-1);
TempSocket = Link.ListenSocketfd;
Link.ListenSocketfd = 0;
Link.Listened = FALSE;
return (Listen(TempSocket));
}
int PDU_Service :: Listen(int sock)
{
int TempSocket;
if(Link.Connected)
Link.Close();
Link.ListenSocketfd = sock;
Link.Listened = TRUE;
while (!Link.Connected)
{
if(!Link.Accept())
return ( FALSE );
AAssociateRQ :: Read( *this );
if(!InterogateAAssociateRQ())
{
TempSocket = Link.ListenSocketfd;
Link.Listened = FALSE;
Link.Close();
Link.Listened = TRUE;
Link.ListenSocketfd = TempSocket;
}
}
TempSocket = Link.ListenSocketfd;
Link.ListenSocketfd = 0;
Link.Listened = 0;
return ( TempSocket );
}
BOOL PDU_Service :: Multiplex(int Socketfd)
{
if(Link.Connected)
Link.Close();
Link.Connected = TRUE;
Link.Socketfd = Socketfd;
AAssociateRQ :: Read( *this );
if(!InterogateAAssociateRQ())
{
Link.Close();
return(FALSE);
}
return (TRUE);
}
BOOL PDU_Service :: Close()
{
PDataTF :: Length = 0;
if ( Link.Connected )
{
AReleaseRQ :: Write ( *this );
AReleaseRP :: Read ( *this );
}
Link.Close();
return ( TRUE );
}
// Support routine to get Presentation Context ID from UID
BYTE PDU_Service :: GetAcceptedPCIDForTransferSyntax(UID &uid)
{
UINT Index;
PresentationContextAccept PCA;
Index = 0;
while (Index < AcceptedPresentationContexts.GetSize() )
{
PCA = AcceptedPresentationContexts.Get ( Index );
/* fprintf(stderr, "Comparing: %s with %s [%d:%d]\n",
PCA.TrnSyntax.TransferSyntaxName.GetBuffer(1),
uid.GetBuffer(1),
PCA.TrnSyntax.TransferSyntaxName.GetSize(),
uid.GetSize());
*/
if(PCA.TrnSyntax.TransferSyntaxName == uid)
return ( PCA.PresentationContextID );
++Index;
}
return ( 0 );
}
BYTE PDU_Service :: GetPresentationContextID(UID &uid)
{
UINT Index;
Array <PresentationContext> PCArray ( FALSE );
PresentationContext PresContext;
PCArray = (AAssociateRQ :: PresContexts);
Index = 0;
while ( Index < PCArray.GetSize())
{
PresContext = PCArray.Get ( Index );
PresContext.TrnSyntax.ClearType = FALSE;
// printf("PDU_Service::GetPresentationContextID, Compare \"%s\"\n",
// PresContext.AbsSyntax.AbstractSyntaxName.GetBuffer(1));
if(PresContext.AbsSyntax.AbstractSyntaxName == uid)
return ( PresContext.PresentationContextID );
++Index;
}
return ( 0 );
}
// Get the PCID for a given AbsUID and TrnUID
BYTE PDU_Service :: GetPresentationContextID(UID &AbsUID, UID &TrnUID)
{
UINT Index;
Array <PresentationContext> PCArray ( FALSE );
PresentationContext PresContext;
PCArray = (AAssociateRQ :: PresContexts);
Index = 0;
// printf("GetPresentationContextID, Searching for: %s/%s\n", AbsUID.GetBuffer(1),
// TrnUID.GetBuffer(1));
while ( Index < PCArray.GetSize())
{
PresContext = PCArray.Get ( Index );
PresContext.TrnSyntax.ClearType = FALSE;
if(PresContext.AbsSyntax.AbstractSyntaxName == AbsUID)
{
// printf("GetPresentationContextID :: Found %s in AAssociateRQ :: PresContexts\n",
// AbsUID.GetBuffer(1));
UINT Index;
PresentationContextAccept PCA;
Index = 0;
while (Index < AcceptedPresentationContexts.GetSize() )
{
PCA = AcceptedPresentationContexts.Get ( Index );
// printf("PCA.TrnSyntax.TransferSyntaxName = %s\n",
// PCA.TrnSyntax.TransferSyntaxName.GetBuffer(1));
if((PCA.TrnSyntax.TransferSyntaxName == TrnUID) &&
(PCA.PresentationContextID == PresContext.PresentationContextID))
{
// printf("Returning Good!");
return ( PCA.PresentationContextID );
}
++Index;
}
}
++Index;
}
// printf("GetPresentationContextID :: Error, cannot find in Accepted PC List\n");
return ( 0 );
}
BOOL PDU_Service :: GetTransferSyntaxUID(BYTE PCID, UID &uid)
{
UINT Index;
PresentationContextAccept PCA;
Index = 0;
while (Index < AcceptedPresentationContexts.GetSize() )
{
PCA = AcceptedPresentationContexts.Get ( Index );
if(PCA.PresentationContextID == PCID)
{
uid = PCA.TrnSyntax.TransferSyntaxName;
return ( TRUE );
}
++Index;
}
return ( FALSE );
}
BOOL PDU_Service :: IsAbstractSyntaxAccepted(UID &uid)
{
BYTE id;
UINT Index;
PresentationContextAccept PCA;
id = GetPresentationContextID(uid);
if (!id)
return FALSE;
Index = 0;
while (Index < AcceptedPresentationContexts.GetSize() )
{
PCA = AcceptedPresentationContexts.Get ( Index );
/* fprintf(stderr, "Comparing: %s with %s [%d:%d]\n",
PCA.TrnSyntax.TransferSyntaxName.GetBuffer(1),
uid.GetBuffer(1),
PCA.TrnSyntax.TransferSyntaxName.GetSize(),
uid.GetSize());
*/
if(PCA.PresentationContextID == id)
return TRUE;
++Index;
}
return FALSE;
}
BOOL PDU_Service :: ClearAbstractSyntaxs ()
{
while (ProposedAbstractSyntaxs.GetSize())
ProposedAbstractSyntaxs.RemoveAt ( 0 );
return ( TRUE );
}
BOOL PDU_Service :: AddAbstractSyntax(UID &uid)
{
UINT Index;
AbstractSyntax AbsSyntax;
Index = 0;
while ( Index < ProposedAbstractSyntaxs.GetSize() )
{
if ( uid ==
ProposedAbstractSyntaxs.Get ( Index ).AbstractSyntaxName)
return ( TRUE ); // already in array
++Index;
}
AbsSyntax.Set(uid);
ProposedAbstractSyntaxs.Add ( AbsSyntax );
return ( TRUE );
}
BOOL PDU_Service :: AddAbstractSyntaxAlias (
UID &UIDSource,
UID &UIDAlias)
{
UINT Index;
Array <PresentationContext> PCArray ( FALSE );
PresentationContext PresContext;
// First check to see if it's already aliased
if ( GetPresentationContextID ( UIDAlias ) )
return ( TRUE );
PCArray = (AAssociateRQ :: PresContexts);
Index = 0;
while ( Index < PCArray.GetSize())
{
PresContext = PCArray.Get ( Index );
PresContext.TrnSyntax.ClearType = FALSE;
if(PresContext.AbsSyntax.AbstractSyntaxName == UIDSource)
{
PresentationContext vPresContext = PresContext;
vPresContext.TrnSyntax.ClearType = FALSE;
PresContext.TrnSyntax.Clear();
UINT SubIndex = 0;
while (SubIndex < vPresContext.TrnSyntax.GetSize() )
{
PresContext.TrnSyntax.Add(vPresContext.TrnSyntax.Get(SubIndex));
++SubIndex;
}
PresContext.TrnSyntax.ClearType = TRUE;
PresContext.AbsSyntax.AbstractSyntaxName = UIDAlias;
AAssociateRQ :: AddPresentationContext(PresContext);
PresContext.TrnSyntax.ClearType = FALSE;
return ( TRUE );
}
++Index;
}
// The source was not proposed
return ( FALSE );
}
BOOL PDU_Service :: SetApplicationContext(UID &uid)
{
AAssociateRQ :: SetApplicationContext(uid);
return ( TRUE );
}
BOOL PDU_Service :: SetLocalAddress(BYTE *address)
{
AAssociateRQ :: SetCallingApTitle(address);
return ( TRUE );
}
BOOL PDU_Service :: SetRemoteAddress(BYTE *address)
{
AAssociateRQ :: SetCalledApTitle(address);
return ( TRUE );
}
BOOL PDU_Service :: GetLocalAddress(BYTE *address)
{
memset(address, 0, 17); memcpy(address, AAssociateAC :: CallingApTitle, 16);
return ( TRUE );
}
BOOL PDU_Service :: GetRemoteAddress(BYTE *address)
{
memset(address, 0, 17); memcpy(address, AAssociateAC :: CalledApTitle, 16);
return ( TRUE );
}
BOOL PDU_Service :: AddTransferSyntaxs(PresentationContext &PresContext)
{
UID uid;
TransferSyntax TrnSyntax;
char szAbstractSyntax[100];
/*
uid = PresContext.AbsSyntax.AbstractSyntaxName;
strncpy(szAbstractSyntax, (char*)uid.GetBuffer(0), uid.GetSize());
szAbstractSyntax[uid.GetSize()] = 0;
if ((strcmp(szAbstractSyntax, "1.2.840.10008.1.1") != 0) && // c-echo
(strstr(szAbstractSyntax, ".5.1.4.1.2.") == 0)) // c-find
{
uid.Set("1.2.840.10008.1.2.4.70"); // LJ LosslessJPEG
TrnSyntax.Set(uid);
PresContext.AddTransferSyntax(TrnSyntax);
}
*/
uid.Set("1.2.840.10008.1.2"); // LJ ImplicitLittleEndian
TrnSyntax.Set(uid);
PresContext.AddTransferSyntax(TrnSyntax);
// uid.Set("1.2.840.10008.1.2.1"); // LJ ExplicitLittleEndian
// TrnSyntax.Set(uid);
// PresContext.AddTransferSyntax(TrnSyntax);
return ( TRUE );
}
BOOL PDU_Service :: GetImplementationClass(ImplementationClass &ImpClass)
{
ImpClass.Set((BYTE*) IMPLEMENTATION_CLASS_STRING);
return(TRUE);
}
BOOL PDU_Service :: GetImplementationVersion(ImplementationVersion &ImpVersion)
{
ImpVersion.Set((BYTE*) IMPLEMENTATION_VERSION_STRING);
return ( TRUE );
}
BOOL PDU_Service :: Connect ( BYTE *ip, BYTE *port )
{
UINT Index;
PresentationContext PresContext;
UserInformation UserInfo;
MaximumSubLength MaxSubLength;
BYTE ItemType;
PDataTF :: Length = 0;
if (!ProposedAbstractSyntaxs.GetSize () )
return ( FALSE ); // must proposed atleast one SOP class
AAssociateRQ :: ClearPresentationContexts();
Index = 0;
//AddTransferSyntaxs(PresContext);
while ( Index < ProposedAbstractSyntaxs.GetSize())
{
PresContext.TrnSyntax.Clear();
PresContext.SetAbstractSyntax ( ProposedAbstractSyntaxs.Get ( Index ) );
AddTransferSyntaxs(PresContext);
PresContext.PresentationContextID = uniq8odd();
AAssociateRQ :: AddPresentationContext ( PresContext );
++Index;
}
PresContext.TrnSyntax.ClearType = FALSE;
MaxSubLength.Set(16384); // we can do all DICOM can handle
GetImplementationClass(UserInfo.ImpClass);
GetImplementationVersion(UserInfo.ImpVersion);
UserInfo.SetMax(MaxSubLength);
AAssociateRQ :: SetUserInformation ( UserInfo );
// ok, every thing proposed, now try and contact the remote end
if ( ! Link.Open ( (char*) ip, (char*)port ) )
return ( FALSE ); // Socket level Error
AAssociateRQ :: Size();
AAssociateRQ :: Write ( *this );
(*this) >> ItemType;
// fprintf(stderr, "Got Response Type: %d\n", ItemType);
switch ( ItemType )
{
case 0x02:
AAssociateAC :: ReadDynamic ( * this );
if(!InterogateAAssociateAC())
{
//printf("Failed to InterogateAAssociateAC\n");
Link.Close();
return ( FALSE );
}
return ( TRUE ); // this is what we want to happen
case 0x03:
AAssociateRJ :: ReadDynamic ( * this );
Link.Close();
return ( FALSE );
default:
// corrupt transmission
Link.Close();
return ( FALSE );
}
return ( FALSE );
}