#include "iclXCFMemoryGrabber.h" #include #include #include #include "Memory/Interface.hpp" #include "Memory/SynchronizedQueue.hpp" #include #include #include #include #include #include "iclXCFUtils.h" #include using memory::interface::MemoryPtr; using memory::interface::MemoryInterface; using memory::interface::EventSource; using memory::interface::TriggeredAction; using memory::interface::Condition; using memory::interface::Subscription; using memory::interface::Event; using memory::interface::MemoryInterfaceException; using memory::interface::Attachments; namespace icl{ struct XCFMemoryGrabberImpl : public Grabber{ std::string m_xpath; MemoryPtr m_memInterface; boost::shared_ptr m_evtSrc; TriggeredAction *m_action; Condition m_condition; Subscription *m_subscription; bool m_ignoreDesired; ImgBase *m_buffer; Attachments m_attachments; Converter m_converter; XCFMemoryGrabberImpl(const std::string &memoryName,const std::string &xpath): m_xpath(xpath),m_action(0),m_subscription(0),m_ignoreDesired(false),m_buffer(0){ try{ m_memInterface = MemoryPtr(MemoryInterface::getInstance(memoryName)); m_evtSrc = boost::shared_ptr(new EventSource()); m_action = new TriggeredAction(boost::bind(&EventSource::push, m_evtSrc, _1)); m_condition = Condition(Event::INSERT,m_xpath); m_subscription = new Subscription(m_memInterface->add(m_condition,*m_action)); }catch(const MemoryInterfaceException& ex){ std::cerr << "MemoryInterfaceException: " << ex.what() << std::endl; }catch(const std::exception &ex){ std::cerr << "std::exception: " << ex.what() << std::endl; }catch(...){ std::cerr << "An Unknown Error Occured!" << std::endl; } } ~XCFMemoryGrabberImpl(){ m_memInterface->unsubscribe (*m_subscription); ICL_DELETE(m_action); ICL_DELETE(m_subscription); ICL_DELETE(m_buffer); } const ImgBase *grabUD(ImgBase **ppoDst){ Event e; while (m_evtSrc->next(e)) { // this call locks! xmltio::LocationPtr loc = xmltio::find (e.getDocument(),m_xpath); if (!loc) break; ImgBase *poOutput = 0; XCFUtils::ImageDescription d = XCFUtils::getImageDescription(*loc); ImgBase **usedDstImage = m_ignoreDesired ? ppoDst : &m_buffer; poOutput->setTime (d.time); m_memInterface->getAttachments(e.getDocument().getRootLocation().getDocumentText(), m_attachments); XCFUtils::unserialize(m_attachments[d.uri],d,usedDstImage); if(!m_ignoreDesired){ *ppoDst = prepareOutput (ppoDst); m_converter.apply(*usedDstImage,*ppoDst); } return *ppoDst; } // if we arrive here, something went wrong ERROR_LOG("Unable to grab next image from memory"); return 0; } }; /// this can be implemented only if XCFMemoryGrabberImpl is defined properly (here) void XCFMemoryGrabberImplDelOp::delete_func(XCFMemoryGrabberImpl *impl){ ERROR_LOG("this class should not be used yet, because it was not tested at all!"); ICL_DELETE(impl); } XCFMemoryGrabber::XCFMemoryGrabber(const std::string &memoryName, const std::string &imageXPath): impl(new XCFMemoryGrabberImpl(memoryName,imageXPath)){ } const ImgBase *XCFMemoryGrabber::grabUD(ImgBase **ppoDst){ return impl->grab(ppoDst); } }