class Subscriptions
{
public:
- using Callback = std::function<void (Ts...)>;
+ using Callback = std::function<void (const Ts&...)>;
using Callbacks = std::list<Callback>;
Connection makeConn(typename Callbacks::iterator i) noexcept
template <typename C, typename... T2s>
Connection add(C& c, void (C::*m)(T2s...))
{
- return add([&c, m](Ts&&... values){ (c.*m)(std::forward<Ts>(values)...); });
+ return add([&c, m](const Ts&... values){ (c.*m)(values...); });
}
void remove(typename Callbacks::iterator i)
m_callbacks.erase(i);
}
- void notify(Ts&&... values)
+ void notify(const Ts&... values)
{
std::lock_guard lock(m_mutex);
for (auto& cb : m_callbacks)
#pragma once
+#include "subscriptions.h"
+
#include <string>
#include <vector>
-#include "notifer.h"
-
namespace STG
{
struct Tariff;
struct TariffData;
-struct Tariffs {
- virtual ~Tariffs() = default;
+class Tariffs
+{
+ public:
+ virtual ~Tariffs() = default;
- virtual int ReadTariffs() = 0;
- virtual const Tariff* FindByName(const std::string& name) const = 0;
- virtual const Tariff* GetNoTariff() const = 0;
- virtual int Del(const std::string& name, const Admin* admin) = 0;
- virtual int Add(const std::string& name, const Admin* admin) = 0;
- virtual int Chg(const TariffData& td, const Admin* admin) = 0;
+ virtual int ReadTariffs() = 0;
+ virtual const Tariff* FindByName(const std::string& name) const = 0;
+ virtual const Tariff* GetNoTariff() const = 0;
+ virtual int Del(const std::string& name, const Admin* admin) = 0;
+ virtual int Add(const std::string& name, const Admin* admin) = 0;
+ virtual int Chg(const TariffData& td, const Admin* admin) = 0;
- virtual void AddNotifierAdd(NotifierBase<TariffData>* notifier) = 0;
- virtual void DelNotifierAdd(NotifierBase<TariffData>* notifier) = 0;
+ template <typename F>
+ auto onAdd(F&& f) { return m_onAddCallbacks.add(std::forward<F>(f)); }
+ template <typename F>
+ auto onDel(F&& f) { return m_onDelCallbacks.add(std::forward<F>(f)); }
- virtual void AddNotifierDel(NotifierBase<TariffData>* notifier) = 0;
- virtual void DelNotifierDel(NotifierBase<TariffData>* notifier) = 0;
+ virtual void GetTariffsData(std::vector<TariffData>* tdl) const = 0;
- virtual void GetTariffsData(std::vector<TariffData>* tdl) const = 0;
+ virtual size_t Count() const = 0;
- virtual size_t Count() const = 0;
+ virtual const std::string& GetStrError() const = 0;
- virtual const std::string& GetStrError() const = 0;
+ protected:
+ Subscriptions<TariffData> m_onAddCallbacks;
+ Subscriptions<TariffData> m_onDelCallbacks;
};
}
#include <string>
+#include <cstdint>
+
namespace STG
{
virtual bool TariffInUse(const std::string& tariffName) const = 0;
template <typename F>
- auto onUserAdd(F&& f) { return m_onAddCallbacks.add(std::forward<F>(f)); }
+ auto onAdd(F&& f) { return m_onAddCallbacks.add(std::forward<F>(f)); }
template <typename F>
- auto onUserDel(F&& f) { return m_onDelCallbacks.add(std::forward<F>(f)); }
+ auto onDel(F&& f) { return m_onDelCallbacks.add(std::forward<F>(f)); }
virtual int Add(const std::string& login, const Admin* admin) = 0;
virtual void Del(const std::string& login, const Admin* admin) = 0;
printfd(__FILE__, "AUTH_AO::Start()\n");
GetUsers();
-m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); });
-m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); });
+m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
+m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); });
//-----------------------------------------------------------------------------
int AUTH_IA::Start()
{
-m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); });
+m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
if (PrepareNet())
return -1;
{
GetUsers();
-m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); });
-m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); });
+m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
+m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
pinger.SetDelayTime(pingSettings.GetPingDelay());
pinger.Start();
InitEncrypt(rsSettings.GetPassword());
-m_onAddUserConn = users->onUserAdd([this](auto user){ AddUser(user); });
-m_onDelUserConn = users->onUserDel([this](auto user){ DelUser(user); });
+m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
+m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
if (GetUsers())
return -1;
lastReconnectTry(0),
reconnectTimeout(1),
sock(-1),
- addDelTariffNotifier(*this),
logger(STG::PluginLogger::get("smux"))
{
smuxHandlers[SMUX_PDUs_PR_close] = &SMUX::CloseHandler;
users->CloseSearch(h);
-m_onAddUserConn = users->onUserAdd([this](auto user){
+m_onAddUserConn = users->onAdd([this](auto user){
SetNotifier(user);
UpdateTables();
});
-m_onDelUserConn = users->onUserDel([this](auto user){
+m_onDelUserConn = users->onDel([this](auto user){
UnsetNotifier(user);
UpdateTables();
});
-tariffs->AddNotifierAdd(&addDelTariffNotifier);
-tariffs->AddNotifierDel(&addDelTariffNotifier);
+auto updateTables = [this](const STG::TariffData&){ UpdateTables(); };
+m_onAddTariffConn = tariffs->onAdd(updateTables);
+m_onDelTariffConn = tariffs->onDel(updateTables);
}
void SMUX::ResetNotifiers()
{
-tariffs->DelNotifierDel(&addDelTariffNotifier);
-tariffs->DelNotifierAdd(&addDelTariffNotifier);
+m_onAddTariffConn.disconnect();
+m_onDelTariffConn.disconnect();
m_onAddUserConn.disconnect();
m_onDelUserConn.disconnect();
UserPtr userPtr;
};
//-----------------------------------------------------------------------------
-class ADD_DEL_TARIFF_NOTIFIER : public STG::NotifierBase<STG::TariffData> {
-public:
- explicit ADD_DEL_TARIFF_NOTIFIER(SMUX & s)
- : STG::NotifierBase<STG::TariffData>(), smux(s) {}
- void notify(const STG::TariffData &) override;
-
-private:
- SMUX & smux;
-};
-//-----------------------------------------------------------------------------
class SMUX : public STG::Plugin {
public:
SMUX();
STG::ScopedConnection m_onAddUserConn;
STG::ScopedConnection m_onDelUserConn;
+ STG::ScopedConnection m_onAddTariffConn;
+ STG::ScopedConnection m_onDelTariffConn;
std::list<CHG_AFTER_NOTIFIER> notifiers;
- ADD_DEL_TARIFF_NOTIFIER addDelTariffNotifier;
STG::PluginLogger logger;
};
{
smux.UpdateTables();
}
-
-inline
-void ADD_DEL_TARIFF_NOTIFIER::notify(const STG::TariffData &)
-{
-smux.UpdateTables();
-}
tariffs.erase(ti);
}
-auto ni = onDelNotifiers.begin();
-while (ni != onDelNotifiers.end())
- {
- (*ni)->notify(td);
- ++ni;
- }
+m_onDelCallbacks.notify(td);
WriteServLog("%s Tariff \'%s\' deleted.",
admin->logStr().c_str(),
return -1;
}
-// Fire all "on add" notifiers
-auto ni = onAddNotifiers.begin();
-while (ni != onAddNotifiers.end())
- {
- (*ni)->notify(tariffs.back().GetTariffData());
- ++ni;
- }
+m_onAddCallbacks.notify(tariffs.back().GetTariffData());
WriteServLog("%s Tariff \'%s\' added.",
admin->logStr().c_str(), name.c_str());
}
}
//-----------------------------------------------------------------------------
-void TariffsImpl::AddNotifierAdd(NotifierBase<TariffData> * n)
-{
-std::lock_guard<std::mutex> lock(m_mutex);
-onAddNotifiers.insert(n);
-}
-//-----------------------------------------------------------------------------
-void TariffsImpl::DelNotifierAdd(NotifierBase<TariffData> * n)
-{
-std::lock_guard<std::mutex> lock(m_mutex);
-onAddNotifiers.erase(n);
-}
-//-----------------------------------------------------------------------------
-void TariffsImpl::AddNotifierDel(NotifierBase<TariffData> * n)
-{
-std::lock_guard<std::mutex> lock(m_mutex);
-onDelNotifiers.insert(n);
-}
-//-----------------------------------------------------------------------------
-void TariffsImpl::DelNotifierDel(NotifierBase<TariffData> * n)
-{
-std::lock_guard<std::mutex> lock(m_mutex);
-onDelNotifiers.erase(n);
-}
-//-----------------------------------------------------------------------------
int Add(const std::string & name, const Admin * admin) override;
int Chg(const TariffData & td, const Admin * admin) override;
- void AddNotifierAdd(NotifierBase<TariffData> * notifier) override;
- void DelNotifierAdd(NotifierBase<TariffData> * notifier) override;
-
- void AddNotifierDel(NotifierBase<TariffData> * notifier) override;
- void DelNotifierDel(NotifierBase<TariffData> * notifier) override;
-
void GetTariffsData(std::vector<TariffData> * tdl) const override;
const std::string & GetStrError() const override { return strError; }
mutable std::mutex m_mutex;
std::string strError;
TariffImpl noTariff;
-
- std::set<NotifierBase<TariffData>*> onAddNotifiers;
- std::set<NotifierBase<TariffData>*> onDelNotifiers;
};
}
dirName[DIR_NUM] = "NULL";
-m_onAddUserConn = users->onUserImplAdd([this](auto user){
+m_onAddUserConn = users->onImplAdd([this](auto user){
AsyncPoolST::enqueue([this, user](){ SetUserNotifiers(user); });
});
-m_onDelUserConn = users->onUserImplDel([this](auto user){
+m_onDelUserConn = users->onImplDel([this](auto user){
AsyncPoolST::enqueue([this, user](){ UnSetUserNotifiers(user); });
AsyncPoolST::enqueue([this, user](){ DelUser(user->GetCurrIP()); });
});
bool TariffInUse(const std::string & tariffName) const override;
template <typename F>
- auto onUserImplAdd(F&& f) { return m_onAddImplCallbacks.add(std::forward<F>(f)); }
+ auto onImplAdd(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)); }
+ auto onImplDel(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& /*name*/, const STG::Admin* /*admin*/) override { return 0; }
int Chg(const STG::TariffData& /*td*/, const STG::Admin* /*admin*/) override { return 0; }
- void AddNotifierAdd(STG::NotifierBase<STG::TariffData>*) override {}
- void DelNotifierAdd(STG::NotifierBase<STG::TariffData>*) override {}
-
- void AddNotifierDel(STG::NotifierBase<STG::TariffData>*) override {}
- void DelNotifierDel(STG::NotifierBase<STG::TariffData>*) override {}
-
void GetTariffsData(std::vector<STG::TariffData>* /*tdl*/) const override {}
size_t Count() const override { return 0; }