34std::atomic<kcenon::pacs::network::dicom_server*> g_server{
nullptr};
37std::atomic<bool> g_running{
true};
43void signal_handler(
int signal) {
44 std::cout <<
"\nReceived signal " << signal <<
", shutting down...\n";
47 auto* server = g_server.load();
56void install_signal_handlers() {
57 std::signal(SIGINT, signal_handler);
58 std::signal(SIGTERM, signal_handler);
60 std::signal(SIGHUP, signal_handler);
68void print_usage(
const char* program_name) {
70Echo SCP - DICOM Connectivity Test Server
72Usage: )" << program_name << R"( <port> <ae_title> [options]
75 port Port number to listen on (typically 104 or 11112)
76 ae_title Application Entity Title for this server (max 16 chars)
79 --max-assoc <n> Maximum concurrent associations (default: 10)
80 --timeout <sec> Idle timeout in seconds (default: 300)
81 --help Show this help message
84 )" << program_name << R"( 11112 MY_PACS
85 )" << program_name << R"( 104 DICOM_SERVER --max-assoc 20
88 - Press Ctrl+C to stop the server gracefully
89 - The server responds to C-ECHO requests from any calling AE
93 1 Error - Failed to start server
111 std::string& ae_title,
112 size_t& max_associations,
113 uint32_t& idle_timeout) {
120 for (
int i = 1; i < argc; ++i) {
121 if (std::string(argv[i]) ==
"--help" || std::string(argv[i]) ==
"-h") {
128 int port_int = std::stoi(argv[1]);
129 if (port_int < 1 || port_int > 65535) {
130 std::cerr <<
"Error: Port must be between 1 and 65535\n";
133 port =
static_cast<uint16_t
>(port_int);
134 }
catch (
const std::exception&) {
135 std::cerr <<
"Error: Invalid port number '" << argv[1] <<
"'\n";
141 if (ae_title.length() > 16) {
142 std::cerr <<
"Error: AE title exceeds 16 characters\n";
147 max_associations = 10;
151 for (
int i = 3; i < argc; ++i) {
152 std::string arg = argv[i];
154 if (arg ==
"--max-assoc" && i + 1 < argc) {
156 int val = std::stoi(argv[++i]);
158 std::cerr <<
"Error: max-assoc must be positive\n";
161 max_associations =
static_cast<size_t>(val);
162 }
catch (
const std::exception&) {
163 std::cerr <<
"Error: Invalid max-assoc value\n";
166 }
else if (arg ==
"--timeout" && i + 1 < argc) {
168 int val = std::stoi(argv[++i]);
170 std::cerr <<
"Error: timeout cannot be negative\n";
173 idle_timeout =
static_cast<uint32_t
>(val);
174 }
catch (
const std::exception&) {
175 std::cerr <<
"Error: Invalid timeout value\n";
179 std::cerr <<
"Error: Unknown option '" << arg <<
"'\n";
191std::string current_timestamp() {
192 auto now = std::chrono::system_clock::now();
193 auto time_t_now = std::chrono::system_clock::to_time_t(now);
194 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
195 now.time_since_epoch()) % 1000;
197 std::ostringstream oss;
198 oss << std::put_time(std::localtime(&time_t_now),
"%Y-%m-%d %H:%M:%S");
199 oss <<
'.' << std::setfill(
'0') << std::setw(3) << ms.count();
213 const std::string& ae_title,
214 size_t max_associations,
215 uint32_t idle_timeout) {
220 std::cout <<
"\nStarting Echo SCP...\n";
221 std::cout <<
" AE Title: " << ae_title <<
"\n";
222 std::cout <<
" Port: " << port <<
"\n";
223 std::cout <<
" Max Associations: " << max_associations <<
"\n";
224 std::cout <<
" Idle Timeout: " << idle_timeout <<
" seconds\n\n";
227 server_config config;
228 config.ae_title = ae_title;
230 config.max_associations = max_associations;
231 config.idle_timeout = std::chrono::seconds{idle_timeout};
232 config.implementation_class_uid =
"1.2.826.0.1.3680043.2.1545.1";
233 config.implementation_version_name =
"ECHO_SCP_001";
236 dicom_server server{config};
240 server.register_service(std::make_shared<verification_scp>());
243 server.on_association_established([](
const association& assoc) {
244 std::cout <<
"[" << current_timestamp() <<
"] "
245 <<
"Association established from: " << assoc.calling_ae()
246 <<
" -> " << assoc.called_ae() <<
"\n";
249 server.on_association_released([](
const association& assoc) {
250 std::cout <<
"[" << current_timestamp() <<
"] "
251 <<
"Association released: " << assoc.calling_ae() <<
"\n";
254 server.on_error([](
const std::string& error) {
255 std::cerr <<
"[" << current_timestamp() <<
"] "
256 <<
"Error: " <<
error <<
"\n";
260 auto result = server.start();
261 if (result.is_err()) {
262 std::cerr <<
"Failed to start server: " << result.error().message <<
"\n";
267 std::cout <<
"=================================================\n";
268 std::cout <<
" Echo SCP is running on port " << port <<
"\n";
269 std::cout <<
" Press Ctrl+C to stop\n";
270 std::cout <<
"=================================================\n\n";
273 server.wait_for_shutdown();
276 auto stats = server.get_statistics();
278 std::cout <<
"=================================================\n";
279 std::cout <<
" Server Statistics\n";
280 std::cout <<
"=================================================\n";
281 std::cout <<
" Total Associations: " << stats.total_associations <<
"\n";
282 std::cout <<
" Rejected Associations: " << stats.rejected_associations <<
"\n";
283 std::cout <<
" Messages Processed: " << stats.messages_processed <<
"\n";
284 std::cout <<
" Bytes Received: " << stats.bytes_received <<
"\n";
285 std::cout <<
" Bytes Sent: " << stats.bytes_sent <<
"\n";
286 std::cout <<
" Uptime: " << stats.uptime().count() <<
" seconds\n";
287 std::cout <<
"=================================================\n";
295int main(
int argc,
char* argv[]) {
297 _____ ____ _ _ ___ ____ ____ ____
298 | ____/ ___| | | |/ _ \ / ___| / ___| _ \
299 | _|| | | |_| | | | | \___ \| | | |_) |
300 | |__| |___| _ | |_| | ___) | |___| __/
301 |_____\____|_| |_|\___/ |____/ \____|_|
303 DICOM Connectivity Test Server
307 std::string ae_title;
308 size_t max_associations = 0;
309 uint32_t idle_timeout = 0;
311 if (!parse_arguments(argc, argv, port, ae_title, max_associations, idle_timeout)) {
312 print_usage(argv[0]);
317 install_signal_handlers();
319 bool success = run_server(port, ae_title, max_associations, idle_timeout);
321 std::cout <<
"\nEcho SCP terminated\n";
322 return success ? 0 : 1;
Multi-threaded DICOM server for handling multiple associations.
@ error
Node returned an error.
DICOM Server configuration structures.
DICOM Verification SCP service (C-ECHO handler)