comparison Plugins/Engine/PluginsHttpHandler.cpp @ 1066:bb82e5e818e9

OnStoredInstance callback in plugins
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Jul 2014 18:39:02 +0200
parents 8d1845feb277
children ace99e272203
comparison
equal deleted inserted replaced
1065:921532f67770 1066:bb82e5e818e9
35 #include "../../Core/ChunkedBuffer.h" 35 #include "../../Core/ChunkedBuffer.h"
36 #include "../../Core/OrthancException.h" 36 #include "../../Core/OrthancException.h"
37 #include "../../Core/Toolbox.h" 37 #include "../../Core/Toolbox.h"
38 #include "../../Core/HttpServer/HttpOutput.h" 38 #include "../../Core/HttpServer/HttpOutput.h"
39 #include "../../Core/ImageFormats/PngWriter.h" 39 #include "../../Core/ImageFormats/PngWriter.h"
40 #include "../../OrthancServer/ServerToolbox.h"
40 41
41 #include <boost/regex.hpp> 42 #include <boost/regex.hpp>
42 #include <glog/logging.h> 43 #include <glog/logging.h>
43 44
44 namespace Orthanc 45 namespace Orthanc
77 78
78 79
79 80
80 struct PluginsHttpHandler::PImpl 81 struct PluginsHttpHandler::PImpl
81 { 82 {
82 typedef std::pair<boost::regex*, OrthancPluginRestCallback> Callback; 83 typedef std::pair<boost::regex*, OrthancPluginRestCallback> RestCallback;
83 typedef std::list<Callback> Callbacks; 84 typedef std::list<RestCallback> RestCallbacks;
85 typedef std::list<OrthancPluginOnStoredInstanceCallback> OnStoredCallbacks;
84 86
85 ServerContext& context_; 87 ServerContext& context_;
86 Callbacks callbacks_; 88 RestCallbacks restCallbacks_;
87 OrthancRestApi* restApi_; 89 OrthancRestApi* restApi_;
90 OnStoredCallbacks onStoredCallbacks_;
88 91
89 PImpl(ServerContext& context) : context_(context), restApi_(NULL) 92 PImpl(ServerContext& context) : context_(context), restApi_(NULL)
90 { 93 {
91 } 94 }
92 }; 95 };
96
97
98 static char* CopyString(const std::string& str)
99 {
100 char *result = reinterpret_cast<char*>(malloc(str.size() + 1));
101 if (result == NULL)
102 {
103 throw OrthancException(ErrorCode_NotEnoughMemory);
104 }
105
106 if (str.size() == 0)
107 {
108 result[0] = '\0';
109 }
110 else
111 {
112 memcpy(result, &str[0], str.size() + 1);
113 }
114
115 return result;
116 }
93 117
94 118
95 PluginsHttpHandler::PluginsHttpHandler(ServerContext& context) 119 PluginsHttpHandler::PluginsHttpHandler(ServerContext& context)
96 { 120 {
97 pimpl_.reset(new PImpl(context)); 121 pimpl_.reset(new PImpl(context));
98 } 122 }
99 123
100 124
101 PluginsHttpHandler::~PluginsHttpHandler() 125 PluginsHttpHandler::~PluginsHttpHandler()
102 { 126 {
103 for (PImpl::Callbacks::iterator it = pimpl_->callbacks_.begin(); 127 for (PImpl::RestCallbacks::iterator it = pimpl_->restCallbacks_.begin();
104 it != pimpl_->callbacks_.end(); ++it) 128 it != pimpl_->restCallbacks_.end(); ++it)
105 { 129 {
106 // Delete the regular expression associated with this callback 130 // Delete the regular expression associated with this callback
107 delete it->first; 131 delete it->first;
108 } 132 }
109 } 133 }
140 std::vector<std::string> groups; 164 std::vector<std::string> groups;
141 std::vector<const char*> cgroups; 165 std::vector<const char*> cgroups;
142 166
143 // Loop over the callbacks registered by the plugins 167 // Loop over the callbacks registered by the plugins
144 bool found = false; 168 bool found = false;
145 for (PImpl::Callbacks::const_iterator it = pimpl_->callbacks_.begin(); 169 for (PImpl::RestCallbacks::const_iterator it = pimpl_->restCallbacks_.begin();
146 it != pimpl_->callbacks_.end() && !found; ++it) 170 it != pimpl_->restCallbacks_.end() && !found; ++it)
147 { 171 {
148 // Check whether the regular expression associated to this 172 // Check whether the regular expression associated to this
149 // callback matches the URI 173 // callback matches the URI
150 boost::cmatch what; 174 boost::cmatch what;
151 if (boost::regex_match(flatUri.c_str(), what, *(it->first))) 175 if (boost::regex_match(flatUri.c_str(), what, *(it->first)))
245 return true; 269 return true;
246 } 270 }
247 } 271 }
248 272
249 273
274 void PluginsHttpHandler::SignalStoredInstance(DicomInstanceToStore& instance,
275 const std::string& instanceId)
276 {
277 for (PImpl::OnStoredCallbacks::const_iterator
278 callback = pimpl_->onStoredCallbacks_.begin();
279 callback != pimpl_->onStoredCallbacks_.end(); ++callback)
280 {
281 (*callback) (reinterpret_cast<OrthancPluginDicomInstance*>(&instance),
282 instanceId.c_str());
283 }
284 }
285
286
287
250 static void CopyToMemoryBuffer(OrthancPluginMemoryBuffer& target, 288 static void CopyToMemoryBuffer(OrthancPluginMemoryBuffer& target,
251 const void* data, 289 const void* data,
252 size_t size) 290 size_t size)
253 { 291 {
254 target.size = size; 292 target.size = size;
291 { 329 {
292 const _OrthancPluginRestCallback& p = 330 const _OrthancPluginRestCallback& p =
293 *reinterpret_cast<const _OrthancPluginRestCallback*>(parameters); 331 *reinterpret_cast<const _OrthancPluginRestCallback*>(parameters);
294 332
295 LOG(INFO) << "Plugin has registered a REST callback on: " << p.pathRegularExpression; 333 LOG(INFO) << "Plugin has registered a REST callback on: " << p.pathRegularExpression;
296 pimpl_->callbacks_.push_back(std::make_pair(new boost::regex(p.pathRegularExpression), p.callback)); 334 pimpl_->restCallbacks_.push_back(std::make_pair(new boost::regex(p.pathRegularExpression), p.callback));
335 }
336
337
338
339 void PluginsHttpHandler::RegisterOnStoredInstanceCallback(const void* parameters)
340 {
341 const _OrthancPluginOnStoredInstanceCallback& p =
342 *reinterpret_cast<const _OrthancPluginOnStoredInstanceCallback*>(parameters);
343
344 LOG(INFO) << "Plugin has registered an OnStoredInstance callback";
345 pimpl_->onStoredCallbacks_.push_back(p.callback);
297 } 346 }
298 347
299 348
300 349
301 void PluginsHttpHandler::AnswerBuffer(const void* parameters) 350 void PluginsHttpHandler::AnswerBuffer(const void* parameters)
544 throw OrthancException(ErrorCode_UnknownResource); 593 throw OrthancException(ErrorCode_UnknownResource);
545 } 594 }
546 } 595 }
547 596
548 597
549 char* PluginsHttpHandler::CopyString(const std::string& str) const 598 static void AccessDicomInstance(_OrthancPluginService service,
550 { 599 const void* parameters)
551 char *result = reinterpret_cast<char*>(malloc(str.size() + 1)); 600 {
552 if (result == NULL) 601 const _OrthancPluginAccessDicomInstance& p =
553 { 602 *reinterpret_cast<const _OrthancPluginAccessDicomInstance*>(parameters);
554 throw OrthancException(ErrorCode_NotEnoughMemory); 603
555 } 604 DicomInstanceToStore& instance =
556 605 *reinterpret_cast<DicomInstanceToStore*>(p.instance);
557 if (str.size() == 0) 606
558 { 607 switch (service)
559 result[0] = '\0'; 608 {
560 } 609 case _OrthancPluginService_GetInstanceRemoteAet:
561 else 610 *p.resultString = instance.GetRemoteAet().c_str();
562 { 611 return;
563 memcpy(result, &str[0], str.size() + 1); 612
564 } 613 case _OrthancPluginService_GetInstanceSize:
565 614 *p.resultInt64 = instance.GetBufferSize();
566 return result; 615 return;
616
617 case _OrthancPluginService_GetInstanceData:
618 *p.resultString = instance.GetBufferData();
619 return;
620
621 case _OrthancPluginService_GetInstanceJson:
622 case _OrthancPluginService_GetInstanceSimplifiedJson:
623 {
624 Json::StyledWriter writer;
625 std::string s;
626
627 if (service == _OrthancPluginService_GetInstanceJson)
628 {
629 s = writer.write(instance.GetJson());
630 }
631 else
632 {
633 Json::Value simplified;
634 SimplifyTags(simplified, instance.GetJson());
635 s = writer.write(simplified);
636 }
637
638 *p.resultStringToFree = CopyString(s);
639 return;
640 }
641
642 default:
643 throw OrthancException(ErrorCode_InternalError);
644 }
567 } 645 }
568 646
569 647
570 bool PluginsHttpHandler::InvokeService(_OrthancPluginService service, 648 bool PluginsHttpHandler::InvokeService(_OrthancPluginService service,
571 const void* parameters) 649 const void* parameters)
574 { 652 {
575 case _OrthancPluginService_RegisterRestCallback: 653 case _OrthancPluginService_RegisterRestCallback:
576 RegisterRestCallback(parameters); 654 RegisterRestCallback(parameters);
577 return true; 655 return true;
578 656
657 case _OrthancPluginService_RegisterOnStoredInstanceCallback:
658 RegisterOnStoredInstanceCallback(parameters);
659 return true;
660
579 case _OrthancPluginService_AnswerBuffer: 661 case _OrthancPluginService_AnswerBuffer:
580 AnswerBuffer(parameters); 662 AnswerBuffer(parameters);
581 return true; 663 return true;
582 664
583 case _OrthancPluginService_CompressAndAnswerPngImage: 665 case _OrthancPluginService_CompressAndAnswerPngImage:
638 720
639 case _OrthancPluginService_LookupInstance: 721 case _OrthancPluginService_LookupInstance:
640 LookupResource(ResourceType_Instance, parameters); 722 LookupResource(ResourceType_Instance, parameters);
641 return true; 723 return true;
642 724
725 case _OrthancPluginService_GetInstanceRemoteAet:
726 case _OrthancPluginService_GetInstanceSize:
727 case _OrthancPluginService_GetInstanceData:
728 case _OrthancPluginService_GetInstanceJson:
729 case _OrthancPluginService_GetInstanceSimplifiedJson:
730 AccessDicomInstance(service, parameters);
731 return true;
732
643 default: 733 default:
644 return false; 734 return false;
645 } 735 }
646 } 736 }
647 737