comparison Framework/Toolbox/IWebService.h @ 251:192e6e349e69 am-2

first usage of new message system (in SDL only)
author am@osimis.io
date Mon, 02 Jul 2018 18:13:46 +0200
parents 5412adf19980
children 40b21c1f8b8d
comparison
equal deleted inserted replaced
250:5e642859267e 251:192e6e349e69
11 * 11 *
12 * This program is distributed in the hope that it will be useful, but 12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details. 15 * Affero General Public License for more details.
16 * 16 *
17 * You should have received a copy of the GNU Affero General Public License 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/>. 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 **/ 19 **/
20 20
21 21
22 #pragma once 22 #pragma once
23 23
24 #include <Core/IDynamicObject.h> 24 #include <Core/IDynamicObject.h>
25 25 #include "../../Framework/Messages/IObserver.h"
26 #include <string> 26 #include <string>
27 27
28 namespace OrthancStone 28 namespace OrthancStone
29 { 29 {
30 class IWebService : public boost::noncopyable 30 class IWebService
31 {
32 public:
33 class ICallback : public boost::noncopyable
34 { 31 {
32 protected:
33 MessageBroker& broker_;
35 public: 34 public:
36 virtual ~ICallback() 35 class ICallback : public IObserver
37 { 36 {
38 } 37 public:
38 struct HttpRequestSuccessMessage: public IMessage
39 {
40 const std::string& Uri;
41 const void* Answer;
42 size_t AnswerSize;
43 Orthanc::IDynamicObject* Payload;
44 HttpRequestSuccessMessage(const std::string& uri,
45 const void* answer,
46 size_t answerSize,
47 Orthanc::IDynamicObject* payload)
48 : IMessage(MessageType_HttpRequestSuccess),
49 Uri(uri),
50 Answer(answer),
51 AnswerSize(answerSize),
52 Payload(payload)
53 {}
54 };
39 55
40 virtual void NotifyError(const std::string& uri, 56 struct HttpRequestErrorMessage: public IMessage
41 Orthanc::IDynamicObject* payload) = 0; 57 {
58 const std::string& Uri;
59 Orthanc::IDynamicObject* Payload;
60 HttpRequestErrorMessage(const std::string& uri,
61 Orthanc::IDynamicObject* payload)
62 : IMessage(MessageType_HttpRequestError),
63 Uri(uri),
64 Payload(payload)
65 {}
66 };
42 67
43 virtual void NotifySuccess(const std::string& uri, 68 ICallback(MessageBroker& broker)
44 const void* answer, 69 : IObserver(broker)
45 size_t answerSize, 70 {}
46 Orthanc::IDynamicObject* payload) = 0; 71 virtual ~ICallback()
72 {
73 }
74
75 virtual void HandleMessage(IObservable& from, const IMessage& message)
76 {
77 switch(message.GetType())
78 {
79 case MessageType_HttpRequestError:
80 { const HttpRequestErrorMessage& msg = dynamic_cast<const HttpRequestErrorMessage&>(message);
81 OnHttpRequestError(msg.Uri,
82 msg.Payload);
83 }; break;
84
85 case MessageType_HttpRequestSuccess:
86 {
87 const HttpRequestSuccessMessage& msg = dynamic_cast<const HttpRequestSuccessMessage&>(message);
88 OnHttpRequestSuccess(msg.Uri,
89 msg.Answer,
90 msg.AnswerSize,
91 msg.Payload);
92 }; break;
93
94 }
95 }
96
97 virtual void OnHttpRequestError(const std::string& uri,
98 Orthanc::IDynamicObject* payload) = 0;
99
100 virtual void OnHttpRequestSuccess(const std::string& uri,
101 const void* answer,
102 size_t answerSize,
103 Orthanc::IDynamicObject* payload) = 0;
104 };
105
106 IWebService(MessageBroker& broker)
107 : broker_(broker)
108 {}
109
110 virtual ~IWebService()
111 {
112 }
113
114 virtual void ScheduleGetRequest(ICallback& callback,
115 const std::string& uri,
116 Orthanc::IDynamicObject* payload) = 0;
117
118 virtual void SchedulePostRequest(ICallback& callback,
119 const std::string& uri,
120 const std::string& body,
121 Orthanc::IDynamicObject* payload) = 0;
47 }; 122 };
48
49 virtual ~IWebService()
50 {
51 }
52
53 virtual void ScheduleGetRequest(ICallback& callback,
54 const std::string& uri,
55 Orthanc::IDynamicObject* payload) = 0;
56
57 virtual void SchedulePostRequest(ICallback& callback,
58 const std::string& uri,
59 const std::string& body,
60 Orthanc::IDynamicObject* payload) = 0;
61 };
62 } 123 }