git.stg.codes
/
stg.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge remote-tracking branch 'github/master'
[stg.git]
/
projects
/
stargazer
/
services_impl.cpp
diff --git
a/projects/stargazer/services_impl.cpp
b/projects/stargazer/services_impl.cpp
index 90c23f812acd856a5ec282e0f97f7006eba41af5..36640c88988ce9f4cf0e9aad3228a6199f181eac 100644
(file)
--- a/
projects/stargazer/services_impl.cpp
+++ b/
projects/stargazer/services_impl.cpp
@@
-18,37
+18,36
@@
* Author : Maxim Mamontov <faust@stargazer.dp.ua>
*/
* Author : Maxim Mamontov <faust@stargazer.dp.ua>
*/
-#include <cerrno>
-#include <cassert>
-#include <algorithm>
+#include "services_impl.h"
#include "stg/admin.h"
#include "stg/admin.h"
+#include "stg/admin_conf.h"
+#include "stg/store.h"
#include "stg/common.h"
#include "stg/common.h"
-#include "services_impl.h"
+
+#include <algorithm>
+#include <cassert>
+
+using STG::ServicesImpl;
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
-SERVICES_IMPL::SERVICES_IMPL(STORE * st)
- : SERVICES(),
- data(),
- store(st),
- WriteServLog(GetStgLogger()),
+ServicesImpl::ServicesImpl(Store * st)
+ : store(st),
+ WriteServLog(Logger::get()),
searchDescriptors(),
searchDescriptors(),
- handle(0),
- mutex(),
- strError()
+ handle(0)
{
{
-pthread_mutex_init(&mutex, NULL);
Read();
}
//-----------------------------------------------------------------------------
Read();
}
//-----------------------------------------------------------------------------
-int S
ERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN
* admin)
+int S
ervicesImpl::Add(const ServiceConf & service, const Admin
* admin)
{
{
-
STG_LOCKER lock(&
mutex);
-const
PRIV * priv = admin->GetP
riv();
+
std::lock_guard<std::mutex> lock(
mutex);
+const
auto& priv = admin->p
riv();
-if (!priv
->
serviceChg)
+if (!priv
.
serviceChg)
{
{
- std::string s = admin->
GetL
ogStr() + " Add service \'" + service.name + "\'. Access denied.";
+ std::string s = admin->
l
ogStr() + " Add service \'" + service.name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
@@
-59,7
+58,7
@@
iterator si(std::find(data.begin(), data.end(), service));
if (si != data.end())
{
strError = "Service \'" + service.name + "\' cannot not be added. Service already exist.";
if (si != data.end())
{
strError = "Service \'" + service.name + "\' cannot not be added. Service 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;
}
@@
-69,35
+68,35
@@
data.push_back(service);
if (store->AddService(service.name) == 0)
{
WriteServLog("%s Service \'%s\' added.",
if (store->AddService(service.name) == 0)
{
WriteServLog("%s Service \'%s\' added.",
- admin->
GetL
ogStr().c_str(), service.name.c_str());
+ admin->
l
ogStr().c_str(), service.name.c_str());
return 0;
}
strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError();
return 0;
}
strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError();
-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;
}
//-----------------------------------------------------------------------------
-int S
ERVICES_IMPL::Del(const std::string & name, const ADMIN
* admin)
+int S
ervicesImpl::Del(const std::string & name, const Admin
* admin)
{
{
-
STG_LOCKER lock(&
mutex);
-const
PRIV * priv = admin->GetP
riv();
+
std::lock_guard<std::mutex> lock(
mutex);
+const
auto& priv = admin->p
riv();
-if (!priv
->
serviceChg)
+if (!priv
.
serviceChg)
{
{
- std::string s = admin->
GetL
ogStr() + " Delete service \'" + name + "\'. Access denied.";
+ std::string s = admin->
l
ogStr() + " Delete service \'" + name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
}
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
}
-iterator si(std::find(data.begin(), data.end(), S
ERVICE_CONF
(name)));
+iterator si(std::find(data.begin(), data.end(), S
erviceConf
(name)));
if (si == data.end())
{
strError = "Service \'" + name + "\' cannot be deleted. Service does not exist.";
if (si == data.end())
{
strError = "Service \'" + name + "\' cannot be deleted. Service 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;
}
@@
-107,30
+106,30
@@
while (csi != searchDescriptors.end())
{
if (csi->second == si)
(csi->second)++;
{
if (csi->second == si)
(csi->second)++;
-
csi++
;
+
++csi
;
}
}
-data.
remove(*
si);
+data.
erase(
si);
if (store->DelService(name) < 0)
{
strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError();
if (store->DelService(name) < 0)
{
strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError();
- 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;
}
-WriteServLog("%s Service \'%s\' deleted.", admin->
GetL
ogStr().c_str(), name.c_str());
+WriteServLog("%s Service \'%s\' deleted.", admin->
l
ogStr().c_str(), name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
return 0;
}
//-----------------------------------------------------------------------------
-int S
ERVICES_IMPL::Change(const SERVICE_CONF & service, const ADMIN
* admin)
+int S
ervicesImpl::Change(const ServiceConf & service, const Admin
* admin)
{
{
-
STG_LOCKER lock(&
mutex);
-const
PRIV * priv = admin->GetP
riv();
+
std::lock_guard<std::mutex> lock(
mutex);
+const
auto& priv = admin->p
riv();
-if (!priv
->
serviceChg)
+if (!priv
.
serviceChg)
{
{
- std::string s = admin->
GetL
ogStr() + " Change service \'" + service.name + "\'. Access denied.";
+ std::string s = admin->
l
ogStr() + " Change service \'" + service.name + "\'. Access denied.";
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
strError = "Access denied.";
WriteServLog(s.c_str());
return -1;
@@
-141,13
+140,13
@@
iterator si(std::find(data.begin(), data.end(), service));
if (si == data.end())
{
strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist.";
if (si == data.end())
{
strError = "Service \'" + service.name + "\' cannot be changed " + ". Service 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;
}
-printfd(__FILE__, "Old cost = %f, old pay day = %
d\n", si->cost, (unsigned)si->payDay
);
+printfd(__FILE__, "Old cost = %f, old pay day = %
u\n", si->cost, static_cast<unsigned>(si->payDay)
);
*si = service;
*si = service;
-printfd(__FILE__, "New cost = %f, New pay day = %
d\n", si->cost, (unsigned)si->payDay
);
+printfd(__FILE__, "New cost = %f, New pay day = %
u\n", si->cost, static_cast<unsigned>(si->payDay)
);
if (store->SaveService(service))
{
WriteServLog("Cannot write service %s.", service.name.c_str());
if (store->SaveService(service))
{
WriteServLog("Cannot write service %s.", service.name.c_str());
@@
-156,14
+155,14
@@
if (store->SaveService(service))
}
WriteServLog("%s Service \'%s\' changed.",
}
WriteServLog("%s Service \'%s\' changed.",
- admin->
GetL
ogStr().c_str(), service.name.c_str());
+ admin->
l
ogStr().c_str(), service.name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
return 0;
}
//-----------------------------------------------------------------------------
-bool S
ERVICES_IMPL
::Read()
+bool S
ervicesImpl
::Read()
{
{
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
std::vector<std::string> servicesList;
if (store->GetServicesList(&servicesList) < 0)
{
std::vector<std::string> servicesList;
if (store->GetServicesList(&servicesList) < 0)
{
@@
-173,7
+172,7
@@
if (store->GetServicesList(&servicesList) < 0)
for (size_t i = 0; i < servicesList.size(); i++)
{
for (size_t i = 0; i < servicesList.size(); i++)
{
- S
ERVICE_CONF
service;
+ S
erviceConf
service;
if (store->RestoreService(&service, servicesList[i]))
{
if (store->RestoreService(&service, servicesList[i]))
{
@@
-186,15
+185,15
@@
for (size_t i = 0; i < servicesList.size(); i++)
return false;
}
//-----------------------------------------------------------------------------
return false;
}
//-----------------------------------------------------------------------------
-bool S
ERVICES_IMPL::Find(const std::string & name, SERVICE_CONF
* service) const
+bool S
ervicesImpl::Find(const std::string & name, ServiceConf
* service) const
{
assert(service != NULL && "Pointer to service is not null");
{
assert(service != NULL && "Pointer to service is not null");
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
if (data.empty())
return true;
if (data.empty())
return true;
-const_iterator si(std::find(data.begin(), data.end(), S
ERVICE_CONF
(name)));
+const_iterator si(std::find(data.begin(), data.end(), S
erviceConf
(name)));
if (si != data.end())
{
if (si != data.end())
{
@@
-205,15
+204,15
@@
if (si != data.end())
return true;
}
//-----------------------------------------------------------------------------
return true;
}
//-----------------------------------------------------------------------------
-bool S
ERVICES_IMPL::Find(const std::string & name, SERVICE_CONF_RES
* service) const
+bool S
ervicesImpl::Find(const std::string & name, ServiceConfOpt
* service) const
{
assert(service != NULL && "Pointer to service is not null");
{
assert(service != NULL && "Pointer to service is not null");
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
if (data.empty())
return true;
if (data.empty())
return true;
-const_iterator si(std::find(data.begin(), data.end(), S
ERVICE_CONF
(name)));
+const_iterator si(std::find(data.begin(), data.end(), S
erviceConf
(name)));
if (si != data.end())
{
if (si != data.end())
{
@@
-224,16
+223,16
@@
if (si != data.end())
return true;
}
//-----------------------------------------------------------------------------
return true;
}
//-----------------------------------------------------------------------------
-bool S
ERVICES_IMPL
::Exists(const std::string & name) const
+bool S
ervicesImpl
::Exists(const std::string & name) const
{
{
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
if (data.empty())
{
printfd(__FILE__, "No services in the system!\n");
return true;
}
if (data.empty())
{
printfd(__FILE__, "No services in the system!\n");
return true;
}
-const_iterator si(std::find(data.begin(), data.end(), S
ERVICE_CONF
(name)));
+const_iterator si(std::find(data.begin(), data.end(), S
erviceConf
(name)));
if (si != data.end())
return true;
if (si != data.end())
return true;
@@
-241,17
+240,17
@@
if (si != data.end())
return false;
}
//-----------------------------------------------------------------------------
return false;
}
//-----------------------------------------------------------------------------
-int S
ERVICES_IMPL
::OpenSearch() const
+int S
ervicesImpl
::OpenSearch() const
{
{
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
handle++;
searchDescriptors[handle] = data.begin();
return handle;
}
//-----------------------------------------------------------------------------
handle++;
searchDescriptors[handle] = data.begin();
return handle;
}
//-----------------------------------------------------------------------------
-int S
ERVICES_IMPL::SearchNext(int h, SERVICE_CONF
* service) const
+int S
ervicesImpl::SearchNext(int h, ServiceConf
* service) const
{
{
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
if (searchDescriptors.find(h) == searchDescriptors.end())
{
WriteServLog("SERVICES. Incorrect search handle.");
if (searchDescriptors.find(h) == searchDescriptors.end())
{
WriteServLog("SERVICES. Incorrect search handle.");
@@
-266,9
+265,9
@@
if (searchDescriptors[h] == data.end())
return 0;
}
//-----------------------------------------------------------------------------
return 0;
}
//-----------------------------------------------------------------------------
-int S
ERVICES_IMPL
::CloseSearch(int h) const
+int S
ervicesImpl
::CloseSearch(int h) const
{
{
-
STG_LOCKER lock(&
mutex);
+
std::lock_guard<std::mutex> lock(
mutex);
if (searchDescriptors.find(h) != searchDescriptors.end())
{
searchDescriptors.erase(searchDescriptors.find(h));
if (searchDescriptors.find(h) != searchDescriptors.end())
{
searchDescriptors.erase(searchDescriptors.find(h));