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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #pragma once
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
40b21c1f8b8d more usage of IObservable/IObserver
am@osimis.io
parents: 251
diff changeset
29 #include <Core/Logging.h>
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 namespace OrthancStone
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 {
288
8c8da145fefa renamings and doc
am@osimis.io
parents: 269
diff changeset
33 // The IWebService performs HTTP requests.
8c8da145fefa renamings and doc
am@osimis.io
parents: 269
diff changeset
34 // Since applications can run in native or WASM environment and, since
8c8da145fefa renamings and doc
am@osimis.io
parents: 269
diff changeset
35 // in a WASM environment, the WebService is asynchronous, the IWebservice
8c8da145fefa renamings and doc
am@osimis.io
parents: 269
diff changeset
36 // also implements an asynchronous interface: you must schedule a request
8c8da145fefa renamings and doc
am@osimis.io
parents: 269
diff changeset
37 // and you'll be notified when the response/error is ready.
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
38 class IWebService
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
39 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
40 protected:
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
41 MessageBroker& broker_;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
42 public:
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
43 typedef std::map<std::string, std::string> Headers;
257
9afafb192180 using PAM
am@osimis.io
parents: 252
diff changeset
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
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
76 class ICallback : public IObserver
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
77 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
78 public:
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
79 struct HttpRequestSuccessMessage: public IMessage
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
80 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
81 const std::string& Uri;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
82 const void* Answer;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
83 size_t AnswerSize;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
84 Orthanc::IDynamicObject* Payload;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
85 HttpRequestSuccessMessage(const std::string& uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
86 const void* answer,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
87 size_t answerSize,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
88 Orthanc::IDynamicObject* payload)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
89 : IMessage(MessageType_HttpRequestSuccess),
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
90 Uri(uri),
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
91 Answer(answer),
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
92 AnswerSize(answerSize),
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
93 Payload(payload)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
94 {}
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
95 };
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
97 struct HttpRequestErrorMessage: public IMessage
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
98 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
99 const std::string& Uri;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
100 Orthanc::IDynamicObject* Payload;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
101 HttpRequestErrorMessage(const std::string& uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
102 Orthanc::IDynamicObject* payload)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
103 : IMessage(MessageType_HttpRequestError),
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
104 Uri(uri),
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
105 Payload(payload)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
106 {}
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
107 };
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents: 212
diff changeset
108
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
109 ICallback(MessageBroker& broker)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
110 : IObserver(broker)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
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
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
114 }
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
115 virtual ~ICallback()
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
116 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
117 }
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
118
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
119 virtual void HandleMessage(IObservable& from, const IMessage& message)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
120 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
121 switch(message.GetType())
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
122 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
123 case MessageType_HttpRequestError:
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
124 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
125 const HttpRequestErrorMessage& msg = dynamic_cast<const HttpRequestErrorMessage&>(message);
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
126 OnHttpRequestError(msg.Uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
127 msg.Payload);
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
128 }; break;
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents: 212
diff changeset
129
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
130 case MessageType_HttpRequestSuccess:
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
131 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
132 const HttpRequestSuccessMessage& msg = dynamic_cast<const HttpRequestSuccessMessage&>(message);
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
133 OnHttpRequestSuccess(msg.Uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
134 msg.Answer,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
135 msg.AnswerSize,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
136 msg.Payload);
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
137 }; break;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
138 default:
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
139 VLOG("unhandled message type" << message.GetType());
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
140 }
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
141 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents: 212
diff changeset
142
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
143 virtual void OnHttpRequestError(const std::string& uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
144 Orthanc::IDynamicObject* payload) = 0;
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
146 virtual void OnHttpRequestSuccess(const std::string& uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
147 const void* answer,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
148 size_t answerSize,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
149 Orthanc::IDynamicObject* payload) = 0;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
150 };
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents: 212
diff changeset
151
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
152 IWebService(MessageBroker& broker)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
153 : broker_(broker)
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
154 {}
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
156 virtual ~IWebService()
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
157 {
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
158 }
251
192e6e349e69 first usage of new message system (in SDL only)
am@osimis.io
parents: 212
diff changeset
159
269
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
160 virtual void ScheduleGetRequest(ICallback& callback,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
161 const std::string& uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
162 const Headers& headers,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
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
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
172 virtual void SchedulePostRequest(ICallback& callback,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
173 const std::string& uri,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
174 const Headers& headers,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
175 const std::string& body,
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
176 Orthanc::IDynamicObject* payload) = 0;
0dfa83535cd7 indentation
am@osimis.io
parents: 268
diff changeset
177 };
57
d20e25cfcf3a IWebService
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
178 }