Mercurial > hg > orthanc
comparison Plugins/Engine/PluginsManager.cpp @ 899:bb0a51561016 plugins
refactoring
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 18 Jun 2014 13:29:09 +0200 |
parents | 7000fc86fe62 |
children | 7d88f3f4a3b3 |
comparison
equal
deleted
inserted
replaced
898:7000fc86fe62 | 899:bb0a51561016 |
---|---|
132 } | 132 } |
133 | 133 |
134 | 134 |
135 int32_t PluginsManager::InvokeService(OrthancPluginContext* context, | 135 int32_t PluginsManager::InvokeService(OrthancPluginContext* context, |
136 OrthancPluginService service, | 136 OrthancPluginService service, |
137 const void* parameters) | 137 const void* params) |
138 { | 138 { |
139 switch (service) | 139 switch (service) |
140 { | 140 { |
141 case OrthancPluginService_LogError: | 141 case OrthancPluginService_LogError: |
142 LOG(ERROR) << reinterpret_cast<const char*>(parameters); | 142 LOG(ERROR) << reinterpret_cast<const char*>(params); |
143 return 0; | 143 return 0; |
144 | 144 |
145 case OrthancPluginService_LogWarning: | 145 case OrthancPluginService_LogWarning: |
146 LOG(WARNING) << reinterpret_cast<const char*>(parameters); | 146 LOG(WARNING) << reinterpret_cast<const char*>(params); |
147 return 0; | 147 return 0; |
148 | 148 |
149 case OrthancPluginService_LogInfo: | 149 case OrthancPluginService_LogInfo: |
150 LOG(INFO) << reinterpret_cast<const char*>(parameters); | 150 LOG(INFO) << reinterpret_cast<const char*>(params); |
151 return 0; | 151 return 0; |
152 | 152 |
153 default: | 153 default: |
154 break; | 154 break; |
155 } | 155 } |
156 | 156 |
157 PluginsManager* that = reinterpret_cast<PluginsManager*>(context->pluginsManager); | 157 PluginsManager* that = reinterpret_cast<PluginsManager*>(context->pluginsManager); |
158 | 158 |
159 if (that->HasServiceProvider() && | 159 for (std::list<IPluginServiceProvider*>::iterator |
160 that->serviceProvider_->Handle(service, parameters)) | 160 it = that->serviceProviders_.begin(); |
161 { | 161 it != that->serviceProviders_.end(); it++) |
162 return 0; | 162 { |
163 try | |
164 { | |
165 if ((*it)->InvokeService(service, params)) | |
166 { | |
167 return 0; | |
168 } | |
169 } | |
170 catch (OrthancException&) | |
171 { | |
172 // This service provider has failed, go to the next | |
173 } | |
163 } | 174 } |
164 | 175 |
165 LOG(ERROR) << "Plugin invoking unknown service " << service; | 176 LOG(ERROR) << "Plugin invoking unknown service " << service; |
166 return -1; | 177 return -1; |
167 } | 178 } |
168 | 179 |
169 | 180 |
170 void PluginsManager::RegisterRestCallback(const OrthancPluginContext* context, | 181 PluginsManager::PluginsManager() |
171 const char* pathRegularExpression, | |
172 OrthancPluginRestCallback callback) | |
173 { | |
174 LOG(INFO) << "Plugin has registered a REST callback on: " << pathRegularExpression; | |
175 PluginsManager* manager = reinterpret_cast<PluginsManager*>(context->pluginsManager); | |
176 manager->restCallbacks_.push_back(std::make_pair(pathRegularExpression, callback)); | |
177 } | |
178 | |
179 | |
180 static void AnswerBuffer(OrthancPluginRestOutput* output, | |
181 const char* answer, | |
182 uint32_t answerSize, | |
183 const char* mimeType) | |
184 { | |
185 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(output); | |
186 translatedOutput->AnswerBufferWithContentType(answer, answerSize, mimeType); | |
187 } | |
188 | |
189 | |
190 PluginsManager::PluginsManager() : | |
191 serviceProvider_(NULL) | |
192 { | 182 { |
193 memset(&context_, 0, sizeof(context_)); | 183 memset(&context_, 0, sizeof(context_)); |
194 context_.pluginsManager = this; | 184 context_.pluginsManager = this; |
195 context_.orthancVersion = ORTHANC_VERSION; | 185 context_.orthancVersion = ORTHANC_VERSION; |
196 context_.FreeBuffer = ::free; | 186 context_.FreeBuffer = ::free; |
197 context_.InvokeService = InvokeService; | 187 context_.InvokeService = InvokeService; |
198 context_.RegisterRestCallback = RegisterRestCallback; | |
199 context_.AnswerBuffer = AnswerBuffer; | |
200 } | 188 } |
201 | 189 |
202 PluginsManager::~PluginsManager() | 190 PluginsManager::~PluginsManager() |
203 { | 191 { |
204 for (Plugins::iterator it = plugins_.begin(); it != plugins_.end(); it++) | 192 for (Plugins::iterator it = plugins_.begin(); it != plugins_.end(); it++) |
296 } | 284 } |
297 } | 285 } |
298 } | 286 } |
299 } | 287 } |
300 } | 288 } |
301 | |
302 | |
303 IPluginServiceProvider& PluginsManager::GetServiceProvider() const | |
304 { | |
305 if (!HasServiceProvider()) | |
306 { | |
307 throw OrthancException(ErrorCode_BadRequest); | |
308 } | |
309 else | |
310 { | |
311 return *serviceProvider_; | |
312 } | |
313 } | |
314 } | 289 } |