Mercurial > hg > orthanc-stone
comparison Framework/Toolbox/OrthancApiClient.cpp @ 270:2d64f4d39610 am-2
backup (work in progress)
author | am@osimis.io |
---|---|
date | Thu, 23 Aug 2018 14:45:04 +0200 |
parents | |
children | 46c5296d867e |
comparison
equal
deleted
inserted
replaced
269:0dfa83535cd7 | 270:2d64f4d39610 |
---|---|
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-2018 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 #include "OrthancApiClient.h" | |
22 | |
23 #include "MessagingToolbox.h" | |
24 #include <Core/OrthancException.h> | |
25 | |
26 namespace OrthancStone { | |
27 | |
28 struct OrthancApiClient::InternalGetJsonResponseReadyMessage : | |
29 public IMessage | |
30 { | |
31 OrthancApiClient::BaseRequest* request_; | |
32 Json::Value response_; | |
33 | |
34 InternalGetJsonResponseReadyMessage(OrthancApiClient::BaseRequest* request, | |
35 const Json::Value& response) | |
36 : IMessage(MessageType_OrthancApi_InternalGetJsonResponseReady), | |
37 request_(request), | |
38 response_(response) | |
39 { | |
40 } | |
41 | |
42 }; | |
43 | |
44 struct OrthancApiClient::InternalGetJsonResponseErrorMessage : | |
45 public IMessage | |
46 { | |
47 OrthancApiClient::BaseRequest* request_; | |
48 | |
49 InternalGetJsonResponseErrorMessage(OrthancApiClient::BaseRequest* request) | |
50 : IMessage(MessageType_OrthancApi_InternalGetJsonResponseError), | |
51 request_(request) | |
52 { | |
53 } | |
54 }; | |
55 | |
56 | |
57 // this class handles a single request to the OrthancApiClient. | |
58 // Once the response is ready, it will emit a message to the responseObserver | |
59 // the responseObserver must handle only that message (and not all messages from the OrthancApiClient) | |
60 class OrthancApiClient::BaseRequest: | |
61 public IObserver, | |
62 public IObservable, | |
63 public Orthanc::IDynamicObject | |
64 { | |
65 public: | |
66 std::string uri_; | |
67 OrthancApiClient& orthanc_; | |
68 MessageType messageToEmitWhenResponseReady_; | |
69 OrthancApiClient::Mode mode_; | |
70 | |
71 public: | |
72 BaseRequest( | |
73 OrthancApiClient& orthanc, | |
74 IObserver& responseObserver, | |
75 const std::string& uri, | |
76 MessageType messageToEmitWhenResponseReady, | |
77 OrthancApiClient::Mode mode) | |
78 : IObserver(orthanc.broker_), | |
79 IObservable(orthanc.broker_), | |
80 uri_(uri), | |
81 orthanc_(orthanc), | |
82 messageToEmitWhenResponseReady_(messageToEmitWhenResponseReady), | |
83 mode_(mode) | |
84 { | |
85 // this object will emit only a single message, the one the final responseObserver is expecting | |
86 DeclareEmittableMessage(messageToEmitWhenResponseReady); | |
87 | |
88 // this object is observing the OrthancApi so it must handle all messages | |
89 DeclareHandledMessage(MessageType_OrthancApi_InternalGetJsonResponseReady); | |
90 DeclareIgnoredMessage(MessageType_OrthancApi_InternalGetJsonResponseError); | |
91 | |
92 orthanc_.RegisterObserver(*this); | |
93 this->RegisterObserver(responseObserver); | |
94 } | |
95 virtual ~BaseRequest() {} | |
96 | |
97 // mainly maps OrthancApi internal messages to a message that is expected by the responseObserver | |
98 virtual void HandleMessage(IObservable& from, const IMessage& message) | |
99 { | |
100 switch (message.GetType()) | |
101 { | |
102 case MessageType_OrthancApi_InternalGetJsonResponseReady: | |
103 { | |
104 const OrthancApiClient::InternalGetJsonResponseReadyMessage& messageReceived = dynamic_cast<const OrthancApiClient::InternalGetJsonResponseReadyMessage&>(message); | |
105 EmitMessage(OrthancApiClient::GetJsonResponseReadyMessage(messageToEmitWhenResponseReady_, messageReceived.request_->uri_, messageReceived.response_)); | |
106 orthanc_.ReleaseRequest(messageReceived.request_); | |
107 }; break; | |
108 default: | |
109 throw MessageNotDeclaredException(message.GetType()); | |
110 } | |
111 } | |
112 | |
113 }; | |
114 | |
115 | |
116 class OrthancApiClient::WebCallback : public IWebService::ICallback | |
117 { | |
118 private: | |
119 OrthancApiClient& that_; | |
120 | |
121 public: | |
122 WebCallback(MessageBroker& broker, OrthancApiClient& that) : | |
123 IWebService::ICallback(broker), | |
124 that_(that) | |
125 { | |
126 } | |
127 | |
128 virtual void OnHttpRequestSuccess(const std::string& uri, | |
129 const void* answer, | |
130 size_t answerSize, | |
131 Orthanc::IDynamicObject* payload) | |
132 { | |
133 OrthancApiClient::BaseRequest* request = dynamic_cast<OrthancApiClient::BaseRequest*>(payload); // the BaseRequests objects belongs to the OrthancApiClient and is deleted in ReleaseRequest when it has been "consumed" | |
134 | |
135 switch (request->mode_) | |
136 { | |
137 case OrthancApiClient::Mode_GetJson: | |
138 { | |
139 Json::Value response; | |
140 if (MessagingToolbox::ParseJson(response, answer, answerSize)) | |
141 { | |
142 OrthancApiClient::InternalGetJsonResponseReadyMessage msg(request, response); | |
143 that_.EmitMessage(msg); | |
144 } | |
145 else | |
146 { | |
147 OrthancApiClient::InternalGetJsonResponseErrorMessage msg(request); | |
148 that_.EmitMessage(msg); | |
149 } | |
150 }; break; | |
151 | |
152 default: | |
153 that_.ReleaseRequest(request); | |
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
155 } | |
156 } | |
157 | |
158 virtual void OnHttpRequestError(const std::string& uri, | |
159 Orthanc::IDynamicObject* payload) | |
160 { | |
161 OrthancApiClient::BaseRequest* request = dynamic_cast<OrthancApiClient::BaseRequest*>(payload); // the BaseRequests objects belongs to the OrthancApiClient and is deleted in ReleaseRequest when it has been "consumed" | |
162 | |
163 switch (request->mode_) | |
164 { | |
165 case OrthancApiClient::Mode_GetJson: | |
166 { | |
167 OrthancApiClient::InternalGetJsonResponseErrorMessage msg(request); | |
168 that_.EmitMessage(msg); | |
169 }; break; | |
170 | |
171 default: | |
172 that_.ReleaseRequest(request); | |
173 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
174 } | |
175 } | |
176 }; | |
177 | |
178 OrthancApiClient::OrthancApiClient(MessageBroker &broker, IWebService &orthanc) | |
179 : IObservable(broker), | |
180 orthanc_(orthanc), | |
181 webCallback_(new OrthancApiClient::WebCallback(broker, *this)) | |
182 { | |
183 DeclareEmittableMessage(MessageType_OrthancApi_InternalGetJsonResponseReady); | |
184 DeclareEmittableMessage(MessageType_OrthancApi_InternalGetJsonResponseError); | |
185 } | |
186 | |
187 void OrthancApiClient::ScheduleGetJsonRequest(IObserver &responseObserver, const std::string &uri, MessageType messageToEmitWhenResponseReady) | |
188 { | |
189 OrthancApiClient::BaseRequest* request = new OrthancApiClient::BaseRequest(*this, | |
190 responseObserver, | |
191 uri, | |
192 messageToEmitWhenResponseReady, | |
193 OrthancApiClient::Mode_GetJson); | |
194 orthanc_.ScheduleGetRequest(*webCallback_, uri, IWebService::Headers(), request); | |
195 requestsInProgress_.insert(request); | |
196 } | |
197 | |
198 void OrthancApiClient::ReleaseRequest(BaseRequest* request) | |
199 { | |
200 requestsInProgress_.erase(request); | |
201 delete request; | |
202 } | |
203 | |
204 } |