Mercurial > hg > orthanc-stone
annotate Framework/Toolbox/IWebService.h @ 299:3897f9f28cfa am-callable-and-promise
backup work in progress: updated messaging framework with ICallable
author | am@osimis.io |
---|---|
date | Fri, 14 Sep 2018 16:44:01 +0200 |
parents | 8c8da145fefa |
children | b4abaeb783b1 |
rev | line source |
---|---|
57 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
135
e2fe9352f240
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
113
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
57 | 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. | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
16 * |
57 | 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 #pragma once | |
23 | |
212
5412adf19980
resort to OrthancFramework
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
201
diff
changeset
|
24 #include <Core/IDynamicObject.h> |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
25 #include "../../Framework/Messages/IObserver.h" |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
26 #include "../../Framework/Messages/ICallable.h" |
57 | 27 #include <string> |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
28 #include <map> |
252 | 29 #include <Core/Logging.h> |
57 | 30 |
31 namespace OrthancStone | |
32 { | |
288 | 33 // The IWebService performs HTTP requests. |
34 // Since applications can run in native or WASM environment and, since | |
35 // in a WASM environment, the WebService is asynchronous, the IWebservice | |
36 // also implements an asynchronous interface: you must schedule a request | |
37 // and you'll be notified when the response/error is ready. | |
269 | 38 class IWebService |
39 { | |
40 protected: | |
41 MessageBroker& broker_; | |
42 public: | |
43 typedef std::map<std::string, std::string> Headers; | |
257 | 44 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
45 struct NewHttpRequestSuccessMessage: public BaseMessage<MessageType_HttpRequestSuccess> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
46 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
47 const std::string& Uri; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
48 const void* Answer; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
49 size_t AnswerSize; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
50 Orthanc::IDynamicObject* Payload; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
51 NewHttpRequestSuccessMessage(const std::string& uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
52 const void* answer, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
53 size_t answerSize, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
54 Orthanc::IDynamicObject* payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
55 : BaseMessage(), |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
56 Uri(uri), |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
57 Answer(answer), |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
58 AnswerSize(answerSize), |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
59 Payload(payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
60 {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
61 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
62 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
63 struct NewHttpRequestErrorMessage: public BaseMessage<MessageType_HttpRequestError> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
64 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
65 const std::string& Uri; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
66 Orthanc::IDynamicObject* Payload; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
67 NewHttpRequestErrorMessage(const std::string& uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
68 Orthanc::IDynamicObject* payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
69 : BaseMessage(), |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
70 Uri(uri), |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
71 Payload(payload) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
72 {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
73 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
74 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
75 |
269 | 76 class ICallback : public IObserver |
77 { | |
78 public: | |
79 struct HttpRequestSuccessMessage: public IMessage | |
80 { | |
81 const std::string& Uri; | |
82 const void* Answer; | |
83 size_t AnswerSize; | |
84 Orthanc::IDynamicObject* Payload; | |
85 HttpRequestSuccessMessage(const std::string& uri, | |
86 const void* answer, | |
87 size_t answerSize, | |
88 Orthanc::IDynamicObject* payload) | |
89 : IMessage(MessageType_HttpRequestSuccess), | |
90 Uri(uri), | |
91 Answer(answer), | |
92 AnswerSize(answerSize), | |
93 Payload(payload) | |
94 {} | |
95 }; | |
57 | 96 |
269 | 97 struct HttpRequestErrorMessage: public IMessage |
98 { | |
99 const std::string& Uri; | |
100 Orthanc::IDynamicObject* Payload; | |
101 HttpRequestErrorMessage(const std::string& uri, | |
102 Orthanc::IDynamicObject* payload) | |
103 : IMessage(MessageType_HttpRequestError), | |
104 Uri(uri), | |
105 Payload(payload) | |
106 {} | |
107 }; | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
108 |
269 | 109 ICallback(MessageBroker& broker) |
110 : IObserver(broker) | |
111 { | |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
112 // DeclareHandledMessage(MessageType_HttpRequestError); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
113 // DeclareHandledMessage(MessageType_HttpRequestSuccess); |
269 | 114 } |
115 virtual ~ICallback() | |
116 { | |
117 } | |
57 | 118 |
269 | 119 virtual void HandleMessage(IObservable& from, const IMessage& message) |
120 { | |
121 switch(message.GetType()) | |
122 { | |
123 case MessageType_HttpRequestError: | |
124 { | |
125 const HttpRequestErrorMessage& msg = dynamic_cast<const HttpRequestErrorMessage&>(message); | |
126 OnHttpRequestError(msg.Uri, | |
127 msg.Payload); | |
128 }; break; | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
129 |
269 | 130 case MessageType_HttpRequestSuccess: |
131 { | |
132 const HttpRequestSuccessMessage& msg = dynamic_cast<const HttpRequestSuccessMessage&>(message); | |
133 OnHttpRequestSuccess(msg.Uri, | |
134 msg.Answer, | |
135 msg.AnswerSize, | |
136 msg.Payload); | |
137 }; break; | |
138 default: | |
139 VLOG("unhandled message type" << message.GetType()); | |
140 } | |
141 } | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
142 |
269 | 143 virtual void OnHttpRequestError(const std::string& uri, |
144 Orthanc::IDynamicObject* payload) = 0; | |
57 | 145 |
269 | 146 virtual void OnHttpRequestSuccess(const std::string& uri, |
147 const void* answer, | |
148 size_t answerSize, | |
149 Orthanc::IDynamicObject* payload) = 0; | |
150 }; | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
151 |
269 | 152 IWebService(MessageBroker& broker) |
153 : broker_(broker) | |
154 {} | |
57 | 155 |
269 | 156 virtual ~IWebService() |
157 { | |
158 } | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
159 |
269 | 160 virtual void ScheduleGetRequest(ICallback& callback, |
161 const std::string& uri, | |
162 const Headers& headers, | |
163 Orthanc::IDynamicObject* payload) = 0; | |
251
192e6e349e69
first usage of new message system (in SDL only)
am@osimis.io
parents:
212
diff
changeset
|
164 |
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
165 virtual void GetAsync(const std::string& uri, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
166 const Headers& headers, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
167 Orthanc::IDynamicObject* payload, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
168 MessageHandler<IWebService::NewHttpRequestSuccessMessage>* successCallback, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
169 MessageHandler<IWebService::NewHttpRequestErrorMessage>* failureCallback) = 0; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
170 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
288
diff
changeset
|
171 |
269 | 172 virtual void SchedulePostRequest(ICallback& callback, |
173 const std::string& uri, | |
174 const Headers& headers, | |
175 const std::string& body, | |
176 Orthanc::IDynamicObject* payload) = 0; | |
177 }; | |
57 | 178 } |