File: storage_commit.c

package info (click to toggle)
ctn 3.2.0~dfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,936 kB
  • sloc: ansic: 179,652; makefile: 7,007; java: 1,863; csh: 1,067; yacc: 523; sh: 424; cpp: 394; sql: 389; lex: 170
file content (474 lines) | stat: -rw-r--r-- 12,186 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
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
/*
+-+-+-+-+-+-+-+-+-
*/
/*
**		     Electronic Radiology Laboratory
**		   Mallinckrodt Institute of Radiology
**		Washington University School of Medicine
**
** Module Name(s):
** Author, Date:	Steve Moore
** Intent:		This program finds a study in the CTN IDB
**			(image database) and sends a storage commit
**
** Last Update:		$Author: smm $, $Date: 2001-12-21 16:43:43 $
** Source File:		$RCSfile: storage_commit.c,v $
** Revision:		$Revision: 1.5 $
** Status:		$State: Exp $
*/

static char rcsid[] = "$Revision: 1.5 $ $RCSfile: storage_commit.c,v $";

#include "ctn_os.h"

#if 0
#include <stdio.h>
#include <stdlib.h>
#endif

#include "dicom.h"
#include "ctnthread.h"
#include "condition.h"
#include "tbl.h"
#include "lst.h"
#include "idb.h"
#include "dulprotocol.h"
#include "dicom_objects.h"
#include "dicom_messages.h"
#include "dicom_services.h"
#include "manage.h"
#include "fis.h"

#include "storage_commit.h"

CTNBOOLEAN silent = TRUE;

static char
   *dbname = "INSTRUMENT_IDB";

typedef struct {
    void *reserved[2];
    IDB_Query query;
}   QUERY_LIST_ITEM;

static CONDITION
selectCallback(IDB_Query * queryResponse,
	       long count, LST_HEAD * lst);

static CTNBOOLEAN verbose = FALSE;
CTNBOOLEAN verboseDUL = FALSE;
char *controlDatabase = "CTNControl";

static void usageerror();
static void openDatabase(const char *dbname, IDB_HANDLE ** handle);
static void closeDatabase(IDB_HANDLE ** handle);
static void openControl(char *controlDatabase, DMAN_HANDLE ** dmanHandle);
static void openFIS(char *fisDatabase, FIS_HANDLE ** fisHandle);
static LST_HEAD *
selectImages(IDB_HANDLE ** h, const char *patientID,
	     const char *studyInstanceUID, const char *accessionNumber);
static void
insertDBRecords(FIS_HANDLE ** handle, const char *callingAETitle,
		const char *destinationAETitle, LST_HEAD ** imageLlist,
		char *transactionUID);
static void
sendImages(LST_HEAD * imageList, char *destinationAETitle,
	   DMAN_HANDLE ** dmanHandle);
CONDITION
establishAssociation(DUL_NETWORKKEY ** networkKey,
                     char *destination,
                     DMAN_HANDLE ** handle,
                     DUL_ASSOCIATIONKEY ** sendAssociation,
                     DUL_ASSOCIATESERVICEPARAMETERS * params);
CONDITION
sendStorageCommitRequest(FIS_HANDLE ** fisHandle, char *transactionUID,
                         DUL_ASSOCIATIONKEY ** association,
                         DUL_ASSOCIATESERVICEPARAMETERS * params);

static void
loadTitleAddressParameters(DUL_ASSOCIATESERVICEPARAMETERS * params,
			   const char *callingAPTitle,
			   const char *calledAPTitle)
{
    char localHost[256];

    (void) gethostname(localHost, sizeof(localHost));
    strcpy(params->callingPresentationAddress, localHost);
    strcpy(params->calledAPTitle, calledAPTitle);
    strcpy(params->callingAPTitle, callingAPTitle);
}

