Startup/Shutdown sequence

To implement startup and shutdown sequence, a recommended practice would be to implement a context class to hold the SCA instance (dca::DcaInstance) and other SCA objects. An instance of the context could then be passed to a startup function.

class MyDcaContext
{
public:

        MyDcaContext() : inShutDown (false)
        { }

        virtual ~MyDcaContext()
        {
                // signal DCA worker threads to shutdown
                inShutDown = true;

                 // this cancels the download or update task if it is currently running
                updateModule.cancelUpdate();

                // this cancels all long-lasting functions and forces them to return immediately
                // such as database merge processes...
                dcaInstance.signal( DCA_SIG_ABORT );

                // ...
                // wait for worker threads to finish
                // ...

                // all other DCA objects are destructed in reverse construction order
        }

        // initialize the DCA objects
        virtual bool Init();

        // Construction order is important!
        // the DCA instance must always be created first.
        dca::DcaInstance dcaInstance;
        dca::InitData initData;
        dca::License license;
        dca::UpdateModule updateModule;
        dca::DbConnectionData connectData;
        dca::DbConnection dbConnection;
        dca::UrlClassification urlClassification;
        dca::UrlDbClassifier urlDbClassifier;

        volatile bool inShutDown;
        ...
};

The startup function should first create the dca::DcaInstance object of the context class, and then the other SCA objects.

// creates all instances in a meaningful order
bool MyDcaContext::Init()
{
        try
        {
                ...
                dcaInstance = dca::DcaInstance::create( initData );
                license = dcaInstance.createLicense( licenseData );
                ...
                dbConnection = dcaInstance.createDbConnection ( connectData );
                urlClassification = dca::UrlClassification::create( license );
                UrlDbClassifier = urlClassification.createUrlDbClassifier( dbConnection );
                ...

                return true;
        }
        catch( const dca::ExDca& ex )
        {
                return false;
        }
        catch(...)
        {
                return false;
        }
}

Call startup in the main() function in the identical scope:

int main()
{
        try
        {
                ...
                MyDcaContext myContext;

                // start up
                myContext.Init();

                // run application
                DoWork( myContext );

                // MyContext automatically shutdown
                // The DCA instances will be destroyed in the
                // reverse order of construction.

                return 0;
        }
        catch( const dca::ExDca& ex )
        {
                ...
        }
        catch(...)
        {
                ...
        }

        // error occurred
        return 10;

}

Generated on 26 Sep 2016 for dca_interface by  doxygen 1.6.1