comparison Framework/Toolbox/OrthancApiClient.cpp @ 271:46c5296d867e am-2

OracleWebService and BasicSdlApplicationContext using the same global mutex
author am@osimis.io
date Thu, 23 Aug 2018 17:12:54 +0200
parents 2d64f4d39610
children 3897f9f28cfa
comparison
equal deleted inserted replaced
270:2d64f4d39610 271:46c5296d867e
56 56
57 // this class handles a single request to the OrthancApiClient. 57 // this class handles a single request to the OrthancApiClient.
58 // Once the response is ready, it will emit a message to the responseObserver 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) 59 // the responseObserver must handle only that message (and not all messages from the OrthancApiClient)
60 class OrthancApiClient::BaseRequest: 60 class OrthancApiClient::BaseRequest:
61 public IObserver, 61 // public IObserver,
62 public IObservable, 62 public IObservable,
63 public Orthanc::IDynamicObject 63 public Orthanc::IDynamicObject
64 { 64 {
65 public: 65 public:
66 std::string uri_; 66 std::string uri_;
73 OrthancApiClient& orthanc, 73 OrthancApiClient& orthanc,
74 IObserver& responseObserver, 74 IObserver& responseObserver,
75 const std::string& uri, 75 const std::string& uri,
76 MessageType messageToEmitWhenResponseReady, 76 MessageType messageToEmitWhenResponseReady,
77 OrthancApiClient::Mode mode) 77 OrthancApiClient::Mode mode)
78 : IObserver(orthanc.broker_), 78 :
79 //IObserver(orthanc.broker_),
79 IObservable(orthanc.broker_), 80 IObservable(orthanc.broker_),
80 uri_(uri), 81 uri_(uri),
81 orthanc_(orthanc), 82 orthanc_(orthanc),
82 messageToEmitWhenResponseReady_(messageToEmitWhenResponseReady), 83 messageToEmitWhenResponseReady_(messageToEmitWhenResponseReady),
83 mode_(mode) 84 mode_(mode)
84 { 85 {
85 // this object will emit only a single message, the one the final responseObserver is expecting 86 // this object will emit only a single message, the one the final responseObserver is expecting
86 DeclareEmittableMessage(messageToEmitWhenResponseReady); 87 DeclareEmittableMessage(messageToEmitWhenResponseReady);
87 88
88 // this object is observing the OrthancApi so it must handle all messages 89 // // this object is observing the OrthancApi so it must handle all messages
89 DeclareHandledMessage(MessageType_OrthancApi_InternalGetJsonResponseReady); 90 // DeclareHandledMessage(MessageType_OrthancApi_InternalGetJsonResponseReady);
90 DeclareIgnoredMessage(MessageType_OrthancApi_InternalGetJsonResponseError); 91 // DeclareIgnoredMessage(MessageType_OrthancApi_InternalGetJsonResponseError);
91 92
92 orthanc_.RegisterObserver(*this); 93 //orthanc_.RegisterObserver(*this);
93 this->RegisterObserver(responseObserver); 94 this->RegisterObserver(responseObserver);
94 } 95 }
95 virtual ~BaseRequest() {} 96 virtual ~BaseRequest() {}
96 97
97 // mainly maps OrthancApi internal messages to a message that is expected by the responseObserver 98 // // mainly maps OrthancApi internal messages to a message that is expected by the responseObserver
98 virtual void HandleMessage(IObservable& from, const IMessage& message) 99 // virtual void HandleMessage(IObservable& from, const IMessage& message)
99 { 100 // {
100 switch (message.GetType()) 101 // switch (message.GetType())
101 { 102 // {
102 case MessageType_OrthancApi_InternalGetJsonResponseReady: 103 // case MessageType_OrthancApi_InternalGetJsonResponseReady:
103 { 104 // {
104 const OrthancApiClient::InternalGetJsonResponseReadyMessage& messageReceived = dynamic_cast<const OrthancApiClient::InternalGetJsonResponseReadyMessage&>(message); 105 // const OrthancApiClient::InternalGetJsonResponseReadyMessage& messageReceived = dynamic_cast<const OrthancApiClient::InternalGetJsonResponseReadyMessage&>(message);
105 EmitMessage(OrthancApiClient::GetJsonResponseReadyMessage(messageToEmitWhenResponseReady_, messageReceived.request_->uri_, messageReceived.response_)); 106 // EmitMessage(OrthancApiClient::GetJsonResponseReadyMessage(messageToEmitWhenResponseReady_, messageReceived.request_->uri_, messageReceived.response_));
106 orthanc_.ReleaseRequest(messageReceived.request_); 107 // orthanc_.ReleaseRequest(messageReceived.request_);
107 }; break; 108 // }; break;
108 default: 109 // default:
109 throw MessageNotDeclaredException(message.GetType()); 110 // throw MessageNotDeclaredException(message.GetType());
110 } 111 // }
111 } 112 // }
112 113
113 }; 114 };
114 115
115 116
116 class OrthancApiClient::WebCallback : public IWebService::ICallback 117 class OrthancApiClient::WebCallback : public IWebService::ICallback
137 case OrthancApiClient::Mode_GetJson: 138 case OrthancApiClient::Mode_GetJson:
138 { 139 {
139 Json::Value response; 140 Json::Value response;
140 if (MessagingToolbox::ParseJson(response, answer, answerSize)) 141 if (MessagingToolbox::ParseJson(response, answer, answerSize))
141 { 142 {
142 OrthancApiClient::InternalGetJsonResponseReadyMessage msg(request, response); 143 request->EmitMessage(OrthancApiClient::GetJsonResponseReadyMessage(request->messageToEmitWhenResponseReady_, request->uri_, response));
143 that_.EmitMessage(msg);
144 } 144 }
145 else 145 else
146 { 146 {
147 OrthancApiClient::InternalGetJsonResponseErrorMessage msg(request); 147 // OrthancApiClient::InternalGetJsonResponseErrorMessage msg(request);
148 that_.EmitMessage(msg); 148 // that_.EmitMessage(msg);
149 } 149 }
150 }; break; 150 }; break;
151 151
152 default: 152 default:
153 that_.ReleaseRequest(request);
154 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 153 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
155 } 154 }
155 that_.ReleaseRequest(request);
156 } 156 }
157 157
158 virtual void OnHttpRequestError(const std::string& uri, 158 virtual void OnHttpRequestError(const std::string& uri,
159 Orthanc::IDynamicObject* payload) 159 Orthanc::IDynamicObject* payload)
160 { 160 {
162 162
163 switch (request->mode_) 163 switch (request->mode_)
164 { 164 {
165 case OrthancApiClient::Mode_GetJson: 165 case OrthancApiClient::Mode_GetJson:
166 { 166 {
167 OrthancApiClient::InternalGetJsonResponseErrorMessage msg(request); 167 // OrthancApiClient::InternalGetJsonResponseErrorMessage msg(request);
168 that_.EmitMessage(msg); 168 // that_.EmitMessage(msg);
169 // TODO: the request shall send an error message
169 }; break; 170 }; break;
170 171
171 default: 172 default:
172 that_.ReleaseRequest(request);
173 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); 173 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
174 } 174 }
175 that_.ReleaseRequest(request);
175 } 176 }
176 }; 177 };
177 178
178 OrthancApiClient::OrthancApiClient(MessageBroker &broker, IWebService &orthanc) 179 OrthancApiClient::OrthancApiClient(MessageBroker &broker, IWebService &orthanc)
179 : IObservable(broker), 180 : IObservable(broker),