dicom-toolkit-net 0.5.0

Async DICOM networking: association management, DIMSE services (C-ECHO, C-STORE, C-FIND, C-GET, C-MOVE)
Documentation
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
//! Generic DICOM SCP server framework.
//!
//! [`DicomServer`] manages a TCP listener, concurrent association handling,
//! request routing to service providers, and graceful shutdown.
//!
//! # Quick start
//!
//! ```rust,no_run
//! use dicom_toolkit_net::server::{DicomServer, FileStoreProvider};
//!
//! #[tokio::main]
//! async fn main() {
//!     let server = DicomServer::builder()
//!         .ae_title("MYPACS")
//!         .port(4242)
//!         .store_provider(FileStoreProvider::new("/tmp/dicom"))
//!         .build()
//!         .await
//!         .expect("bind port");
//!
//!     server.run().await.expect("server error");
//! }
//! ```

use std::path::{Path, PathBuf};
use std::sync::Arc;

use tokio::net::TcpListener;
use tokio_util::sync::CancellationToken;
use tracing::{error, info, warn};

use dicom_toolkit_core::error::DcmResult;
use dicom_toolkit_data::{DataSet, FileFormat};
use dicom_toolkit_dict::tags;

use crate::association::Association;
use crate::config::AssociationConfig;
use crate::services::find::handle_find_rq;
use crate::services::get::handle_get_rq;
use crate::services::provider::{
    DestinationLookup, FindEvent, FindServiceProvider, GetEvent, GetServiceProvider, MoveEvent,
    MoveServiceProvider, RetrieveItem, StaticDestinationLookup, StoreEvent, StoreResult,
    StoreServiceProvider, STATUS_UNRECOGNISED_OPERATION,
};
use crate::services::r#move::handle_move_rq;
use crate::services::store::handle_store_rq;

// ── Service registry ──────────────────────────────────────────────────────────

/// Holds optional provider implementations for each DIMSE service.
struct ServiceRegistry {
    store: Option<Arc<dyn AnyStoreProvider>>,
    find: Option<Arc<dyn AnyFindProvider>>,
    get: Option<Arc<dyn AnyGetProvider>>,
    r#move: Option<Arc<dyn AnyMoveProvider>>,
    dest_lookup: Arc<dyn DestinationLookup>,
    local_ae: String,
}

// ── Type-erased provider wrappers ─────────────────────────────────────────────

// We need object-safe versions of the provider traits because they use
// `impl Future` return types which aren't object-safe. We use a small
// wrapper that boxes the futures.

trait AnyStoreProvider: Send + Sync + 'static {
    fn on_store<'a>(
        &'a self,
        event: StoreEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = StoreResult> + Send + 'a>>;
}

impl<P: StoreServiceProvider> AnyStoreProvider for P {
    fn on_store<'a>(
        &'a self,
        event: StoreEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = StoreResult> + Send + 'a>> {
        Box::pin(StoreServiceProvider::on_store(self, event))
    }
}

trait AnyFindProvider: Send + Sync + 'static {
    fn on_find<'a>(
        &'a self,
        event: FindEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<DataSet>> + Send + 'a>>;
}

impl<P: FindServiceProvider> AnyFindProvider for P {
    fn on_find<'a>(
        &'a self,
        event: FindEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<DataSet>> + Send + 'a>> {
        Box::pin(FindServiceProvider::on_find(self, event))
    }
}

trait AnyGetProvider: Send + Sync + 'static {
    fn on_get<'a>(
        &'a self,
        event: GetEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<RetrieveItem>> + Send + 'a>>;
}

impl<P: GetServiceProvider> AnyGetProvider for P {
    fn on_get<'a>(
        &'a self,
        event: GetEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<RetrieveItem>> + Send + 'a>> {
        Box::pin(GetServiceProvider::on_get(self, event))
    }
}

trait AnyMoveProvider: Send + Sync + 'static {
    fn on_move<'a>(
        &'a self,
        event: MoveEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<RetrieveItem>> + Send + 'a>>;
}

impl<P: MoveServiceProvider> AnyMoveProvider for P {
    fn on_move<'a>(
        &'a self,
        event: MoveEvent,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Vec<RetrieveItem>> + Send + 'a>> {
        Box::pin(MoveServiceProvider::on_move(self, event))
    }
}

// ── Type-erased adapters for SCP handler functions ────────────────────────────

struct DynStoreAdapter(Arc<dyn AnyStoreProvider>);
impl StoreServiceProvider for DynStoreAdapter {
    async fn on_store(&self, event: StoreEvent) -> StoreResult {
        self.0.on_store(event).await
    }
}

