From 4bfdd57093f0193a97f3d89894443deb5d85bfc1 Mon Sep 17 00:00:00 2001 From: Edouard DUPIN Date: Wed, 31 May 2017 07:37:51 +0200 Subject: [PATCH] [DEV] better callback api but error on a mutable and const elements ... --- lutinMacro_zeus.py | 5 +- tools/player-video/appl/MediaDecoder.cpp | 41 ++++----- tools/player-video/appl/MediaDecoder.hpp | 2 +- tools/player-video/appl/widget/ListViewer.cpp | 91 +++++++++---------- zeus/AbstractFunction.hpp | 2 +- zeus/ActionNotification.hpp | 2 +- zeus/Future.hpp | 15 +++ zeus/ObjectRemote.hpp | 6 +- zeus/zeus-File.impl.cpp | 6 +- 9 files changed, 90 insertions(+), 80 deletions(-) diff --git a/lutinMacro_zeus.py b/lutinMacro_zeus.py index 02b13ef..853c75e 100644 --- a/lutinMacro_zeus.py +++ b/lutinMacro_zeus.py @@ -310,7 +310,7 @@ class FunctionDefinition: else: param_data += elem["name"] out += param_data - out += ");\n" + out += ") const;\n" return out; def generate_cpp_proxy(self, space, class_name): out = ""; @@ -327,7 +327,7 @@ class FunctionDefinition: else: param_data += elem["name"] out += param_data - out += ") {\n" + out += ") const {\n" space += " " if self.is_action == True: out += space + 'return m_obj.callAction("' + self.name + '"' @@ -703,6 +703,7 @@ class ServiceDefinition: out += "#include \n" out += "#include \n" out += "#include \n" + out += "#include <" + self.prop["file_name_class_header"] + ">\n" for elem in self.imports: prop = zeus_object_to_dictionary(elem) #out += "#include <" + prop["file_name_class_header"] + ">\n" diff --git a/tools/player-video/appl/MediaDecoder.cpp b/tools/player-video/appl/MediaDecoder.cpp index f0ca281..b49dfc5 100644 --- a/tools/player-video/appl/MediaDecoder.cpp +++ b/tools/player-video/appl/MediaDecoder.cpp @@ -392,14 +392,14 @@ void appl::MediaDecoder::init(ememory::SharedPtr _property, uint } APPL_WARNING("Get File"); media.getFile().andThen( - [=](zeus::Future _fut) mutable { + [=](const zeus::ProxyFile& _proxy) mutable { APPL_WARNING("Receive ProxyFile"); - m_remote->m_fileHandle = _fut.get(); + m_remote->m_fileHandle = _proxy; APPL_WARNING("We have handle"); m_remote->m_fileHandle.getSize().andThen( - [=](zeus::Future _fut) mutable { + [=](const uint64_t& _value) mutable { APPL_WARNING("Receive FileSize to index property"); - uint64_t sizeOfBuffer = _fut.get(); + uint64_t sizeOfBuffer = _value; APPL_WARNING("pppllloooppp " << sizeOfBuffer); m_remote->m_buffer.resize(sizeOfBuffer, 0); APPL_WARNING("pppllloooppp"); @@ -501,7 +501,7 @@ int64_t appl::MediaDecoder::seekFunc(int64_t _offset, int _whence) { return m_remote->m_bufferReadPosition; } -bool appl::StreamBuffering::addDataCallback(zeus::Future _fut, int64_t _positionRequest) { +bool appl::StreamBuffering::addDataCallback(const zeus::Raw& _data, int64_t _positionRequest) { #ifdef DEBUG std::this_thread::sleep_for(std::chrono::milliseconds(10)); #endif @@ -509,15 +509,10 @@ bool appl::StreamBuffering::addDataCallback(zeus::Future _fut, int64_ std::unique_lock lock(m_mutex); bool find = false; m_callInProgress = false; - if (_fut.hasError() == true) { - APPL_ERROR("Error when loading data (1 MB)"); - return true; - } // TODO : Check buffer size ... - zeus::Raw buffer = _fut.get(); - APPL_DEBUG(" ==> receive DATA : " << _positionRequest << " size=" << buffer.size()); + APPL_DEBUG(" ==> receive DATA : " << _positionRequest << " size=" << _data.size()); // copy data - memcpy(&m_buffer[_positionRequest], buffer.data(), buffer.size()); + memcpy(&m_buffer[_positionRequest], _data.data(), _data.size()); // Update the buffer data and positionning // find if the position correspond at a last positioning: @@ -525,20 +520,20 @@ bool appl::StreamBuffering::addDataCallback(zeus::Future _fut, int64_ while (it != m_bufferFillSection.end()) { if ( _positionRequest >= it->first && _positionRequest < it->second) { - if (_positionRequest + buffer.size() > it->second){ - it->second = _positionRequest + buffer.size(); + if (_positionRequest + _data.size() > it->second){ + it->second = _positionRequest + _data.size(); } find = true; break; } else if (it->second == _positionRequest) { - it->second += buffer.size(); + it->second += _data.size(); find = true; break; } auto it2 = it; ++it2; if ( it2 != m_bufferFillSection.end() - && _positionRequest + buffer.size() >= it2->first) { + && _positionRequest + _data.size() >= it2->first) { it2->first = _positionRequest; find = true; break; @@ -550,7 +545,7 @@ bool appl::StreamBuffering::addDataCallback(zeus::Future _fut, int64_ } if (find == false) { APPL_ERROR("insert new element in the list of values"); - m_bufferFillSection.insert(it, std::pair(_positionRequest, _positionRequest + buffer.size())); + m_bufferFillSection.insert(it, std::pair(_positionRequest, _positionRequest + _data.size())); } } checkIfWeNeedMoreDataFromNetwork(); @@ -625,8 +620,8 @@ void appl::StreamBuffering::checkIfWeNeedMoreDataFromNetwork() { APPL_DEBUG("Request DATA: " << it->second << " size=" << sizeRequest); auto futData = m_fileHandle.getPart(it->second, it->second + sizeRequest); auto localShared = ememory::dynamicPointerCast(sharedFromThis()); - futData.andThen([=](zeus::Future _fut) mutable { - return localShared->addDataCallback(_fut, it->second); + futData.andThen([=](const zeus::Raw& _value) mutable { + return localShared->addDataCallback(_value, it->second); }); m_callInProgress = true; } @@ -643,8 +638,8 @@ void appl::StreamBuffering::checkIfWeNeedMoreDataFromNetwork() { APPL_DEBUG("Request DATA : " << m_bufferReadPosition << " size=" << sizeRequest); auto futData = m_fileHandle.getPart(m_bufferReadPosition, m_bufferReadPosition+sizeRequest); auto localShared = ememory::dynamicPointerCast(sharedFromThis()); - futData.andThen([=](zeus::Future _fut) mutable { - return localShared->addDataCallback(_fut, m_bufferReadPosition); + futData.andThen([=](const zeus::Raw& _value) mutable { + return localShared->addDataCallback(_value, m_bufferReadPosition); }); m_callInProgress = true; // nothing more to do ... @@ -665,8 +660,8 @@ void appl::StreamBuffering::checkIfWeNeedMoreDataFromNetwork() { APPL_DEBUG("Request DATA : " << m_bufferReadPosition << " size=" << sizeRequest); auto futData = m_fileHandle.getPart(m_bufferReadPosition, m_bufferReadPosition + sizeRequest); auto localShared = ememory::dynamicPointerCast(sharedFromThis()); - futData.andThen([=](zeus::Future _fut) mutable { - return localShared->addDataCallback(_fut, m_bufferReadPosition); + futData.andThen([=](const zeus::Raw& _value) mutable { + return localShared->addDataCallback(_value, m_bufferReadPosition); }); m_callInProgress = true; if (find == false) { diff --git a/tools/player-video/appl/MediaDecoder.hpp b/tools/player-video/appl/MediaDecoder.hpp index bb48374..605f039 100644 --- a/tools/player-video/appl/MediaDecoder.hpp +++ b/tools/player-video/appl/MediaDecoder.hpp @@ -69,7 +69,7 @@ namespace appl { bool m_callInProgress; bool m_stopRequested; public: - bool addDataCallback(zeus::Future _fut, int64_t _positionRequest); + bool addDataCallback(const zeus::Raw& _data, int64_t _positionRequest); void checkIfWeNeedMoreDataFromNetwork(); uint64_t getSize() { std::unique_lock lock(m_mutex); diff --git a/tools/player-video/appl/widget/ListViewer.cpp b/tools/player-video/appl/widget/ListViewer.cpp index e8f3d56..6ffe6e9 100644 --- a/tools/player-video/appl/widget/ListViewer.cpp +++ b/tools/player-video/appl/widget/ListViewer.cpp @@ -79,32 +79,31 @@ void appl::ElementProperty::loadData() { auto tmpProperty = sharedFromThis(); // Get the media zeus::Future futMedia = m_remoteServiceVideo.get(m_id); - futMedia.andElse([=](zeus::Future _fut) mutable { + futMedia.andElse([=](const std::string& _error, const std::string& _help) mutable { APPL_INFO(" [" << tmpProperty->m_id << "] get media error: " << tmpProperty->m_id); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_title = "[ERROR] can not get media informations
" + _fut.getErrorType() + ": " + _fut.getErrorHelp(); + tmpProperty->m_title = "[ERROR] can not get media informations
" + _error + ": " + _help; tmpProperty->m_metadataUpdated = appl::statusLoadingData::done; } m_widget->markToRedraw(); return true; }); - futMedia.andThen([=](zeus::Future _fut) mutable { + futMedia.andThen([=](const zeus::ProxyMedia& _media) mutable { APPL_INFO(" [" << tmpProperty->m_id << "] get media: " << tmpProperty->m_id); - zeus::ProxyMedia media = _fut.get(); - if (media.exist() == false) { + if (_media.exist() == false) { APPL_ERROR("get media error"); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_title = "[ERROR] can not get media informations (2)
" + _fut.getErrorType() + ": " + _fut.getErrorHelp(); + tmpProperty->m_title = "[ERROR] can not get media informations (2)"; tmpProperty->m_metadataUpdated = appl::statusLoadingData::done; } m_widget->markToRedraw(); return true; } - media.getMetadata("title") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("title") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -114,11 +113,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get title: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get title: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_title = _fut.get(); + tmpProperty->m_title = _value; } m_widget->markToRedraw(); { @@ -130,8 +129,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMetadata("series-name") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("series-name") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -141,11 +140,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_ERROR(" [" << tmpProperty->m_id << "] get serie: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_ERROR(" [" << tmpProperty->m_id << "] get serie: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_serie = _fut.get(); + tmpProperty->m_serie = _value; } m_widget->markToRedraw(); { @@ -157,8 +156,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMetadata("saison") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("saison") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -168,11 +167,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get saison: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get saison: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_saison = _fut.get(); + tmpProperty->m_saison = _value; } m_widget->markToRedraw(); { @@ -184,8 +183,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMetadata("episode") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("episode") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -195,11 +194,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get episode: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get episode: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_episode = _fut.get(); + tmpProperty->m_episode = _value; } m_widget->markToRedraw(); { @@ -211,8 +210,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMetadata("description") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("description") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -222,11 +221,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get description: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get description: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_description = _fut.get(); + tmpProperty->m_description = _value; } m_widget->markToRedraw(); { @@ -238,8 +237,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMetadata("production-methode") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("production-methode") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -249,11 +248,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get production-methode: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get production-methode: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_productMethode = _fut.get(); + tmpProperty->m_productMethode = _value; } m_widget->markToRedraw(); { @@ -265,8 +264,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMetadata("type") - .andElse([=](zeus::Future _fut) mutable { + _media.getMetadata("type") + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -276,11 +275,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get type: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get type: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_type = _fut.get(); + tmpProperty->m_type = _value; } m_widget->markToRedraw(); { @@ -292,8 +291,8 @@ void appl::ElementProperty::loadData() { } return true; }); - media.getMineType() - .andElse([=](zeus::Future _fut) mutable { + _media.getMineType() + .andElse([=](const std::string& _error, const std::string& _help) mutable { { std::unique_lock lock(tmpProperty->m_mutex); tmpProperty->m_nbElementLoaded++; @@ -303,11 +302,11 @@ void appl::ElementProperty::loadData() { } return true; }) - .andThen([=](zeus::Future _fut) mutable { - APPL_INFO(" [" << tmpProperty->m_id << "] get mine-type: " << _fut.get()); + .andThen([=](const std::string& _value) mutable { + APPL_INFO(" [" << tmpProperty->m_id << "] get mine-type: " << _value); { std::unique_lock lock(tmpProperty->m_mutex); - tmpProperty->m_mineType = _fut.get(); + tmpProperty->m_mineType = _value; if (etk::start_with(tmpProperty->m_mineType, "video") == true) { // TODO : Optimise this ... tmpProperty->m_thumb = egami::load("DATA:Video.svg", ivec2(128,128)); diff --git a/zeus/AbstractFunction.hpp b/zeus/AbstractFunction.hpp index 7a2b957..427574b 100644 --- a/zeus/AbstractFunction.hpp +++ b/zeus/AbstractFunction.hpp @@ -6,9 +6,9 @@ #pragma once #include #include -#include #include #include +#include #include diff --git a/zeus/ActionNotification.hpp b/zeus/ActionNotification.hpp index 55686fd..37fc0ac 100644 --- a/zeus/ActionNotification.hpp +++ b/zeus/ActionNotification.hpp @@ -28,7 +28,7 @@ namespace zeus { void notify(const std::string& _value) { if (m_interface != nullptr) { - m_interface->answerValue(m_transactionId, m_source, m_destination, _value); + m_interface->progressNotify(m_transactionId, m_source, m_destination, _value); } } }; diff --git a/zeus/Future.hpp b/zeus/Future.hpp index be89cb0..360c152 100644 --- a/zeus/Future.hpp +++ b/zeus/Future.hpp @@ -85,6 +85,14 @@ namespace zeus { }); return *this; } + Future& andThen(std::function _callback) { + zeus::FutureBase::andThen( + [=](zeus::FutureBase _fut) { + zeus::Future tmp(_fut); + return _callback(tmp.get()); + }); + return *this; + } /** * @brief Attach callback on a specific return action (ERROR) * @param[in] _callback Handle on the function to call in case of error on the call @@ -96,6 +104,13 @@ namespace zeus { }); return *this; } + Future& andElse(std::function _callback) { + zeus::FutureBase::andElse( + [=](zeus::FutureBase _fut) { + return _callback(_fut.getErrorType(), _fut.getErrorHelp()); + }); + return *this; + } /** * @brief Attach callback on activity of the action if user set some return information * @param[in] _callback Handle on the function to call in progress information diff --git a/zeus/ObjectRemote.hpp b/zeus/ObjectRemote.hpp index feb4100..9804422 100644 --- a/zeus/ObjectRemote.hpp +++ b/zeus/ObjectRemote.hpp @@ -85,7 +85,7 @@ namespace zeus { */ class ObjectRemote { private: - ememory::SharedPtr m_interface; + mutable ememory::SharedPtr m_interface; public: /** * @brief @@ -113,7 +113,7 @@ namespace zeus { * @return A generic future with all datas */ template - zeus::FutureBase call(const std::string& _functionName, _ARGS&&... _args) { + zeus::FutureBase call(const std::string& _functionName, _ARGS&&... _args) const { if ( m_interface == nullptr || m_interface->m_interfaceWeb == nullptr) { ememory::SharedPtr ret = zeus::message::Answer::create(nullptr); // TODO : This is a real bad case ... @@ -133,7 +133,7 @@ namespace zeus { * @return */ template - zeus::FutureBase callAction(const std::string& _functionName, _ARGS&&... _args) { + zeus::FutureBase callAction(const std::string& _functionName, _ARGS&&... _args) const { zeus::FutureBase tmp = call(_functionName, _args...); tmp.setAction(); return tmp; diff --git a/zeus/zeus-File.impl.cpp b/zeus/zeus-File.impl.cpp index 9821073..3655ae2 100644 --- a/zeus/zeus-File.impl.cpp +++ b/zeus/zeus-File.impl.cpp @@ -120,10 +120,10 @@ zeus::Raw zeus::FileImpl::getPart(uint64_t _start, uint64_t _stop) { std::string zeus::storeInFile(zeus::ProxyFile _file, std::string _filename) { zeus::ActionNotification tmp; - return zeus::storeInFile(_file, _filename, tmp); + return zeus::storeInFileNotify(_file, _filename, tmp); } -std::string zeus::storeInFile(zeus::ProxyFile _file, std::string _filename, zeus::ActionNotification _notification) { +std::string zeus::storeInFileNotify(zeus::ProxyFile _file, std::string _filename, zeus::ActionNotification _notification) { auto futSize = _file.getSize(); auto futSha = _file.getSha512(); futSize.wait(); @@ -150,7 +150,7 @@ std::string zeus::storeInFile(zeus::ProxyFile _file, std::string _filename, zeus offset += nbElement; retSize -= nbElement; ZEUS_VERBOSE("read: " << offset << "/" << futSize.get() << " " << buffer.size()); - "{\"pourcent\":" + etk::to_string(float(offset)/float(buffer.size())) + ", \"comment\":\"download\"" + _notification.notify("{\"pourcent\":" + etk::to_string(float(offset)/float(buffer.size())) + ", \"comment\":\"download\""); } nodeFile.fileClose(); // get the final sha512 of the file: