watcher_manager.h 5.6 KB
Newer Older
S
sun_fan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef WATCHER_MANAGER_H_
#define WATCHER_MANAGER_H_
#include <atomic>
#include <iostream>
#include <map>
#include <mutex>
#include <thread>
#include <vector>

#include "iremote_stub.h"
#include "iwatcher.h"
X
add ut  
xionglei6 已提交
26
#include "list.h"
S
sun_fan 已提交
27
#include "message_parcel.h"
S
sun_fan 已提交
28
#include "param_utils.h"
S
sun_fan 已提交
29 30 31 32 33 34 35 36 37 38 39
#include "parcel.h"
#include "system_ability.h"
#include "watcher_manager_stub.h"

namespace OHOS {
namespace init_param {
class WatcherManager : public SystemAbility, public WatcherManagerStub {
public:
    DECLARE_SYSTEM_ABILITY(WatcherManager);
    DISALLOW_COPY_AND_MOVE(WatcherManager);
    explicit WatcherManager(int32_t systemAbilityId, bool runOnCreate = true)
X
add ut  
xionglei6 已提交
40
        : SystemAbility(systemAbilityId, runOnCreate)
S
sun_fan 已提交
41
    {
X
add ut  
xionglei6 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    }
    ~WatcherManager() override;

    class ParamWatcher;
    class WatcherGroup;
    using ParamWatcherPtr = std::shared_ptr<WatcherManager::ParamWatcher>;
    using WatcherGroupPtr = std::shared_ptr<WatcherManager::WatcherGroup>;

    // For death event procession
    class DeathRecipient final : public IRemoteObject::DeathRecipient {
    public:
        explicit DeathRecipient(WatcherManager *manager) : manager_(manager) {}
        ~DeathRecipient() final = default;
        DISALLOW_COPY_AND_MOVE(DeathRecipient);
        void OnRemoteDied(const wptr<IRemoteObject> &remote) final;
    private:
        WatcherManager *manager_;
S
sun_fan 已提交
59
    };
X
add ut  
xionglei6 已提交
60 61 62 63
    sptr<IRemoteObject::DeathRecipient> GetDeathRecipient()
    {
        return deathRecipient_;
    }
S
sun_fan 已提交
64 65 66

    class ParamWatcher {
    public:
X
add ut  
xionglei6 已提交
67 68 69 70 71
        ParamWatcher(uint32_t watcherId, const sptr<IWatcher> &watcher, const WatcherGroupPtr &group)
            : watcherId_(watcherId), watcher_(watcher), group_(group)
        {
            ListInit(&groupNode_);
        }
S
sun_fan 已提交
72
        ~ParamWatcher() = default;
X
add ut  
xionglei6 已提交
73

S
sun_fan 已提交
74 75 76 77
        uint32_t GetWatcherId()
        {
            return watcherId_;
        }
X
add ut  
xionglei6 已提交
78 79 80 81 82 83 84 85 86 87 88 89
        WatcherGroupPtr GetWatcherGroup()
        {
            return group_;
        }
        ListNode *GetGroupNode()
        {
            return &groupNode_;
        }
        sptr<IWatcher> GetRemoteWatcher()
        {
            return watcher_;
        }
S
sun_fan 已提交
90 91
        void ProcessParameterChange(const std::string &name, const std::string &value)
        {
S
sun_fan 已提交
92
#ifndef STARTUP_INIT_TEST
S
sun_fan 已提交
93
            watcher_->OnParamerterChange(name, value);
S
sun_fan 已提交
94
#endif
S
sun_fan 已提交
95 96
        }
    private:
X
xionglei6 已提交
97
        ListNode groupNode_;
S
sun_fan 已提交
98 99
        uint32_t watcherId_ = { 0 };
        sptr<IWatcher> watcher_;
X
add ut  
xionglei6 已提交
100
        WatcherGroupPtr group_ { nullptr };
S
sun_fan 已提交
101 102 103 104
    };