struct DynFindAdapter(Arc<dyn AnyFindProvider>);
impl FindServiceProvider for DynFindAdapter {
    async fn on_find(&self, event: FindEvent) -> Vec<DataSet> {
        self.0.on_find(event).await
    }
}

struct DynGetAdapter(Arc<dyn AnyGetProvider>);
impl GetServiceProvider for DynGetAdapter {
    async fn on_get(&self, event: GetEvent) -> Vec<RetrieveItem> {
        self.0.on_get(event).await
    }
}

struct DynMoveAdapter(Arc<dyn AnyMoveProvider>);
impl MoveServiceProvider for DynMoveAdapter {
    async fn on_move(&self, event: MoveEvent) -> Vec<RetrieveItem> {
        self.0.on_move(event).await
    }
}

// ── DicomServer ───────────────────────────────────────────────────────────────

/// A generic async DICOM SCP server.
///
/// Build with [`DicomServer::builder()`], then call [`DicomServer::run()`]
/// to accept and dispatch connections.  Graceful shutdown is achieved by
/// calling [`DicomServer::shutdown()`] from another task.
pub struct DicomServer {
    listener: TcpListener,
    registry: Arc<ServiceRegistry>,
    config: Arc<AssociationConfig>,
    max_associations: usize,
    token: CancellationToken,
}

impl DicomServer {
    /// Create a [`DicomServerBuilder`].
    pub fn builder() -> DicomServerBuilder {
        DicomServerBuilder::default()
    }

    /// Return the local address the server is listening on.
    pub fn local_addr(&self) -> std::io::Result<std::net::SocketAddr> {
        self.listener.local_addr()
    }

    /// Return a [`CancellationToken`] that, when cancelled, causes
    /// [`run()`](Self::run) to stop accepting new connections and return.
    pub fn cancellation_token(&self) -> CancellationToken {
        self.token.clone()
    }

    /// Stop the server gracefully.  In-flight associations complete normally.
    pub fn shutdown(&self) {
        self.token.cancel();
    }

    /// Run the server until [`shutdown()`](Self::shutdown) is called.
    ///
    /// Returns `Ok(())` when shutdown is clean, or an error if the listener
    /// fails.
    pub async fn run(self) -> DcmResult<()> {
        let semaphore = Arc::new(tokio::sync::Semaphore::new(self.max_associations));
        info!(
            ae = %self.registry.local_ae,
            addr = ?self.listener.local_addr(),
            "DICOM server listening"
        );

        loop {
            tokio::select! {
                _ = self.token.cancelled() => {
                    info!("DICOM server shutting down");
                    break;
                }
                result = self.listener.accept() => {
                    match result {
                        Err(e) => {
                            error!("accept error: {}", e);
                            continue;
                        }
                        Ok((stream, peer_addr)) => {
                            let permit = match semaphore.clone().try_acquire_owned() {
                                Ok(p) => p,
                                Err(_) => {
                                    warn!(%peer_addr, "max associations reached, rejecting");
                                    drop(stream);
                                    continue;
                                }
                            };

                            let registry = Arc::clone(&self.registry);
                            let config = Arc::clone(&self.config);

                            tokio::spawn(async move {
                                let _permit = permit;
                                match handle_connection(stream, &registry, &config).await {
                                    Ok(()) => {}
                                    Err(e) => {
                                        warn!(%peer_addr, "connection error: {}", e);
                                    }
                                }
                            });
                        }
                    }
                }
            }
        }
        Ok(())
    }
}

// ── Connection handler ────────────────────────────────────────────────────────

