comparison OrthancFramework/Sources/DicomNetworking/DicomAssociationParameters.cpp @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Core/DicomNetworking/DicomAssociationParameters.cpp@661c931f22ad
children bf7b9edf6b81
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 /**
2 * Orthanc - A Lightweight, RESTful DICOM Store
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * In addition, as a special exception, the copyright holders of this
13 * program give permission to link the code of its release with the
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it
15 * that use the same license as the "OpenSSL" library), and distribute
16 * the linked executables. You must obey the GNU General Public License
17 * in all respects for all of the code used other than "OpenSSL". If you
18 * modify file(s) with this exception, you may extend this exception to
19 * your version of the file(s), but you are not obligated to do so. If
20 * you do not wish to do so, delete this exception statement from your
21 * version. If you delete this exception statement from all source files
22 * in the program, then also delete it here.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 **/
32
33
34 #include "../PrecompiledHeaders.h"
35 #include "DicomAssociationParameters.h"
36
37 #include "../Compatibility.h"
38 #include "../Logging.h"
39 #include "../OrthancException.h"
40 #include "../SerializationToolbox.h"
41 #include "NetworkingCompatibility.h"
42
43 #include <boost/thread/mutex.hpp>
44
45 // By default, the timeout for client DICOM connections is set to 10 seconds
46 static boost::mutex defaultTimeoutMutex_;
47 static uint32_t defaultTimeout_ = 10;
48
49
50 namespace Orthanc
51 {
52 void DicomAssociationParameters::CheckHost(const std::string& host)
53 {
54 if (host.size() > HOST_NAME_MAX - 10)
55 {
56 throw OrthancException(ErrorCode_ParameterOutOfRange,
57 "Invalid host name (too long): " + host);
58 }
59 }
60
61
62 uint32_t DicomAssociationParameters::GetDefaultTimeout()
63 {
64 boost::mutex::scoped_lock lock(defaultTimeoutMutex_);
65 return defaultTimeout_;
66 }
67
68
69 DicomAssociationParameters::DicomAssociationParameters() :
70 localAet_("ORTHANC"),
71 timeout_(GetDefaultTimeout())
72 {
73 remote_.SetApplicationEntityTitle("ANY-SCP");
74 }
75
76
77 DicomAssociationParameters::DicomAssociationParameters(const std::string& localAet,
78 const RemoteModalityParameters& remote) :
79 localAet_(localAet),
80 timeout_(GetDefaultTimeout())
81 {
82 SetRemoteModality(remote);
83 }
84
85
86 void DicomAssociationParameters::SetRemoteModality(const RemoteModalityParameters& remote)
87 {
88 CheckHost(remote.GetHost());
89 remote_ = remote;
90 }
91
92
93 void DicomAssociationParameters::SetRemoteHost(const std::string& host)
94 {
95 CheckHost(host);
96 remote_.SetHost(host);
97 }
98
99
100 bool DicomAssociationParameters::IsEqual(const DicomAssociationParameters& other) const
101 {
102 return (localAet_ == other.localAet_ &&
103 remote_.GetApplicationEntityTitle() == other.remote_.GetApplicationEntityTitle() &&
104 remote_.GetHost() == other.remote_.GetHost() &&
105 remote_.GetPortNumber() == other.remote_.GetPortNumber() &&
106 remote_.GetManufacturer() == other.remote_.GetManufacturer() &&
107 timeout_ == other.timeout_);
108 }
109
110
111 static const char* const LOCAL_AET = "LocalAet";
112 static const char* const REMOTE = "Remote";
113 static const char* const TIMEOUT = "Timeout"; // New in Orthanc in 1.7.0
114
115
116 void DicomAssociationParameters::SerializeJob(Json::Value& target) const
117 {
118 if (target.type() != Json::objectValue)
119 {
120 throw OrthancException(ErrorCode_InternalError);
121 }
122 else
123 {
124 target[LOCAL_AET] = localAet_;
125 remote_.Serialize(target[REMOTE], true /* force advanced format */);
126 target[TIMEOUT] = timeout_;
127 }
128 }
129
130
131 DicomAssociationParameters DicomAssociationParameters::UnserializeJob(const Json::Value& serialized)
132 {
133 if (serialized.type() == Json::objectValue)
134 {
135 DicomAssociationParameters result;
136
137 result.remote_ = RemoteModalityParameters(serialized[REMOTE]);
138 result.localAet_ = SerializationToolbox::ReadString(serialized, LOCAL_AET);
139 result.timeout_ = SerializationToolbox::ReadInteger(serialized, TIMEOUT, GetDefaultTimeout());
140
141 return result;
142 }
143 else
144 {
145 throw OrthancException(ErrorCode_BadFileFormat);
146 }
147 }
148
149
150 void DicomAssociationParameters::SetDefaultTimeout(uint32_t seconds)
151 {
152 LOG(INFO) << "Default timeout for DICOM connections if Orthanc acts as SCU (client): "
153 << seconds << " seconds (0 = no timeout)";
154
155 {
156 boost::mutex::scoped_lock lock(defaultTimeoutMutex_);
157 defaultTimeout_ = seconds;
158 }
159 }
160 }