1/*
2 * Copyright (c) 2011-2015, Intel Corporation
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation and/or
13 * other materials provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors
16 * may be used to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30#pragma once
31
32#include "ConfigurableElement.h"
33#include "SubsystemPlugins.h"
34#include "Results.h"
35#include <log/Logger.h>
36#include <list>
37#include <string>
38#include <memory>
39
40class CSubsystemLibrary;
41class DynamicLibrary;
42
43class CSystemClass final : public CConfigurableElement
44{
45public:
46    /**
47     * @param[in] logger the logger provided by the client
48     * it need to be given to the subsystem library
49     */
50    CSystemClass(core::log::Logger &logger);
51    virtual ~CSystemClass();
52
53    /** Load subsystem plugin and fill the corresponding libraries.
54     *
55     * @param[out] strError is filled with new line separated errors if the function returns false,
56     *                         undefined otherwise.
57     * @param[in] pSubsystemPlugins The plugins to load.
58     * @param[in] bVirtualSubsystemFallback If a subsystem can not be found, use the virtual one.
59     *
60     * @return true if the plugins succesfully started or that a fallback is available,
61               false otherwise.
62     */
63    bool loadSubsystems(std::string &strError, const CSubsystemPlugins *pSubsystemPlugins,
64                        bool bVirtualSubsystemFallback = false);
65    // Subsystem factory
66    const CSubsystemLibrary *getSubsystemLibrary() const;
67
68    /**
69      * Look for subsystems that need to be resynchronized.
70      * Consume the need to be resynchronized
71      * and fill a syncer set with all syncers that need to be resynchronized
72      *
73      * @param[out] syncerSet The syncer set to fill
74      * @param[out] infos Relevant informations client may want to log
75      */
76    void checkForSubsystemsToResync(CSyncerSet &syncerSet, core::Results &infos);
77
78    /**
79      * Reset subsystems need to resync flag.
80      */
81    void cleanSubsystemsNeedToResync();
82
83    // base
84    virtual std::string getKind() const;
85
86    bool getMappingData(const std::string &strKey, const std::string *&pStrValue) const override;
87    std::string getFormattedMapping() const override;
88
89private:
90    CSystemClass(const CSystemClass &);
91    CSystemClass &operator=(const CSystemClass &);
92    // base
93    virtual bool childrenAreDynamic() const;
94
95    /** Load shared libraries subsystem plugins.
96     *
97     * @param[out] errors is the list of error that occured during loadings.
98     * @param[in] pSubsystemPlugins The plugins to load.
99     *
100     * @return true if all plugins have been succesfully loaded, false otherwises.
101     */
102    bool loadSubsystemsFromSharedLibraries(core::Results &errors,
103                                           const CSubsystemPlugins *pSubsystemPlugins);
104
105    /** Load subsystem plugin shared libraries.
106     *
107     * @param[in,out] lstrPluginFiles is the path list of the plugins shared libraries to load.
108     *                Successfully loaded plugins are removed from the list.
109     * @param[out] errors is the list of error that occured during loadings.
110     *
111     * @return true if at least one plugin has been succesfully loaded, false otherwise.
112     *         When false is returned, some plugins MIHGT have been loaded
113     *         but the lstrPluginFiles is accurate.
114     */
115    bool loadPlugins(std::list<std::string> &lstrPluginFiles, core::Results &errors);
116
117    // Subsystem factory
118    CSubsystemLibrary *_pSubsystemLibrary;
119    std::list<std::unique_ptr<DynamicLibrary>>
120        _subsystemLibraryHandleList; /**< Contains the list of all open plugin libs. */
121
122    /** Application Logger we need to provide to plugins */
123    core::log::Logger &_logger;
124
125    /** The entry point symbol that must be implemented by plugins
126     */
127    static const char entryPointSymbol[];
128};
129