File: TestServiceClassUser1.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 (320 lines) | stat: -rw-r--r-- 8,398 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
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
/*=========================================================================

  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 "gdcmTesting.h"

/*
 * This small example show how the association pipeline works.
 * the start association is an important step as it will define all the object
 * to be send in subsequence C-operation (c-echo, c-store, c-find, c-move).
 * In this example we will demonstrate how we can send a JPEG-Lossless object
 * and then further on, a non-jpeg encapsulated file
 *
 * The test also uses the Subject/Observer API for progress report.
 */

int TestServiceClassUser1(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, "TestServiceClassUser1" );

  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() )
    {
    std::cerr << "Could not StartAssociation" << std::endl;
    std::cerr << "Error log is:" << std::endl;
    std::cerr << error_log.str() << std::endl;
    return 1;
    }

  // C-ECHO
  if( !scu.SendEcho() )
    {
    std::cerr << "Could not Echo" << std::endl;
    std::cerr << "Error log is:" << std::endl;
    std::cerr << error_log.str() << std::endl;
    return 1;
    }

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

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

  if( !generator.GenerateFromFilenames(filenames) )
    {
    return 1;
    }

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

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

  // C-STORE MRImageStorage/JPEGLossless
  if( !scu.SendStore( filename.c_str() ) )
    {
    std::cerr << "Could not C-Store" << std::endl;
    std::cerr << "Error log is:" << std::endl;
    std::cerr << error_log.str() << std::endl;
    return 1;
    }

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

  //filename = std::string(directory) + "/MR-MONO2-12-an2.acr";
  filename = std::string(directory) + "/MR_Spectroscopy_SIEMENS_OF.dcm";
  filenames.clear();
  filenames.push_back( filename );

  if( !generator.GenerateFromFilenames(filenames) )
    {
    return 1;
    }

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

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

  // C-STORE MRImageStorage/LittleEndianImplicit
  if( !scu.SendStore( filename.c_str() ) )
    {
    std::cerr << "Could not SendStore" << std::endl;
    std::cerr << "Error log is:" << std::endl;
    std::cerr << error_log.str() << std::endl;
    return 1;
    }

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

  // customize the find query
  gdcm::DataSet findds;
  gdcm::Attribute<0x10,0x10> pn1 ={"ABCDEFGH^IJKLM"};
  findds.Insert( pn1.GetAsDataElement() );
  gdcm::Attribute<0x10,0x20> pid;
  findds.Insert( pid.GetAsDataElement() );

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

  // make sure the query is valid
  if (!findquery->ValidateQuery())
    {
    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
  std::vector<gdcm::DataSet> datasets;
  if( !scu.SendFind(findquery, datasets) )
    {
    return 1;
    }

  // Need to make sure we have one dataset
  if( datasets.size() != 1 )
    {
    std::cerr << "size: " << datasets.size() << std::endl;
    return 1;
    }
  datasets[0].Print( std::cout );

  // C-find the second patient
  gdcm::Attribute<0x10,0x10> pn2 ={"XXXXXXXXXXX"};
  findquery->GetQueryDataSet().Replace( pn2.GetAsDataElement() );
  if( !scu.SendFind(findquery, datasets) )
    {
    return 1;
    }

  if( datasets.size() != 2 )
    {
    std::cerr << "size: " << datasets.size() << std::endl;
    return 1;
    }
  std::cout << std::endl;

  datasets[1].Print( std::cout );

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

  // C-MOVE
  // customize the move query
  gdcm::DataSet moveds;
  // use results from the c-find to construct the c-move query:
  moveds.Insert( datasets[0].GetDataElement( pid.GetTag() ) );

  gdcm::SmartPointer<gdcm::BaseRootQuery> movequery =
    gdcm::CompositeNetworkFunctions::ConstructQuery(
      gdcm::ePatientRootType, gdcm::ePatient, moveds, gdcm::eMove);
  // make sure the query is valid
  if (!movequery->ValidateQuery())
    {
    return 1;
    }

  //generator.SetDefaultTransferSyntax( gdcm::TransferSyntax::JPEGLosslessProcess14_1 );

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

  scu.SetPresentationContexts( generator.GetPresentationContexts() );

  scu.SetPortSCP( moveReturnPort );

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

  // C-MOVE
  std::vector<gdcm::DataSet> data;
  if( !scu.SendMove(movequery, data) )
    {
    return 1;
    }

  if( data.size() != 1 )
    {
    std::cerr << "data size: " << data.size() << std::endl;
    return 1;
    }

  // SendMove + dataset is implicit by default:
  gdcm::Writer writer;
  writer.GetFile().GetHeader().SetDataSetTransferSyntax(
    gdcm::TransferSyntax::ImplicitVRLittleEndian );
  writer.GetFile().SetDataSet( data[0] );
  const char *outfilename = "dummy.dcm";
  writer.SetFileName( outfilename );
  if( !writer.Write() )
    {
    return 1;
    }

  if( !gdcm::System::FileExists(outfilename) )
    {
    std::cerr << "FileExists: " << outfilename << std::endl;
    return 1;
    }

  char digest_str[33];
  if( !gdcm::Testing::ComputeFileMD5(outfilename, digest_str) )
    {
    return 1;
    }

  if( strcmp( digest_str, "ae1f9a1bfc617f73ae8f72f81777dc03") != 0 )
    {
    std::cerr << "md5: " << digest_str << std::endl;
    return 1;
    }

  // TODO: testing of CMove + JPEG Lossless is a *lot* more difficult
  // since we have to assume some behavior on the remote side (SCP) which
  // we cannot query.

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

  // scu dstor will close the connection (!= association)

  return 0;
}