]> git.stg.codes - stg.git/blobdiff - projects/stargazer/users_impl.h
Start replacing notifiers with subscriptions.
[stg.git] / projects / stargazer / users_impl.h
index cd7d2932e86124376429ca78055221b5a3a599aa..b5a37063079c14d1af26cc545b5cdbbefe8faddc 100644 (file)
@@ -64,109 +64,101 @@ std::list<UserImpl>::iterator iter;
 time_t  delTime;
 };
 //-----------------------------------------------------------------------------
-class UsersImpl : public Users {
+class UsersImpl : public Users
+{
     friend class PROPERTY_NOTIFER_IP_BEFORE;
     friend class PROPERTY_NOTIFER_IP_AFTER;
 
-public:
-    using UserImplPtr = UserImpl*;
-
-    UsersImpl(SettingsImpl * s, Store * store,
-              Tariffs * tariffs, Services & svcs,
-              const Admin& sysAdmin);
-
-    int             FindByName(const std::string & login, UserPtr * user) override;
-    int             FindByName(const std::string & login, ConstUserPtr * user) const override;
-    bool            Exists(const std::string & login) const override;
-
-    bool            TariffInUse(const std::string & tariffName) const override;
+    public:
+        using UserImplPtr = UserImpl*;
 
-    void            AddNotifierUserAdd(NotifierBase<UserPtr> *) override;
-    void            DelNotifierUserAdd(NotifierBase<UserPtr> *) override;
+        UsersImpl(SettingsImpl * s, Store * store,
+                  Tariffs * tariffs, Services & svcs,
+                  const Admin& sysAdmin);
 
-    void            AddNotifierUserDel(NotifierBase<UserPtr> *) override;
-    void            DelNotifierUserDel(NotifierBase<UserPtr> *) override;
+        int             FindByName(const std::string & login, UserPtr * user) override;
+        int             FindByName(const std::string & login, ConstUserPtr * user) const override;
+        bool            Exists(const std::string & login) const override;
 
-    void            AddNotifierUserAdd(NotifierBase<UserImplPtr> *);
-    void            DelNotifierUserAdd(NotifierBase<UserImplPtr> *);
+        bool            TariffInUse(const std::string & tariffName) const override;
 
-    void            AddNotifierUserDel(NotifierBase<UserImplPtr> *);
-    void            DelNotifierUserDel(NotifierBase<UserImplPtr> *);
+        template <typename F>
+        auto            onUserImplAdd(F&& f) { return m_onAddImplCallbacks.add(std::forward<F>(f)); }
+        template <typename F>
+        auto            onUserImplDel(F&& f) { return m_onDelImplCallbacks.add(std::forward<F>(f)); }
 
-    int             Add(const std::string & login, const Admin * admin) override;
-    void            Del(const std::string & login, const Admin * admin) override;
+        int             Add(const std::string & login, const Admin * admin) override;
+        void            Del(const std::string & login, const Admin * admin) override;
 
-    bool            Authorize(const std::string & login, uint32_t ip,
-                              uint32_t enabledDirs, const Auth * auth) override;
-    bool            Unauthorize(const std::string & login,
-                                const Auth * auth,
-                                const std::string & reason) override;
+        bool            Authorize(const std::string & login, uint32_t ip,
+                                  uint32_t enabledDirs, const Auth * auth) override;
+        bool            Unauthorize(const std::string & login,
+                                    const Auth * auth,
+                                    const std::string & reason) override;
 
-    int             ReadUsers() override;
-    size_t          Count() const override { return users.size(); }
+        int             ReadUsers() override;
+        size_t          Count() const override { return users.size(); }
 
-    int             FindByIPIdx(uint32_t ip, UserPtr * user) const override;
-    int             FindByIPIdx(uint32_t ip, UserImpl ** user) const;
-    bool            IsIPInIndex(uint32_t ip) const override;
-    bool            IsIPInUse(uint32_t ip, const std::string & login, ConstUserPtr * user) const override;
+        int             FindByIPIdx(uint32_t ip, UserPtr * user) const override;
+        int             FindByIPIdx(uint32_t ip, UserImpl ** user) const;
+        bool            IsIPInIndex(uint32_t ip) const override;
+        bool            IsIPInUse(uint32_t ip, const std::string & login, ConstUserPtr * user) const override;
 
-    unsigned int    OpenSearch() override;
-    int             SearchNext(int handler, UserPtr * user) override;
-    int             SearchNext(int handler, UserImpl ** user);
-    int             CloseSearch(int handler) override;
+        unsigned int    OpenSearch() override;
+        int             SearchNext(int handler, UserPtr * user) override;
+        int             SearchNext(int handler, UserImpl ** user);
+        int             CloseSearch(int handler) override;
 
-    int             Start() override;
-    int             Stop() override;
+        int             Start() override;
+        int             Stop() override;
 
-private:
-    UsersImpl(const UsersImpl & rvalue);
-    UsersImpl & operator=(const UsersImpl & rvalue);
+    private:
+        UsersImpl(const UsersImpl & rvalue);
+        UsersImpl & operator=(const UsersImpl & rvalue);
 
-    void            AddToIPIdx(user_iter user);
-    void            DelFromIPIdx(uint32_t ip);
-    bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
+        void            AddToIPIdx(user_iter user);
+        void            DelFromIPIdx(uint32_t ip);
+        bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
 
-    bool            FindByNameNonLock(const std::string & login, user_iter * user);
-    bool            FindByNameNonLock(const std::string & login, const_user_iter * user) const;
+        bool            FindByNameNonLock(const std::string & login, user_iter * user);
+        bool            FindByNameNonLock(const std::string & login, const_user_iter * user) const;
 
-    void            RealDelUser();
-    void            ProcessActions();
+        void            RealDelUser();
+        void            ProcessActions();
 
-    void            AddUserIntoIndexes(user_iter user);
-    void            DelUserFromIndexes(user_iter user);
+        void            AddUserIntoIndexes(user_iter user);
+        void            DelUserFromIndexes(user_iter user);
 
-    void            Run(std::stop_token token);
-    void            NewMinute(const struct tm & t);
-    void            NewDay(const struct tm & t);
-    void            DayResetTraff(const struct tm & t);
+        void            Run(std::stop_token token);
+        void            NewMinute(const struct tm & t);
+        void            NewDay(const struct tm & t);
+        void            DayResetTraff(const struct tm & t);
 
-    bool            TimeToWriteDetailStat(const struct tm & t);
+        bool            TimeToWriteDetailStat(const struct tm & t);
 
-    std::list<UserImpl>                  users;
-    std::list<USER_TO_DEL>                usersToDelete;
+        std::list<UserImpl>                  users;
+        std::list<USER_TO_DEL>                usersToDelete;
 
-    std::map<uint32_t, user_iter>         ipIndex;
-    std::map<std::string, user_iter>      loginIndex;
+        std::map<uint32_t, user_iter>         ipIndex;
+        std::map<std::string, user_iter>      loginIndex;
 
-    SettingsImpl *     settings;
-    Tariffs *           m_tariffs;
-    Services &          m_services;
-    Store *             m_store;
-    const Admin&        m_sysAdmin;
-    Logger &        WriteServLog;
+        SettingsImpl *     settings;
+        Tariffs *           m_tariffs;
+        Services &          m_services;
+        Store *             m_store;
+        const Admin&        m_sysAdmin;
+        Logger &        WriteServLog;
 
-    bool                isRunning;
+        bool                isRunning;
 
-    mutable std::mutex      m_mutex;
-    std::jthread            m_thread;
-    mutable unsigned int    handle;
+        mutable std::mutex      m_mutex;
+        std::jthread            m_thread;
+        mutable unsigned int    handle;
 
-    mutable std::map<unsigned int, user_iter>  searchDescriptors;
+        mutable std::map<unsigned int, user_iter>  searchDescriptors;
 
-    std::set<NotifierBase<UserPtr>*> onAddNotifiers;
-    std::set<NotifierBase<UserPtr>*> onDelNotifiers;
-    std::set<NotifierBase<UserImplPtr>*> onAddNotifiersImpl;
-    std::set<NotifierBase<UserImplPtr>*> onDelNotifiersImpl;
+        Subscriptions<UserImplPtr> m_onAddImplCallbacks;
+        Subscriptions<UserImplPtr> m_onDelImplCallbacks;
 };
 
 }