    class WatcherGroup {
    public:
X
add ut  
xionglei6 已提交
105 106 107 108
        WatcherGroup(uint32_t groupId, const std::string &key) : groupId_(groupId), keyPrefix_(key)
        {
            ListInit(&watchers_);
        }
S
sun_fan 已提交
109 110 111 112
        ~WatcherGroup() = default;
        void AddWatcher(const ParamWatcherPtr &watcher);
        void ProcessParameterChange(const std::string &name, const std::string &value);

X
xionglei6 已提交
113
        const std::string GetKeyPrefix() const
S
sun_fan 已提交
114 115 116
        {
            return keyPrefix_;
        }
X
xionglei6 已提交
117
        bool Emptry() const
X
xionglei6@huawei.com 已提交
118
        {
X
add ut  
xionglei6 已提交
119
            return ListEmpty(watchers_);
S
sun_fan 已提交
120
        }
X
xionglei6 已提交
121
        uint32_t GetGroupId() const
S
sun_fan 已提交
122 123 124
        {
            return groupId_;
        }
X
add ut  
xionglei6 已提交
125 126 127 128
        ListNode *GetWatchers()
        {
            return &watchers_;
        }
S
sun_fan 已提交
129 130 131
    private:
        uint32_t groupId_;
        std::string keyPrefix_ { };
X
add ut  
xionglei6 已提交
132
        ListNode watchers_;
S
sun_fan 已提交
133 134 135 136
    };

    uint32_t AddWatcher(const std::string &keyPrefix, const sptr<IWatcher> &watcher) override;
    int32_t DelWatcher(const std::string &keyPrefix, uint32_t watcherId) override;
X
add ut  
xionglei6 已提交
137 138 139 140
    int32_t DelWatcher(WatcherGroupPtr group, ParamWatcherPtr watcher);
    ParamWatcherPtr GetWatcher(uint32_t watcherId);
    ParamWatcherPtr GetWatcher(const wptr<IRemoteObject> &remote);

S
sun_fan 已提交
141
#ifndef STARTUP_INIT_TEST
S
sun_fan 已提交
142
protected:
S
sun_fan 已提交
143
#endif
S
sun_fan 已提交
144 145
    void OnStart() override;
    void OnStop() override;
S
sun_fan 已提交
146
#ifndef STARTUP_INIT_TEST
S
sun_fan 已提交
147
private:
S
sun_fan 已提交
148 149
#endif
    void ProcessWatcherMessage(const std::vector<char> &buffer, uint32_t dataSize);
S
sun_fan 已提交
150 151 152
    WatcherGroupPtr GetWatcherGroup(uint32_t groupId);
    WatcherGroupPtr GetWatcherGroup(const std::string &keyPrefix);
    void DelWatcherGroup(WatcherGroupPtr group);
X
add ut  
xionglei6 已提交
153 154
    void AddParamWatcher(const std::string &keyPrefix, WatcherGroupPtr group, ParamWatcherPtr watcher);
    void DelParamWatcher(ParamWatcherPtr watcher);
S
sun_fan 已提交
155 156
    void RunLoop();
    void StartLoop();
S
sun_fan 已提交
157
    void StopLoop();
S
sun_fan 已提交
158 159 160
    void SendLocalChange(const std::string &keyPrefix, ParamWatcherPtr watcher);
    int SendMessage(WatcherGroupPtr group, int type);
    int GetServerFd(bool retry);
X
add ut  
xionglei6 已提交
161 162
    int GetWatcherId(uint32_t &watcherId);
    int GetGroupId(uint32_t &groupId);
S
sun_fan 已提交
163 164 165 166 167 168 169
private:
    std::atomic<uint32_t> watcherId_ { 0 };
    std::atomic<uint32_t> groupId_ { 0 };
    std::mutex mutex_;
    std::mutex watcherMutex_;
    int serverFd_ { -1 };
    std::thread *pRecvThread_ { nullptr };
170
    std::atomic<bool> stop_ { false };
S
sun_fan 已提交
171 172
    std::map<std::string, uint32_t> groupMap_ {};
    std::map<uint32_t, WatcherGroupPtr> watcherGroups_ {};
X
add ut  
xionglei6 已提交
173 174
    std::map<uint32_t, ParamWatcherPtr> watchers_ {};
    sptr<IRemoteObject::DeathRecipient> deathRecipient_ {};
S
sun_fan 已提交
175 176 177 178
};
} // namespace init_param
} // namespace OHOS
#endif // WATCHER_MANAGER_H_