comparison Core/DicomNetworking/ReusableDicomUserConnection.cpp @ 2382:7284093111b0

big reorganization to cleanly separate framework vs. server
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 29 Aug 2017 21:17:35 +0200
parents OrthancServer/DicomProtocol/ReusableDicomUserConnection.cpp@a3a65de1840f
children 878b59270859
comparison
equal deleted inserted replaced
2381:b8969010b534 2382:7284093111b0
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 Osimis, 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 "ReusableDicomUserConnection.h"
36
37 #include "../Logging.h"
38 #include "../OrthancException.h"
39
40 namespace Orthanc
41 {
42 static boost::posix_time::ptime Now()
43 {
44 return boost::posix_time::microsec_clock::local_time();
45 }
46
47 void ReusableDicomUserConnection::Open(const std::string& localAet,
48 const RemoteModalityParameters& remote)
49 {
50 if (connection_ != NULL &&
51 connection_->GetLocalApplicationEntityTitle() == localAet &&
52 connection_->GetRemoteApplicationEntityTitle() == remote.GetApplicationEntityTitle() &&
53 connection_->GetRemoteHost() == remote.GetHost() &&
54 connection_->GetRemotePort() == remote.GetPort() &&
55 connection_->GetRemoteManufacturer() == remote.GetManufacturer())
56 {
57 // The current connection can be reused
58 LOG(INFO) << "Reusing the previous SCU connection";
59 return;
60 }
61
62 Close();
63
64 connection_ = new DicomUserConnection();
65 connection_->SetLocalApplicationEntityTitle(localAet);
66 connection_->SetRemoteModality(remote);
67 connection_->Open();
68 }
69
70 void ReusableDicomUserConnection::Close()
71 {
72 if (connection_ != NULL)
73 {
74 delete connection_;
75 connection_ = NULL;
76 }
77 }
78
79 void ReusableDicomUserConnection::CloseThread(ReusableDicomUserConnection* that)
80 {
81 for (;;)
82 {
83 boost::this_thread::sleep(boost::posix_time::milliseconds(100));
84 if (!that->continue_)
85 {
86 //LOG(INFO) << "Finishing the thread watching the global SCU connection";
87 return;
88 }
89
90 {
91 boost::mutex::scoped_lock lock(that->mutex_);
92 if (that->connection_ != NULL &&
93 Now() >= that->lastUse_ + that->timeBeforeClose_)
94 {
95 LOG(INFO) << "Closing the global SCU connection after timeout";
96 that->Close();
97 }
98 }
99 }
100 }
101
102
103 ReusableDicomUserConnection::Locker::Locker(ReusableDicomUserConnection& that,
104 const std::string& localAet,
105 const RemoteModalityParameters& remote) :
106 ::Orthanc::Locker(that)
107 {
108 that.Open(localAet, remote);
109 connection_ = that.connection_;
110 }
111
112
113 DicomUserConnection& ReusableDicomUserConnection::Locker::GetConnection()
114 {
115 if (connection_ == NULL)
116 {
117 throw OrthancException(ErrorCode_InternalError);
118 }
119
120 return *connection_;
121 }
122
123 ReusableDicomUserConnection::ReusableDicomUserConnection() :
124 connection_(NULL),
125 timeBeforeClose_(boost::posix_time::seconds(5)) // By default, close connection after 5 seconds
126 {
127 lastUse_ = Now();
128 continue_ = true;
129 closeThread_ = boost::thread(CloseThread, this);
130 }
131
132 ReusableDicomUserConnection::~ReusableDicomUserConnection()
133 {
134 if (continue_)
135 {
136 LOG(ERROR) << "INTERNAL ERROR: ReusableDicomUserConnection::Finalize() should be invoked manually to avoid mess in the destruction order!";
137 Finalize();
138 }
139 }
140
141 void ReusableDicomUserConnection::SetMillisecondsBeforeClose(uint64_t ms)
142 {
143 boost::mutex::scoped_lock lock(mutex_);
144
145 if (ms == 0)
146 {
147 ms = 1;
148 }
149
150 timeBeforeClose_ = boost::posix_time::milliseconds(ms);
151 }
152
153 void ReusableDicomUserConnection::Lock()
154 {
155 mutex_.lock();
156 }
157
158 void ReusableDicomUserConnection::Unlock()
159 {
160 if (connection_ != NULL &&
161 connection_->GetRemoteManufacturer() == ModalityManufacturer_StoreScp)
162 {
163 // "storescp" from DCMTK has problems when reusing a
164 // connection. Always close.
165 Close();
166 }
167
168 lastUse_ = Now();
169 mutex_.unlock();
170 }
171
172
173 void ReusableDicomUserConnection::Finalize()
174 {
175 if (continue_)
176 {
177 continue_ = false;
178
179 if (closeThread_.joinable())
180 {
181 closeThread_.join();
182 }
183
184 Close();
185 }
186 }
187 }
188