File: gdcmCStoreMessages.cxx

package info (click to toggle)
gdcm 2.4.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 32,868 kB
  • sloc: cpp: 188,481; ansic: 124,526; xml: 41,799; sh: 7,162; python: 3,667; cs: 2,128; java: 1,344; lex: 1,290; tcl: 677; php: 128; makefile: 116
file content (351 lines) | stat: -rw-r--r-- 10,499 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
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
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0.txt
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *=========================================================================*/
#include "gdcmCStoreMessages.h"

#include "gdcmUIDs.h"
#include "gdcmAttribute.h"
#include "gdcmFile.h"
#include "gdcmImplicitDataElement.h"
#include "gdcmPresentationContextRQ.h"
#include "gdcmCommandDataSet.h"
#include "gdcmBasePDU.h"
#include "gdcmPDataTFPDU.h"
#include "gdcmMediaStorage.h"
#include "gdcmULConnection.h"
#include "gdcmWriter.h"

#include <limits>

namespace gdcm{

class DataSetWriter : public Writer
{
public:
  DataSetWriter()
    {
    SetWriteDataSetOnly( true );
    }
};

namespace network{

std::vector<PresentationDataValue> CStoreRQ::ConstructPDV(
const ULConnection &inConnection, File const & file )
{
const DataSet* inDataSet = &file.GetDataSet();

  std::vector<PresentationDataValue> thePDVs;
  PresentationContextRQ pc( UIDs::VerificationSOPClass );
  uint8_t prescontid;
{
  assert( inDataSet );
  PresentationDataValue thePDV;
#if 0
  std::string UIDString;
  thePDV.SetPresentationContextID(
    PresentationContextRQ::AssignPresentationContextID(*inDataSet, UIDString));
#else
{
  MediaStorage mst;
  if (!mst.SetFromDataSet(*inDataSet))
    {
    throw Exception("Missing MediaStorage");
    }
  UIDs uid;
  uid.SetFromUID( MediaStorage::GetMSString(mst) );
  //as.SetNameFromUID( uid );
  pc.GetAbstractSyntax().SetName( uid.GetString() );
}

//  prescontid = inConnection.GetPresentationContextIDFromAbstractSyntax(as);
//  thePDV.SetPresentationContextID( prescontid );
#endif

  thePDV.SetCommand(true);
  thePDV.SetLastFragment(true);
  //ignore incoming data set, make your own

  CommandDataSet ds;
  {
  DataElement de( Tag(0x0,0x2) );
  de.SetVR( VR::UI );
  if( !inDataSet->FindDataElement( Tag(0x0008, 0x0016) ) ||
    inDataSet->GetDataElement( Tag(0x0008, 0x0016) ).IsEmpty() )
    {
    throw Exception("Missing SOP Class UID");
    }
  const DataElement& msclass = inDataSet->GetDataElement( Tag(0x0008, 0x0016) );
  const char *uid = msclass.GetByteValue()->GetPointer();
  assert( uid );
  std::string suid = std::string(uid, msclass.GetByteValue()->GetLength());

  // self check
//  const PresentationContextAC * pc = inConnection.GetPresentationContextACByID(prescontid);
//  assert( pc );
//  TransferSyntaxSub const &tssub = pc->GetTransferSyntax();
  const TransferSyntax & fmits = file.GetHeader().GetDataSetTransferSyntax();
  const char *tsuidvalue = fmits.GetString();
//  TransferSyntaxSub tssub;
//  tssub.SetName( tsuidvalue );
  pc.GetTransferSyntax(0).SetName( tsuidvalue );

  std::string tsuid = fmits.GetString();

  prescontid = inConnection.GetPresentationContextIDFromPresentationContext(pc);
  // prescontid cannot possibly be unknown since we are only looking in our own
  // AAssociateRQPDU
  assert( prescontid != 0 );
  const PresentationContextRQ * rqpc = inConnection.GetPresentationContextRQByID(prescontid);
  assert( rqpc );

  // Now let's see if this best matching PresentationContextRQ can be found in the AC
  // section of the AAssociateACPDU
  const PresentationContextAC * acpc = inConnection.GetPresentationContextACByID(prescontid);

  // the following make sure that the accepted Presentation Context match the actual encoding
  // of the current File
  // ADV: technically we could use an explicit VR encoded dataset and send it over
  // an implicit TS accecpted Transfer syntax. However thing do not interchange well
  // so we really need a filter to check whether conversion is ok or not.
  if( acpc == 0 )
    {
    // Technically we should fallback to something else. Anyway lets' give up
    // and hope the user will convert the encapsulated stream to something else...
    throw Exception("Server side refuse our proposed PC.");
    }

  TransferSyntaxSub const & actssub = acpc->GetTransferSyntax();
  assert( rqpc->GetNumberOfTransferSyntaxes() == 1 ); // TODO FIXME
  TransferSyntaxSub const & rqtssub = rqpc->GetTransferSyntax(0);
  if( !(actssub == rqtssub) )
    {
    gdcmDebugMacro( "Faulty Presentation Context : "
      << (int)acpc->GetPresentationContextID() );
    throw Exception("Server side refuse our proposed PC for context id" );
    }

#if 0
  // For some reason using a dcmtk 3.5.4 server. The PresCont even if refused returned
  // filled with the default Implicit Little Endian. So make sure TS matches
  TransferSyntaxSub const & actssub = acpc->GetTransferSyntax();
  TransferSyntaxSub const & dummy0 = pc.GetTransferSyntax(0);
  if( !(actssub == pc.GetTransferSyntax(0)) )
    {
    gdcmDebugMacro( "Faulty Presentation Context : "
      << (int)acpc->GetPresentationContextID() );
    throw Exception("Server side refuse our proposed PC for context id" );
    }
#endif

  thePDV.SetPresentationContextID( prescontid );

  assert(suid.size() < std::numeric_limits<uint32_t>::max());
  de.SetByteValue( suid.c_str(), (uint32_t)suid.size()  );
  ds.Insert( de );
  }

  {
  assert( inDataSet->FindDataElement( Tag(0x0008, 0x0018) ) );
  const DataElement& msinst = inDataSet->GetDataElement( Tag(0x0008, 0x0018) );
  std::string suid;
  DataElement de( Tag(0x0,0x1000) );
  de.SetVR( VR::UI );
  if( !msinst.IsEmpty() )
    {
    const ByteValue* bv = msinst.GetByteValue();
    if( bv )
      {
      const char *uid = bv->GetPointer();
      assert( uid );
      suid = std::string(uid, bv->GetLength() );
      assert(suid.size() < std::numeric_limits<uint32_t>::max());
      }
    }
  de.SetByteValue( suid.c_str(), (uint32_t)suid.size()  );
  ds.Insert( de );
  }

  {
  Attribute<0x0,0x100> at = { 1 };
  ds.Insert( at.GetAsDataElement() );
  }
static uint32_t messageid = 1;
  {
  Attribute<0x0,0x110> at = { 0 };
  at.SetValue( (unsigned short)messageid++ );
  assert( messageid < std::numeric_limits<uint32_t>::max());
  ds.Insert( at.GetAsDataElement() );
  }
  {
  Attribute<0x0,0x700> at = { 2 };
  ds.Insert( at.GetAsDataElement() );
  }
  {
  Attribute<0x0,0x800> at = { 1 };
  ds.Insert( at.GetAsDataElement() );
  }
  {
  Attribute<0x0,0x0> at = { 0 };
  unsigned int glen = ds.GetLength<ImplicitDataElement>();
  assert( (glen % 2) == 0 );
  at.SetValue( glen );
  ds.Insert( at.GetAsDataElement() );
  }

  thePDV.SetDataSet(ds);

  //!!!Mathieu, I assume you'll want to fix this
  thePDVs.push_back(thePDV);
}

  // now let's chunk'ate the dataset:
{
  std::stringstream ss;
#if 0
  inDataSet->Write<ImplicitDataElement,SwapperNoOp>( ss );
#else
  DataSetWriter writer;
  writer.SetStream( ss );
  writer.SetFile( file );
  writer.Write();
#endif

  std::string ds_copy = ss.str();
  // E: 0006:0308 DUL Illegal PDU Length 16390.  Max expected 16384
  //const size_t maxpdu = 16384 - 6;
  size_t maxpdu = 16378;
  maxpdu = inConnection.GetMaxPDUSize() - 6;
  size_t len = ds_copy.size();
  const char *begin = ds_copy.c_str();
  const char *end = begin + len;
  const char *cur = begin;
  while( cur < end )
    {
    size_t remaining = std::min( maxpdu , (size_t)(end - cur) );
    std::string sub( cur, remaining );

    PresentationDataValue thePDV;
    std::string UIDString;
    thePDV.SetPresentationContextID( prescontid );

    thePDV.SetBlob( sub );
    cur += remaining;

    if( cur < end )
      thePDV.SetMessageHeader( 0 );
    else
      {
      assert( cur == end );
      thePDV.SetMessageHeader( 2 );
      }
    thePDVs.push_back(thePDV);
    }
}

  return thePDVs;

}

//private hack
std::vector<PresentationDataValue> CStoreRQ::ConstructPDV(
const ULConnection &inConnection, const BaseRootQuery* inRootQuery)
{
  std::vector<PresentationDataValue> thePDVs;
  (void)inRootQuery;
  (void)inConnection;
  assert( 0 && "TODO" );
  return thePDVs;
}

//private hack
std::vector<PresentationDataValue>  CStoreRSP::ConstructPDV(const ULConnection &, const BaseRootQuery* inRootQuery)
{
  std::vector<PresentationDataValue> thePDVs;
  (void)inRootQuery;
  assert( 0 && "TODO" );
  return thePDVs;
}

std::vector<PresentationDataValue> CStoreRSP::ConstructPDV(const DataSet* inDataSet, const BasePDU* inPDU){
  std::vector<PresentationDataValue> thePDVs;

///should be passed the received dataset, ie, the cstorerq, so that
///the cstorersp contains the appropriate SOP instance UIDs.
  CommandDataSet ds;

  uint32_t theMessageID = 0;
  Attribute<0x0,0x0110> at3 = { 0 };
  at3.SetFromDataSet( *inDataSet );
  theMessageID = at3.GetValue();

  const DataElement &de1 = inDataSet->GetDataElement( Tag( 0x0000,0x0002 ) );
  const DataElement &de2 = inDataSet->GetDataElement( Tag( 0x0000,0x1000 ) );
  //pass back the instance UIDs in the response
  ds.Insert(de1);
  ds.Insert(de2);

  //code is from the presentationdatavalue::myinit2
    {
    // Command Field
    Attribute<0x0,0x100> at = { 32769 };
    ds.Insert( at.GetAsDataElement() );
    }
    {
    // Message ID Being Responded To
    Attribute<0x0,0x120> at = { 1 };
    at.SetValue( (unsigned short)theMessageID );
    ds.Insert( at.GetAsDataElement() );
    }
    {
    Attribute<0x0,0x800> at = { 257 };
    ds.Insert( at.GetAsDataElement() );
    }
    {
    Attribute<0x0,0x900> at = { 0 };
    ds.Insert( at.GetAsDataElement() );
    }
    {
    Attribute<0x0,0x0> at = { 0 };
    unsigned int glen = ds.GetLength<ImplicitDataElement>();
    assert( (glen % 2) == 0 );
    at.SetValue( glen );
    ds.Insert( at.GetAsDataElement() );
    }

  PresentationDataValue pdv;

  // FIXME
  // how do we retrieve the actual PresID from the AAssociate?
  const PDataTFPDU* theDataPDU = dynamic_cast<const PDataTFPDU*>(inPDU);
  assert (theDataPDU);
  uint8_t thePDVValue;
  PresentationDataValue const &input_pdv = theDataPDU->GetPresentationDataValue(0);
  thePDVValue = input_pdv.GetPresentationContextID();

  pdv.SetPresentationContextID( thePDVValue );

  pdv.SetDataSet(ds);

  pdv.SetMessageHeader(3);
  thePDVs.push_back(pdv);

  return thePDVs;
}

}//namespace network
}//namespace gdcm