async fn handle_connection(
    stream: tokio::net::TcpStream,
    registry: &ServiceRegistry,
    config: &AssociationConfig,
) -> DcmResult<()> {
    let peer = stream.peer_addr().ok();
    if let Some(addr) = peer {
        info!(%addr, "association accepted");
    }

    let mut assoc = Association::accept(stream, config).await?;

    loop {
        let (ctx_id, cmd) = match assoc.recv_dimse_command().await {
            Ok(c) => c,
            Err(_) => break,
        };

        let command_field = cmd.get_u16(tags::COMMAND_FIELD).unwrap_or(0);

        match command_field {
            // C-ECHO-RQ — always handled, no provider required.
            0x0030 => {
                let msg_id = cmd.get_u16(tags::MESSAGE_ID).unwrap_or(1);
                let sop_class = cmd
                    .get_string(tags::AFFECTED_SOP_CLASS_UID)
                    .unwrap_or("1.2.840.10008.1.1")
                    .trim_end_matches('\0')
                    .to_string();
                let mut rsp = DataSet::new();
                rsp.set_uid(tags::AFFECTED_SOP_CLASS_UID, &sop_class);
                rsp.set_u16(tags::COMMAND_FIELD, 0x8030); // C-ECHO-RSP
                rsp.set_u16(tags::MESSAGE_ID_BEING_RESPONDED_TO, msg_id);
                rsp.set_u16(tags::COMMAND_DATA_SET_TYPE, 0x0101);
                rsp.set_u16(tags::STATUS, 0x0000);
                assoc.send_dimse_command(ctx_id, &rsp).await?;
            }

            // C-STORE-RQ
            0x0001 => {
                if let Some(provider) = &registry.store {
                    let adapter = DynStoreAdapter(Arc::clone(provider));
                    handle_store_rq(&mut assoc, ctx_id, &cmd, &adapter).await?;
                } else {
                    send_refused(&mut assoc, ctx_id, &cmd, 0x8001).await?;
                }
            }

            // C-FIND-RQ
            0x0020 => {
                if let Some(provider) = &registry.find {
                    let adapter = DynFindAdapter(Arc::clone(provider));
                    handle_find_rq(&mut assoc, ctx_id, &cmd, &adapter).await?;
                } else {
                    send_refused(&mut assoc, ctx_id, &cmd, 0x8020).await?;
                }
            }

            // C-GET-RQ
            0x0010 => {
                if let Some(provider) = &registry.get {
                    let adapter = DynGetAdapter(Arc::clone(provider));
                    handle_get_rq(&mut assoc, ctx_id, &cmd, &adapter).await?;
                } else {
                    send_refused(&mut assoc, ctx_id, &cmd, 0x8010).await?;
                }
            }

            // C-MOVE-RQ
            0x0021 => {
                if let Some(provider) = &registry.r#move {
                    let adapter = DynMoveAdapter(Arc::clone(provider));
                    handle_move_rq(
                        &mut assoc,
                        ctx_id,
                        &cmd,
                        &adapter,
                        registry.dest_lookup.as_ref(),
                        &registry.local_ae,
                    )
                    .await?;
                } else {
                    send_refused(&mut assoc, ctx_id, &cmd, 0x8021).await?;
                }
            }

            _ => {
                // Unrecognised command — send failure and continue.
                warn!(command_field, "unrecognised DIMSE command");
                break;
            }
        }
    }

    let _ = assoc.release().await;
    Ok(())
}

/// Send a failure response when no provider is registered for the service.
async fn send_refused(
    assoc: &mut Association,
    ctx_id: u8,
    cmd: &DataSet,
    rsp_command_field: u16,
) -> DcmResult<()> {
    let sop_class = cmd
        .get_string(tags::AFFECTED_SOP_CLASS_UID)
        .unwrap_or_default()
        .trim_end_matches('\0')
        .to_string();
    let msg_id = cmd.get_u16(tags::MESSAGE_ID).unwrap_or(1);

    let mut rsp = DataSet::new();
    rsp.set_uid(tags::AFFECTED_SOP_CLASS_UID, &sop_class);
    rsp.set_u16(tags::COMMAND_FIELD, rsp_command_field);
    rsp.set_u16(tags::MESSAGE_ID_BEING_RESPONDED_TO, msg_id);
    rsp.set_u16(tags::COMMAND_DATA_SET_TYPE, 0x0101);
    rsp.set_u16(tags::STATUS, STATUS_UNRECOGNISED_OPERATION);
    assoc.send_dimse_command(ctx_id, &rsp).await
}

// ── DicomServerBuilder ────────────────────────────────────────────────────────

/// Builder for [`DicomServer`].
pub struct DicomServerBuilder {
    ae_title: String,
    port: u16,
    max_associations: usize,
    config: Option<AssociationConfig>,
    store: Option<Arc<dyn AnyStoreProvider>>,
    find: Option<Arc<dyn AnyFindProvider>>,
    get: Option<Arc<dyn AnyGetProvider>>,
    r#move: Option<Arc<dyn AnyMoveProvider>>,
    dest_lookup: Option<Arc<dyn DestinationLookup>>,
}

impl Default for DicomServerBuilder {
    fn default() -> Self {
        Self {
            ae_title: "DICOMRS".to_string(),
            port: 4242,
            max_associations: 100,
            config: None,
            store: None,
            find: None,
            get: None,
            r#move: None,
            dest_lookup: None,
        }
    }
}

