From 3e3df32d6d5a426ff11ac1b3734374bf25046b7f Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 15:39:07 +0300 Subject: [PATCH 01/16] Service and corporation permissions added --- include/stg/admin_conf.h | 30 +++--- include/stg/admin_conf.inc.h | 26 ++--- .../plugins/store/files/file_store.cpp | 12 +++ tests/test_admin_conf.cpp | 95 ++++++++++++------- 4 files changed, 106 insertions(+), 57 deletions(-) diff --git a/include/stg/admin_conf.h b/include/stg/admin_conf.h index 32012f34..df6d9e22 100644 --- a/include/stg/admin_conf.h +++ b/include/stg/admin_conf.h @@ -23,20 +23,24 @@ struct PRIV userPasswd(0), userAddDel(0), adminChg(0), - tariffChg(0) - {}; - PRIV(uint16_t p) - : userStat((p & 0x0003) >> 0x00), - userConf((p & 0x000C) >> 0x02), - userCash((p & 0x0030) >> 0x04), - userPasswd((p & 0x00C0) >> 0x06), - userAddDel((p & 0x0300) >> 0x08), - adminChg((p & 0x0C00) >> 0x0A), - tariffChg((p & 0x3000) >> 0x0C) + tariffChg(0), + serviceChg(0), + corpChg(0) + {} + PRIV(uint32_t p) + : userStat((p & 0x00000003) >> 0x00), + userConf((p & 0x0000000C) >> 0x02), + userCash((p & 0x00000030) >> 0x04), + userPasswd((p & 0x000000C0) >> 0x06), + userAddDel((p & 0x00000300) >> 0x08), + adminChg((p & 0x00000C00) >> 0x0A), + tariffChg((p & 0x00003000) >> 0x0C), + serviceChg((p & 0x0000C000) >> 0x0E), + corpChg((p & 0x00030000) >> 0x10) {} - uint16_t ToInt() const; - void FromInt(uint16_t p); + uint32_t ToInt() const; + void FromInt(uint32_t p); uint16_t userStat; uint16_t userConf; @@ -45,6 +49,8 @@ struct PRIV uint16_t userAddDel; uint16_t adminChg; uint16_t tariffChg; + uint16_t serviceChg; + uint16_t corpChg; }; //----------------------------------------------------------------------------- struct ADMIN_CONF diff --git a/include/stg/admin_conf.inc.h b/include/stg/admin_conf.inc.h index 44897a08..f744936e 100644 --- a/include/stg/admin_conf.inc.h +++ b/include/stg/admin_conf.inc.h @@ -8,28 +8,32 @@ #define ADMIN_CONF_INC_H inline -uint16_t PRIV::ToInt() const +uint32_t PRIV::ToInt() const { -uint16_t p = (userStat << 0) | +uint32_t p = (userStat << 0) | (userConf << 2) | (userCash << 4) | (userPasswd << 6) | (userAddDel << 8) | (adminChg << 10) | - (tariffChg << 12); + (tariffChg << 12) | + (serviceChg << 14) | + (corpChg << 16); return p; } inline -void PRIV::FromInt(uint16_t p) +void PRIV::FromInt(uint32_t p) { -userStat = (p & 0x0003) >> 0x00; // 1+2 -userConf = (p & 0x000C) >> 0x02; // 4+8 -userCash = (p & 0x0030) >> 0x04; // 10+20 -userPasswd = (p & 0x00C0) >> 0x06; // 40+80 -userAddDel = (p & 0x0300) >> 0x08; // 100+200 -adminChg = (p & 0x0C00) >> 0x0A; // 400+800 -tariffChg = (p & 0x3000) >> 0x0C; // 1000+2000 +userStat = (p & 0x00000003) >> 0x00; // 1+2 +userConf = (p & 0x0000000C) >> 0x02; // 4+8 +userCash = (p & 0x00000030) >> 0x04; // 10+20 +userPasswd = (p & 0x000000C0) >> 0x06; // 40+80 +userAddDel = (p & 0x00000300) >> 0x08; // 100+200 +adminChg = (p & 0x00000C00) >> 0x0A; // 400+800 +tariffChg = (p & 0x00003000) >> 0x0C; // 1000+2000 +serviceChg = (p & 0x0000C000) >> 0x0E; // 4000+8000 +corpChg = (p & 0x00030000) >> 0x10; // 10000+20000 } #endif diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index 1a16ed91..6d8de6c2 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -1280,6 +1280,8 @@ strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login cf.WriteInt("UsrAddDel", ac.priv.userAddDel); cf.WriteInt("ChgTariff", ac.priv.tariffChg); cf.WriteInt("ChgAdmin", ac.priv.adminChg); + cf.WriteInt("ChgService", ac.priv.serviceChg); + cf.WriteInt("ChgCorp", ac.priv.corpChg); } return 0; @@ -1407,6 +1409,16 @@ else return -1; } +if (cf.ReadInt("ChgService", &a, 0) == 0) + ac->priv.serviceChg = a; +else + ac->priv.serviceChg = 0; + +if (cf.ReadInt("ChgCorp", &a, 0) == 0) + ac->priv.corpChg = a; +else + ac->priv.corpChg = 0; + return 0; } //----------------------------------------------------------------------------- diff --git a/tests/test_admin_conf.cpp b/tests/test_admin_conf.cpp index 7a54c480..dad51b18 100644 --- a/tests/test_admin_conf.cpp +++ b/tests/test_admin_conf.cpp @@ -6,12 +6,12 @@ namespace tut { struct priv_data { enum { - MIX2 = 0x06C6, // 2103210 - ONES = 0x1555, - MIX3 = 0x1B1B, // 3210321 - TWOS = 0x2AAA, - MIX1 = 0x24E4, // 0123012 - THREES = 0x3FFF + MIX2 = 0x0002C6C6, // 210321032 + ONES = 0x00015555, + MIX3 = 0x00031B1B, // 321032103 + TWOS = 0x0002AAAA, + MIX1 = 0x0000E4E4, // 012301230 + THREES = 0x0003FFFF }; }; @@ -35,6 +35,8 @@ namespace tut ensure("zero.userAddDel == 0", zero.userAddDel == 0); ensure("zero.adminChg == 0", zero.adminChg == 0); ensure("zero.tariffChg == 0", zero.tariffChg == 0); + ensure("zero.serviceChg == 0", zero.serviceChg == 0); + ensure("zero.corpChg == 0", zero.corpChg == 0); ensure("zero.ToInt() == 0", zero.ToInt() == 0); } @@ -43,13 +45,13 @@ namespace tut template<> void testobject::test<2>() { - set_test_name("Check uint16_t conversions"); + set_test_name("Check uint32_t conversions"); - for (uint16_t i = 0; i < 4; ++i) { + for (uint8_t i = 0; i < 4; ++i) { // 'i' is extra trash in high bits - PRIV priv1(ONES | (i << 0x0E)); // All 1 + PRIV priv1(ONES | (i << 0x12)); // All 1 ensure_equals("priv1.userStat == 1", priv1.userStat, 1); ensure_equals("priv1.userConf == 1", priv1.userConf, 1); @@ -58,10 +60,12 @@ namespace tut ensure_equals("priv1.userAddDel == 1", priv1.userAddDel, 1); ensure_equals("priv1.adminChg == 1", priv1.adminChg, 1); ensure_equals("priv1.tariffChg == 1", priv1.tariffChg, 1); + ensure_equals("priv1.serviceChg == 1", priv1.serviceChg, 1); + ensure_equals("priv1.corpChg == 1", priv1.corpChg, 1); - ensure_equals("priv1.ToInt() == 0x1555", priv1.ToInt(), static_cast(ONES)); + ensure_equals("priv1.ToInt() == 0x00015555", priv1.ToInt(), static_cast(ONES)); - PRIV priv2(TWOS | (i << 0x0E)); // All 2 + PRIV priv2(TWOS | (i << 0x12)); // All 2 ensure_equals("priv2.userStat == 2", priv2.userStat, 2); ensure_equals("priv2.userConf == 2", priv2.userConf, 2); @@ -70,10 +74,12 @@ namespace tut ensure_equals("priv2.userAddDel == 2", priv2.userAddDel, 2); ensure_equals("priv2.adminChg == 2", priv2.adminChg, 2); ensure_equals("priv2.tariffChg == 2", priv2.tariffChg, 2); + ensure_equals("priv2.serviceChg == 2", priv2.serviceChg, 2); + ensure_equals("priv2.corpChg == 2", priv2.corpChg, 2); - ensure_equals("priv2.ToInt() = 0x2AAA", priv2.ToInt(), static_cast(TWOS)); + ensure_equals("priv2.ToInt() = 0x0002AAAA", priv2.ToInt(), static_cast(TWOS)); - PRIV priv3(THREES | (i << 0x0E)); // All 3 + PRIV priv3(THREES | (i << 0x12)); // All 3 ensure_equals("priv3.userStat == 3", priv3.userStat, 3); ensure_equals("priv3.userConf == 3", priv3.userConf, 3); @@ -82,10 +88,12 @@ namespace tut ensure_equals("priv3.userAddDel == 3", priv3.userAddDel, 3); ensure_equals("priv3.adminChg == 3", priv3.adminChg, 3); ensure_equals("priv3.tariffChg == 3", priv3.tariffChg, 3); + ensure_equals("priv3.serviceChg == 3", priv3.serviceChg, 3); + ensure_equals("priv3.corpChg == 3", priv3.corpChg, 3); - ensure_equals("priv2.ToInt() = 0x3FFF", priv3.ToInt(), static_cast(THREES)); + ensure_equals("priv3.ToInt() = 0x0003FFFF", priv3.ToInt(), static_cast(THREES)); - PRIV pm1(MIX1 | (i << 0x0E)); // 0123012 + PRIV pm1(MIX1 | (i << 0x12)); // 012301230 ensure_equals("pm1.userStat == 0", pm1.userStat, 0); ensure_equals("pm1.userConf == 1", pm1.userConf, 1); @@ -94,10 +102,12 @@ namespace tut ensure_equals("pm1.userAddDel == 0", pm1.userAddDel, 0); ensure_equals("pm1.adminChg == 1", pm1.adminChg, 1); ensure_equals("pm1.tariffChg == 2", pm1.tariffChg, 2); + ensure_equals("pm1.serviceChg == 3", pm1.serviceChg, 3); + ensure_equals("pm1.corpChg == 0", pm1.corpChg, 0); - ensure_equals("pm1.ToInt() = 0x24E4", pm1.ToInt(), static_cast(MIX1)); + ensure_equals("pm1.ToInt() = 0xE4E4", pm1.ToInt(), static_cast(MIX1)); - PRIV pm2(MIX2 | (i << 0x0E)); // 0123012 + PRIV pm2(MIX2 | (i << 0x12)); // 210321032 ensure_equals("pm2.userStat == 2", pm2.userStat, 2); ensure_equals("pm2.userConf == 1", pm2.userConf, 1); @@ -106,10 +116,12 @@ namespace tut ensure_equals("pm2.userAddDel == 2", pm2.userAddDel, 2); ensure_equals("pm2.adminChg == 1", pm2.adminChg, 1); ensure_equals("pm2.tariffChg == 0", pm2.tariffChg, 0); + ensure_equals("pm2.serviceChg == 3", pm2.serviceChg, 3); + ensure_equals("pm2.corpChg == 2", pm2.corpChg, 2); - ensure_equals("pm2.ToInt() = 0x06C6", pm2.ToInt(), static_cast(MIX2)); + ensure_equals("pm2.ToInt() = 0x0002C6C6", pm2.ToInt(), static_cast(MIX2)); - PRIV pm3(MIX3 | (i << 0x0E)); // 3210321 + PRIV pm3(MIX3 | (i << 0x12)); // 321032103 ensure_equals("pm3.userStat == 3", pm3.userStat, 3); ensure_equals("pm3.userConf == 2", pm3.userConf, 2); @@ -118,8 +130,10 @@ namespace tut ensure_equals("pm3.userAddDel == 3", pm3.userAddDel, 3); ensure_equals("pm3.adminChg == 2", pm3.adminChg, 2); ensure_equals("pm3.tariffChg == 1", pm3.tariffChg, 1); + ensure_equals("pm3.serviceChg == 0", pm3.serviceChg, 0); + ensure_equals("pm3.corpChg == 3", pm3.corpChg, 3); - ensure_equals("pm3.ToInt() = 0x1B1B", pm3.ToInt(), static_cast(MIX3)); + ensure_equals("pm3.ToInt() = 0x00031B1B", pm3.ToInt(), static_cast(MIX3)); } @@ -129,14 +143,15 @@ namespace tut template<> void testobject::test<3>() { - set_test_name("Check explicit uint16_t conversions"); + set_test_name("Check explicit uint32_t conversions"); - for (uint16_t i = 0; i < 4; ++i) { + for (uint8_t i = 0; i < 4; ++i) { // 'i' is extra trash in high bits PRIV priv1; - priv1.FromInt(ONES | (i << 0x0E)); // All 1 + priv1.FromInt(ONES | (i << 0x12)); // All 1 + ensure_equals("priv1.userStat == 1", priv1.userStat, 1); ensure_equals("priv1.userConf == 1", priv1.userConf, 1); @@ -145,11 +160,13 @@ namespace tut ensure_equals("priv1.userAddDel == 1", priv1.userAddDel, 1); ensure_equals("priv1.adminChg == 1", priv1.adminChg, 1); ensure_equals("priv1.tariffChg == 1", priv1.tariffChg, 1); + ensure_equals("priv1.serviceChg == 1", priv1.serviceChg, 1); + ensure_equals("priv1.corpChg == 1", priv1.corpChg, 1); - ensure_equals("priv1.ToInt() == 0x1555", priv1.ToInt(), static_cast(ONES)); + ensure_equals("priv1.ToInt() == 0x00015555", priv1.ToInt(), static_cast(ONES)); PRIV priv2; - priv2.FromInt(TWOS | (i << 0x0E)); // All 2 + priv2.FromInt(TWOS | (i << 0x12)); // All 2 ensure_equals("priv2.userStat == 2", priv2.userStat, 2); ensure_equals("priv2.userConf == 2", priv2.userConf, 2); @@ -158,11 +175,13 @@ namespace tut ensure_equals("priv2.userAddDel == 2", priv2.userAddDel, 2); ensure_equals("priv2.adminChg == 2", priv2.adminChg, 2); ensure_equals("priv2.tariffChg == 2", priv2.tariffChg, 2); + ensure_equals("priv2.serviceChg == 2", priv2.serviceChg, 2); + ensure_equals("priv2.corpChg == 2", priv2.corpChg, 2); - ensure_equals("priv2.ToInt() = 0x2AAA", priv2.ToInt(), static_cast(TWOS)); + ensure_equals("priv2.ToInt() = 0x0002AAAA", priv2.ToInt(), static_cast(TWOS)); PRIV priv3; - priv3.FromInt(THREES | (i << 0x0E)); // All 3 + priv3.FromInt(THREES | (i << 0x12)); // All 3 ensure_equals("priv3.userStat == 3", priv3.userStat, 3); ensure_equals("priv3.userConf == 3", priv3.userConf, 3); @@ -171,11 +190,13 @@ namespace tut ensure_equals("priv3.userAddDel == 3", priv3.userAddDel, 3); ensure_equals("priv3.adminChg == 3", priv3.adminChg, 3); ensure_equals("priv3.tariffChg == 3", priv3.tariffChg, 3); + ensure_equals("priv3.serviceChg == 3", priv3.serviceChg, 3); + ensure_equals("priv3.corpChg == 3", priv3.corpChg, 3); - ensure_equals("priv2.ToInt() = 0x3FFF", priv3.ToInt(), static_cast(THREES)); + ensure_equals("priv3.ToInt() = 0x0003FFFF", priv3.ToInt(), static_cast(THREES)); PRIV pm1; - pm1.FromInt(MIX1 | (i << 0x0E)); // 0123012 + pm1.FromInt(MIX1 | (i << 0x12)); // 012301230 ensure_equals("pm1.userStat == 0", pm1.userStat, 0); ensure_equals("pm1.userConf == 1", pm1.userConf, 1); @@ -184,11 +205,13 @@ namespace tut ensure_equals("pm1.userAddDel == 0", pm1.userAddDel, 0); ensure_equals("pm1.adminChg == 1", pm1.adminChg, 1); ensure_equals("pm1.tariffChg == 2", pm1.tariffChg, 2); + ensure_equals("pm1.serviceChg == 3", pm1.serviceChg, 3); + ensure_equals("pm1.corpChg == 0", pm1.corpChg, 0); - ensure_equals("pm1.ToInt() = 0x24E4", pm1.ToInt(), static_cast(MIX1)); + ensure_equals("pm1.ToInt() = 0xE4E4", pm1.ToInt(), static_cast(MIX1)); PRIV pm2; - pm2.FromInt(MIX2 | (i << 0x0E)); // 0123012 + pm2.FromInt(MIX2 | (i << 0x12)); // 210321032 ensure_equals("pm2.userStat == 2", pm2.userStat, 2); ensure_equals("pm2.userConf == 1", pm2.userConf, 1); @@ -197,11 +220,13 @@ namespace tut ensure_equals("pm2.userAddDel == 2", pm2.userAddDel, 2); ensure_equals("pm2.adminChg == 1", pm2.adminChg, 1); ensure_equals("pm2.tariffChg == 0", pm2.tariffChg, 0); + ensure_equals("pm2.serviceChg == 3", pm2.serviceChg, 3); + ensure_equals("pm2.corpChg == 2", pm2.corpChg, 2); - ensure_equals("pm2.ToInt() = 0x06C6", pm2.ToInt(), static_cast(MIX2)); + ensure_equals("pm2.ToInt() = 0x0002C6C6", pm2.ToInt(), static_cast(MIX2)); PRIV pm3; - pm3.FromInt(MIX3 | (i << 0x0E)); // 3210321 + pm3.FromInt(MIX3 | (i << 0x12)); // 321032103 ensure_equals("pm3.userStat == 3", pm3.userStat, 3); ensure_equals("pm3.userConf == 2", pm3.userConf, 2); @@ -210,8 +235,10 @@ namespace tut ensure_equals("pm3.userAddDel == 3", pm3.userAddDel, 3); ensure_equals("pm3.adminChg == 2", pm3.adminChg, 2); ensure_equals("pm3.tariffChg == 1", pm3.tariffChg, 1); + ensure_equals("pm3.serviceChg == 0", pm3.serviceChg, 0); + ensure_equals("pm3.corpChg == 3", pm3.corpChg, 3); - ensure_equals("pm3.ToInt() = 0x1B1B", pm3.ToInt(), static_cast(MIX3)); + ensure_equals("pm3.ToInt() = 0x00031B1B", pm3.ToInt(), static_cast(MIX3)); } -- 2.44.2 From 305dd917d2ba383e8c468c006d1d75edbc705337 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 15:41:07 +0300 Subject: [PATCH 02/16] Services and coroprations interface added --- include/stg/corporations.h | 42 ++++++++++++++++++++++++++++++++++++++ include/stg/services.h | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 include/stg/corporations.h create mode 100644 include/stg/services.h diff --git a/include/stg/corporations.h b/include/stg/corporations.h new file mode 100644 index 00000000..cc147fc6 --- /dev/null +++ b/include/stg/corporations.h @@ -0,0 +1,42 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef CORPORATIONS_H +#define CORPORATIONS_H + +#include + +#include "corp_conf.h" + +class CORPORATIONS { +public: + virtual int Add(const CORP_CONF & corp) = 0; + virtual int Del(const std::string & name) = 0; + virtual int Change(const CORP_CONF & corp) = 0; + virtual bool FindCorp(const std::string & name, CORP_CONF & corp) = 0; + virtual bool CorpExists(const std::string & name) const = 0; + virtual const std::string & GetStrError() const = 0; + + virtual int OpenSearch() const = 0; + virtual int SearchNext(int, CORP_CONF * corp) const = 0; + virtual int CloseSearch(int) const = 0; +}; + +#endif diff --git a/include/stg/services.h b/include/stg/services.h new file mode 100644 index 00000000..2f1eec71 --- /dev/null +++ b/include/stg/services.h @@ -0,0 +1,42 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef SERVICES_H +#define SERVICES_H + +#include + +#include "service_conf.h" + +class SERVICES { +public: + virtual int Add(const SERVICE_CONF & service) = 0; + virtual int Del(const std::string & name) = 0; + virtual int Change(const SERVICE_CONF & service) = 0; + virtual bool FindService(const std::string & name, SERVICE_CONF & service) = 0; + virtual bool ServiceExists(const std::string & name) const = 0; + virtual const std::string & GetStrError() const = 0; + + virtual int OpenSearch() const = 0; + virtual int SearchNext(int, SERVICE_CONF * service) const = 0; + virtual int CloseSearch(int) const = 0; +}; + +#endif -- 2.44.2 From a5ce0ca3674081d3b761eb1bc88654d6fd9d3755 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 15:42:04 +0300 Subject: [PATCH 03/16] Fix fild types in services and corporations --- include/stg/corp_conf.h | 9 +++++++-- include/stg/service_conf.h | 24 ++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/include/stg/corp_conf.h b/include/stg/corp_conf.h index 88a84f18..e29a5dcf 100644 --- a/include/stg/corp_conf.h +++ b/include/stg/corp_conf.h @@ -1,10 +1,15 @@ #ifndef CORP_CONF_H #define CORP_CONF_H +#include + struct CORP_CONF { -string name; -double cash; +CORP_CONF(const std::string & n) : name(n), cash(0) {} +CORP_CONF(const std::string & n, double c) : name(n), cash(c) {} + +std::string name; +double cash; }; #endif //CORP_CONF_H diff --git a/include/stg/service_conf.h b/include/stg/service_conf.h index 6c958c41..ea3495b3 100644 --- a/include/stg/service_conf.h +++ b/include/stg/service_conf.h @@ -1,12 +1,28 @@ #ifndef SERVICE_CONF_H #define SERVICE_CONF_H +#include + struct SERVICE_CONF { -string name; -string comment; -double cost; -int payDay; +SERVICE_CONF(const std::string & n) + : name(n), comment(), cost(0), payDay(0) +{} +SERVICE_CONF(const std::string & n, double c) + : name(n), comment(), cost(c), payDay(0) +{} +SERVICE_CONF(const std::string & n, double c, unsigned p) + : name(n), comment(), cost(c), payDay(p) +{} +SERVICE_CONF(const std::string & n, double c, + unsigned p, const std::string & com) + : name(n), comment(com), cost(c), payDay(p) +{} + +std::string name; +std::string comment; +double cost; +unsigned payDay; }; #endif //SERVICE_CONF_H -- 2.44.2 From fcafc446aa1f15975b1802e47952ed371446e4d4 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 15:42:34 +0300 Subject: [PATCH 04/16] Initial services implementation added --- projects/stargazer/services_impl.cpp | 307 +++++++++++++++++++++++++++ projects/stargazer/services_impl.h | 67 ++++++ 2 files changed, 374 insertions(+) create mode 100644 projects/stargazer/services_impl.cpp create mode 100644 projects/stargazer/services_impl.h diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp new file mode 100644 index 00000000..400b34b3 --- /dev/null +++ b/projects/stargazer/services_impl.cpp @@ -0,0 +1,307 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#include +#include +#include + +#include "stg/admin.h" +#include "stg/common.h" +#include "services_impl.h" + +//----------------------------------------------------------------------------- +SERVICES_IMPL::SERVICES_IMPL(STORE * st) + : SERVICES(), + data(), + store(st), + WriteServLog(GetStgLogger()), + searchDescriptors(), + handle(0) +{ +pthread_mutex_init(&mutex, NULL); +ReadServices(); +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->adminChg) + { + string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +ADMIN_IMPL adm(0, login, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); + +if (ai != data.end()) + { + strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + + return -1; + } + +data.push_back(adm); + +if (store->AddAdmin(login) == 0) + { + WriteServLog("%s Administrator \'%s\' added.", + admin->GetLogStr().c_str(), login.c_str()); + return 0; + } + +strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError(); +WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + +return -1; +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::Del(const string & login, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +ADMIN_IMPL adm(0, login, ""); +const PRIV * priv = admin->GetPriv(); + +if (!priv->adminChg) + { + string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +admin_iter ai(find(data.begin(), data.end(), adm)); + +if (ai == data.end()) + { + strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + +map::iterator si; +si = searchDescriptors.begin(); +while (si != searchDescriptors.end()) + { + if (si->second == ai) + (si->second)++; + si++; + } + +data.remove(*ai); +if (store->DelAdmin(login) < 0) + { + strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError(); + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + + return -1; + } + +WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str()); +return 0; +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->adminChg) + { + string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +ADMIN_IMPL adm(0, ac.login, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); + +if (ai == data.end()) + { + strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + +*ai = ac; +if (store->SaveAdmin(ac)) + { + WriteServLog("Cannot write admin %s.", ac.login.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + return -1; + } + +WriteServLog("%s Administrator \'%s\' changed.", + admin->GetLogStr().c_str(), ac.login.c_str()); + +return 0; +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::ReadAdmins() +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +vector adminsList; +if (store->GetAdminsList(&adminsList) < 0) + { + WriteServLog(store->GetStrError().c_str()); + return -1; + } + +for (unsigned int i = 0; i < adminsList.size(); i++) + { + ADMIN_CONF ac(0, adminsList[i], ""); + + if (store->RestoreAdmin(&ac, adminsList[i])) + { + WriteServLog(store->GetStrError().c_str()); + return -1; + } + + data.push_back(ADMIN_IMPL(ac)); + } +return 0; +} +//----------------------------------------------------------------------------- +void SERVICES_IMPL::PrintAdmins() const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const_admin_iter ai(data.begin()); +while (ai != data.end()) + { + ai->Print(); + ai++; + } +} +//----------------------------------------------------------------------------- +bool SERVICES_IMPL::FindAdmin(const string & l, ADMIN ** admin) +{ +assert(admin != NULL && "Pointer to admin is not null"); + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + { + printfd(__FILE__, "no admin in system!\n"); + *admin = &noAdmin; + return false; + } + +ADMIN_IMPL adm(0, l, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); + +if (ai != data.end()) + { + *admin = &(*ai); + return false; + } + +return true; +} +//----------------------------------------------------------------------------- +bool SERVICES_IMPL::AdminExists(const string & login) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + { + printfd(__FILE__, "no admin in system!\n"); + return true; + } + +ADMIN_IMPL adm(0, login, ""); +const_admin_iter ai(find(data.begin(), data.end(), adm)); + +if (ai != data.end()) + return true; + +return false; +} +//----------------------------------------------------------------------------- +bool SERVICES_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + { + printfd(__FILE__, "no admin in system!\n"); + return true; + } + +ADMIN_IMPL adm(0, login, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); + +if (ai == data.end()) + { + return false; + } + +if (ai->GetPassword() != password) + { + return false; + } + +*admin = &(*ai); + +return true; +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::OpenSearch() const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +handle++; +searchDescriptors[handle] = data.begin(); +return handle; +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::SearchNext(int h, ADMIN_CONF * ac) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (searchDescriptors.find(h) == searchDescriptors.end()) + { + WriteServLog("SERVICES. Incorrect search handle."); + return -1; + } + +if (searchDescriptors[h] == data.end()) + return -1; + +ADMIN_IMPL a = *searchDescriptors[h]++; + +*ac = a.GetConf(); + +return 0; +} +//----------------------------------------------------------------------------- +int SERVICES_IMPL::CloseSearch(int h) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (searchDescriptors.find(h) != searchDescriptors.end()) + { + searchDescriptors.erase(searchDescriptors.find(h)); + return 0; + } + +WriteServLog("SERVICES. Incorrect search handle."); +return -1; +} +//----------------------------------------------------------------------------- diff --git a/projects/stargazer/services_impl.h b/projects/stargazer/services_impl.h new file mode 100644 index 00000000..4c7044fa --- /dev/null +++ b/projects/stargazer/services_impl.h @@ -0,0 +1,67 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef SERVICES_IMPL_H +#define SERVICES_IMPL_H + +#include + +#include +#include +#include + +#include "stg/services.h" +#include "stg/service_conf.h" +#include "stg/locker.h" +#include "stg/store.h" +#include "stg/noncopyable.h" + +class ADMIN; + +class SERVICES_IMPL : private NONCOPYABLE, public SERVICES { +public: + SERVICES_IMPL(STORE * st); + virtual SERVICES_IMPL() {} + + int Add(const SERVICE_CONF & service, const ADMIN * admin); + int Del(const std::string & name, const ADMIN * admin); + int Change(const SERVICE_CONF & service, const ADMIN * admin); + bool FindService(const std::string & name, SERVICE_CONF & service); + bool ServiceExists(const std::string & name) const; + const std::string & GetStrError() const; + + int OpenSearch() const; + int SearchNext(int, SERVICE_CONF * service) const; + int CloseSearch(int) const; + +private: + typedef list::iterator srv_iter; + typedef list::const_iterator const_srv_iter; + + bool ReadServices(); + + std::list data; + STORE * store; + STG_LOGGER & WriteServLog; + mutable std::map searchDescriptors; + mutable unsigned int handle; + mutable pthread_mutex_t mutex; + std::string strError; +}; -- 2.44.2 From 9ff682964a02109507959c858c2571e1e6780ee8 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 15:43:05 +0300 Subject: [PATCH 05/16] Fix some headers in admins implementation --- projects/stargazer/admins_impl.cpp | 2 -- projects/stargazer/admins_impl.h | 21 +++++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 3fe2c10b..53775717 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -32,8 +32,6 @@ #include #include -#include "stg/admins.h" -#include "stg/admin.h" #include "stg/common.h" #include "admins_impl.h" #include "admin_impl.h" diff --git a/projects/stargazer/admins_impl.h b/projects/stargazer/admins_impl.h index 170f3a4f..2ec99129 100644 --- a/projects/stargazer/admins_impl.h +++ b/projects/stargazer/admins_impl.h @@ -35,6 +35,7 @@ #include #include +#include #include "stg/admins.h" #include "stg/admin.h" @@ -48,8 +49,8 @@ public: ADMINS_IMPL(STORE * st); virtual ~ADMINS_IMPL() {} - int Add(const string & login, const ADMIN * admin); - int Del(const string & login, const ADMIN * admin); + int Add(const std::string & login, const ADMIN * admin); + int Del(const std::string & login, const ADMIN * admin); int Change(const ADMIN_CONF & ac, const ADMIN * admin); void PrintAdmins() const; const ADMIN * GetSysAdmin() const { return &stg; } @@ -71,15 +72,15 @@ private: int ReadAdmins(); - ADMIN_IMPL stg; - ADMIN_IMPL noAdmin; - list data; - STORE * store; - STG_LOGGER & WriteServLog; - mutable map searchDescriptors; - mutable unsigned int handle; + ADMIN_IMPL stg; + ADMIN_IMPL noAdmin; + std::list data; + STORE * store; + STG_LOGGER & WriteServLog; + mutable std::map searchDescriptors; + mutable unsigned int handle; mutable pthread_mutex_t mutex; - std::string strError; + std::string strError; }; #endif -- 2.44.2 From 82ede2a92a721d1c8f0e3fd109a2594f2096306e Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 15:43:58 +0300 Subject: [PATCH 06/16] Admin, service and corporation sesnsors added for SUMX --- .../stargazer/plugins/other/smux/STG-MIB.mib | 27 +++++++++ .../stargazer/plugins/other/smux/sensors.h | 60 +++++++++++++++++++ .../stargazer/plugins/other/smux/smux.cpp | 14 +++++ projects/stargazer/plugins/other/smux/smux.h | 13 +++- 4 files changed, 111 insertions(+), 3 deletions(-) diff --git a/projects/stargazer/plugins/other/smux/STG-MIB.mib b/projects/stargazer/plugins/other/smux/STG-MIB.mib index d41edc52..4d9179bf 100644 --- a/projects/stargazer/plugins/other/smux/STG-MIB.mib +++ b/projects/stargazer/plugins/other/smux/STG-MIB.mib @@ -183,4 +183,31 @@ totalTariffs OBJECT-TYPE DEFVAL { 0 } ::= { tariffs 1 } +totalAdmins OBJECT-TYPE + SYNTAX Integer32 + MAX-ACCESS read-only + STATUS current + DESCRIPTION + "Total admins registered in the billing" + DEFVAL { 0 } + ::= { admins 1 } + +totalServices OBJECT-TYPE + SYNTAX Integer32 + MAX-ACCESS read-only + STATUS current + DESCRIPTION + "Total services registered in the billing" + DEFVAL { 0 } + ::= { services 1 } + +totalCorporations OBJECT-TYPE + SYNTAX Integer32 + MAX-ACCESS read-only + STATUS current + DESCRIPTION + "Total corporations registered in the billing" + DEFVAL { 0 } + ::= { corporations 1 } + END diff --git a/projects/stargazer/plugins/other/smux/sensors.h b/projects/stargazer/plugins/other/smux/sensors.h index f2840636..16331b15 100644 --- a/projects/stargazer/plugins/other/smux/sensors.h +++ b/projects/stargazer/plugins/other/smux/sensors.h @@ -178,6 +178,66 @@ class TotalTariffsSensor : public Sensor { const TARIFFS & tariffs; }; +class TotalAdminsSensor : public Sensor { + public: + TotalAdminsSensor(const ADMINS & a) : admins(a) {} + virtual ~TotalAdminsSensor() {} + + bool GetValue(ObjectSyntax_t * objectSyntax) const + { + ValueToOS(admins.GetAdminsNum(), objectSyntax); + return true; + } + +#ifdef DEBUG + std::string ToString() const + { std::string res; x2str(admins.GetAdminsNum(), res); return res; } +#endif + + private: + const ADMINS & admins; +}; + +class TotalServicesSensor : public Sensor { + public: + TotalServicesSensor(const SERVICES & s) : services(s) {} + virtual ~TotalServicesSensor() {} + + bool GetValue(ObjectSyntax_t * objectSyntax) const + { + ValueToOS(services.GetServicesNum(), objectSyntax); + return true; + } + +#ifdef DEBUG + std::string ToString() const + { std::string res; x2str(services.GetServicesNum(), res); return res; } +#endif + + private: + const SERVICES & services; +}; + +class TotalCorporationsSensor : public Sensor { + public: + TotalCorporationsSensor(const CORPORATIONS & c) : corporations(c) {} + virtual ~TotalCorporationsSensor() {} + + bool GetValue(ObjectSyntax_t * objectSyntax) const + { + ValueToOS(corporations.GetCorporationsNum(), objectSyntax); + return true; + } + +#ifdef DEBUG + std::string ToString() const + { std::string res; x2str(services.GetCorporationsNum(), res); return res; } +#endif + + private: + const CORPORATIONS & corporations; +}; + template class ConstSensor : public Sensor { public: diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index 79e9d208..ac77ce6e 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -15,6 +15,11 @@ #include "stg/common.h" #include "stg/plugin_creator.h" +#include "stg/users.h" +#include "stg/tariffs.h" +#include "stg/admins.h" +#include "stg/services.h" +#include "stg/corporations.h" #include "smux.h" #include "utils.h" @@ -89,6 +94,9 @@ SMUX::SMUX() : PLUGIN(), users(NULL), tariffs(NULL), + admins(NULL), + services(NULL), + corporations(NULL), running(false), stopped(true), sock(-1) @@ -145,6 +153,12 @@ sensors[OID(".1.3.6.1.4.1.38313.1.1.11")] = new FreeMbUsersSensor(*users); sensors[OID(".1.3.6.1.4.1.38313.1.1.12")] = new TariffChangeUsersSensor(*users); // Tariffs sensors[OID(".1.3.6.1.4.1.38313.1.2.1")] = new TotalTariffsSensor(*tariffs); +// Admins +sensors[OID(".1.3.6.1.4.1.38313.1.3.1")] = new TotalAdminsSensor(*admins); +// Services +sensors[OID(".1.3.6.1.4.1.38313.1.4.1")] = new TotalServicesSensor(*services); +// Corporations +sensors[OID(".1.3.6.1.4.1.38313.1.5.1")] = new TotalCorporationsSensor(*corporations); // Table data tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *users); diff --git a/projects/stargazer/plugins/other/smux/smux.h b/projects/stargazer/plugins/other/smux/smux.h index 08e23c38..7ab603df 100644 --- a/projects/stargazer/plugins/other/smux/smux.h +++ b/projects/stargazer/plugins/other/smux/smux.h @@ -12,8 +12,6 @@ #include "stg/os_int.h" #include "stg/plugin.h" #include "stg/module_settings.h" -#include "stg/users.h" -#include "stg/tariffs.h" #include "sensors.h" #include "tables.h" @@ -24,6 +22,10 @@ extern "C" PLUGIN * GetPlugin(); class USER; class SETTINGS; class SMUX; +class USERS; +class TARIFFS; +class SERVICES; +class CORPORATIONS; typedef bool (SMUX::*SMUXPacketHandler)(const SMUX_PDUs_t * pdus); typedef bool (SMUX::*PDUsHandler)(const PDUs_t * pdus); @@ -56,7 +58,9 @@ public: void SetUsers(USERS * u) { users = u; } void SetTariffs(TARIFFS * t) { tariffs = t; } - void SetAdmins(ADMINS *) {} + void SetAdmins(ADMINS * a) { admins = a; } + void SetServices(SERVICES * s) { services = s; } + void SetCorporations(CORPORATIONS * c) { corporations = c; } void SetTraffcounter(TRAFFCOUNTER *) {} void SetStore(STORE *) {} void SetStgSettings(const SETTINGS *) {} @@ -93,6 +97,9 @@ private: USERS * users; TARIFFS * tariffs; + ADMINS * admins; + SERVICES * services; + CORPORATIONS * corporations; mutable std::string errorStr; SMUX_SETTINGS smuxSettings; -- 2.44.2 From 8141c8c568b83be890c8d8d3953e976ad0ad8ad1 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:04:25 +0300 Subject: [PATCH 07/16] Simplified admins API --- include/stg/admins.h | 11 ++++++----- projects/stargazer/admins_impl.cpp | 10 +++++----- projects/stargazer/admins_impl.h | 15 +++++++++------ 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/include/stg/admins.h b/include/stg/admins.h index 169f1a26..7536a4a5 100644 --- a/include/stg/admins.h +++ b/include/stg/admins.h @@ -33,12 +33,13 @@ public: virtual int Change(const ADMIN_CONF & ac, const ADMIN * admin) = 0; virtual const ADMIN * GetSysAdmin() const = 0; virtual const ADMIN * GetNoAdmin() const = 0; - virtual bool FindAdmin(const std::string & l, ADMIN ** admin) = 0; - virtual bool AdminExists(const std::string & login) const = 0; - virtual bool AdminCorrect(const std::string & login, - const std::string & password, - ADMIN ** admin) = 0; + virtual bool Find(const std::string & l, ADMIN ** admin) = 0; + virtual bool Exists(const std::string & login) const = 0; + virtual bool Correct(const std::string & login, + const std::string & password, + ADMIN ** admin) = 0; virtual const std::string & GetStrError() const = 0; + virtual size_t Count() const = 0; virtual int OpenSearch() const = 0; virtual int SearchNext(int, ADMIN_CONF * ac) const = 0; diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 53775717..a780fcb3 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -50,7 +50,7 @@ ADMINS_IMPL::ADMINS_IMPL(STORE * st) handle(0) { pthread_mutex_init(&mutex, NULL); -ReadAdmins(); +Read(); } //----------------------------------------------------------------------------- int ADMINS_IMPL::Add(const string & login, const ADMIN * admin) @@ -174,7 +174,7 @@ WriteServLog("%s Administrator \'%s\' changed.", return 0; } //----------------------------------------------------------------------------- -int ADMINS_IMPL::ReadAdmins() +int ADMINS_IMPL::Read() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); vector adminsList; @@ -210,7 +210,7 @@ while (ai != data.end()) } } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::FindAdmin(const string & l, ADMIN ** admin) +bool ADMINS_IMPL::Find(const string & l, ADMIN ** admin) { assert(admin != NULL && "Pointer to admin is not null"); @@ -234,7 +234,7 @@ if (ai != data.end()) return true; } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::AdminExists(const string & login) const +bool ADMINS_IMPL::Exists(const string & login) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) @@ -252,7 +252,7 @@ if (ai != data.end()) return false; } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin) +bool ADMINS_IMPL::Correct(const string & login, const std::string & password, ADMIN ** admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) diff --git a/projects/stargazer/admins_impl.h b/projects/stargazer/admins_impl.h index 2ec99129..a1ca0f98 100644 --- a/projects/stargazer/admins_impl.h +++ b/projects/stargazer/admins_impl.h @@ -42,6 +42,7 @@ #include "stg/locker.h" #include "stg/store.h" #include "stg/noncopyable.h" +#include "stg/logger.h" #include "admin_impl.h" class ADMINS_IMPL : private NONCOPYABLE, public ADMINS { @@ -55,13 +56,15 @@ public: void PrintAdmins() const; const ADMIN * GetSysAdmin() const { return &stg; } const ADMIN * GetNoAdmin() const { return &noAdmin; } - bool FindAdmin(const std::string & l, ADMIN ** admin); - bool AdminExists(const std::string & login) const; - bool AdminCorrect(const std::string & login, - const std::string & password, - ADMIN ** admin); + bool Find(const std::string & l, ADMIN ** admin); + bool Exists(const std::string & login) const; + bool Correct(const std::string & login, + const std::string & password, + ADMIN ** admin); const std::string & GetStrError() const { return strError; } + size_t Count() const { return data.size(); } + int OpenSearch() const; int SearchNext(int, ADMIN_CONF * ac) const; int CloseSearch(int) const; @@ -70,7 +73,7 @@ private: typedef list::iterator admin_iter; typedef list::const_iterator const_admin_iter; - int ReadAdmins(); + int Read(); ADMIN_IMPL stg; ADMIN_IMPL noAdmin; -- 2.44.2 From 13121b693bd98a04532195b9631b862b6136b3c7 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:06:02 +0300 Subject: [PATCH 08/16] More clear plugin API --- include/stg/plugin.h | 18 +++++++++------ .../stargazer/plugins/authorization/ao/ao.h | 5 ----- .../authorization/inetaccess/inetaccess.h | 4 ---- .../plugins/authorization/stress/stress.h | 5 ----- .../plugins/capture/cap_debug/debug_cap.h | 6 ----- .../stargazer/plugins/capture/cap_nf/cap_nf.h | 5 ----- .../capture/divert_freebsd/divert_cap.h | 5 ----- .../plugins/capture/ether_freebsd/ether_cap.h | 5 ----- .../plugins/capture/ether_linux/ether_cap.h | 6 ----- .../plugins/capture/ipq_linux/ipq_cap.h | 6 ----- .../configuration/rpcconfig/rpcconfig.h | 1 - .../configuration/sgconfig-ng/stgconfig.h | 1 - .../configuration/sgconfig/stgconfig.h | 1 - .../configuration/sgconfig2/stgconfig.h | 1 - .../plugins/configuration/xrconfig/xrconfig.h | 1 - projects/stargazer/plugins/other/ping/ping.h | 5 ----- .../stargazer/plugins/other/radius/radius.h | 3 --- .../stargazer/plugins/other/rscript/rscript.h | 5 ----- projects/stargazer/plugins/other/smux/smux.h | 3 --- .../plugins/other/userstat/userstat.h | 22 ++++++++----------- 20 files changed, 20 insertions(+), 88 deletions(-) diff --git a/include/stg/plugin.h b/include/stg/plugin.h index 35a5a4c9..845a3d15 100644 --- a/include/stg/plugin.h +++ b/include/stg/plugin.h @@ -35,6 +35,8 @@ #include "admins.h" #include "users.h" #include "tariffs.h" +#include "services.h" +#include "corporations.h" class TRAFFCOUNTER; class SETTINGS; @@ -43,13 +45,15 @@ struct MODULE_SETTINGS; class PLUGIN : private NONCOPYABLE { public: - virtual void SetUsers(USERS * u) = 0; - virtual void SetTariffs(TARIFFS * t) = 0; - virtual void SetAdmins(ADMINS * a) = 0; - virtual void SetTraffcounter(TRAFFCOUNTER * tc) = 0; - virtual void SetStore(STORE * st) = 0; - virtual void SetStgSettings(const SETTINGS * s) = 0; - virtual void SetSettings(const MODULE_SETTINGS & s) = 0; + virtual void SetUsers(USERS *) {} + virtual void SetTariffs(TARIFFS *) {} + virtual void SetAdmins(ADMINS *) {} + virtual void SetServices(SERVICES *) {} + virtual void SetCorporations(CORPORATIONS *) {} + virtual void SetTraffcounter(TRAFFCOUNTER *) {} + virtual void SetStore(STORE *) {} + virtual void SetStgSettings(const SETTINGS *) {} + virtual void SetSettings(const MODULE_SETTINGS &) {} virtual int ParseSettings() = 0; virtual int Start() = 0; diff --git a/projects/stargazer/plugins/authorization/ao/ao.h b/projects/stargazer/plugins/authorization/ao/ao.h index a0c0953d..cac18dfd 100644 --- a/projects/stargazer/plugins/authorization/ao/ao.h +++ b/projects/stargazer/plugins/authorization/ao/ao.h @@ -73,11 +73,6 @@ public: virtual ~AUTH_AO(){}; void SetUsers(USERS * u) { users = u; } - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} - void SetTraffcounter(TRAFFCOUNTER *) {} - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); diff --git a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h index fe556495..a648ec04 100644 --- a/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h +++ b/projects/stargazer/plugins/authorization/inetaccess/inetaccess.h @@ -204,10 +204,6 @@ public: virtual ~AUTH_IA(); void SetUsers(USERS * u) { users = u; } - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} - void SetTraffcounter(TRAFFCOUNTER *) {} - void SetStore(STORE *) {} void SetStgSettings(const SETTINGS * s) { stgSettings = s; } void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/authorization/stress/stress.h b/projects/stargazer/plugins/authorization/stress/stress.h index bfcf56d0..fe9ccd9d 100644 --- a/projects/stargazer/plugins/authorization/stress/stress.h +++ b/projects/stargazer/plugins/authorization/stress/stress.h @@ -88,11 +88,6 @@ public: virtual ~AUTH_STRESS() {} void SetUsers(USERS * u); - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} - void SetTraffcounter(TRAFFCOUNTER *) {} - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); diff --git a/projects/stargazer/plugins/capture/cap_debug/debug_cap.h b/projects/stargazer/plugins/capture/cap_debug/debug_cap.h index 207aa40e..5e5e2cf6 100644 --- a/projects/stargazer/plugins/capture/cap_debug/debug_cap.h +++ b/projects/stargazer/plugins/capture/cap_debug/debug_cap.h @@ -68,18 +68,12 @@ public: DEBUG_CAP(); virtual ~DEBUG_CAP() {} - void SetUsers(USERS * u) {} - void SetTariffs(TARIFFS * t) {} - void SetAdmins(ADMINS * a) {} void SetTraffcounter(TRAFFCOUNTER * tc); - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); int Reload() { return 0; } int ParseSettings() { return 0; } - void SetSettings(const MODULE_SETTINGS & s) {} bool IsRunning(); const string & GetStrError() const; const string GetVersion() const; diff --git a/projects/stargazer/plugins/capture/cap_nf/cap_nf.h b/projects/stargazer/plugins/capture/cap_nf/cap_nf.h index 80373433..ccdd6708 100644 --- a/projects/stargazer/plugins/capture/cap_nf/cap_nf.h +++ b/projects/stargazer/plugins/capture/cap_nf/cap_nf.h @@ -92,12 +92,7 @@ public: NF_CAP(); ~NF_CAP(); - void SetUsers(USERS *) {} - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} void SetTraffcounter(TRAFFCOUNTER * tc) { traffCnt = tc; } - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h index f5bb432b..432ee4b1 100644 --- a/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h +++ b/projects/stargazer/plugins/capture/divert_freebsd/divert_cap.h @@ -48,12 +48,7 @@ public: DIVERT_CAP(); virtual ~DIVERT_CAP() {} - void SetUsers(USERS *) {} - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} void SetTraffcounter(TRAFFCOUNTER * tc) { traffCnt = tc; } - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); diff --git a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.h b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.h index 05357212..b51cc56d 100644 --- a/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.h +++ b/projects/stargazer/plugins/capture/ether_freebsd/ether_cap.h @@ -95,12 +95,7 @@ public: BPF_CAP(); virtual ~BPF_CAP() {} - void SetUsers(USERS *) {} - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} void SetTraffcounter(TRAFFCOUNTER * tc) { traffCnt = tc; } - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); diff --git a/projects/stargazer/plugins/capture/ether_linux/ether_cap.h b/projects/stargazer/plugins/capture/ether_linux/ether_cap.h index ed7ba299..c03a6066 100644 --- a/projects/stargazer/plugins/capture/ether_linux/ether_cap.h +++ b/projects/stargazer/plugins/capture/ether_linux/ether_cap.h @@ -49,19 +49,13 @@ public: ETHER_CAP(); virtual ~ETHER_CAP() {} - void SetUsers(USERS *) {} - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} void SetTraffcounter(TRAFFCOUNTER * tc) { traffCnt = tc; } - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); int Reload() { return 0; } bool IsRunning() { return isRunning; } - void SetSettings(const MODULE_SETTINGS &) {} int ParseSettings() { return 0; } const std::string & GetStrError() const { return errorStr; } const std::string GetVersion() const; diff --git a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.h b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.h index 6253fe3a..990aae3b 100644 --- a/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.h +++ b/projects/stargazer/plugins/capture/ipq_linux/ipq_cap.h @@ -44,19 +44,13 @@ public: IPQ_CAP(); virtual ~IPQ_CAP() {} - void SetUsers(USERS *) {} - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} void SetTraffcounter(TRAFFCOUNTER * tc) { traffCnt = tc; } - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} int Start(); int Stop(); int Reload() { return 0; } bool IsRunning() { return isRunning; } - void SetSettings(const MODULE_SETTINGS &) {} int ParseSettings() { return 0; } const std::string & GetStrError() const { return errorStr; } const std::string GetVersion() const; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h index 1213f7d3..1095dd73 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.h @@ -58,7 +58,6 @@ public: void SetTariffs(TARIFFS * t) { tariffs = t; } void SetAdmins(ADMINS * a) { admins = a; } void SetStore(STORE * s) { store = s; } - void SetTraffcounter(TRAFFCOUNTER *) {} void SetStgSettings(const SETTINGS * s); void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/configuration/sgconfig-ng/stgconfig.h b/projects/stargazer/plugins/configuration/sgconfig-ng/stgconfig.h index 329a114c..fd3a9e62 100644 --- a/projects/stargazer/plugins/configuration/sgconfig-ng/stgconfig.h +++ b/projects/stargazer/plugins/configuration/sgconfig-ng/stgconfig.h @@ -29,7 +29,6 @@ public: void SetTariffs(TARIFFS * t) { tariffs = t; } void SetAdmins(ADMINS * a) { admins = a; } void SetStore(STORE * s) { store = s; } - void SetTraffcounter(TRAFFCOUNTER *) {} void SetStgSettings(const SETTINGS * s) { stgSettings = s; } void SetSettings(const MODULE_SETTINGS & s) { modSettings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h index ce1c3af8..d1506ea8 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h +++ b/projects/stargazer/plugins/configuration/sgconfig/stgconfig.h @@ -34,7 +34,6 @@ public: void SetTariffs(TARIFFS * t) { tariffs = t; } void SetAdmins(ADMINS * a) { admins = a; } void SetStore(STORE * s) { store = s; } - void SetTraffcounter(TRAFFCOUNTER *) {} void SetStgSettings(const SETTINGS * s) { stgSettings = s; } void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h b/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h index 1c8743d1..52d78018 100644 --- a/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h +++ b/projects/stargazer/plugins/configuration/sgconfig2/stgconfig.h @@ -35,7 +35,6 @@ public: void SetTariffs(TARIFFS * t) { tariffs = t; } void SetAdmins(ADMINS * a) { admins = a; } void SetStore(STORE * s) { store = s; } - void SetTraffcounter(TRAFFCOUNTER *) {} void SetStgSettings(const SETTINGS * s) { stgConfigSettings = s; } void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h b/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h index 4630edb4..219f8d22 100644 --- a/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h +++ b/projects/stargazer/plugins/configuration/xrconfig/xrconfig.h @@ -34,7 +34,6 @@ public: void SetTariffs(TARIFFS * t); void SetAdmins(ADMINS * a); void SetStore(BASE_STORE * s); - void SetTraffcounter(TRAFFCOUNTER * tc){}; void SetStgSettings(const SETTINGS * s); void SetSettings(const MODULE_SETTINGS & s); int ParseSettings(); diff --git a/projects/stargazer/plugins/other/ping/ping.h b/projects/stargazer/plugins/other/ping/ping.h index aecbfb15..618ecc73 100644 --- a/projects/stargazer/plugins/other/ping/ping.h +++ b/projects/stargazer/plugins/other/ping/ping.h @@ -88,11 +88,6 @@ public: virtual ~PING(); void SetUsers(USERS * u); - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} - void SetTraffcounter(TRAFFCOUNTER *) {} - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} void SetSettings(const MODULE_SETTINGS & s); int ParseSettings(); diff --git a/projects/stargazer/plugins/other/radius/radius.h b/projects/stargazer/plugins/other/radius/radius.h index 70c904ae..2a60f117 100644 --- a/projects/stargazer/plugins/other/radius/radius.h +++ b/projects/stargazer/plugins/other/radius/radius.h @@ -86,9 +86,6 @@ public: virtual ~RADIUS() {}; void SetUsers(USERS * u); - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} - void SetTraffcounter(TRAFFCOUNTER *) {} void SetStore(STORE * ); void SetStgSettings(const SETTINGS * s); void SetSettings(const MODULE_SETTINGS & s); diff --git a/projects/stargazer/plugins/other/rscript/rscript.h b/projects/stargazer/plugins/other/rscript/rscript.h index 24d44763..649970e9 100644 --- a/projects/stargazer/plugins/other/rscript/rscript.h +++ b/projects/stargazer/plugins/other/rscript/rscript.h @@ -129,11 +129,6 @@ public: virtual ~REMOTE_SCRIPT(); void SetUsers(USERS * u) { users = u; } - void SetTariffs(TARIFFS *) {} - void SetAdmins(ADMINS *) {} - void SetTraffcounter(TRAFFCOUNTER *) {} - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/other/smux/smux.h b/projects/stargazer/plugins/other/smux/smux.h index 7ab603df..5230a85c 100644 --- a/projects/stargazer/plugins/other/smux/smux.h +++ b/projects/stargazer/plugins/other/smux/smux.h @@ -61,9 +61,6 @@ public: void SetAdmins(ADMINS * a) { admins = a; } void SetServices(SERVICES * s) { services = s; } void SetCorporations(CORPORATIONS * c) { corporations = c; } - void SetTraffcounter(TRAFFCOUNTER *) {} - void SetStore(STORE *) {} - void SetStgSettings(const SETTINGS *) {} void SetSettings(const MODULE_SETTINGS & s) { settings = s; } int ParseSettings(); diff --git a/projects/stargazer/plugins/other/userstat/userstat.h b/projects/stargazer/plugins/other/userstat/userstat.h index 9dd01f25..1c5776f8 100644 --- a/projects/stargazer/plugins/other/userstat/userstat.h +++ b/projects/stargazer/plugins/other/userstat/userstat.h @@ -46,27 +46,23 @@ public: USERSTAT(); ~USERSTAT(); - virtual void SetUsers(USERS * u) { users = u; }; - virtual void SetTariffs(TARIFFS * t) {}; - virtual void SetAdmins(ADMINS * a) {}; - virtual void SetTraffcounter(TRAFFCOUNTER * tc) {}; - virtual void SetStore(BASE_STORE * st) { store = st; }; - virtual void SetStgSettings(const SETTINGS * s) {}; - virtual void SetSettings(const MODULE_SETTINGS & s) { settings = s; }; + virtual void SetUsers(USERS * u) { users = u; } + virtual void SetStore(BASE_STORE * st) { store = st; } + virtual void SetSettings(const MODULE_SETTINGS & s) { settings = s; } virtual int ParseSettings(); virtual int Start(); virtual int Stop(); - virtual bool IsRunning() { return isRunning; }; - virtual const string & GetStrError() const { return errorStr; }; - virtual const string GetVersion() const { return version; }; - virtual uint16_t GetStartPosition() const { return 0; }; - virtual uint16_t GetStopPosition() const { return 0; }; + virtual bool IsRunning() { return isRunning; } + virtual const string & GetStrError() const { return errorStr; } + virtual const string GetVersion() const { return version; } + virtual uint16_t GetStartPosition() const { return 0; } + virtual uint16_t GetStopPosition() const { return 0; } private: struct IsDone : public unary_function { - bool operator()(const DataThread & info) { return info.IsDone(); }; + bool operator()(const DataThread & info) { return info.IsDone(); } }; struct ToLower : public unary_function { -- 2.44.2 From 02be813c72c75c61beaef0266bd5adf713e67b42 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:06:55 +0300 Subject: [PATCH 09/16] Fix compilation errors got after API changes --- .../configuration/rpcconfig/admins_methods.cpp | 10 +++++----- .../plugins/configuration/rpcconfig/info_methods.cpp | 4 ++-- .../plugins/configuration/rpcconfig/rpcconfig.cpp | 2 +- .../configuration/rpcconfig/tariffs_methods.cpp | 6 +++--- .../configuration/rpcconfig/users_methods.cpp | 12 ++++++------ .../plugins/configuration/sgconfig/parser.cpp | 4 ++-- .../plugins/configuration/sgconfig/rsconf.cpp | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/projects/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp b/projects/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp index 2dbc9a7d..3607a806 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp @@ -23,7 +23,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(login, &admin)) +if (admins->Find(login, &admin)) { structVal["result"] = xmlrpc_c::value_boolean(false); *retvalPtr = xmlrpc_c::value_struct(structVal); @@ -67,7 +67,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Invalid admin (logged)'\n"); *retvalPtr = xmlrpc_c::value_boolean(false); @@ -104,7 +104,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -139,7 +139,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * loggedAdmin; -if (admins->FindAdmin(adminInfo.admin, &loggedAdmin)) +if (admins->Find(adminInfo.admin, &loggedAdmin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -147,7 +147,7 @@ if (admins->FindAdmin(adminInfo.admin, &loggedAdmin)) ADMIN * admin; -if (admins->FindAdmin(login, &admin)) +if (admins->Find(login, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/info_methods.cpp b/projects/stargazer/plugins/configuration/rpcconfig/info_methods.cpp index 91f910e0..b971c638 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/info_methods.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/info_methods.cpp @@ -26,9 +26,9 @@ un += " "; un += utsn.nodename; structVal["version"] = xmlrpc_c::value_string(SERVER_VERSION); -structVal["tariff_num"] = xmlrpc_c::value_int(tariffs->GetTariffsNum()); +structVal["tariff_num"] = xmlrpc_c::value_int(tariffs->Count()); structVal["tariff"] = xmlrpc_c::value_int(2); -structVal["users_num"] = xmlrpc_c::value_int(users->GetUserNum()); +structVal["users_num"] = xmlrpc_c::value_int(users->Count()); structVal["uname"] = xmlrpc_c::value_string(un); structVal["dir_num"] = xmlrpc_c::value_int(DIR_NUM); structVal["day_fee"] = xmlrpc_c::value_int(dayFee); diff --git a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index 7968e0dc..1f2b8e18 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -204,7 +204,7 @@ bool RPC_CONFIG::CheckAdmin(const std::string & login, { ADMIN * admin = NULL; -if (!admins->AdminCorrect(login, password, &admin)) +if (!admins->Correct(login, password, &admin)) { return true; } diff --git a/projects/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp b/projects/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp index 76142053..0b110d54 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp @@ -57,7 +57,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -138,7 +138,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -170,7 +170,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; diff --git a/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp b/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp index fcd81a5e..4932e146 100644 --- a/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp +++ b/projects/stargazer/plugins/configuration/rpcconfig/users_methods.cpp @@ -72,7 +72,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin = NULL; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -116,7 +116,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -208,7 +208,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -261,7 +261,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -311,7 +311,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -359,7 +359,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) ADMIN * admin; -if (admins->FindAdmin(adminInfo.admin, &admin)) +if (admins->Find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; diff --git a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp index 30252544..c933b5fe 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/parser.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/parser.cpp @@ -63,13 +63,13 @@ answerList->push_back(""); sprintf(s, "", SERVER_VERSION); answerList->push_back(s); -sprintf(s, "", tariffs->GetTariffsNum()); +sprintf(s, "", tariffs->Count()); answerList->push_back(s); sprintf(s, "", 2); answerList->push_back(s); -sprintf(s, "", users->GetUserNum()); +sprintf(s, "", users->Count()); answerList->push_back(s); sprintf(s, "", un); diff --git a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp index f494ee40..209ea3b2 100644 --- a/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp +++ b/projects/stargazer/plugins/configuration/sgconfig/rsconf.cpp @@ -317,7 +317,7 @@ if (ret < ADM_LOGIN_LEN) return ENODATA; } -if (admins->FindAdmin(login, &currAdmin)) +if (admins->Find(login, &currAdmin)) { // Admin not found state = confHdr; -- 2.44.2 From 0a5045398fd1810d585c00d1e0a6700648c43195 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:07:49 +0300 Subject: [PATCH 10/16] GetNum -> Count --- projects/stargazer/tariffs_impl.cpp | 2 +- projects/stargazer/tariffs_impl.h | 2 +- projects/stargazer/users_impl.cpp | 5 ----- projects/stargazer/users_impl.h | 2 +- 4 files changed, 3 insertions(+), 8 deletions(-) diff --git a/projects/stargazer/tariffs_impl.cpp b/projects/stargazer/tariffs_impl.cpp index d2ea7c9a..a912c338 100644 --- a/projects/stargazer/tariffs_impl.cpp +++ b/projects/stargazer/tariffs_impl.cpp @@ -86,7 +86,7 @@ for (int i = 0; i < tariffsNum; i++) return 0; } //----------------------------------------------------------------------------- -int TARIFFS_IMPL::GetTariffsNum() const +size_t TARIFFS_IMPL::Count() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); return tariffs.size(); diff --git a/projects/stargazer/tariffs_impl.h b/projects/stargazer/tariffs_impl.h index b8aac298..cef5fed7 100644 --- a/projects/stargazer/tariffs_impl.h +++ b/projects/stargazer/tariffs_impl.h @@ -60,7 +60,7 @@ public: int ReadTariffs (); const TARIFF * FindByName(const std::string & name) const; const TARIFF * GetNoTariff() const { return &noTariff; }; - int GetTariffsNum() const; + size_t Count() const; int Del(const std::string & name, const ADMIN * admin); int Add(const std::string & name, const ADMIN * admin); int Chg(const TARIFF_DATA & td, const ADMIN * admin); diff --git a/projects/stargazer/users_impl.cpp b/projects/stargazer/users_impl.cpp index febd90f5..5ad112b9 100644 --- a/projects/stargazer/users_impl.cpp +++ b/projects/stargazer/users_impl.cpp @@ -561,11 +561,6 @@ while (iter != usersToDelete.end()) return; } //----------------------------------------------------------------------------- -int USERS_IMPL::GetUserNum() const -{ -return users.size(); -} -//----------------------------------------------------------------------------- void USERS_IMPL::AddToIPIdx(user_iter user) { printfd(__FILE__, "USERS: Add IP Idx\n"); diff --git a/projects/stargazer/users_impl.h b/projects/stargazer/users_impl.h index 46b17139..4c586b06 100644 --- a/projects/stargazer/users_impl.h +++ b/projects/stargazer/users_impl.h @@ -114,7 +114,7 @@ public: void Del(const std::string & login, const ADMIN * admin); int ReadUsers(); - int GetUserNum() const; + size_t Count() const { return users.size(); } int FindByIPIdx(uint32_t ip, USER_PTR * user) const; int FindByIPIdx(uint32_t ip, USER_IMPL ** user) const; -- 2.44.2 From d25555ce9c6e09344356868780e678c75bbbd164 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:08:32 +0300 Subject: [PATCH 11/16] Basic corporations implementation added --- include/stg/corp_conf.h | 7 + include/stg/corporations.h | 1 + projects/stargazer/corps_impl.cpp | 258 ++++++++++++++++++++++++++++++ projects/stargazer/corps_impl.h | 70 ++++++++ 4 files changed, 336 insertions(+) create mode 100644 projects/stargazer/corps_impl.cpp create mode 100644 projects/stargazer/corps_impl.h diff --git a/include/stg/corp_conf.h b/include/stg/corp_conf.h index e29a5dcf..d6cc2ebb 100644 --- a/include/stg/corp_conf.h +++ b/include/stg/corp_conf.h @@ -5,6 +5,7 @@ struct CORP_CONF { +CORP_CONF() : name(), cash(0) {} CORP_CONF(const std::string & n) : name(n), cash(0) {} CORP_CONF(const std::string & n, double c) : name(n), cash(c) {} @@ -12,4 +13,10 @@ std::string name; double cash; }; +inline +bool operator==(const CORP_CONF & a, const CORP_CONF & b) +{ +return a.name == b.name; +} + #endif //CORP_CONF_H diff --git a/include/stg/corporations.h b/include/stg/corporations.h index cc147fc6..ad23967e 100644 --- a/include/stg/corporations.h +++ b/include/stg/corporations.h @@ -33,6 +33,7 @@ public: virtual bool FindCorp(const std::string & name, CORP_CONF & corp) = 0; virtual bool CorpExists(const std::string & name) const = 0; virtual const std::string & GetStrError() const = 0; + virtual size_t Count() const = 0; virtual int OpenSearch() const = 0; virtual int SearchNext(int, CORP_CONF * corp) const = 0; diff --git a/projects/stargazer/corps_impl.cpp b/projects/stargazer/corps_impl.cpp new file mode 100644 index 00000000..456523dc --- /dev/null +++ b/projects/stargazer/corps_impl.cpp @@ -0,0 +1,258 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#include +#include +#include + +#include "stg/admin.h" +#include "stg/common.h" +#include "corps_impl.h" + +//----------------------------------------------------------------------------- +CORPORATIONS_IMPL::CORPORATIONS_IMPL(STORE * st) + : CORPORATIONS(), + data(), + store(st), + WriteServLog(GetStgLogger()), + searchDescriptors(), + handle(0) +{ +pthread_mutex_init(&mutex, NULL); +Read(); +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::Add(const CORP_CONF & corp, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->corpChg) + { + string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +crp_iter si(find(data.begin(), data.end(), corp)); + +if (si != data.end()) + { + strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + + return -1; + } + +data.push_back(corp); + +if (store->AddCorp(corp.name) == 0) + { + WriteServLog("%s Corporation \'%s\' added.", + admin->GetLogStr().c_str(), corp.name.c_str()); + return 0; + } + +strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError(); +WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + +return -1; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::Del(const string & name, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->corpChg) + { + string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); + +if (si == data.end()) + { + strError = "Corporation \'" + name + "\' cannot be deleted. Corporation does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + +map::iterator csi; +csi = searchDescriptors.begin(); +while (csi != searchDescriptors.end()) + { + if (csi->second == si) + (csi->second)++; + csi++; + } + +data.remove(*si); +if (store->DelCorp(name) < 0) + { + strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError(); + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + + return -1; + } + +WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); +return 0; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::Change(const CORP_CONF & corp, const ADMIN * admin) +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +const PRIV * priv = admin->GetPriv(); + +if (!priv->corpChg) + { + string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied."; + strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; + } + +crp_iter si(find(data.begin(), data.end(), corp)); + +if (si == data.end()) + { + strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation does not exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + return -1; + } + +*si = corp; +if (store->SaveCorp(corp)) + { + WriteServLog("Cannot write corporation %s.", corp.name.c_str()); + WriteServLog("%s", store->GetStrError().c_str()); + return -1; + } + +WriteServLog("%s Corporation \'%s\' changed.", + admin->GetLogStr().c_str(), corp.name.c_str()); + +return 0; +} +//----------------------------------------------------------------------------- +bool CORPORATIONS_IMPL::Read() +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +vector corpsList; +if (store->GetCorpsList(&corpsList) < 0) + { + WriteServLog(store->GetStrError().c_str()); + return true; + } + +for (size_t i = 0; i < corpsList.size(); i++) + { + CORP_CONF corp; + + if (store->RestoreCorp(&corp, corpsList[i])) + { + WriteServLog(store->GetStrError().c_str()); + return true; + } + + data.push_back(corp); + } +return false; +} +//----------------------------------------------------------------------------- +bool CORPORATIONS_IMPL::Find(const string & name, CORP_CONF * corp) +{ +assert(corp != NULL && "Pointer to corporation is not null"); + +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + return false; + +crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); + +if (si != data.end()) + { + *corp = *si; + return false; + } + +return true; +} +//----------------------------------------------------------------------------- +bool CORPORATIONS_IMPL::Exists(const string & name) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (data.empty()) + { + printfd(__FILE__, "no admin in system!\n"); + return true; + } + +const_crp_iter si(find(data.begin(), data.end(), CORP_CONF(name))); + +if (si != data.end()) + return true; + +return false; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::OpenSearch() const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +handle++; +searchDescriptors[handle] = data.begin(); +return handle; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::SearchNext(int h, CORP_CONF * corp) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (searchDescriptors.find(h) == searchDescriptors.end()) + { + WriteServLog("CORPORATIONS. Incorrect search handle."); + return -1; + } + +if (searchDescriptors[h] == data.end()) + return -1; + +*corp = *searchDescriptors[h]++; + +return 0; +} +//----------------------------------------------------------------------------- +int CORPORATIONS_IMPL::CloseSearch(int h) const +{ +STG_LOCKER lock(&mutex, __FILE__, __LINE__); +if (searchDescriptors.find(h) != searchDescriptors.end()) + { + searchDescriptors.erase(searchDescriptors.find(h)); + return 0; + } + +WriteServLog("CORPORATIONS. Incorrect search handle."); +return -1; +} +//----------------------------------------------------------------------------- diff --git a/projects/stargazer/corps_impl.h b/projects/stargazer/corps_impl.h new file mode 100644 index 00000000..ad705c54 --- /dev/null +++ b/projects/stargazer/corps_impl.h @@ -0,0 +1,70 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* + * Author : Maxim Mamontov + */ + +#ifndef CORPORATIONS_IMPL_H +#define CORPORATIONS_IMPL_H + +#include + +#include +#include +#include + +#include "stg/corporations.h" +#include "stg/corp_conf.h" +#include "stg/locker.h" +#include "stg/store.h" +#include "stg/noncopyable.h" +#include "stg/logger.h" + +class ADMIN; + +class CORPORATIONS_IMPL : private NONCOPYABLE, public CORPORATIONS { +public: + CORPORATIONS_IMPL(STORE * st); + virtual ~CORPORATIONS_IMPL() {} + + int Add(const CORP_CONF & corp, const ADMIN * admin); + int Del(const std::string & name, const ADMIN * admin); + int Change(const CORP_CONF & corp, const ADMIN * admin); + bool Find(const std::string & name, CORP_CONF * corp); + bool Exists(const std::string & name) const; + const std::string & GetStrError() const { return strError; } + + int OpenSearch() const; + int SearchNext(int, CORP_CONF * corp) const; + int CloseSearch(int) const; + +private: + typedef list::iterator crp_iter; + typedef list::const_iterator const_crp_iter; + + bool Read(); + + std::list data; + STORE * store; + STG_LOGGER & WriteServLog; + mutable std::map searchDescriptors; + mutable unsigned int handle; + mutable pthread_mutex_t mutex; + std::string strError; +}; + +#endif -- 2.44.2 From 32248221316271f3a09a771ce35b77236f4bbeed Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:09:21 +0300 Subject: [PATCH 12/16] GetNum -> Count in interfaces --- include/stg/services.h | 1 + include/stg/tariffs.h | 3 ++- include/stg/users.h | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/stg/services.h b/include/stg/services.h index 2f1eec71..131e2b55 100644 --- a/include/stg/services.h +++ b/include/stg/services.h @@ -33,6 +33,7 @@ public: virtual bool FindService(const std::string & name, SERVICE_CONF & service) = 0; virtual bool ServiceExists(const std::string & name) const = 0; virtual const std::string & GetStrError() const = 0; + virtual size_t Count() const = 0; virtual int OpenSearch() const = 0; virtual int SearchNext(int, SERVICE_CONF * service) const = 0; diff --git a/include/stg/tariffs.h b/include/stg/tariffs.h index 8c098c8b..b492833b 100644 --- a/include/stg/tariffs.h +++ b/include/stg/tariffs.h @@ -33,13 +33,14 @@ public: virtual int ReadTariffs () = 0; virtual const TARIFF * FindByName(const std::string & name) const = 0; virtual const TARIFF * GetNoTariff() const = 0; - virtual int GetTariffsNum() 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 TARIFF_DATA & td, const ADMIN * admin) = 0; virtual void GetTariffsData(std::list * tdl) = 0; + virtual size_t Count() const = 0; + virtual const std::string & GetStrError() const = 0; }; diff --git a/include/stg/users.h b/include/stg/users.h index 50f5ffe2..86286656 100644 --- a/include/stg/users.h +++ b/include/stg/users.h @@ -44,7 +44,7 @@ public: virtual void Del(const std::string & login, const ADMIN * admin) = 0; virtual int ReadUsers() = 0; - virtual int GetUserNum() const = 0; + virtual size_t Count() const = 0; virtual int FindByIPIdx(uint32_t ip, USER_PTR * user) const = 0; virtual bool IsIPInIndex(uint32_t ip) const = 0; -- 2.44.2 From fd947779e36534941c5d7516ba34e4da21b8406b Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:10:00 +0300 Subject: [PATCH 13/16] Some fixes in service implementation --- .../stargazer/plugins/other/smux/sensors.h | 23 +-- projects/stargazer/services_impl.cpp | 165 ++++++------------ projects/stargazer/services_impl.h | 13 +- 3 files changed, 79 insertions(+), 122 deletions(-) diff --git a/projects/stargazer/plugins/other/smux/sensors.h b/projects/stargazer/plugins/other/smux/sensors.h index 16331b15..55780987 100644 --- a/projects/stargazer/plugins/other/smux/sensors.h +++ b/projects/stargazer/plugins/other/smux/sensors.h @@ -5,6 +5,9 @@ #include "stg/users.h" #include "stg/tariffs.h" +#include "stg/admins.h" +#include "stg/services.h" +#include "stg/corporations.h" #include "stg/user_property.h" #include "stg/ObjectSyntax.h" @@ -29,13 +32,13 @@ class TotalUsersSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(users.GetUserNum(), objectSyntax); + ValueToOS(users.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(users.GetUserNum(), res); return res; } + { std::string res; x2str(users.Count(), res); return res; } #endif private: @@ -165,13 +168,13 @@ class TotalTariffsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(tariffs.GetTariffsNum(), objectSyntax); + ValueToOS(tariffs.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(tariffs.GetTariffsNum(), res); return res; } + { std::string res; x2str(tariffs.Count(), res); return res; } #endif private: @@ -185,13 +188,13 @@ class TotalAdminsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(admins.GetAdminsNum(), objectSyntax); + ValueToOS(admins.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(admins.GetAdminsNum(), res); return res; } + { std::string res; x2str(admins.Count(), res); return res; } #endif private: @@ -205,13 +208,13 @@ class TotalServicesSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(services.GetServicesNum(), objectSyntax); + ValueToOS(services.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(services.GetServicesNum(), res); return res; } + { std::string res; x2str(services.Count(), res); return res; } #endif private: @@ -225,13 +228,13 @@ class TotalCorporationsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const { - ValueToOS(corporations.GetCorporationsNum(), objectSyntax); + ValueToOS(corporations.Count(), objectSyntax); return true; } #ifdef DEBUG std::string ToString() const - { std::string res; x2str(services.GetCorporationsNum(), res); return res; } + { std::string res; x2str(corporations.Count(), res); return res; } #endif private: diff --git a/projects/stargazer/services_impl.cpp b/projects/stargazer/services_impl.cpp index 400b34b3..f7fd1d49 100644 --- a/projects/stargazer/services_impl.cpp +++ b/projects/stargazer/services_impl.cpp @@ -36,7 +36,7 @@ SERVICES_IMPL::SERVICES_IMPL(STORE * st) handle(0) { pthread_mutex_init(&mutex, NULL); -ReadServices(); +Read(); } //----------------------------------------------------------------------------- int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) @@ -44,183 +44,164 @@ int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin) STG_LOCKER lock(&mutex, __FILE__, __LINE__); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; + string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -ADMIN_IMPL adm(0, login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +srv_iter si(find(data.begin(), data.end(), service)); -if (ai != data.end()) +if (si != data.end()) { - strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist."; + strError = "Service \'" + service.name + "\' cannot not be added. Service already exist."; WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -data.push_back(adm); +data.push_back(service); -if (store->AddAdmin(login) == 0) +if (store->AddService(service.name) == 0) { - WriteServLog("%s Administrator \'%s\' added.", - admin->GetLogStr().c_str(), login.c_str()); + WriteServLog("%s Service \'%s\' added.", + admin->GetLogStr().c_str(), service.name.c_str()); return 0; } -strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError(); +strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError(); WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Del(const string & login, const ADMIN * admin) +int SERVICES_IMPL::Del(const string & name, const ADMIN * admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN_IMPL adm(0, login, ""); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; + string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -admin_iter ai(find(data.begin(), data.end(), adm)); +srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); -if (ai == data.end()) +if (si == data.end()) { - strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist."; + strError = "Service \'" + name + "\' cannot be deleted. Service does not exist."; WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -map::iterator si; -si = searchDescriptors.begin(); -while (si != searchDescriptors.end()) +map::iterator csi; +csi = searchDescriptors.begin(); +while (csi != searchDescriptors.end()) { - if (si->second == ai) - (si->second)++; - si++; + if (csi->second == si) + (csi->second)++; + csi++; } -data.remove(*ai); -if (store->DelAdmin(login) < 0) +data.remove(*si); +if (store->DelService(name) < 0) { - strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError(); + strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError(); WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str()); +WriteServLog("%s Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin) +int SERVICES_IMPL::Change(const SERVICE_CONF & service, const ADMIN * admin) { STG_LOCKER lock(&mutex, __FILE__, __LINE__); const PRIV * priv = admin->GetPriv(); -if (!priv->adminChg) +if (!priv->serviceChg) { - string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -ADMIN_IMPL adm(0, ac.login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +srv_iter si(find(data.begin(), data.end(), service)); -if (ai == data.end()) +if (si == data.end()) { - strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist."; + strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist."; WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -*ai = ac; -if (store->SaveAdmin(ac)) +*si = service; +if (store->SaveService(service)) { - WriteServLog("Cannot write admin %s.", ac.login.c_str()); + WriteServLog("Cannot write service %s.", service.name.c_str()); WriteServLog("%s", store->GetStrError().c_str()); return -1; } -WriteServLog("%s Administrator \'%s\' changed.", - admin->GetLogStr().c_str(), ac.login.c_str()); +WriteServLog("%s Service \'%s\' changed.", + admin->GetLogStr().c_str(), service.name.c_str()); return 0; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::ReadAdmins() +bool SERVICES_IMPL::Read() { STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector adminsList; -if (store->GetAdminsList(&adminsList) < 0) +vector servicesList; +if (store->GetServicesList(&servicesList) < 0) { WriteServLog(store->GetStrError().c_str()); - return -1; + return true; } -for (unsigned int i = 0; i < adminsList.size(); i++) +for (size_t i = 0; i < servicesList.size(); i++) { - ADMIN_CONF ac(0, adminsList[i], ""); + SERVICE_CONF service; - if (store->RestoreAdmin(&ac, adminsList[i])) + if (store->RestoreService(&service, servicesList[i])) { WriteServLog(store->GetStrError().c_str()); - return -1; + return true; } - data.push_back(ADMIN_IMPL(ac)); - } -return 0; -} -//----------------------------------------------------------------------------- -void SERVICES_IMPL::PrintAdmins() const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const_admin_iter ai(data.begin()); -while (ai != data.end()) - { - ai->Print(); - ai++; + data.push_back(service); } +return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::FindAdmin(const string & l, ADMIN ** admin) +bool SERVICES_IMPL::Find(const string & name, SERVICE_CONF * service) { -assert(admin != NULL && "Pointer to admin is not null"); +assert(service != NULL && "Pointer to service is not null"); STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - *admin = &noAdmin; return false; - } -ADMIN_IMPL adm(0, l, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); -if (ai != data.end()) +if (si != data.end()) { - *admin = &(*ai); + *service = *si; return false; } return true; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::AdminExists(const string & login) const +bool SERVICES_IMPL::Exists(const string & name) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (data.empty()) @@ -229,42 +210,14 @@ if (data.empty()) return true; } -ADMIN_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +const_srv_iter si(find(data.begin(), data.end(), SERVICE_CONF(name))); -if (ai != data.end()) +if (si != data.end()) return true; return false; } //----------------------------------------------------------------------------- -bool SERVICES_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin) -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - return true; - } - -ADMIN_IMPL adm(0, login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); - -if (ai == data.end()) - { - return false; - } - -if (ai->GetPassword() != password) - { - return false; - } - -*admin = &(*ai); - -return true; -} -//----------------------------------------------------------------------------- int SERVICES_IMPL::OpenSearch() const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); @@ -273,7 +226,7 @@ searchDescriptors[handle] = data.begin(); return handle; } //----------------------------------------------------------------------------- -int SERVICES_IMPL::SearchNext(int h, ADMIN_CONF * ac) const +int SERVICES_IMPL::SearchNext(int h, SERVICE_CONF * service) const { STG_LOCKER lock(&mutex, __FILE__, __LINE__); if (searchDescriptors.find(h) == searchDescriptors.end()) @@ -285,9 +238,7 @@ if (searchDescriptors.find(h) == searchDescriptors.end()) if (searchDescriptors[h] == data.end()) return -1; -ADMIN_IMPL a = *searchDescriptors[h]++; - -*ac = a.GetConf(); +*service = *searchDescriptors[h]++; return 0; } diff --git a/projects/stargazer/services_impl.h b/projects/stargazer/services_impl.h index 4c7044fa..d0c1c52c 100644 --- a/projects/stargazer/services_impl.h +++ b/projects/stargazer/services_impl.h @@ -32,20 +32,21 @@ #include "stg/locker.h" #include "stg/store.h" #include "stg/noncopyable.h" +#include "stg/logger.h" class ADMIN; class SERVICES_IMPL : private NONCOPYABLE, public SERVICES { public: SERVICES_IMPL(STORE * st); - virtual SERVICES_IMPL() {} + virtual ~SERVICES_IMPL() {} int Add(const SERVICE_CONF & service, const ADMIN * admin); int Del(const std::string & name, const ADMIN * admin); int Change(const SERVICE_CONF & service, const ADMIN * admin); - bool FindService(const std::string & name, SERVICE_CONF & service); - bool ServiceExists(const std::string & name) const; - const std::string & GetStrError() const; + bool Find(const std::string & name, SERVICE_CONF * service); + bool Exists(const std::string & name) const; + const std::string & GetStrError() const { return strError; } int OpenSearch() const; int SearchNext(int, SERVICE_CONF * service) const; @@ -55,7 +56,7 @@ private: typedef list::iterator srv_iter; typedef list::const_iterator const_srv_iter; - bool ReadServices(); + bool Read(); std::list data; STORE * store; @@ -65,3 +66,5 @@ private: mutable pthread_mutex_t mutex; std::string strError; }; + +#endif -- 2.44.2 From 1b2fc630ee7edf4f8763bbf0e976789d26ff9372 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:11:21 +0300 Subject: [PATCH 14/16] Default constructor added for SERVICE_CONF --- include/stg/service_conf.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/include/stg/service_conf.h b/include/stg/service_conf.h index ea3495b3..5f015d0b 100644 --- a/include/stg/service_conf.h +++ b/include/stg/service_conf.h @@ -5,6 +5,9 @@ struct SERVICE_CONF { +SERVICE_CONF() + : name(), comment(), cost(0), payDay(0) +{} SERVICE_CONF(const std::string & n) : name(n), comment(), cost(0), payDay(0) {} @@ -25,5 +28,11 @@ double cost; unsigned payDay; }; +inline +bool operator==(const SERVICE_CONF & a, const SERVICE_CONF & b) +{ +return a.name == b.name; +} + #endif //SERVICE_CONF_H -- 2.44.2 From 1911f68bb36127dfecfcd0fd71726069837053a0 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:11:58 +0300 Subject: [PATCH 15/16] Corporations added to compilation --- projects/stargazer/Makefile | 2 ++ 1 file changed, 2 insertions(+) diff --git a/projects/stargazer/Makefile b/projects/stargazer/Makefile index 36739cc6..56702d07 100644 --- a/projects/stargazer/Makefile +++ b/projects/stargazer/Makefile @@ -8,11 +8,13 @@ PROG = stargazer SRCS = ./admin_impl.cpp \ ./admins_impl.cpp \ + ./corps_impl.cpp \ ./eventloop.cpp \ ./main.cpp \ ./pidfile.cpp \ ./plugin_runner.cpp \ ./settings_impl.cpp \ + ./services_impl.cpp \ ./stg_timer.cpp \ ./store_loader.cpp \ ./tariff_impl.cpp \ -- 2.44.2 From cbc0dcab9b85503926c58e82933a6148e81115f5 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Sat, 13 Aug 2011 19:16:46 +0300 Subject: [PATCH 16/16] Add some assertions --- projects/stargazer/plugins/other/smux/smux.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/projects/stargazer/plugins/other/smux/smux.cpp b/projects/stargazer/plugins/other/smux/smux.cpp index ac77ce6e..1300452f 100644 --- a/projects/stargazer/plugins/other/smux/smux.cpp +++ b/projects/stargazer/plugins/other/smux/smux.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -136,6 +137,12 @@ return smuxSettings.ParseSettings(settings); int SMUX::Start() { +assert(users != NULL && "users not NULL"); +assert(tariffs != NULL && "tariffs not NULL"); +assert(admins != NULL && "admins not NULL"); +assert(services != NULL && "services not NULL"); +assert(corporations != NULL && "corporations not NULL"); + if (PrepareNet()) return -1; -- 2.44.2