git.stg.codes
/
stg.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Use async pool instead of EVENT_LOOP.
[stg.git]
/
projects
/
stargazer
/
tariffs_impl.cpp
diff --git
a/projects/stargazer/tariffs_impl.cpp
b/projects/stargazer/tariffs_impl.cpp
index ab3d970fa38f650edd5aa4e9ff5d320c745fa0cb..492f403f7780764383be17750ce601e54bcb8f8d 100644
(file)
--- a/
projects/stargazer/tariffs_impl.cpp
+++ b/
projects/stargazer/tariffs_impl.cpp
@@
-36,73
+36,60
@@
#include "stg/logger.h"
#include "stg/store.h"
#include "stg/admin.h"
#include "stg/logger.h"
#include "stg/store.h"
#include "stg/admin.h"
+#include "stg/admin_conf.h"
#include "tariffs_impl.h"
#include "tariffs_impl.h"
-using
namespace std
;
+using
STG::TariffsImpl
;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-TARIFFS_IMPL::TARIFFS_IMPL(STORE * st)
- : TARIFFS(),
- tariffs(),
- store(st),
- WriteServLog(GetStgLogger()),
- mutex(),
- strError(),
- noTariff(NO_TARIFF_NAME),
- onAddNotifiers(),
- onDelNotifiers()
+TariffsImpl::TariffsImpl(Store * st)
+ : store(st),
+ WriteServLog(Logger::get()),
+ noTariff(NO_TARIFF_NAME)
{
{
-pthread_mutex_init(&mutex, NULL);
-ReadTariffs();
}
//-----------------------------------------------------------------------------
}
//-----------------------------------------------------------------------------
-
TARIFFS_IMPL::~TARIFFS_IMPL
()
+
int TariffsImpl::ReadTariffs
()
{
{
-pthread_mutex_destroy(&mutex);
-}
-//-----------------------------------------------------------------------------
-int TARIFFS_IMPL::ReadTariffs()
-{
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+std::lock_guard<std::mutex> lock(m_mutex);
-
vector<
string> tariffsList;
+
std::vector<std::
string> tariffsList;
if (store->GetTariffsList(&tariffsList))
{
WriteServLog("Cannot get tariffs list.");
WriteServLog("%s", store->GetStrError().c_str());
}
if (store->GetTariffsList(&tariffsList))
{
WriteServLog("Cannot get tariffs list.");
WriteServLog("%s", store->GetStrError().c_str());
}
-
int
tariffsNum = tariffsList.size();
+
Data::size_type
tariffsNum = tariffsList.size();
-for (
int
i = 0; i < tariffsNum; i++)
+for (
Data::size_type
i = 0; i < tariffsNum; i++)
{
{
- T
ARIFF_DATA
td;
+ T
ariffData
td;
if (store->RestoreTariff(&td, tariffsList[i]))
{
WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
WriteServLog("%s", store->GetStrError().c_str());
return -1;
}
if (store->RestoreTariff(&td, tariffsList[i]))
{
WriteServLog("Cannot read tariff %s.", tariffsList[i].c_str());
WriteServLog("%s", store->GetStrError().c_str());
return -1;
}
- tariffs.push_back(T
ARIFF_IMPL
(td));
+ tariffs.push_back(T
ariffImpl
(td));
}
return 0;
}
//-----------------------------------------------------------------------------
}
return 0;
}
//-----------------------------------------------------------------------------
-size_t T
ARIFFS_IMPL
::Count() const
+size_t T
ariffsImpl
::Count() const
{
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
return tariffs.size();
}
//-----------------------------------------------------------------------------
return tariffs.size();
}
//-----------------------------------------------------------------------------
-const
TARIFF * TARIFFS_IMPL::FindByName(const
string & name) const
+const
STG::Tariff* TariffsImpl::FindByName(const std::
string & name) const
{
if (name == NO_TARIFF_NAME)
return &noTariff;
{
if (name == NO_TARIFF_NAME)
return &noTariff;
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<TARIFF_IMPL>::const_iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+std::lock_guard<std::mutex> lock(m_mutex);
+const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
if (ti != tariffs.end())
return &(*ti);
if (ti != tariffs.end())
return &(*ti);
@@
-110,28
+97,27
@@
if (ti != tariffs.end())
return NULL;
}
//-----------------------------------------------------------------------------
return NULL;
}
//-----------------------------------------------------------------------------
-int T
ARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN
* admin)
+int T
ariffsImpl::Chg(const TariffData & td, const Admin
* admin)
{
{
-const
PRIV * priv = admin->GetP
riv();
+const
auto& priv = admin->p
riv();
-if (!priv
->
tariffChg)
+if (!priv
.
tariffChg)
{
{
- st
ring s = admin->GetL
ogStr() + " Change tariff \'"
+ st
d::string s = admin->l
ogStr() + " Change tariff \'"
+ td.tariffConf.name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
}
+ td.tariffConf.name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
}
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
-list<TARIFF_IMPL>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
+auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name));
if (ti == tariffs.end())
{
strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
if (ti == tariffs.end())
{
strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist.";
- WriteServLog("%s %s", admin->
GetL
ogStr().c_str(), strError.c_str());
+ WriteServLog("%s %s", admin->
l
ogStr().c_str(), strError.c_str());
return -1;
}
return -1;
}
@@
-139,42
+125,41
@@
if (ti == tariffs.end())
if (store->SaveTariff(td, td.tariffConf.name))
{
if (store->SaveTariff(td, td.tariffConf.name))
{
- string error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
+ st
d::st
ring error = "Tariff " + td.tariffConf.name + " writing error. " + store->GetStrError();
WriteServLog(error.c_str());
return -1;
}
WriteServLog("%s Tariff \'%s\' changed.",
WriteServLog(error.c_str());
return -1;
}
WriteServLog("%s Tariff \'%s\' changed.",
- admin->
GetL
ogStr().c_str(), td.tariffConf.name.c_str());
+ admin->
l
ogStr().c_str(), td.tariffConf.name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
return 0;
}
//-----------------------------------------------------------------------------
-int T
ARIFFS_IMPL::Del(const string & name, const ADMIN
* admin)
+int T
ariffsImpl::Del(const std::string & name, const Admin
* admin)
{
{
-const
PRIV * priv = admin->GetP
riv();
+const
auto& priv = admin->p
riv();
-if (!priv
->
tariffChg)
+if (!priv
.
tariffChg)
{
{
- st
ring s = admin->GetL
ogStr() + " Delete tariff \'"
+ st
d::string s = admin->l
ogStr() + " Delete tariff \'"
+ name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
}
+ name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
}
-T
ARIFF_DATA
td;
+T
ariffData
td;
{
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
- list<TARIFF_IMPL>::iterator ti;
- ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+ const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
if (ti == tariffs.end())
{
strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
if (ti == tariffs.end())
{
strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
- WriteServLog("%s %s", admin->
GetL
ogStr().c_str(), strError.c_str());
+ WriteServLog("%s %s", admin->
l
ogStr().c_str(), strError.c_str());
return -1;
}
return -1;
}
@@
-184,32
+169,32
@@
TARIFF_DATA td;
WriteServLog("%s", store->GetStrError().c_str());
return -1;
}
WriteServLog("%s", store->GetStrError().c_str());
return -1;
}
-
+
td = ti->GetTariffData();
tariffs.erase(ti);
}
td = ti->GetTariffData();
tariffs.erase(ti);
}
-
std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator
ni = onDelNotifiers.begin();
+
auto
ni = onDelNotifiers.begin();
while (ni != onDelNotifiers.end())
{
while (ni != onDelNotifiers.end())
{
- (*ni)->
N
otify(td);
+ (*ni)->
n
otify(td);
++ni;
}
WriteServLog("%s Tariff \'%s\' deleted.",
++ni;
}
WriteServLog("%s Tariff \'%s\' deleted.",
- admin->
GetL
ogStr().c_str(),
+ admin->
l
ogStr().c_str(),
name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
-int T
ARIFFS_IMPL::Add(const string & name, const ADMIN
* admin)
+int T
ariffsImpl::Add(const std::string & name, const Admin
* admin)
{
{
-const
PRIV * priv = admin->GetP
riv();
+const
auto& priv = admin->p
riv();
-if (!priv
->
tariffChg)
+if (!priv
.
tariffChg)
{
{
- st
ring s = admin->GetL
ogStr() + " Add tariff \'"
+ st
d::string s = admin->l
ogStr() + " Add tariff \'"
+ name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
+ name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
@@
-217,19
+202,18
@@
if (!priv->tariffChg)
}
{
}
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
- list<TARIFF_IMPL>::iterator ti;
- ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+ const auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(name));
if (ti != tariffs.end())
{
strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
if (ti != tariffs.end())
{
strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
- WriteServLog("%s %s", admin->
GetL
ogStr().c_str(), strError.c_str());
+ WriteServLog("%s %s", admin->
l
ogStr().c_str(), strError.c_str());
return -1;
}
return -1;
}
- tariffs.push_back(T
ARIFF_IMPL
(name));
+ tariffs.push_back(T
ariffImpl
(name));
}
if (store->AddTariff(name) < 0)
}
if (store->AddTariff(name) < 0)
@@
-240,52
+224,52
@@
if (store->AddTariff(name) < 0)
}
// Fire all "on add" notifiers
}
// Fire all "on add" notifiers
-
std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator
ni = onAddNotifiers.begin();
+
auto
ni = onAddNotifiers.begin();
while (ni != onAddNotifiers.end())
{
while (ni != onAddNotifiers.end())
{
- (*ni)->
N
otify(tariffs.back().GetTariffData());
+ (*ni)->
n
otify(tariffs.back().GetTariffData());
++ni;
}
WriteServLog("%s Tariff \'%s\' added.",
++ni;
}
WriteServLog("%s Tariff \'%s\' added.",
- admin->
GetL
ogStr().c_str(), name.c_str());
+ admin->
l
ogStr().c_str(), name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
return 0;
}
//-----------------------------------------------------------------------------
-void T
ARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
+void T
ariffsImpl::GetTariffsData(std::vector<TariffData> * tdl) const
{
assert(tdl != NULL && "Tariffs data list is not null");
{
assert(tdl != NULL && "Tariffs data list is not null");
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
-
std::list<TARIFF_IMPL>::const_iterator
it = tariffs.begin();
+
auto
it = tariffs.begin();
for (; it != tariffs.end(); ++it)
{
tdl->push_back(it->GetTariffData());
}
}
//-----------------------------------------------------------------------------
for (; it != tariffs.end(); ++it)
{
tdl->push_back(it->GetTariffData());
}
}
//-----------------------------------------------------------------------------
-void T
ARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA
> * n)
+void T
ariffsImpl::AddNotifierAdd(NotifierBase<TariffData
> * n)
{
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
onAddNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
onAddNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
-void T
ARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA
> * n)
+void T
ariffsImpl::DelNotifierAdd(NotifierBase<TariffData
> * n)
{
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
onAddNotifiers.erase(n);
}
//-----------------------------------------------------------------------------
onAddNotifiers.erase(n);
}
//-----------------------------------------------------------------------------
-void T
ARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA
> * n)
+void T
ariffsImpl::AddNotifierDel(NotifierBase<TariffData
> * n)
{
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
onDelNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
onDelNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
-void T
ARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA
> * n)
+void T
ariffsImpl::DelNotifierDel(NotifierBase<TariffData
> * n)
{
{
-
STG_LOCKER lock(&mutex, __FILE__, __LINE__
);
+
std::lock_guard<std::mutex> lock(m_mutex
);
onDelNotifiers.erase(n);
}
//-----------------------------------------------------------------------------
onDelNotifiers.erase(n);
}
//-----------------------------------------------------------------------------