Mercurial > hg > orthanc-stone
comparison Framework/Oracle/GenericOracleRunner.cpp @ 1098:17660df24c36 broker
simplification of IOracleRunner
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 25 Oct 2019 13:01:24 +0200 |
parents | e6d2ff8f1ab4 |
children | 98cdfe5768a4 |
comparison
equal
deleted
inserted
replaced
1097:4383382db01d | 1098:17660df24c36 |
---|---|
85 << " to " << answer.size() << " bytes"; | 85 << " to " << answer.size() << " bytes"; |
86 } | 86 } |
87 } | 87 } |
88 | 88 |
89 | 89 |
90 static void Execute(IMessageEmitter& emitter, | 90 static IMessage* Execute(const HttpCommand& command) |
91 boost::weak_ptr<IObserver>& receiver, | |
92 const HttpCommand& command) | |
93 { | 91 { |
94 Orthanc::HttpClient client; | 92 Orthanc::HttpClient client; |
95 client.SetUrl(command.GetUrl()); | 93 client.SetUrl(command.GetUrl()); |
96 client.SetMethod(command.GetMethod()); | 94 client.SetMethod(command.GetMethod()); |
97 client.SetTimeout(command.GetTimeout()); | 95 client.SetTimeout(command.GetTimeout()); |
113 Orthanc::HttpClient::HttpHeaders answerHeaders; | 111 Orthanc::HttpClient::HttpHeaders answerHeaders; |
114 client.ApplyAndThrowException(answer, answerHeaders); | 112 client.ApplyAndThrowException(answer, answerHeaders); |
115 | 113 |
116 DecodeAnswer(answer, answerHeaders); | 114 DecodeAnswer(answer, answerHeaders); |
117 | 115 |
118 HttpCommand::SuccessMessage message(command, answerHeaders, answer); | 116 return new HttpCommand::SuccessMessage(command, answerHeaders, answer); |
119 emitter.EmitMessage(receiver, message); | 117 } |
120 } | 118 |
121 | 119 |
122 | 120 static IMessage* Execute(const Orthanc::WebServiceParameters& orthanc, |
123 static void Execute(IMessageEmitter& emitter, | 121 const OrthancRestApiCommand& command) |
124 const Orthanc::WebServiceParameters& orthanc, | |
125 boost::weak_ptr<IObserver>& receiver, | |
126 const OrthancRestApiCommand& command) | |
127 { | 122 { |
128 Orthanc::HttpClient client(orthanc, command.GetUri()); | 123 Orthanc::HttpClient client(orthanc, command.GetUri()); |
129 client.SetMethod(command.GetMethod()); | 124 client.SetMethod(command.GetMethod()); |
130 client.SetTimeout(command.GetTimeout()); | 125 client.SetTimeout(command.GetTimeout()); |
131 | 126 |
141 Orthanc::HttpClient::HttpHeaders answerHeaders; | 136 Orthanc::HttpClient::HttpHeaders answerHeaders; |
142 client.ApplyAndThrowException(answer, answerHeaders); | 137 client.ApplyAndThrowException(answer, answerHeaders); |
143 | 138 |
144 DecodeAnswer(answer, answerHeaders); | 139 DecodeAnswer(answer, answerHeaders); |
145 | 140 |
146 OrthancRestApiCommand::SuccessMessage message(command, answerHeaders, answer); | 141 return new OrthancRestApiCommand::SuccessMessage(command, answerHeaders, answer); |
147 emitter.EmitMessage(receiver, message); | 142 } |
148 } | 143 |
149 | 144 |
150 | 145 static IMessage* Execute(const Orthanc::WebServiceParameters& orthanc, |
151 static void Execute(IMessageEmitter& emitter, | 146 const GetOrthancImageCommand& command) |
152 const Orthanc::WebServiceParameters& orthanc, | |
153 boost::weak_ptr<IObserver>& receiver, | |
154 const GetOrthancImageCommand& command) | |
155 { | 147 { |
156 Orthanc::HttpClient client(orthanc, command.GetUri()); | 148 Orthanc::HttpClient client(orthanc, command.GetUri()); |
157 client.SetTimeout(command.GetTimeout()); | 149 client.SetTimeout(command.GetTimeout()); |
158 | 150 |
159 CopyHttpHeaders(client, command.GetHttpHeaders()); | 151 CopyHttpHeaders(client, command.GetHttpHeaders()); |
162 Orthanc::HttpClient::HttpHeaders answerHeaders; | 154 Orthanc::HttpClient::HttpHeaders answerHeaders; |
163 client.ApplyAndThrowException(answer, answerHeaders); | 155 client.ApplyAndThrowException(answer, answerHeaders); |
164 | 156 |
165 DecodeAnswer(answer, answerHeaders); | 157 DecodeAnswer(answer, answerHeaders); |
166 | 158 |
167 command.ProcessHttpAnswer(emitter, receiver, answer, answerHeaders); | 159 return command.ProcessHttpAnswer(answer, answerHeaders); |
168 } | 160 } |
169 | 161 |
170 | 162 |
171 static void Execute(IMessageEmitter& emitter, | 163 static IMessage* Execute(const Orthanc::WebServiceParameters& orthanc, |
172 const Orthanc::WebServiceParameters& orthanc, | 164 const GetOrthancWebViewerJpegCommand& command) |
173 boost::weak_ptr<IObserver>& receiver, | |
174 const GetOrthancWebViewerJpegCommand& command) | |
175 { | 165 { |
176 Orthanc::HttpClient client(orthanc, command.GetUri()); | 166 Orthanc::HttpClient client(orthanc, command.GetUri()); |
177 client.SetTimeout(command.GetTimeout()); | 167 client.SetTimeout(command.GetTimeout()); |
178 | 168 |
179 CopyHttpHeaders(client, command.GetHttpHeaders()); | 169 CopyHttpHeaders(client, command.GetHttpHeaders()); |
182 Orthanc::HttpClient::HttpHeaders answerHeaders; | 172 Orthanc::HttpClient::HttpHeaders answerHeaders; |
183 client.ApplyAndThrowException(answer, answerHeaders); | 173 client.ApplyAndThrowException(answer, answerHeaders); |
184 | 174 |
185 DecodeAnswer(answer, answerHeaders); | 175 DecodeAnswer(answer, answerHeaders); |
186 | 176 |
187 command.ProcessHttpAnswer(emitter, receiver, answer); | 177 return command.ProcessHttpAnswer(answer); |
188 } | 178 } |
189 | 179 |
190 | 180 |
191 void GenericOracleRunner::Run(boost::weak_ptr<IObserver>& receiver, | 181 IMessage* GenericOracleRunner::Run(IOracleCommand& command) |
192 IOracleCommand& command) | |
193 { | 182 { |
194 try | 183 try |
195 { | 184 { |
196 switch (command.GetType()) | 185 switch (command.GetType()) |
197 { | 186 { |
198 case IOracleCommand::Type_Sleep: | 187 case IOracleCommand::Type_Sleep: |
199 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType, | 188 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType, |
200 "Sleep command cannot be executed by the runner"); | 189 "Sleep command cannot be executed by the runner"); |
201 break; | |
202 | 190 |
203 case IOracleCommand::Type_Http: | 191 case IOracleCommand::Type_Http: |
204 Execute(emitter_, receiver, dynamic_cast<const HttpCommand&>(command)); | 192 return Execute(dynamic_cast<const HttpCommand&>(command)); |
205 break; | |
206 | 193 |
207 case IOracleCommand::Type_OrthancRestApi: | 194 case IOracleCommand::Type_OrthancRestApi: |
208 Execute(emitter_, orthanc_, receiver, dynamic_cast<const OrthancRestApiCommand&>(command)); | 195 return Execute(orthanc_, dynamic_cast<const OrthancRestApiCommand&>(command)); |
209 break; | |
210 | 196 |
211 case IOracleCommand::Type_GetOrthancImage: | 197 case IOracleCommand::Type_GetOrthancImage: |
212 Execute(emitter_, orthanc_, receiver, dynamic_cast<const GetOrthancImageCommand&>(command)); | 198 return Execute(orthanc_, dynamic_cast<const GetOrthancImageCommand&>(command)); |
213 break; | |
214 | 199 |
215 case IOracleCommand::Type_GetOrthancWebViewerJpeg: | 200 case IOracleCommand::Type_GetOrthancWebViewerJpeg: |
216 Execute(emitter_, orthanc_, receiver, dynamic_cast<const GetOrthancWebViewerJpegCommand&>(command)); | 201 return Execute(orthanc_, dynamic_cast<const GetOrthancWebViewerJpegCommand&>(command)); |
217 break; | |
218 | 202 |
219 case IOracleCommand::Type_Custom: | 203 case IOracleCommand::Type_Custom: |
220 dynamic_cast<CustomOracleCommand&>(command).Execute(emitter_, receiver, *this); | 204 return dynamic_cast<CustomOracleCommand&>(command).Execute(*this); |
221 break; | |
222 | 205 |
223 default: | 206 default: |
224 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | 207 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
225 } | 208 } |
226 } | 209 } |
227 catch (Orthanc::OrthancException& e) | 210 catch (Orthanc::OrthancException& e) |
228 { | 211 { |
229 LOG(ERROR) << "Exception within the oracle: " << e.What(); | 212 LOG(ERROR) << "Exception within the oracle: " << e.What(); |
230 emitter_.EmitMessage(receiver, OracleCommandExceptionMessage(command, e)); | 213 return new OracleCommandExceptionMessage(command, e); |
231 } | 214 } |
232 catch (...) | 215 catch (...) |
233 { | 216 { |
234 LOG(ERROR) << "Threaded exception within the oracle"; | 217 LOG(ERROR) << "Threaded exception within the oracle"; |
235 emitter_.EmitMessage(receiver, OracleCommandExceptionMessage | 218 return new OracleCommandExceptionMessage(command, Orthanc::ErrorCode_InternalError); |
236 (command, Orthanc::ErrorCode_InternalError)); | |
237 } | 219 } |
238 } | 220 } |
239 } | 221 } |