File: TestServiceClassUser2.cxx

package info (click to toggle)
gdcm 3.0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 26,880 kB
  • sloc: cpp: 203,477; ansic: 78,582; xml: 48,129; python: 3,459; cs: 2,308; java: 1,629; lex: 1,290; sh: 334; php: 128; makefile: 117
file content (286 lines) | stat: -rw-r--r-- 7,945 bytes parent folder | download | duplicates (2)
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
/*=========================================================================

  Program: GDCM (Grassroots DICOM). A DICOM library

  Copyright (c) 2006-2011 Mathieu Malaterre
  All rights reserved.
  See Copyright.txt or http://gdcm.sourceforge.net/Copyright.html for details.

     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notice for more information.

=========================================================================*/
#include "gdcmServiceClassUser.h"
#include "gdcmDataEvent.h"
#include "gdcmSimpleSubjectWatcher.h"
#include "gdcmPresentationContextGenerator.h"
#include "gdcmAttribute.h"
#include "gdcmCompositeNetworkFunctions.h"
#include "gdcmTransferSyntax.h"
#include "gdcmWriter.h"
#include "gdcmReader.h"
#include "gdcmUIDGenerator.h"

#include "gdcmTesting.h"

/*
 * This test make sure we can send/receive as many dataset as we want
 */

