comparison Plugins/Engine/PluginsManager.cpp @ 898:7000fc86fe62 plugins

improved plugin api
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 18 Jun 2014 09:18:28 +0200
parents bafc9d592632
children bb0a51561016
comparison
equal deleted inserted replaced
897:bafc9d592632 898:7000fc86fe62
130 assert(getVersion != NULL); 130 assert(getVersion != NULL);
131 return getVersion(); 131 return getVersion();
132 } 132 }
133 133
134 134
135 static void LogError(const char* str) 135 int32_t PluginsManager::InvokeService(OrthancPluginContext* context,
136 { 136 OrthancPluginService service,
137 LOG(ERROR) << str; 137 const void* parameters)
138 } 138 {
139 139 switch (service)
140 static void LogWarning(const char* str) 140 {
141 { 141 case OrthancPluginService_LogError:
142 LOG(WARNING) << str; 142 LOG(ERROR) << reinterpret_cast<const char*>(parameters);
143 } 143 return 0;
144 144
145 static void LogInfo(const char* str) 145 case OrthancPluginService_LogWarning:
146 { 146 LOG(WARNING) << reinterpret_cast<const char*>(parameters);
147 LOG(INFO) << str; 147 return 0;
148 } 148
149 case OrthancPluginService_LogInfo:
150 LOG(INFO) << reinterpret_cast<const char*>(parameters);
151 return 0;
152
153 default:
154 break;
155 }
156
157 PluginsManager* that = reinterpret_cast<PluginsManager*>(context->pluginsManager);
158
159 if (that->HasServiceProvider() &&
160 that->serviceProvider_->Handle(service, parameters))
161 {
162 return 0;
163 }
164
165 LOG(ERROR) << "Plugin invoking unknown service " << service;
166 return -1;
167 }
168
149 169
150 void PluginsManager::RegisterRestCallback(const OrthancPluginContext* context, 170 void PluginsManager::RegisterRestCallback(const OrthancPluginContext* context,
151 const char* pathRegularExpression, 171 const char* pathRegularExpression,
152 OrthancPluginRestCallback callback) 172 OrthancPluginRestCallback callback)
153 { 173 {
154 LOG(INFO) << "Plugin has registered a REST callback on: " << pathRegularExpression; 174 LOG(INFO) << "Plugin has registered a REST callback on: " << pathRegularExpression;
155 PluginsManager* manager = reinterpret_cast<PluginsManager*>(context->pimpl); 175 PluginsManager* manager = reinterpret_cast<PluginsManager*>(context->pluginsManager);
156 manager->restCallbacks_.push_back(std::make_pair(pathRegularExpression, callback)); 176 manager->restCallbacks_.push_back(std::make_pair(pathRegularExpression, callback));
157 } 177 }
158 178
159 179
160 static void AnswerBuffer(OrthancPluginRestOutput* output, 180 static void AnswerBuffer(OrthancPluginRestOutput* output,
165 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(output); 185 HttpOutput* translatedOutput = reinterpret_cast<HttpOutput*>(output);
166 translatedOutput->AnswerBufferWithContentType(answer, answerSize, mimeType); 186 translatedOutput->AnswerBufferWithContentType(answer, answerSize, mimeType);
167 } 187 }
168 188
169 189
170 PluginsManager::PluginsManager() 190 PluginsManager::PluginsManager() :
191 serviceProvider_(NULL)
171 { 192 {
172 memset(&context_, 0, sizeof(context_)); 193 memset(&context_, 0, sizeof(context_));
173 context_.pimpl = this; 194 context_.pluginsManager = this;
174 context_.orthancVersion = ORTHANC_VERSION; 195 context_.orthancVersion = ORTHANC_VERSION;
175 context_.FreeBuffer = ::free; 196 context_.FreeBuffer = ::free;
176 context_.LogError = LogError; 197 context_.InvokeService = InvokeService;
177 context_.LogWarning = LogWarning;
178 context_.LogInfo = LogInfo;
179 context_.RegisterRestCallback = RegisterRestCallback; 198 context_.RegisterRestCallback = RegisterRestCallback;
180 context_.AnswerBuffer = AnswerBuffer; 199 context_.AnswerBuffer = AnswerBuffer;
181 } 200 }
182 201
183 PluginsManager::~PluginsManager() 202 PluginsManager::~PluginsManager()
277 } 296 }
278 } 297 }
279 } 298 }
280 } 299 }
281 } 300 }
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 }
282 } 314 }