From 9a3b17b82a1e5ecce56406c0be6af4d9e4b9d33f Mon Sep 17 00:00:00 2001 From: Patrick Ohly Date: Fri, 26 Apr 2013 02:13:50 -0700 Subject: engine: eliminate virtual checkForSuspend/Abort() Since the introduction of SuspendFlags, the ony remaining user of the virtual aspect of checkForSuspend/checkForAbort() was the testing code. By using the suspend/abort request mechanism in SuspendFlags, it becomes possible to move checkForSuspend/Abort() into SuspendFlags itself. This will be useful to use it outside of a SyncContext member. --- src/client-test-app.cpp | 7 ++----- src/syncevo/SuspendFlags.cpp | 23 +++++++++++++++++++++-- src/syncevo/SuspendFlags.h | 17 +++++++++++++++-- src/syncevo/SyncContext.cpp | 44 ++++++++++++++------------------------------ src/syncevo/SyncContext.h | 21 --------------------- test/ClientTest.cpp | 2 +- test/ClientTest.h | 8 +++----- 7 files changed, 56 insertions(+), 66 deletions(-) diff --git a/src/client-test-app.cpp b/src/client-test-app.cpp index f2607bbc..c0a13f1c 100644 --- a/src/client-test-app.cpp +++ b/src/client-test-app.cpp @@ -382,7 +382,7 @@ public: SyncContext::prepare(); if (m_options.m_prepareCallback && m_options.m_prepareCallback(*this, m_options)) { - m_options.m_isAborted = true; + m_options.m_isAborted = SuspendFlags::getSuspendFlags().abort(); } } @@ -392,14 +392,11 @@ public: if (!m_started) { m_started = true; if (m_options.m_startCallback(*this, m_options)) { - m_options.m_isAborted = true; + m_options.m_isAborted = SuspendFlags::getSuspendFlags().abort(); } } } - virtual bool checkForAbort() { return m_options.m_isAborted; } - virtual bool checkForSuspend() {return m_options.m_isSuspended;} - virtual boost::shared_ptr createTransportAgent() { boost::shared_ptrwrapper = m_options.m_transport; diff --git a/src/syncevo/SuspendFlags.cpp b/src/syncevo/SuspendFlags.cpp index 0f46bc49..b82484b0 100644 --- a/src/syncevo/SuspendFlags.cpp +++ b/src/syncevo/SuspendFlags.cpp @@ -115,9 +115,28 @@ SuspendFlags::State SuspendFlags::getState() const { } } -void SuspendFlags::checkForNormal() const +bool SuspendFlags::isAborted() { - if (getState() != SuspendFlags::NORMAL) { + printSignals(); + return getState() == ABORT; +} + +bool SuspendFlags::isSuspended() +{ + printSignals(); + return getState() == SUSPEND; +} + +bool SuspendFlags::isNormal() +{ + printSignals(); + return getState() == NORMAL; +} + +void SuspendFlags::checkForNormal() +{ + printSignals(); + if (getState() != NORMAL) { SE_THROW_EXCEPTION_STATUS(StatusException, "aborting as requested by user", (SyncMLStatus)sysync::LOCERR_USERABORT); diff --git a/src/syncevo/SuspendFlags.h b/src/syncevo/SuspendFlags.h index e6f9c07f..47b8e5ba 100644 --- a/src/syncevo/SuspendFlags.h +++ b/src/syncevo/SuspendFlags.h @@ -81,12 +81,25 @@ class SuspendFlags uint32_t getReceivedSignals() const { return m_receivedSignals; } /** - * Throws a "aborting as requested by user" StatusException with + * Checks for status changes and returns true iff status is ABORT. + */ + bool isAborted(); + /** + * Checks for status changes and returns true iff status is SUSPEND. + */ + bool isSuspended(); + /** + * Checks for status changes and returns true iff status is NORMAL. + */ + bool isNormal(); + + /** + * Checks for status changes and throws a "aborting as requested by user" StatusException with * LOCERR_USERABORT as status code if the current state is not * NORMAL. In other words, suspend and abort are both * treated like an abort request. */ - void checkForNormal() const; + void checkForNormal(); /** * Users of this class can read a single char for each received diff --git a/src/syncevo/SyncContext.cpp b/src/syncevo/SyncContext.cpp index b044b4a4..a0344fa3 100644 --- a/src/syncevo/SyncContext.cpp +++ b/src/syncevo/SyncContext.cpp @@ -1967,20 +1967,6 @@ void SyncContext::displaySourceProgress(sysync::TProgressEventEnum type, } } -bool SyncContext::checkForAbort() -{ - SuspendFlags &flags(SuspendFlags::getSuspendFlags()); - flags.printSignals(); - return flags.getState() == SuspendFlags::ABORT; -} - -bool SyncContext::checkForSuspend() -{ - SuspendFlags &flags(SuspendFlags::getSuspendFlags()); - flags.printSignals(); - return flags.getState() == SuspendFlags::SUSPEND; -} - void SyncContext::throwError(const string &error) { throwError(SyncMLStatus(STATUS_FATAL + sysync::LOCAL_STATUS_CODE), error); @@ -3502,8 +3488,8 @@ SyncMLStatus SyncContext::doSync() Sleep(atoi(delay)); } - if (checkForSuspend() || - checkForAbort()) { + SuspendFlags &flags = SuspendFlags::getSuspendFlags(); + if (!flags.isNormal()) { return (SyncMLStatus)sysync::LOCERR_USERABORT; } @@ -3536,8 +3522,7 @@ SyncMLStatus SyncContext::doSync() //by pass the exception if we will try again with legacy SANFormat } - if (checkForSuspend() || - checkForAbort()) { + if (!flags.isNormal()) { return (SyncMLStatus)sysync::LOCERR_USERABORT; } @@ -3556,8 +3541,7 @@ SyncMLStatus SyncContext::doSync() } } - if (checkForSuspend() || - checkForAbort()) { + if (!flags.isNormal()) { return (SyncMLStatus)sysync::LOCERR_USERABORT; } @@ -3712,7 +3696,7 @@ SyncMLStatus SyncContext::doSync() // GOTDATA state. // After exception occurs, stepCmd will be set to abort to force // aborting, must avoid to change it back to suspend cmd. - if (checkForSuspend() && stepCmd == sysync::STEPCMD_GOTDATA) { + if (flags.isSuspended() && stepCmd == sysync::STEPCMD_GOTDATA) { SE_LOG_DEBUG(NULL, "suspending before SessionStep() in STEPCMD_GOTDATA as requested by user"); stepCmd = sysync::STEPCMD_SUSPEND; } @@ -3730,7 +3714,7 @@ SyncMLStatus SyncContext::doSync() if ((stepCmd == sysync::STEPCMD_RESENDDATA || stepCmd == sysync::STEPCMD_SENTDATA || stepCmd == sysync::STEPCMD_NEEDDATA) && - checkForAbort()) { + flags.isAborted()) { SE_LOG_DEBUG(NULL, "aborting before SessionStep() in %s as requested by script", Step2String(stepCmd).c_str()); stepCmd = sysync::STEPCMD_ABORT; @@ -3806,7 +3790,7 @@ SyncMLStatus SyncContext::doSync() stepCmd = sysync::STEPCMD_ABORT; continue; } else if (stepCmd == sysync::STEPCMD_SENDDATA && - checkForAbort()) { + flags.isAborted()) { // Catch outgoing message and abort if requested by user. SE_LOG_DEBUG(NULL, "aborting after SessionStep() in STEPCMD_SENDDATA as requested by user"); stepCmd = sysync::STEPCMD_ABORT; @@ -3988,11 +3972,11 @@ SyncMLStatus SyncContext::doSync() case TransportAgent::FAILED: { // Send might have failed because of abort or // suspend request. - if (checkForSuspend()) { + if (flags.isSuspended()) { SE_LOG_DEBUG(NULL, "suspending after TransportAgent::FAILED as requested by user"); stepCmd = sysync::STEPCMD_SUSPEND; break; - } else if (checkForAbort()) { + } else if (flags.isAborted()) { SE_LOG_DEBUG(NULL, "aborting after TransportAgent::FAILED as requested by user"); stepCmd = sysync::STEPCMD_ABORT; break; @@ -4019,7 +4003,7 @@ SyncMLStatus SyncContext::doSync() // Resend after having ensured that the retryInterval is over. if (resendDelay > 0) { if (Sleep(resendDelay) > 0) { - if (checkForSuspend()) { + if (flags.isSuspended()) { SE_LOG_DEBUG(NULL, "suspending after premature exit from sleep() caused by user suspend"); stepCmd = sysync::STEPCMD_SUSPEND; } else { @@ -4038,11 +4022,11 @@ SyncMLStatus SyncContext::doSync() case TransportAgent::CANCELED: // Send might have failed because of abort or // suspend request. - if (checkForSuspend()) { + if (flags.isSuspended()) { SE_LOG_DEBUG(NULL, "suspending after TransportAgent::CANCELED as requested by user"); stepCmd = sysync::STEPCMD_SUSPEND; break; - } else if (checkForAbort()) { + } else if (flags.isAborted()) { SE_LOG_DEBUG(NULL, "aborting after TransportAgent::CANCELED as requested by user"); stepCmd = sysync::STEPCMD_ABORT; break; @@ -4094,11 +4078,11 @@ SyncMLStatus SyncContext::doSync() // If we get here without error, then close down connection normally. // Otherwise destruct the agent without further communication. - if (!status && !checkForAbort()) { + if (!status && !flags.isAborted()) { try { m_agent->shutdown(); // TODO: implement timeout for peers which fail to respond - while (!checkForAbort() && + while (!flags.isAborted() && m_agent->wait(true) == TransportAgent::ACTIVE) { // TODO: allow aborting the sync here } diff --git a/src/syncevo/SyncContext.h b/src/syncevo/SyncContext.h index a72e2e9c..4a4e1bfc 100644 --- a/src/syncevo/SyncContext.h +++ b/src/syncevo/SyncContext.h @@ -640,27 +640,6 @@ class SyncContext : public SyncConfig { */ virtual void reportStepCmd(sysync::uInt16 stepCmd) {} - /** - * Called to find out whether user wants to abort sync. - * - * Will be called regularly. Once it has flagged an abort, all - * following calls should return the same value. When the engine - * aborts, the sync is shut down as soon as possible. The next - * sync most likely has to be done in slow mode, so don't do this - * unless absolutely necessary. - * - * @return true if user wants to abort - */ - virtual bool checkForAbort(); - - /** - * Called to find out whether user wants to suspend sync. - * - * Same as checkForAbort(), but the session is finished - * gracefully so that it can be resumed. - */ - virtual bool checkForSuspend(); - private: /** initialize members as part of constructors */ void init(); diff --git a/test/ClientTest.cpp b/test/ClientTest.cpp index 305ea27d..e6e8c285 100644 --- a/test/ClientTest.cpp +++ b/test/ClientTest.cpp @@ -5329,7 +5329,7 @@ public: m_messageCount++; if (m_interruptAtMessage >= 0 && m_messageCount > m_interruptAtMessage) { - m_options->m_isSuspended = true; + m_options->m_isSuspended = SuspendFlags::getSuspendFlags().suspend(); } m_wrappedAgent->getReply(data, len, contentType); } diff --git a/test/ClientTest.h b/test/ClientTest.h index 7fa37a22..f4e3a75c 100644 --- a/test/ClientTest.h +++ b/test/ClientTest.h @@ -32,6 +32,7 @@ #include #include #include +#include #include "test.h" #include "ClientTestAssert.h" @@ -138,9 +139,8 @@ struct SyncOptions { int m_retryDuration; int m_retryInterval; - bool m_isSuspended; - - bool m_isAborted; + boost::shared_ptr m_isSuspended; + boost::shared_ptr m_isAborted; /** * Callback to be invoked after setting up local sources, but @@ -176,8 +176,6 @@ struct SyncOptions { m_isWBXML(isWBXML), m_retryDuration(300), m_retryInterval(60), - m_isSuspended(false), - m_isAborted(false), m_startCallback(startCallback), m_transport (transport) {} -- cgit v1.2.3