From 5e6cbc28da3e3cee92d2a008096d6916794590c2 Mon Sep 17 00:00:00 2001 From: Dave Miller Date: Mon, 23 Oct 2017 14:25:05 -0600 Subject: [PATCH] Update import/export example logic Prior to this commit, LogSessionImport and LogSessionExport examples were reporting errors. This exposed an existing bug in polysync-core-cpp-api. This commit adds some improvements to the logic, including resource management for the _importer and _exporter member variables. Following the bugfix (CORE-653) merge, this commit will enable proper functionality for LogSessionImport and LogSessionExport examples. --- LogSessionExport/LogSessionExport.cpp | 21 +++++++++++++++------ LogSessionExport/LogSessionExport.hpp | 4 +++- LogSessionExport/main.cpp | 2 +- LogSessionImport/LogSessionImport.cpp | 11 +++++++++-- LogSessionImport/LogSessionImport.hpp | 3 ++- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/LogSessionExport/LogSessionExport.cpp b/LogSessionExport/LogSessionExport.cpp index e8008cd..e0d1940 100644 --- a/LogSessionExport/LogSessionExport.cpp +++ b/LogSessionExport/LogSessionExport.cpp @@ -32,12 +32,13 @@ SessionExportExample::SessionExportExample( - int sessionId, + ps_rnr_session_id sessionId, const std::string & sessionPath ) : _sessionId( sessionId ), _sessionPath( sessionPath ), - _exporter() + _exporter(), + _transferComplete( false ) { // Subscribe to ApplicationEventMessage to determine when // the application connects to the PolySync bus. @@ -56,7 +57,9 @@ void SessionExportExample::handleEvent( if( auto event = polysync::datamodel::getSubclass< polysync::ApplicationEventMessage >( message ) ) { - if( event->getEventKind() == polysync::EventKind::Init ) + auto eventKind = event->getEventKind(); + + if( eventKind == polysync::EventKind::Init ) { // This is the actual usage of the export API. _exporter = std::unique_ptr< polysync::LogSessionExport >{ @@ -70,6 +73,11 @@ void SessionExportExample::handleEvent( this, &SessionExportExample::handleTransferStatus ); } + else if( eventKind == polysync::EventKind::Ok and _transferComplete ) + { + _exporter.reset(); + polysync::Application::getInstance()->disconnectPolySync(); + } } } @@ -87,6 +95,7 @@ void SessionExportExample::handleTransferStatus( break; case polysync::LogSessionTransferState::Error: std::cout << "Error" << std::endl; + polysync::Application::getInstance()->disconnectPolySync(); break; case polysync::LogSessionTransferState::Initial: std::cout << "Initial" << std::endl; @@ -104,11 +113,11 @@ void SessionExportExample::handleTransferStatus( std::cout << "TransferringLogfiles" << std::endl; break; case polysync::LogSessionTransferState::Complete: - std::cout << "Complete" << std::endl; - polysync::Application::getInstance()->disconnectPolySync(); + std::cout << "Complete" << std::endl; + _transferComplete = true; break; default: std::cout << "Unknown polysync::LogSessionTransferState" - << std::endl; + << std::endl; } } diff --git a/LogSessionExport/LogSessionExport.hpp b/LogSessionExport/LogSessionExport.hpp index f43ea93..cd761cb 100644 --- a/LogSessionExport/LogSessionExport.hpp +++ b/LogSessionExport/LogSessionExport.hpp @@ -46,7 +46,8 @@ class SessionExportExample : public polysync::DataSubscriber public: SessionExportExample( - int sessionId, const std::string & sessionPath = {} ); + ps_rnr_session_id sessionId, + const std::string & sessionPath = {} ); private: @@ -62,6 +63,7 @@ class SessionExportExample : public polysync::DataSubscriber std::unique_ptr< polysync::LogSessionExport > _exporter; + bool _transferComplete; }; diff --git a/LogSessionExport/main.cpp b/LogSessionExport/main.cpp index c4cdda6..5e543c6 100644 --- a/LogSessionExport/main.cpp +++ b/LogSessionExport/main.cpp @@ -37,7 +37,7 @@ int main( int argc, char * argv[] ) return -1; } - auto sessionId = std::atoi( argv[1] ); + auto sessionId = std::stoull( argv[1] ); std::unique_ptr< SessionExportExample > exportExample; diff --git a/LogSessionImport/LogSessionImport.cpp b/LogSessionImport/LogSessionImport.cpp index c02079a..8677a14 100644 --- a/LogSessionImport/LogSessionImport.cpp +++ b/LogSessionImport/LogSessionImport.cpp @@ -31,7 +31,8 @@ SessionImportExample::SessionImportExample( const std::string & sessionPath ) : _sessionPath( sessionPath ), - _importer() + _importer(), + _transferComplete( false ) { // Subscribe to ApplicationEventMessage to determine when // the application connects to the PolySync bus. @@ -90,6 +91,11 @@ void SessionImportExample::handleEvent( this, &SessionImportExample::handleTransferStatus ); } + else if( eventKind == polysync::EventKind::Ok && _transferComplete ) + { + _importer.reset(); + polysync::Application::getInstance()->disconnectPolySync(); + } } } @@ -107,6 +113,7 @@ void SessionImportExample::handleTransferStatus( break; case polysync::LogSessionTransferState::Error : std::cout << "Error" << std::endl; + polysync::Application::getInstance()->disconnectPolySync(); break; case polysync::LogSessionTransferState::Initial : std::cout << "Initial" << std::endl; @@ -125,7 +132,7 @@ void SessionImportExample::handleTransferStatus( break; case polysync::LogSessionTransferState::Complete : std::cout << "Complete" << std::endl; - polysync::Application::getInstance()->disconnectPolySync(); + _transferComplete = true; break; default: std::cout << "Unknown" << std::endl; diff --git a/LogSessionImport/LogSessionImport.hpp b/LogSessionImport/LogSessionImport.hpp index 487b195..6ffb553 100644 --- a/LogSessionImport/LogSessionImport.hpp +++ b/LogSessionImport/LogSessionImport.hpp @@ -63,7 +63,8 @@ class SessionImportExample : public polysync::DataSubscriber const std::string _sessionPath; std::unique_ptr< polysync::LogSessionImport > _importer; - + + bool _transferComplete; };