impl DicomServerBuilder {
    /// Set the server's AE title.
    pub fn ae_title(mut self, ae: impl Into<String>) -> Self {
        self.ae_title = ae.into();
        self
    }

    /// Set the TCP port to listen on.
    pub fn port(mut self, port: u16) -> Self {
        self.port = port;
        self
    }

    /// Maximum number of simultaneous associations.
    pub fn max_associations(mut self, n: usize) -> Self {
        self.max_associations = n;
        self
    }

    /// Override the full [`AssociationConfig`].
    pub fn config(mut self, cfg: AssociationConfig) -> Self {
        self.config = Some(cfg);
        self
    }

    /// Register a C-STORE provider.
    pub fn store_provider(mut self, p: impl StoreServiceProvider) -> Self {
        self.store = Some(Arc::new(p));
        self
    }

    /// Register a C-FIND provider.
    pub fn find_provider(mut self, p: impl FindServiceProvider) -> Self {
        self.find = Some(Arc::new(p));
        self
    }

    /// Register a C-GET provider.
    pub fn get_provider(mut self, p: impl GetServiceProvider) -> Self {
        self.get = Some(Arc::new(p));
        self
    }

    /// Register a C-MOVE provider.
    pub fn move_provider(mut self, p: impl MoveServiceProvider) -> Self {
        self.r#move = Some(Arc::new(p));
        self
    }

    /// Register a destination lookup for C-MOVE sub-associations.
    pub fn move_destination_lookup(mut self, l: impl DestinationLookup) -> Self {
        self.dest_lookup = Some(Arc::new(l));
        self
    }

    /// Build the [`DicomServer`], binding the TCP listener immediately.
    ///
    /// # Errors
    ///
    /// Returns an error if the port cannot be bound.
    pub async fn build(self) -> DcmResult<DicomServer> {
        let ae = self.ae_title.clone();
        let config = self.config.unwrap_or_else(|| AssociationConfig {
            local_ae_title: ae.clone(),
            accept_all_transfer_syntaxes: true,
            ..Default::default()
        });

        let listener = TcpListener::bind(("0.0.0.0", self.port)).await?;

        let dest_lookup: Arc<dyn DestinationLookup> = self
            .dest_lookup
            .unwrap_or_else(|| Arc::new(StaticDestinationLookup::new(vec![])));

        let registry = Arc::new(ServiceRegistry {
            store: self.store,
            find: self.find,
            get: self.get,
            r#move: self.r#move,
            dest_lookup,
            local_ae: ae,
        });

        Ok(DicomServer {
            listener,
            registry,
            config: Arc::new(config),
            max_associations: self.max_associations,
            token: CancellationToken::new(),
        })
    }
}

// ── Built-in providers ────────────────────────────────────────────────────────

/// A ready-to-use [`StoreServiceProvider`] that saves received DICOM
/// instances as `.dcm` files in a given directory.
///
/// # Example
///
/// ```rust,no_run
/// use dicom_toolkit_net::server::{DicomServer, FileStoreProvider};
///
/// # async fn run() {
/// let server = DicomServer::builder()
///     .store_provider(FileStoreProvider::new("/tmp/dicom"))
///     .build()
///     .await
///     .unwrap();
/// # }
/// ```
pub struct FileStoreProvider {
    dir: PathBuf,
}

impl FileStoreProvider {
    /// Create a new `FileStoreProvider` that stores files in `dir`.
    pub fn new(dir: impl AsRef<Path>) -> Self {
        Self {
            dir: dir.as_ref().to_path_buf(),
        }
    }
}

impl StoreServiceProvider for FileStoreProvider {
    async fn on_store(&self, event: StoreEvent) -> StoreResult {
        let ff =
            FileFormat::from_dataset(&event.sop_class_uid, &event.sop_instance_uid, event.dataset);

        let safe: String = event
            .sop_instance_uid
            .chars()
            .map(|c| {
                if c.is_alphanumeric() || c == '.' {
                    c
                } else {
                    '_'
                }
            })
            .collect();

        let dest = self.dir.join(format!("{safe}.dcm"));

        match ff.save(&dest) {
            Ok(()) => {
                info!(path = %dest.display(), "stored instance");
                StoreResult::success()
            }
            Err(e) => {
                error!(path = %dest.display(), error = %e, "failed to save instance");
                StoreResult::failure(crate::services::provider::STATUS_PROCESSING_FAILURE)
            }
        }
    }
}