int TestServiceClassUser2(int argc, char *argv[])
{
  if( argc < 5 )
    {
    std::cerr << argv[0] << " aetitle call portno moveReturnPort remote" << std::endl;
    return 1;
    }
  std::string aetitle = argv[1]; // the ae title of this computer
  std::string call = argv[2]; // the ae title of the server
  uint16_t portno = (uint16_t)atoi(argv[3]); // the port of the server
  uint16_t moveReturnPort = (uint16_t)atoi(argv[4]); // the port over which return cstore scps are done for cmove
  std::string remote = argv[5]; //the ip address of the remote server

  gdcm::SmartPointer<gdcm::ServiceClassUser> scup = new gdcm::ServiceClassUser;
  gdcm::ServiceClassUser &scu = *scup;
  gdcm::SimpleSubjectWatcher w( &scu, "TestServiceClassUser2" );

  scu.SetHostname( remote.c_str() );
  scu.SetPort( portno );
  scu.SetTimeout( 1000 );
  scu.SetCalledAETitle( call.c_str() );
  scu.SetAETitle( aetitle.c_str() );

  std::ostringstream error_log;
  gdcm::Trace::SetErrorStream( error_log );

  if( !scu.InitializeConnection() )
    {
    return 1;
    }

  gdcm::PresentationContextGenerator generator;
  if( !generator.GenerateFromUID( gdcm::UIDs::VerificationSOPClass ) )
    {
    return 1;
    }

  // make sure to fail if no pres. contexts:
  if( scu.StartAssociation() )
    {
    return 1;
    }

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

  if( !scu.StartAssociation() )
    {
    return 1;
    }

  // C-ECHO
  if( !scu.SendEcho() )
    {
    return 1;
    }

  if( !scu.StopAssociation() )
    {
    return 1;
    }

  gdcm::Directory::FilenamesType filenames;
  const char *directory = gdcm::Testing::GetDataRoot();
  // storescu -R -xs --call GDCM_STORE macminig4 11112 gdcmData/gdcm-MR-PHILIPS-16-NonSquarePixels.dcm
  std::string filename = std::string(directory) + "/gdcm-MR-PHILIPS-16-NonSquarePixels.dcm";
  filenames.push_back( filename );

  if( !generator.GenerateFromUID( gdcm::UIDs::MRImageStorage ) )
    {
    return 1;
    }

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

  if( !scu.StartAssociation() )
    {
    return 1;
    }

  gdcm::Reader reader;
  reader.SetFileName( filename.c_str() );
  if( !reader.Read() )
    {
    return 1;
    }

  gdcm::File & file = reader.GetFile();
  gdcm::UIDGenerator uid;
  const int nmax = 500;
  for( int i = 0; i < nmax; ++i )
    {
    gdcm::DataSet & ds = file.GetDataSet();
    std::string instance_uid = uid.Generate();
      {
      gdcm::Attribute<0x8,0x18> at;
      at.SetValue( instance_uid.c_str() );
      ds.Replace( at.GetAsDataElement() );
      }
      {
      gdcm::Attribute<0x10,0x20> at;
      at.SetValue( "TestServiceClassUser2" );
      ds.Replace( at.GetAsDataElement() );
      }
    if( !scu.SendStore( file.GetDataSet() ) )
      {
      return 1;
      }
    }

  if( !scu.StopAssociation() )
    {
    return 1;
    }

  // customize the find query
  gdcm::DataSet findds;
  gdcm::Attribute<0x10,0x20> pid ={"TestServiceClassUser2"};
  findds.Insert( pid.GetAsDataElement() );
  gdcm::Attribute<0x8,0x18> iuid;
  findds.Insert( iuid.GetAsDataElement() );

  gdcm::SmartPointer<gdcm::BaseRootQuery> findquery =
    gdcm::CompositeNetworkFunctions::ConstructQuery(
      gdcm::ePatientRootType, gdcm::eImage, findds);

  // make sure the query is valid
  if (!findquery->ValidateQuery(false))
    {
    return 1;
    }

  // Generate the PresentationContext array from the query UID:
  if( !generator.GenerateFromUID( findquery->GetAbstractSyntaxUID() ) )
    {
    return 1;
    }

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

  if( !scu.StartAssociation() )
    {
    return 1;
    }

  // C-FIND
  // $ findscu --call GDCM_STORE -P  macminig4 11112 -k 8,52=IMAGE -k
  //   10,20=TestServiceClassUser2  -k 20,d  -k 20,e -k 8,18
  // gdcmscu --find --call GDCM_STORE --image --patientroot  macminig4 11112
  //   -k 10,20=TestServiceClassUser2 -k 8,18
  std::vector<gdcm::DataSet> datasets;
  if( !scu.SendFind(findquery, datasets) )
    {
    return 1;
    }

  if( !scu.StopAssociation() )
    {
    return 1;
    }

  // C-MOVE
  // customize the move query
  gdcm::DataSet moveds1;
  // use results from the C-FIND to construct the c-move query:
  moveds1.Insert( datasets[0].GetDataElement( gdcm::Tag(0x10,0x20) ) );
  moveds1.Insert( datasets[0].GetDataElement( gdcm::Tag(0x20,0xd) ) );
  moveds1.Insert( datasets[0].GetDataElement( gdcm::Tag(0x20,0xe) ) );

  gdcm::SmartPointer<gdcm::BaseRootQuery> movequery1 =
    gdcm::CompositeNetworkFunctions::ConstructQuery(
      gdcm::ePatientRootType, gdcm::eImage, moveds1, gdcm::eMove);

  // Generate the PresentationContext array from the query UID:
  if( !generator.GenerateFromUID( movequery1->GetAbstractSyntaxUID() ) )
    {
    return 1;
    }

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

  scu.SetPortSCP( moveReturnPort );

  if( !scu.StartAssociation() )
    {
    return 1;
    }

  size_t ndatasets = 0;
  for(
    std::vector<gdcm::DataSet>::const_iterator cfind_it = datasets.begin();
    cfind_it != datasets.end(); ++cfind_it )
    {
    gdcm::DataSet &queryds = movequery1->GetQueryDataSet();
    const gdcm::DataElement &instanceuid = cfind_it->GetDataElement( gdcm::Tag(0x8,0x18) );
    queryds.Replace( instanceuid );

    // C-MOVE
    std::vector<gdcm::DataSet> data;
    if( !scu.SendMove(movequery1, data) )
      {
      std::cerr << "CMove Failure for: " << instanceuid << std::endl;
      std::cerr << "Error log is:" << std::endl;
      std::cerr << error_log.str() << std::endl;
      return 1;
      }
    if( data.size() != 1 )
      {
      std::cerr << "data size: " << data.size() << std::endl;
      return 1;
      }
    ++ndatasets;
    }

  //std::cerr << "Total number of dataset: " << ndatasets << std::endl;

  // Now let's do this again with a simpler request:
  gdcm::DataSet moveds2;
  moveds2.Insert( pid.GetAsDataElement() );
  gdcm::SmartPointer<gdcm::BaseRootQuery> movequery2 =
    gdcm::CompositeNetworkFunctions::ConstructQuery(
      gdcm::ePatientRootType, gdcm::ePatient, moveds2, gdcm::eMove );

  const char outputdir[] = "TestServiceClassUser2";
  // Make sure output dir exist, it will not be created
  if( gdcm::System::FileIsDirectory( outputdir ) )
    {
    // cleanups old files:
    gdcm::System::DeleteDirectory( outputdir );
    }
  gdcm::System::MakeDirectory( outputdir );

  if( !scu.SendMove(movequery2, outputdir) )
    {
    std::cerr << "CMove Failure for Patient ID: " << pid.GetValue() << std::endl;
    return 1;
    }

  // Compare results:
  gdcm::Directory dir;
  unsigned int ndatasets2 = dir.Load( outputdir, true );

  if( ndatasets != ndatasets2 )
    {
    std::cerr << "Incompatible number of results: " << ndatasets << " vs " <<
      ndatasets2 << std::endl;
    return 1;
    }

  if( !scu.StopAssociation() )
    {
    return 1;
    }
  // scu dstor will close the connection (!= association)

  return 0;
}