Mercurial > hg > orthanc-stone
comparison OrthancStone/Sources/Deprecated/Toolbox/BaseWebService.cpp @ 1512:244ad1e4e76a
reorganization of folders
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 16:21:02 +0200 |
parents | Framework/Deprecated/Toolbox/BaseWebService.cpp@30deba7bc8e2 |
children |
comparison
equal
deleted
inserted
replaced
1511:9dfeee74c1e6 | 1512:244ad1e4e76a |
---|---|
1 /** | |
2 * Stone of Orthanc | |
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 Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "BaseWebService.h" | |
23 | |
24 #include "../../Messages/IObservable.h" | |
25 #include "../../../Platforms/Generic/IOracleCommand.h" | |
26 | |
27 #include <OrthancException.h> | |
28 | |
29 #include <boost/shared_ptr.hpp> | |
30 #include <algorithm> | |
31 #include <Logging.h> | |
32 | |
33 namespace Deprecated | |
34 { | |
35 | |
36 | |
37 class BaseWebService::BaseWebServicePayload : public Orthanc::IDynamicObject | |
38 { | |
39 private: | |
40 std::unique_ptr< MessageHandler<IWebService::HttpRequestSuccessMessage> > userSuccessHandler_; | |
41 std::unique_ptr< MessageHandler<IWebService::HttpRequestErrorMessage> > userFailureHandler_; | |
42 std::unique_ptr< Orthanc::IDynamicObject> userPayload_; | |
43 | |
44 public: | |
45 BaseWebServicePayload(MessageHandler<IWebService::HttpRequestSuccessMessage>* userSuccessHandler, | |
46 MessageHandler<IWebService::HttpRequestErrorMessage>* userFailureHandler, | |
47 Orthanc::IDynamicObject* userPayload) : | |
48 userSuccessHandler_(userSuccessHandler), | |
49 userFailureHandler_(userFailureHandler), | |
50 userPayload_(userPayload) | |
51 { | |
52 } | |
53 | |
54 void HandleSuccess(const IWebService::HttpRequestSuccessMessage& message) const | |
55 { | |
56 if (userSuccessHandler_.get() != NULL) | |
57 { | |
58 // recreate a success message with the user payload | |
59 IWebService::HttpRequestSuccessMessage successMessage(message.GetUri(), | |
60 message.GetAnswer(), | |
61 message.GetAnswerSize(), | |
62 message.GetAnswerHttpHeaders(), | |
63 userPayload_.get()); | |
64 userSuccessHandler_->Apply(successMessage); | |
65 } | |
66 else | |
67 { | |
68 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
69 } | |
70 } | |
71 | |
72 void HandleFailure(const IWebService::HttpRequestErrorMessage& message) const | |
73 { | |
74 if (userFailureHandler_.get() != NULL) | |
75 { | |
76 // recreate a failure message with the user payload | |
77 IWebService::HttpRequestErrorMessage failureMessage(message.GetUri(), | |
78 message.GetHttpStatus(), | |
79 userPayload_.get()); | |
80 | |
81 userFailureHandler_->Apply(failureMessage); | |
82 } | |
83 } | |
84 | |
85 }; | |
86 | |
87 | |
88 void BaseWebService::GetAsync(const std::string& uri, | |
89 const HttpHeaders& headers, | |
90 Orthanc::IDynamicObject* payload /* takes ownership */, | |
91 MessageHandler<IWebService::HttpRequestSuccessMessage>* successCallback, | |
92 MessageHandler<IWebService::HttpRequestErrorMessage>* failureCallback, | |
93 unsigned int timeoutInSeconds) | |
94 { | |
95 if (!cacheEnabled_ || cache_.find(uri) == cache_.end()) | |
96 { | |
97 GetAsyncInternal(uri, headers, | |
98 new BaseWebService::BaseWebServicePayload(successCallback, failureCallback, payload), // ownership is transfered | |
99 new DeprecatedCallable<BaseWebService, IWebService::HttpRequestSuccessMessage> | |
100 (GetSharedObserver(), &BaseWebService::CacheAndNotifyHttpSuccess), | |
101 new DeprecatedCallable<BaseWebService, IWebService::HttpRequestErrorMessage> | |
102 (GetSharedObserver(), &BaseWebService::NotifyHttpError), | |
103 timeoutInSeconds); | |
104 } | |
105 else | |
106 { | |
107 // put the uri on top of the most recently accessed list | |
108 std::deque<std::string>::iterator it = std::find(orderedCacheKeys_.begin(), orderedCacheKeys_.end(), uri); | |
109 if (it != orderedCacheKeys_.end()) | |
110 { | |
111 std::string uri = *it; | |
112 orderedCacheKeys_.erase(it); | |
113 orderedCacheKeys_.push_front(uri); | |
114 } | |
115 | |
116 // create a command and "post" it to the Oracle so it is executed and commited "later" | |
117 NotifyHttpSuccessLater(cache_[uri], payload, successCallback); | |
118 } | |
119 | |
120 } | |
121 | |
122 | |
123 | |
124 void BaseWebService::NotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message) | |
125 { | |
126 if (message.HasPayload()) | |
127 { | |
128 dynamic_cast<const BaseWebServicePayload&>(message.GetPayload()).HandleSuccess(message); | |
129 } | |
130 else | |
131 { | |
132 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
133 } | |
134 } | |
135 | |
136 void BaseWebService::CacheAndNotifyHttpSuccess(const IWebService::HttpRequestSuccessMessage& message) | |
137 { | |
138 if (cacheEnabled_) | |
139 { | |
140 while (cacheCurrentSize_ + message.GetAnswerSize() > cacheMaxSize_ && orderedCacheKeys_.size() > 0) | |
141 { | |
142 VLOG(1) << "BaseWebService: clearing cache: " << cacheCurrentSize_ << "/" << cacheMaxSize_ << "(" << message.GetAnswerSize() << ")"; | |
143 const std::string& oldestUri = orderedCacheKeys_.back(); | |
144 HttpCache::iterator it = cache_.find(oldestUri); | |
145 if (it != cache_.end()) | |
146 { | |
147 cacheCurrentSize_ -= it->second->GetAnswerSize(); | |
148 cache_.erase(it); | |
149 } | |
150 orderedCacheKeys_.pop_back(); | |
151 | |
152 } | |
153 | |
154 boost::shared_ptr<CachedHttpRequestSuccessMessage> cachedMessage(new CachedHttpRequestSuccessMessage(message)); | |
155 cache_[message.GetUri()] = cachedMessage; | |
156 orderedCacheKeys_.push_front(message.GetUri()); | |
157 cacheCurrentSize_ += message.GetAnswerSize(); | |
158 } | |
159 | |
160 NotifyHttpSuccess(message); | |
161 } | |
162 | |
163 void BaseWebService::NotifyHttpError(const IWebService::HttpRequestErrorMessage& message) | |
164 { | |
165 if (message.HasPayload()) | |
166 { | |
167 dynamic_cast<const BaseWebServicePayload&>(message.GetPayload()).HandleFailure(message); | |
168 } | |
169 else | |
170 { | |
171 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
172 } | |
173 } | |
174 | |
175 | |
176 | |
177 | |
178 } |