This repository has been archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
/
serviceprovider.go
541 lines (507 loc) · 18.9 KB
/
serviceprovider.go
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
// This file defines ServiceProvider (i.e., a DICOM server).
package netdicom
import (
"crypto/tls"
"fmt"
"net"
dicom "github.com/grailbio/go-dicom"
"github.com/grailbio/go-dicom/dicomio"
"github.com/grailbio/go-dicom/dicomlog"
"github.com/grailbio/go-netdicom/dimse"
"github.com/grailbio/go-netdicom/sopclass"
)
// CMoveResult is an object streamed by CMove implementation.
type CMoveResult struct {
Remaining int // Number of files remaining to be sent. Set -1 if unknown.
Err error
Path string // Path name of the DICOM file being copied. Used only for reporting errors.
DataSet *dicom.DataSet // Contents of the file.
}
func handleCStore(
cb CStoreCallback,
connState ConnectionState,
c *dimse.CStoreRq, data []byte,
cs *serviceCommandState) {
status := dimse.Status{Status: dimse.StatusUnrecognizedOperation}
if cb != nil {
status = cb(
connState,
cs.context.transferSyntaxUID,
c.AffectedSOPClassUID,
c.AffectedSOPInstanceUID,
data)
}
resp := &dimse.CStoreRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
AffectedSOPInstanceUID: c.AffectedSOPInstanceUID,
Status: status,
}
cs.sendMessage(resp, nil)
}
func handleCFind(
params ServiceProviderParams,
connState ConnectionState,
c *dimse.CFindRq, data []byte,
cs *serviceCommandState) {
if params.CFind == nil {
cs.sendMessage(&dimse.CFindRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: dimse.Status{Status: dimse.StatusUnrecognizedOperation, ErrorComment: "No callback found for C-FIND"},
}, nil)
return
}
elems, err := readElementsInBytes(data, cs.context.transferSyntaxUID)
if err != nil {
cs.sendMessage(&dimse.CFindRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: dimse.Status{Status: dimse.StatusUnrecognizedOperation, ErrorComment: err.Error()},
}, nil)
return
}
dicomlog.Vprintf(1, "dicom.serviceProvider: C-FIND-RQ payload: %s", elementsString(elems))
status := dimse.Status{Status: dimse.StatusSuccess}
responseCh := make(chan CFindResult, 128)
go func() {
params.CFind(connState, cs.context.transferSyntaxUID, c.AffectedSOPClassUID, elems, responseCh)
}()
for resp := range responseCh {
if resp.Err != nil {
status = dimse.Status{
Status: dimse.CFindUnableToProcess,
ErrorComment: resp.Err.Error(),
}
break
}
dicomlog.Vprintf(1, "dicom.serviceProvider: C-FIND-RSP: %s", elementsString(resp.Elements))
payload, err := writeElementsToBytes(resp.Elements, cs.context.transferSyntaxUID)
if err != nil {
dicomlog.Vprintf(0, "dicom.serviceProvider: C-FIND: encode error %v", err)
status = dimse.Status{
Status: dimse.CFindUnableToProcess,
ErrorComment: err.Error(),
}
break
}
cs.sendMessage(&dimse.CFindRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNonNull,
Status: dimse.Status{Status: dimse.StatusPending},
}, payload)
}
cs.sendMessage(&dimse.CFindRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: status}, nil)
// Drain the responses in case of errors
for range responseCh {
}
}
func handleCMove(
params ServiceProviderParams,
connState ConnectionState,
c *dimse.CMoveRq, data []byte,
cs *serviceCommandState) {
sendError := func(err error) {
cs.sendMessage(&dimse.CMoveRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: dimse.Status{Status: dimse.StatusUnrecognizedOperation, ErrorComment: err.Error()},
}, nil)
}
if params.CMove == nil {
cs.sendMessage(&dimse.CMoveRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: dimse.Status{Status: dimse.StatusUnrecognizedOperation, ErrorComment: "No callback found for C-MOVE"},
}, nil)
return
}
remoteHostPort, ok := params.RemoteAEs[c.MoveDestination]
if !ok {
sendError(fmt.Errorf("C-MOVE destination '%v' not registered in the server", c.MoveDestination))
return
}
elems, err := readElementsInBytes(data, cs.context.transferSyntaxUID)
if err != nil {
sendError(err)
return
}
dicomlog.Vprintf(1, "dicom.serviceProvider: C-MOVE-RQ payload: %s", elementsString(elems))
responseCh := make(chan CMoveResult, 128)
go func() {
params.CMove(connState, cs.context.transferSyntaxUID, c.AffectedSOPClassUID, elems, responseCh)
}()
// responseCh :=
status := dimse.Status{Status: dimse.StatusSuccess}
var numSuccesses, numFailures uint16
for resp := range responseCh {
if resp.Err != nil {
status = dimse.Status{
Status: dimse.CFindUnableToProcess,
ErrorComment: resp.Err.Error(),
}
break
}
dicomlog.Vprintf(0, "dicom.serviceProvider: C-MOVE: Sending %v to %v(%s)", resp.Path, c.MoveDestination, remoteHostPort)
err := runCStoreOnNewAssociation(params.AETitle, c.MoveDestination, remoteHostPort, resp.DataSet)
if err != nil {
dicomlog.Vprintf(0, "dicom.serviceProvider: C-MOVE: C-store of %v to %v(%v) failed: %v", resp.Path, c.MoveDestination, remoteHostPort, err)
numFailures++
} else {
numSuccesses++
}
cs.sendMessage(&dimse.CMoveRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
NumberOfRemainingSuboperations: uint16(resp.Remaining),
NumberOfCompletedSuboperations: numSuccesses,
NumberOfFailedSuboperations: numFailures,
Status: dimse.Status{Status: dimse.StatusPending},
}, nil)
}
cs.sendMessage(&dimse.CMoveRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
NumberOfCompletedSuboperations: numSuccesses,
NumberOfFailedSuboperations: numFailures,
Status: status}, nil)
// Drain the responses in case of errors
for range responseCh {
}
}
func handleCGet(
params ServiceProviderParams,
connState ConnectionState,
c *dimse.CGetRq, data []byte, cs *serviceCommandState) {
sendError := func(err error) {
cs.sendMessage(&dimse.CGetRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: dimse.Status{Status: dimse.StatusUnrecognizedOperation, ErrorComment: err.Error()},
}, nil)
}
if params.CGet == nil {
cs.sendMessage(&dimse.CGetRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: dimse.Status{Status: dimse.StatusUnrecognizedOperation, ErrorComment: "No callback found for C-GET"},
}, nil)
return
}
elems, err := readElementsInBytes(data, cs.context.transferSyntaxUID)
if err != nil {
sendError(err)
return
}
dicomlog.Vprintf(1, "dicom.serviceProvider: C-GET-RQ payload: %s", elementsString(elems))
responseCh := make(chan CMoveResult, 128)
go func() {
params.CGet(connState, cs.context.transferSyntaxUID, c.AffectedSOPClassUID, elems, responseCh)
}()
status := dimse.Status{Status: dimse.StatusSuccess}
var numSuccesses, numFailures uint16
for resp := range responseCh {
if resp.Err != nil {
status = dimse.Status{
Status: dimse.CFindUnableToProcess,
ErrorComment: resp.Err.Error(),
}
break
}
subCs, err := cs.disp.newCommand(cs.cm, cs.context /*not used*/)
if err != nil {
status = dimse.Status{
Status: dimse.CFindUnableToProcess,
ErrorComment: err.Error(),
}
break
}
err = runCStoreOnAssociation(subCs.upcallCh, subCs.disp.downcallCh, subCs.cm, subCs.messageID, resp.DataSet)
if err != nil {
dicomlog.Vprintf(0, "dicom.serviceProvider: C-GET: C-store of %v failed: %v", resp.Path, err)
numFailures++
} else {
dicomlog.Vprintf(0, "dicom.serviceProvider: C-GET: Sent %v", resp.Path)
numSuccesses++
}
cs.sendMessage(&dimse.CGetRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
NumberOfRemainingSuboperations: uint16(resp.Remaining),
NumberOfCompletedSuboperations: numSuccesses,
NumberOfFailedSuboperations: numFailures,
Status: dimse.Status{Status: dimse.StatusPending},
}, nil)
cs.disp.deleteCommand(subCs)
}
cs.sendMessage(&dimse.CGetRsp{
AffectedSOPClassUID: c.AffectedSOPClassUID,
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
NumberOfCompletedSuboperations: numSuccesses,
NumberOfFailedSuboperations: numFailures,
Status: status}, nil)
// Drain the responses in case of errors
for range responseCh {
}
}
func handleCEcho(
params ServiceProviderParams,
connState ConnectionState,
c *dimse.CEchoRq, data []byte,
cs *serviceCommandState) {
status := dimse.Status{Status: dimse.StatusUnrecognizedOperation}
if params.CEcho != nil {
status = params.CEcho(connState)
}
dicomlog.Vprintf(0, "dicom.serviceProvider: Received E-ECHO: context: %+v, status: %+v", cs.context, status)
resp := &dimse.CEchoRsp{
MessageIDBeingRespondedTo: c.MessageID,
CommandDataSetType: dimse.CommandDataSetTypeNull,
Status: status,
}
cs.sendMessage(resp, nil)
}
// ServiceProviderParams defines parameters for ServiceProvider.
type ServiceProviderParams struct {
// The application-entity title of the server. Must be nonempty
AETitle string
// Names of remote AEs and their host:ports. Used only by C-MOVE. This
// map should be nonempty iff the server supports CMove.
RemoteAEs map[string]string
// Called on C_ECHO request. If nil, a C-ECHO call will produce an error response.
//
// TODO(saito) Support a default C-ECHO callback?
CEcho CEchoCallback
// Called on C_FIND request.
// If CFindCallback=nil, a C-FIND call will produce an error response.
CFind CFindCallback
// CMove is called on C_MOVE request.
CMove CMoveCallback
// CGet is called on C_GET request. The only difference between cmove
// and cget is that cget uses the same connection to send images back to
// the requester. Generally you shuold set the same function to CMove
// and CGet.
CGet CMoveCallback
// If CStoreCallback=nil, a C-STORE call will produce an error response.
CStore CStoreCallback
// TLSConfig, if non-nil, enables TLS on the connection. See
// https://gist.github.com/michaljemala/d6f4e01c4834bf47a9c4 for an
// example for creating a TLS config from x509 cert files.
TLSConfig *tls.Config
}
// DefaultMaxPDUSize is the the PDU size advertized by go-netdicom.
const DefaultMaxPDUSize = 4 << 20
// CStoreCallback is called C-STORE request. sopInstanceUID is the UID of the
// data. sopClassUID is the data type requested
// (e.g.,"1.2.840.10008.5.1.4.1.1.1.2"), and transferSyntaxUID is the encoding
// of the data (e.g., "1.2.840.10008.1.2.1"). These args are extracted from the
// request packet.
//
// "data" is the payload, i.e., a sequence of serialized dicom.DataElement
// objects in transferSyntaxUID. "data" does not contain metadata elements
// (elements whose Tag.Group=2 -- e.g., TransferSyntaxUID and
// MediaStorageSOPClassUID), since they are stripped by the requster (two key
// metadata are passed as sop{Class,Instance)UID).
//
// The function should store encode the sop{Class,InstanceUID} as the DICOM
// header, followed by data. It should return either dimse.Success0 on success,
// or one of CStoreStatus* error codes on errors.
type CStoreCallback func(
conn ConnectionState,
transferSyntaxUID string,
sopClassUID string,
sopInstanceUID string,
data []byte) dimse.Status
// CFindCallback implements a C-FIND handler. sopClassUID is the data type
// requested (e.g.,"1.2.840.10008.5.1.4.1.1.1.2"), and transferSyntaxUID is the
// data encoding requested (e.g., "1.2.840.10008.1.2.1"). These args are
// extracted from the request packet.
//
// This function should stream CFindResult objects through "ch". The function
// may block. To report a matched DICOM dataset, the function should send one
// CFindResult with a nonempty Element field. To report multiple DICOM-dataset
// matches, the callback should send multiple CFindResult objects, one for each
// dataset. The callback must close the channel after it produces all the
// responses.
type CFindCallback func(
conn ConnectionState,
transferSyntaxUID string,
sopClassUID string,
filters []*dicom.Element,
ch chan CFindResult)
// CMoveCallback implements C-MOVE or C-GET handler. sopClassUID is the data
// type requested (e.g.,"1.2.840.10008.5.1.4.1.1.1.2"), and transferSyntaxUID is
// the data encoding requested (e.g., "1.2.840.10008.1.2.1"). These args are
// extracted from the request packet.
//
// The callback must stream datasets or error to "ch". The callback may
// block. The callback must close the channel after it produces all the
// datasets.
type CMoveCallback func(
conn ConnectionState,
transferSyntaxUID string,
sopClassUID string,
filters []*dicom.Element,
ch chan CMoveResult)
// ConnectionState informs session state to callbacks.
type ConnectionState struct {
// TLS connection state. It is nonempty only when the connection is set up
// over TLS.
TLS tls.ConnectionState
}
// CEchoCallback implements C-ECHO callback. It typically just returns
// dimse.Success.
type CEchoCallback func(conn ConnectionState) dimse.Status
// ServiceProvider encapsulates the state for DICOM server (provider).
type ServiceProvider struct {
params ServiceProviderParams
listener net.Listener
// Label is a unique string used in log messages to identify this provider.
label string
}
func writeElementsToBytes(elems []*dicom.Element, transferSyntaxUID string) ([]byte, error) {
dataEncoder := dicomio.NewBytesEncoderWithTransferSyntax(transferSyntaxUID)
for _, elem := range elems {
dicom.WriteElement(dataEncoder, elem)
}
if err := dataEncoder.Error(); err != nil {
return nil, err
}
return dataEncoder.Bytes(), nil
}
func readElementsInBytes(data []byte, transferSyntaxUID string) ([]*dicom.Element, error) {
decoder := dicomio.NewBytesDecoderWithTransferSyntax(data, transferSyntaxUID)
var elems []*dicom.Element
for !decoder.EOF() {
elem := dicom.ReadElement(decoder, dicom.ReadOptions{})
dicomlog.Vprintf(1, "dicom.serviceProvider: C-FIND: Read elem: %v, err %v", elem, decoder.Error())
if decoder.Error() != nil {
break
}
elems = append(elems, elem)
}
if decoder.Error() != nil {
return nil, decoder.Error()
}
return elems, nil
}
func elementsString(elems []*dicom.Element) string {
s := "["
for i, elem := range elems {
if i > 0 {
s += ", "
}
s += elem.String()
}
return s + "]"
}
// Send "ds" to remoteHostPort using C-STORE. Called as part of C-MOVE.
func runCStoreOnNewAssociation(myAETitle, remoteAETitle, remoteHostPort string, ds *dicom.DataSet) error {
su, err := NewServiceUser(ServiceUserParams{
CalledAETitle: remoteAETitle,
CallingAETitle: myAETitle,
SOPClasses: sopclass.StorageClasses})
if err != nil {
return err
}
defer su.Release()
su.Connect(remoteHostPort)
err = su.CStore(ds)
dicomlog.Vprintf(1, "dicom.serviceProvider: C-STORE subop done: %v", err)
return err
}
// NewServiceProvider creates a new DICOM server object. "listenAddr" is the
// TCP address to listen to. E.g., ":1234" will listen to port 1234 at all the
// IP address that this machine can bind to. Run() will actually start running
// the service.
func NewServiceProvider(params ServiceProviderParams, port string) (*ServiceProvider, error) {
sp := &ServiceProvider{
params: params,
label: newUID("sp"),
}
var err error
if params.TLSConfig != nil {
sp.listener, err = tls.Listen("tcp", port, params.TLSConfig)
} else {
sp.listener, err = net.Listen("tcp", port)
}
if err != nil {
return nil, err
}
return sp, nil
}
func getConnState(conn net.Conn) (cs ConnectionState) {
tlsConn, ok := conn.(*tls.Conn)
if ok {
cs.TLS = tlsConn.ConnectionState()
}
return
}
// RunProviderForConn starts threads for running a DICOM server on "conn". This
// function returns immediately; "conn" will be cleaned up in the background.
func RunProviderForConn(conn net.Conn, params ServiceProviderParams) {
upcallCh := make(chan upcallEvent, 128)
label := newUID("sc")
disp := newServiceDispatcher(label)
disp.registerCallback(dimse.CommandFieldCStoreRq,
func(msg dimse.Message, data []byte, cs *serviceCommandState) {
handleCStore(params.CStore, getConnState(conn), msg.(*dimse.CStoreRq), data, cs)
})
disp.registerCallback(dimse.CommandFieldCFindRq,
func(msg dimse.Message, data []byte, cs *serviceCommandState) {
handleCFind(params, getConnState(conn), msg.(*dimse.CFindRq), data, cs)
})
disp.registerCallback(dimse.CommandFieldCMoveRq,
func(msg dimse.Message, data []byte, cs *serviceCommandState) {
handleCMove(params, getConnState(conn), msg.(*dimse.CMoveRq), data, cs)
})
disp.registerCallback(dimse.CommandFieldCGetRq,
func(msg dimse.Message, data []byte, cs *serviceCommandState) {
handleCGet(params, getConnState(conn), msg.(*dimse.CGetRq), data, cs)
})
disp.registerCallback(dimse.CommandFieldCEchoRq,
func(msg dimse.Message, data []byte, cs *serviceCommandState) {
handleCEcho(params, getConnState(conn), msg.(*dimse.CEchoRq), data, cs)
})
go runStateMachineForServiceProvider(conn, upcallCh, disp.downcallCh, label)
for event := range upcallCh {
disp.handleEvent(event)
}
dicomlog.Vprintf(0, "dicom.serviceProvider(%s): Finished connection %p (remote: %+v)", label, conn, conn.RemoteAddr())
disp.close()
}
// Run listens to incoming connections, accepts them, and runs the DICOM
// protocol. This function never returns.
func (sp *ServiceProvider) Run() {
for {
conn, err := sp.listener.Accept()
if err != nil {
dicomlog.Vprintf(0, "dicom.serviceProvider(%s): Accept error: %v", sp.label, err)
continue
}
dicomlog.Vprintf(0, "dicom.serviceProvider(%s): Accepted connection %p (remote: %+v)", sp.label, conn, conn.RemoteAddr())
go func() { RunProviderForConn(conn, sp.params) }()
}
}
// ListenAddr returns the TCP address that the server is listening on. It is the
// address passed to the NewServiceProvider(), except that if value was of form
// <name>:0, the ":0" part is replaced by the actual port numwber.
func (sp *ServiceProvider) ListenAddr() net.Addr {
return sp.listener.Addr()
}