comparison Core/DicomNetworking/TimeoutDicomConnectionManager.cpp @ 3851:6498739a3c3c

refactoring: TimeoutDicomConnectionManager is now only used by Lua
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 20 Apr 2020 16:46:44 +0200
parents d729d6e8b484
children 9973d10bc5c4
comparison
equal deleted inserted replaced
3850:d729d6e8b484 3851:6498739a3c3c
43 { 43 {
44 return boost::posix_time::microsec_clock::universal_time(); 44 return boost::posix_time::microsec_clock::universal_time();
45 } 45 }
46 46
47 47
48 TimeoutDicomConnectionManager::Resource::Resource(TimeoutDicomConnectionManager& that) : 48 TimeoutDicomConnectionManager::Lock::Lock(TimeoutDicomConnectionManager& that,
49 that_(that) 49 const std::string& localAet,
50 const RemoteModalityParameters& remote) :
51 that_(that),
52 lock_(that_.mutex_)
53 {
54 // Calling "Touch()" will be done by the "~Lock()" destructor
55 that_.OpenInternal(localAet, remote);
56 }
57
58
59 TimeoutDicomConnectionManager::Lock::~Lock()
60 {
61 that_.TouchInternal();
62 }
63
64
65 DicomUserConnection& TimeoutDicomConnectionManager::Lock::GetConnection()
50 { 66 {
51 if (that_.connection_.get() == NULL) 67 if (that_.connection_.get() == NULL)
52 { 68 {
69 // The allocation should have been done by "that_.Open()" in the constructor
53 throw OrthancException(ErrorCode_InternalError); 70 throw OrthancException(ErrorCode_InternalError);
71 }
72 else
73 {
74 return *that_.connection_;
54 } 75 }
55 } 76 }
56 77
57
58 TimeoutDicomConnectionManager::Resource::~Resource()
59 {
60 that_.Touch();
61 }
62 78
63 79 // Mutex must be locked
64 DicomUserConnection& TimeoutDicomConnectionManager::Resource::GetConnection() 80 void TimeoutDicomConnectionManager::TouchInternal()
65 {
66 assert(that_.connection_.get() != NULL);
67 return *that_.connection_;
68 }
69
70
71 void TimeoutDicomConnectionManager::Touch()
72 { 81 {
73 lastUse_ = GetNow(); 82 lastUse_ = GetNow();
74 } 83 }
75 84
76 85
77 void TimeoutDicomConnectionManager::CheckTimeoutInternal() 86 // Mutex must be locked
87 void TimeoutDicomConnectionManager::OpenInternal(const std::string& localAet,
88 const RemoteModalityParameters& remote)
78 { 89 {
79 if (connection_.get() != NULL && 90 if (connection_.get() == NULL ||
80 (GetNow() - lastUse_) >= timeout_) 91 !connection_->IsSameAssociation(localAet, remote))
81 { 92 {
82 Close(); 93 connection_.reset(new DicomUserConnection(localAet, remote));
83 } 94 }
84 } 95 }
85 96
86 97
87 void TimeoutDicomConnectionManager::SetTimeout(unsigned int timeout) 98 // Mutex must be locked
88 { 99 void TimeoutDicomConnectionManager::CloseInternal()
89 timeout_ = boost::posix_time::milliseconds(timeout);
90 CheckTimeoutInternal();
91 }
92
93
94 unsigned int TimeoutDicomConnectionManager::GetTimeout()
95 {
96 return static_cast<unsigned int>(timeout_.total_milliseconds());
97 }
98
99
100 void TimeoutDicomConnectionManager::Close()
101 { 100 {
102 if (connection_.get() != NULL) 101 if (connection_.get() != NULL)
103 { 102 {
104 LOG(INFO) << "Closing inactive DICOM association with modality: " 103 LOG(INFO) << "Closing inactive DICOM association with modality: "
105 << connection_->GetRemoteApplicationEntityTitle(); 104 << connection_->GetRemoteApplicationEntityTitle();
107 connection_.reset(NULL); 106 connection_.reset(NULL);
108 } 107 }
109 } 108 }
110 109
111 110
112 void TimeoutDicomConnectionManager::CheckTimeout() 111 void TimeoutDicomConnectionManager::SetInactivityTimeout(unsigned int milliseconds)
113 { 112 {
114 CheckTimeoutInternal(); 113 boost::mutex::scoped_lock lock(mutex_);
114 timeout_ = boost::posix_time::milliseconds(milliseconds);
115 CloseInternal();
115 } 116 }
116 117
117 118
118 TimeoutDicomConnectionManager::Resource* 119 unsigned int TimeoutDicomConnectionManager::GetInactivityTimeout()
119 TimeoutDicomConnectionManager::AcquireConnection(const std::string& localAet,
120 const RemoteModalityParameters& remote)
121 { 120 {
122 if (connection_.get() == NULL || 121 boost::mutex::scoped_lock lock(mutex_);
123 !connection_->IsSameAssociation(localAet, remote)) 122 return static_cast<unsigned int>(timeout_.total_milliseconds());
123 }
124
125
126 void TimeoutDicomConnectionManager::CloseIfInactive()
127 {
128 boost::mutex::scoped_lock lock(mutex_);
129
130 if (connection_.get() != NULL &&
131 (GetNow() - lastUse_) >= timeout_)
124 { 132 {
125 connection_.reset(new DicomUserConnection(localAet, remote)); 133 CloseInternal();
126 } 134 }
127
128 return new Resource(*this);
129 } 135 }
130 } 136 }