main(int argc, char **argv)
{
    CONDITION
	cond;
    IDB_HANDLE
	* handle;
    IDB_Query
	recs;
    long
        i,
        count;
    static char *callingAETitle = "CTN_INSTRUMENT";
    static char *calledAETitle = "";
    char *patientID = "";
    char *studyInstanceUID = "";
    char *accessionNumber = "";
    LST_HEAD *imageList = NULL;
    DMAN_HANDLE *dmanHandle = NULL;
    FIS_HANDLE *fisHandle = NULL;
    char *fisDatabase = "INSTRUMENT_FIS";
    char transactionUID[DICOM_UI_LENGTH + 1];
    DUL_NETWORKKEY *networkKey;
    DUL_ASSOCIATIONKEY *associationKey = NULL;
    DUL_ASSOCIATESERVICEPARAMETERS serviceParameters;

    while (--argc > 0 && (*++argv)[0] == '-') {
	switch (*(argv[0] + 1)) {
	case 'a':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    callingAETitle = *argv;
	    break;
	case 'd':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    dbname = *argv;
	    break;
	case 'f':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    controlDatabase = *argv;
	    break;
	case 'i':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    fisDatabase = *argv;
	    break;
	case 'p':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    patientID = *argv;
	    break;
	case 'r':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    accessionNumber = *argv;
	    break;
	case 'u':
	    argc--;
	    argv++;
	    if (argc < 1)
		usageerror();
	    studyInstanceUID = *argv;
	    break;
	case 'v':
	    verbose = TRUE;
	    break;
	default:
	    break;
	}
    }

    if (argc < 1)
	usageerror();

    calledAETitle = *argv;

    THR_Init();
    openDatabase(dbname, &handle);
    openControl(controlDatabase, &dmanHandle);
    openFIS(fisDatabase, &fisHandle);

    imageList = selectImages(&handle, patientID, studyInstanceUID, accessionNumber);
    if (LST_Count(&imageList) == 0) {
	fprintf(stderr, "No images found to match the criteria you specified.\n");
	THR_Shutdown();
	exit(1);
    }
    insertDBRecords(&fisHandle, callingAETitle, calledAETitle, &imageList,
		    transactionUID);

    cond = DUL_InitializeNetwork(DUL_NETWORK_TCP, DUL_AEREQUESTOR,
			NULL, DUL_TIMEOUT, DUL_ORDERBIGENDIAN, &networkKey);
    if (cond != DUL_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    DUL_DefaultServiceParameters(&serviceParameters);
    loadTitleAddressParameters(&serviceParameters, callingAETitle, calledAETitle);

    if (establishAssociation(&networkKey,
			     calledAETitle,
			     &dmanHandle,
			     &associationKey,
			     &serviceParameters) != APP_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    sendStorageCommitRequest(&fisHandle, transactionUID,
			     &associationKey, &serviceParameters);

    /* sendImages(imageList, destinationAETitle, &dmanHandle); */
    closeDatabase(&handle);

    (void) DUL_ReleaseAssociation(&associationKey);
    (void) DUL_DropAssociation(&associationKey);
    (void) DUL_ClearServiceParameters(&serviceParameters);

    THR_Shutdown();
    return 0;
}

static void
usageerror()
{
    char msg[] = "\
Usage: storage_commit [-a title] [-d <dbname>] [-f <control>] [-i <fis name>] [-p <pat id>] [-r <req num>] [-u <study UID>] destinationAETitle\n\
\n\
  -a  Set calling AE title (default CTN_INSTRUMENT)\n\
  -d  Set image database name (default INSTRUMENT_IDB)\n\
  -f  Set control database name (default CTNControl)\n\
  -i  Set FIS database name (default INSTRUMENT_FIS)\n\
  -p  Send storage commit req for images with patient ID <pat id>\n\
  -r  Send storage commit req for images with accession number <req num>\n\
  -u  Send storage commit req for images with study instance UID <study UID>\n\
\n\
  destinationAETitle	An AE title found in the control database\n";

    fprintf(stderr, "%s", msg);
    exit(1);
}

static void
openDatabase(const char *dbname, IDB_HANDLE ** handle)
{
    CONDITION cond = 0;

    cond = IDB_Open(dbname, handle);
    if (cond != IDB_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
}

static void
closeDatabase(IDB_HANDLE ** handle)
{
    CONDITION cond = 0;

    cond = IDB_Close(handle);
    if (cond != IDB_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
}

static void
openControl(char *controlDatabase, DMAN_HANDLE ** dmanHandle)
{
    CONDITION cond = 0;

    cond = DMAN_Open(controlDatabase, "", "", dmanHandle);
    if (cond != DMAN_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
}

static void
openFIS(char *fisDatabase, FIS_HANDLE ** fisHandle)
{
    CONDITION cond = 0;

    cond = FIS_Open(fisDatabase, fisHandle);
    if (cond != FIS_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
}


static LST_HEAD *
selectImages(IDB_HANDLE ** h, const char *patientID,
	     const char *studyInstanceUID, const char *accessionNumber)
{
    CONDITION cond;
    IDB_HANDLE *handle;
    IDB_Query recs;
    long count;
    LST_HEAD *imageList = NULL;

    recs.PatientQFlag = recs.StudyQFlag = recs.SeriesQFlag = recs.ImageQFlag = 0;
    recs.PatientNullFlag = recs.StudyNullFlag =
	recs.SeriesNullFlag = recs.ImageNullFlag = 0;


    if (strlen(patientID) != 0) {
	recs.PatientQFlag |= QF_PAT_PatID;
	strcpy(recs.patient.PatID, patientID);
    }
    if (strlen(studyInstanceUID) != 0) {
	recs.StudyQFlag |= QF_STU_StuInsUID;
	strcpy(recs.study.StuInsUID, studyInstanceUID);
    }
    if (strlen(accessionNumber) != 0) {
	recs.StudyQFlag |= QF_STU_AccNum;
	strcpy(recs.study.AccNum, accessionNumber);
    }
    if (imageList == NULL) {
	imageList = LST_Create();
	if (imageList == NULL) {
	    THR_Shutdown();
	    exit(1);
	}
    }
    cond = IDB_Select(h, STUDY_ROOT, IDB_PATIENT_LEVEL,
		 IDB_IMAGE_LEVEL, &recs, &count, selectCallback, imageList);
    if (cond != IDB_NORMAL) {
	COND_DumpConditions();
    }
    return imageList;
}

static void
insertCommitSOPRef(FIS_HANDLE ** fisHandle,
	   FIS_STORAGECOMMITREQRECORD * commit, QUERY_LIST_ITEM * queryItem)
{
    FIS_COMMITSOPREFRECORD commitSOP;
    CONDITION cond;

    commitSOP.Type = FIS_K_COMMITSOPREF;
    cond = FIS_NewRecord(fisHandle, FIS_K_COMMITSOPREF, &commitSOP);
    if (cond != FIS_NORMAL) {
	fprintf(stderr, "Could not make a new Storage Commitment SOP Reference database record\n");
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    strcpy(commitSOP.TraUID, commit->TraUID);
    strcpy(commitSOP.SOPClaUID, queryItem->query.image.SOPClaUID);
    strcpy(commitSOP.SOPInsUID, queryItem->query.image.SOPInsUID);

    commitSOP.Flag |= FIS_K_COMMITSOP_TRAUID |
	FIS_K_COMMITSOP_SOPCLAUID | FIS_K_COMMITSOP_SOPINSUID;

    cond = FIS_Insert(fisHandle, FIS_K_COMMITSOPREF, &commitSOP);
    if (cond != FIS_NORMAL) {
	fprintf(stderr, "Could not insert new FIS Storage Commit SOP Reference into DB\n");
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
}

static void
insertDBRecords(FIS_HANDLE ** fisHandle, const char *callingAETitle,
		const char *destinationAETitle, LST_HEAD ** imageList,
		char *transactionUID)
{
    FIS_STORAGECOMMITREQRECORD commit;
    CONDITION cond;
    QUERY_LIST_ITEM *queryItem;

    commit.Type = FIS_K_STORAGECOMMITREQ;
    cond = FIS_NewRecord(fisHandle, FIS_K_STORAGECOMMITREQ, &commit);
    if (cond != FIS_NORMAL) {
	fprintf(stderr, "Could not make a new Storage Commitment database record\n");
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    strcpy(transactionUID, commit.TraUID);

    strcpy(commit.ReqAE, callingAETitle);
    strcpy(commit.ResAE, destinationAETitle);
    commit.Flag |= FIS_K_SCOMMIT_REQAE | FIS_K_SCOMMIT_RESAE;

    cond = FIS_Insert(fisHandle, FIS_K_STORAGECOMMITREQ, &commit);
    if (cond != FIS_NORMAL) {
	fprintf(stderr, "Could not insert new FIS Storage Commit Request into DB\n");
	COND_DumpConditions();
	THR_Shutdown();
	exit(1);
    }
    queryItem = LST_Head(imageList);
    if (queryItem != NULL)
	(void) LST_Position(imageList, queryItem);

    while ((queryItem = LST_Dequeue(imageList)) != NULL) {
	insertCommitSOPRef(fisHandle, &commit, queryItem);
    }
}



static DUL_ASSOCIATESERVICEPARAMETERS *
createAssociationParameters()
{
    DUL_ASSOCIATESERVICEPARAMETERS *p;
    char localHost[128];

    p = malloc(sizeof(*p));
    if (p == NULL) {
	fprintf(stderr, "Could not allocate Service Parameters structure\n");
	THR_Shutdown();
	exit(1);
    }
    memset(p, 0, sizeof(*p));
    DUL_DefaultServiceParameters(p);
    (void) gethostname(localHost, sizeof(localHost));
    strcpy(p->callingPresentationAddress, localHost);
    p->maxPDU = 16384;

    return p;
}


static CONDITION
selectCallback(IDB_Query * queryResponse, long count, LST_HEAD * lst)
{
    QUERY_LIST_ITEM
    * item;
    IDB_InstanceListElement
	* e1, *e2;

    item = malloc(sizeof(*item));
    if (item == NULL)
	return 0;

    item->query = *queryResponse;
    if (queryResponse->image.InstanceList != NULL) {
	item->query.image.InstanceList = LST_Create();
	if (item->query.image.InstanceList == NULL)
	    return 0;
	e1 = LST_Head(&queryResponse->image.InstanceList);
	(void) LST_Position(&queryResponse->image.InstanceList, e1);
	while (e1 != NULL) {
	    e2 = malloc(sizeof(*e2));
	    *e2 = *e1;
	    (void) LST_Enqueue(&item->query.image.InstanceList, e2);
	    e1 = LST_Next(&queryResponse->image.InstanceList);
	}
    }
    (void) LST_Enqueue(&lst, item);
    return IDB_NORMAL;
}