$Author: faust $
*/
-#include "admin.h"
+#include "admin_impl.h"
#include "common.h"
//-----------------------------------------------------------------------------
-ADMIN::ADMIN()
+ADMIN_IMPL::ADMIN_IMPL()
: conf(),
ip(0),
WriteServLog(GetStgLogger())
{
}
//-----------------------------------------------------------------------------
-ADMIN::ADMIN(const ADMIN_CONF & ac)
+ADMIN_IMPL::ADMIN_IMPL(const ADMIN_CONF & ac)
: conf(ac),
ip(0),
WriteServLog(GetStgLogger())
{
}
//-----------------------------------------------------------------------------
-ADMIN::ADMIN(const PRIV & priv, const std::string & login, const std::string & password)
+ADMIN_IMPL::ADMIN_IMPL(const PRIV & priv,
+ const std::string & login,
+ const std::string & password)
: conf(priv, login, password),
ip(0),
WriteServLog(GetStgLogger())
{
}
//-----------------------------------------------------------------------------
-ADMIN & ADMIN::operator=(const ADMIN & adm)
+ADMIN & ADMIN_IMPL::operator=(const ADMIN & adm)
{
if (&adm == this)
return *this;
-conf = adm.conf;
-ip = adm.ip;
+conf = adm.GetConf();
+ip = adm.GetIP();
return *this;
}
//-----------------------------------------------------------------------------
-ADMIN & ADMIN::operator=(const ADMIN_CONF & ac)
+ADMIN & ADMIN_IMPL::operator=(const ADMIN_CONF & ac)
{
conf = ac;
return *this;
}
//-----------------------------------------------------------------------------
-bool ADMIN::operator==(const ADMIN & rhs) const
+bool ADMIN_IMPL::operator==(const ADMIN & rhs) const
{
return conf.login == rhs.GetLogin();
}
//-----------------------------------------------------------------------------
-bool ADMIN::operator!=(const ADMIN & rhs) const
+bool ADMIN_IMPL::operator!=(const ADMIN & rhs) const
{
return conf.login != rhs.GetLogin();
}
//-----------------------------------------------------------------------------
-bool ADMIN::operator<(const ADMIN & rhs) const
+bool ADMIN_IMPL::operator<(const ADMIN & rhs) const
{
return conf.login < rhs.GetLogin();
}
//-----------------------------------------------------------------------------
-bool ADMIN::operator<=(const ADMIN & rhs) const
+bool ADMIN_IMPL::operator<=(const ADMIN & rhs) const
{
return conf.login <= rhs.GetLogin();
}
//-----------------------------------------------------------------------------
-string ADMIN::GetAdminIPStr() const
+std::string ADMIN_IMPL::GetIPStr() const
{
return inet_ntostring(ip);
}
//-----------------------------------------------------------------------------
-void ADMIN::PrintAdmin() const
+void ADMIN_IMPL::Print() const
{
printfd(__FILE__, "=======================================\n");
printfd(__FILE__, "login %s\n", conf.login.c_str());
printfd(__FILE__, "=======================================\n");
}
//-----------------------------------------------------------------------------
-const string ADMIN::GetLogStr() const
+const std::string ADMIN_IMPL::GetLogStr() const
{
-return "Admin \'" + conf.login + "\', " + GetAdminIPStr() + ":";
+return "Admin \'" + conf.login + "\', " + GetIPStr() + ":";
}
//-----------------------------------------------------------------------------
$Author: faust $
*/
-#ifndef ADMIN_H
-#define ADMIN_H
+#ifndef ADMIN_IMPL_H
+#define ADMIN_IMPL_H
#include <string>
+#include "admin.h"
#include "os_int.h"
#include "admin_conf.h"
#include "stg_logger.h"
-using namespace std;
-
-//-----------------------------------------------------------------------------
-class ADMIN
-{
+class ADMIN_IMPL : public ADMIN {
public:
- ADMIN();
- ADMIN(const ADMIN_CONF & ac);
- ADMIN(const PRIV & priv,
+ ADMIN_IMPL();
+ ADMIN_IMPL(const ADMIN_CONF & ac);
+ ADMIN_IMPL(const PRIV & priv,
const std::string & login,
const std::string & password);
- ~ADMIN() {};
+ virtual ~ADMIN_IMPL() {};
- ADMIN & operator=(const ADMIN &);
- ADMIN & operator=(const ADMIN_CONF &);
- bool operator==(const ADMIN & rhs) const;
- bool operator!=(const ADMIN & rhs) const;
- bool operator<(const ADMIN & rhs) const;
- bool operator<=(const ADMIN & rhs) const;
+ ADMIN & operator=(const ADMIN &);
+ ADMIN & operator=(const ADMIN_CONF &);
+ bool operator==(const ADMIN & rhs) const;
+ bool operator!=(const ADMIN & rhs) const;
+ bool operator<(const ADMIN & rhs) const;
+ bool operator<=(const ADMIN & rhs) const;
- const string & GetPassword() const { return conf.password; };
- const string & GetLogin() const { return conf.login; };
- PRIV const * GetPriv() const { return &conf.priv; };
- uint16_t GetPrivAsInt() const { return conf.priv.ToInt(); };
- const ADMIN_CONF & GetConf() const { return conf; };
- void PrintAdmin() const;
- uint32_t GetAdminIP() const { return ip; };
- string GetAdminIPStr() const;
- void SetAdminIP(uint32_t ip) { ADMIN::ip = ip; };
- const string GetLogStr() const;
+ const std::string & GetPassword() const { return conf.password; };
+ const std::string & GetLogin() const { return conf.login; };
+ PRIV const * GetPriv() const { return &conf.priv; };
+ uint16_t GetPrivAsInt() const { return conf.priv.ToInt(); };
+ const ADMIN_CONF & GetConf() const { return conf; };
+ void Print() const;
+ uint32_t GetIP() const { return ip; };
+ std::string GetIPStr() const;
+ void SetIP(uint32_t ip) { ADMIN_IMPL::ip = ip; };
+ const std::string GetLogStr() const;
private:
ADMIN_CONF conf;
uint32_t ip;
STG_LOGGER & WriteServLog;
};
-//-----------------------------------------------------------------------------
#endif
#include <cassert>
#include <algorithm>
+#include "admins_impl.h"
#include "admins.h"
#include "admin.h"
+#include "admin_impl.h"
#include "common.h"
using namespace std;
//-----------------------------------------------------------------------------
-ADMINS::ADMINS(BASE_STORE * st)
+ADMINS_IMPL::ADMINS_IMPL(BASE_STORE * st)
: stg(0xFFFF, "@stargazer", ""),
noAdmin(0xFFFF, "NO-ADMIN", ""),
data(),
ReadAdmins();
}
//-----------------------------------------------------------------------------
-int ADMINS::Add(const string & login, const ADMIN & admin)
+int ADMINS_IMPL::Add(const string & login, const ADMIN & admin)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
const PRIV * priv = admin.GetPriv();
return -1;
}
-ADMIN adm(0, login, "");
+ADMIN_IMPL adm(0, login, "");
admin_iter ai(find(data.begin(), data.end(), adm));
if (ai != data.end())
}
data.push_back(adm);
-/*ADMIN_CONF ac;
-ac.login = login;*/
-if (store->AddAdmin(login) == 0 /*&& store->SaveAdmin(ac) == 0*/)
+
+if (store->AddAdmin(login) == 0)
{
WriteServLog("%s Administrator \'%s\' added.",
admin.GetLogStr().c_str(), login.c_str());
return -1;
}
//-----------------------------------------------------------------------------
-int ADMINS::Del(const string & login, const ADMIN & admin)
+int ADMINS_IMPL::Del(const string & login, const ADMIN & admin)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-ADMIN adm(0, login, "");
+ADMIN_IMPL adm(0, login, "");
const PRIV * priv = admin.GetPriv();
if (!priv->adminChg)
return 0;
}
//-----------------------------------------------------------------------------
-int ADMINS::Change(const ADMIN_CONF & ac, const ADMIN & admin)
+int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN & admin)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
const PRIV * priv = admin.GetPriv();
return -1;
}
-ADMIN adm(0, ac.login, "");
+ADMIN_IMPL adm(0, ac.login, "");
admin_iter ai(find(data.begin(), data.end(), adm));
if (ai == data.end())
return 0;
}
//-----------------------------------------------------------------------------
-int ADMINS::ReadAdmins()
+int ADMINS_IMPL::ReadAdmins()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
vector<string> adminsList;
return -1;
}
- data.push_back(ADMIN(ac));
+ data.push_back(ADMIN_IMPL(ac));
}
return 0;
}
//-----------------------------------------------------------------------------
-void ADMINS::PrintAdmins() const
+void ADMINS_IMPL::PrintAdmins() const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
const_admin_iter ai(data.begin());
while (ai != data.end())
{
- ai->PrintAdmin();
+ ai->Print();
ai++;
}
}
//-----------------------------------------------------------------------------
-bool ADMINS::FindAdmin(const string & l, ADMIN * admin) const
+bool ADMINS_IMPL::FindAdmin(const string & l, ADMIN ** admin)
{
assert(admin != NULL && "Pointer to admin is not null");
if (data.empty())
{
printfd(__FILE__, "no admin in system!\n");
- *admin = noAdmin;
+ *admin = &noAdmin;
return false;
}
-ADMIN adm(0, l, "");
-const_admin_iter ai(find(data.begin(), data.end(), adm));
+ADMIN_IMPL adm(0, l, "");
+admin_iter ai(find(data.begin(), data.end(), adm));
if (ai != data.end())
{
- *admin = *ai;
+ *admin = &(*ai);
return false;
}
return true;
}
//-----------------------------------------------------------------------------
-bool ADMINS::AdminExists(const string & login) const
+bool ADMINS_IMPL::AdminExists(const string & login) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
if (data.empty())
return true;
}
-ADMIN adm(0, login, "");
+ADMIN_IMPL adm(0, login, "");
const_admin_iter ai(find(data.begin(), data.end(), adm));
if (ai != data.end())
return false;
}
//-----------------------------------------------------------------------------
-bool ADMINS::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const
+bool ADMINS_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
if (data.empty())
return true;
}
-ADMIN adm(0, login, "");
+ADMIN_IMPL adm(0, login, "");
const_admin_iter ai(find(data.begin(), data.end(), adm));
if (ai == data.end())
return true;
}
//-----------------------------------------------------------------------------
-int ADMINS::OpenSearch() const
+int ADMINS_IMPL::OpenSearch() const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
handle++;
return handle;
}
//-----------------------------------------------------------------------------
-int ADMINS::SearchNext(int h, ADMIN_CONF * ac) const
+int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
if (searchDescriptors.find(h) == searchDescriptors.end())
if (searchDescriptors[h] == data.end())
return -1;
-ADMIN a = *searchDescriptors[h]++;
+ADMIN_IMPL a = *searchDescriptors[h]++;
*ac = a.GetConf();
return 0;
}
//-----------------------------------------------------------------------------
-int ADMINS::CloseSearch(int h) const
+int ADMINS_IMPL::CloseSearch(int h) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
if (searchDescriptors.find(h) != searchDescriptors.end())
$Author: faust $
*/
-#ifndef ADMINS_H
-#define ADMINS_H
+#ifndef ADMINS_IMPL_H
+#define ADMINS_IMPL_H
#include <pthread.h>
#include <list>
#include <map>
+#include "admins.h"
#include "admin.h"
+#include "admin_impl.h"
#include "stg_locker.h"
#include "base_store.h"
#include "noncopyable.h"
-using namespace std;
-
-//-----------------------------------------------------------------------------
-class ADMINS : private NONCOPYABLE
-{
+class ADMINS_IMPL : private NONCOPYABLE, public ADMINS {
public:
- ADMINS(BASE_STORE * st);
- ~ADMINS() {};
+ ADMINS_IMPL(BASE_STORE * st);
+ virtual ~ADMINS_IMPL() {};
- int Add(const string & login, const ADMIN & admin);
- int Del(const string & login, const ADMIN & admin);
- int Change(const ADMIN_CONF & ac, const ADMIN & admin);
- void PrintAdmins() const;
- const ADMIN GetSysAdmin() const { return stg; };
- const ADMIN GetNoAdmin() const { return noAdmin; };
- bool FindAdmin(const string & l, ADMIN * admin) const;
- bool AdminExists(const std::string & login) const;
- bool AdminCorrect(const std::string & login,
- const std::string & password,
- ADMIN * admin) const;
- const string & GetStrError() const { return strError; };
+ int Add(const string & login, const ADMIN & admin);
+ int Del(const string & login, const ADMIN & admin);
+ int Change(const ADMIN_CONF & ac, const ADMIN & admin);
+ 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) const;
+ const std::string & GetStrError() const { return strError; };
int OpenSearch() const;
int SearchNext(int, ADMIN_CONF * ac) const;
int CloseSearch(int) const;
private:
- typedef list<ADMIN>::iterator admin_iter;
- typedef list<ADMIN>::const_iterator const_admin_iter;
+ typedef list<ADMIN_IMPL>::iterator admin_iter;
+ typedef list<ADMIN_IMPL>::const_iterator const_admin_iter;
int ReadAdmins();
- ADMIN stg;
- ADMIN noAdmin;
- list<ADMIN> data;
+ ADMIN_IMPL stg;
+ ADMIN_IMPL noAdmin;
+ list<ADMIN_IMPL> data;
BASE_STORE * store;
STG_LOGGER & WriteServLog;
mutable map<int, const_admin_iter> searchDescriptors;
mutable unsigned int handle;
mutable pthread_mutex_t mutex;
- string strError;
+ std::string strError;
};
-//-----------------------------------------------------------------------------
#endif
-
-
#include <ctime>
#include <algorithm> // std::max
-#include "tariff.h"
+#include "tariff_impl.h"
#include "stg_timer.h"
+#include "common.h"
//-----------------------------------------------------------------------------
-TARIFF & TARIFF::operator=(const TARIFF_DATA & td)
+TARIFF & TARIFF_IMPL::operator=(const TARIFF_DATA & td)
{
tariffData = td;
return *this;
}
//-----------------------------------------------------------------------------
-TARIFF & TARIFF::operator=(const TARIFF & t)
+TARIFF & TARIFF_IMPL::operator=(const TARIFF & t)
{
-tariffData = t.tariffData;
+tariffData = t.GetTariffData();
return *this;
}
//-----------------------------------------------------------------------------
-double TARIFF::GetPriceWithTraffType(uint64_t up,
+double TARIFF_IMPL::GetPriceWithTraffType(uint64_t up,
uint64_t down,
int dir,
time_t t) const
return GetPriceWithoutFreeMb(dir, GetTraffByType(up, down) / (1024 * 1024), t);
}
//-----------------------------------------------------------------------------
-int64_t TARIFF::GetTraffByType(uint64_t up, uint64_t down) const
+int64_t TARIFF_IMPL::GetTraffByType(uint64_t up, uint64_t down) const
{
switch (tariffData.tariffConf.traffType)
{
}
}
//-----------------------------------------------------------------------------
-int TARIFF::GetThreshold(int dir) const
+int TARIFF_IMPL::GetThreshold(int dir) const
{
return tariffData.dirPrice[dir].threshold;
}
//-----------------------------------------------------------------------------
-void TARIFF::PrintTariff() const
+void TARIFF_IMPL::Print() const
{
-//printfd(__FILE__, "Traiff name: %s\n", tariffConf.name.c_str());
-//printfd(__FILE__, "Price: %8.3f %8.3f \n", dirPrice[0].GetPrice(0, 0), dirPrice[0].GetPrice(1, 0));
-//printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[1].GetPrice(0), dirPrice[1].GetPrice(1), dirPrice[1].GetThreshold());
-//printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[2].GetPrice(0), dirPrice[2].GetPrice(1), dirPrice[2].GetThreshold());
-//printfd(__FILE__, "Price: %8.3f %8.3f Thr:%d\n", dirPrice[3].GetPrice(0), dirPrice[3].GetPrice(1), dirPrice[3].GetThreshold());
-//printfd(__FILE__, "Free: %8.3f\n", tariffConf.free);
+printfd(__FILE__, "Traiff name: %s\n", tariffData.tariffConf.name.c_str());
}
//-----------------------------------------------------------------------------
-void TARIFF::GetTariffData(TARIFF_DATA * td) const
-{
-*td = tariffData;
-}
-//-----------------------------------------------------------------------------
-int TARIFF::Interval(int dir, time_t t) const
+int TARIFF_IMPL::Interval(int dir, time_t t) const
{
// Start of the day (and end of the night) in sec from 00:00:00
int s1 = tariffData.dirPrice[dir].hDay * 3600 +
}
}
//-----------------------------------------------------------------------------
-double TARIFF::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const
+double TARIFF_IMPL::GetPriceWithoutFreeMb(int dir, int mb, time_t t) const
{
int interval = Interval(dir, t);
$Author: faust $
*/
-#ifndef TARIFF_H
-#define TARIFF_H
+#ifndef TARIFF_IMPL_H
+#define TARIFF_IMPL_H
#include <ctime>
#include <string>
#include <list>
+#include "tariff.h"
#include "os_int.h"
#include "tariff_conf.h"
#define TARIFF_DAY 0
#define TARIFF_NIGHT 1
-class TARIFF
-{
+class TARIFF_IMPL : public TARIFF {
public:
- TARIFF()
+ TARIFF_IMPL()
: tariffData()
{};
- TARIFF(const std::string & name)
+ TARIFF_IMPL(const std::string & name)
: tariffData(name)
{};
- TARIFF(const TARIFF_DATA & td)
+ TARIFF_IMPL(const TARIFF_DATA & td)
: tariffData(td)
{};
- TARIFF(const TARIFF & t)
+ TARIFF_IMPL(const TARIFF_IMPL & t)
: tariffData(t.tariffData)
{};
- ~TARIFF() {};
+ virtual ~TARIFF_IMPL() {};
double GetPriceWithTraffType(uint64_t up,
uint64_t down,
int dir,
time_t t) const;
- double GetFreeMb() const { return tariffData.tariffConf.free; };
- double GetPassiveCost() const { return tariffData.tariffConf.passiveCost; };
- double GetFee() const { return tariffData.tariffConf.fee; };
- double GetFree() const { return tariffData.tariffConf.free; };
+ double GetFreeMb() const { return tariffData.tariffConf.free; }
+ double GetPassiveCost() const { return tariffData.tariffConf.passiveCost; }
+ double GetFee() const { return tariffData.tariffConf.fee; }
+ double GetFree() const { return tariffData.tariffConf.free; }
- void PrintTariff() const;
+ void Print() const;
- const std::string & GetName() const { return tariffData.tariffConf.name; };
- void SetName(const std::string & name) { tariffData.tariffConf.name = name; };
+ const std::string & GetName() const { return tariffData.tariffConf.name; }
+ void SetName(const std::string & name) { tariffData.tariffConf.name = name; }
- int GetTraffType() const { return tariffData.tariffConf.traffType; };
+ int GetTraffType() const { return tariffData.tariffConf.traffType; }
int64_t GetTraffByType(uint64_t up, uint64_t down) const;
int GetThreshold(int dir) const;
- void GetTariffData(TARIFF_DATA * td) const;
+ const TARIFF_DATA & GetTariffData() const { return tariffData; }
TARIFF & operator=(const TARIFF_DATA & td);
TARIFF & operator=(const TARIFF & t);
- bool operator==(const TARIFF & rhs) const { return GetName() == rhs.GetName(); };
- bool operator!=(const TARIFF & rhs) const { return GetName() != rhs.GetName(); };
+ bool operator==(const TARIFF & rhs) const { return GetName() == rhs.GetName(); }
+ bool operator!=(const TARIFF & rhs) const { return GetName() != rhs.GetName(); }
private:
TARIFF_DATA tariffData;
double GetPriceWithoutFreeMb(int dir, int mb, time_t t) const;
int Interval(int dir, time_t t) const;
};
-//-----------------------------------------------------------------------------
#endif
#include <algorithm>
#include <vector>
-#include "tariffs.h"
+#include "tariffs_impl.h"
#include "stg_locker.h"
#include "stg_logger.h"
#include "base_store.h"
using namespace std;
//-----------------------------------------------------------------------------
-TARIFFS::TARIFFS(BASE_STORE * st)
+TARIFFS_IMPL::TARIFFS_IMPL(BASE_STORE * st)
: tariffs(),
store(st),
WriteServLog(GetStgLogger()),
ReadTariffs();
}
//-----------------------------------------------------------------------------
-TARIFFS::~TARIFFS()
+TARIFFS_IMPL::~TARIFFS_IMPL()
{
pthread_mutex_destroy(&mutex);
}
//-----------------------------------------------------------------------------
-int TARIFFS::ReadTariffs()
+int TARIFFS_IMPL::ReadTariffs()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
WriteServLog("%s", store->GetStrError().c_str());
return -1;
}
- tariffs.push_back(TARIFF(td));
+ tariffs.push_back(TARIFF_IMPL(td));
}
return 0;
}
//-----------------------------------------------------------------------------
-int TARIFFS::GetTariffsNum() const
+int TARIFFS_IMPL::GetTariffsNum() const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
return tariffs.size();
}
//-----------------------------------------------------------------------------
-const TARIFF * TARIFFS::FindByName(const string & name) const
+const TARIFF * TARIFFS_IMPL::FindByName(const string & name) const
{
if (name == NO_TARIFF_NAME)
return &noTariff;
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<TARIFF>::const_iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF(name));
+list<TARIFF_IMPL>::const_iterator ti;
+ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
if (ti != tariffs.end())
return &(*ti);
return NULL;
}
//-----------------------------------------------------------------------------
-int TARIFFS::Chg(const TARIFF_DATA & td, const ADMIN & admin)
+int TARIFFS_IMPL::Chg(const TARIFF_DATA & td, const ADMIN & admin)
{
const PRIV * priv = admin.GetPriv();
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<TARIFF>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF(td.tariffConf.name));
+list<TARIFF_IMPL>::iterator ti;
+ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(td.tariffConf.name));
if (ti == tariffs.end())
{
return 0;
}
//-----------------------------------------------------------------------------
-int TARIFFS::Del(const string & name, const ADMIN & admin)
+int TARIFFS_IMPL::Del(const string & name, const ADMIN & admin)
{
const PRIV * priv = admin.GetPriv();
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<TARIFF>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF(name));
+list<TARIFF_IMPL>::iterator ti;
+ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
if (ti == tariffs.end())
{
return 0;
}
//-----------------------------------------------------------------------------
-int TARIFFS::Add(const string & name, const ADMIN & admin)
+int TARIFFS_IMPL::Add(const string & name, const ADMIN & admin)
{
const PRIV * priv = admin.GetPriv();
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<TARIFF>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF(name));
+list<TARIFF_IMPL>::iterator ti;
+ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
if (ti != tariffs.end())
{
return -1;
}
-tariffs.push_back(TARIFF(name));
+tariffs.push_back(TARIFF_IMPL(name));
if (store->AddTariff(name) < 0)
{
return 0;
}
//-----------------------------------------------------------------------------
-void TARIFFS::GetTariffsData(std::list<TARIFF_DATA> * tdl)
+void TARIFFS_IMPL::GetTariffsData(std::list<TARIFF_DATA> * tdl)
{
assert(tdl != NULL && "Tariffs data list is not null");
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-std::list<TARIFF>::const_iterator it = tariffs.begin();
-TARIFF_DATA td;
+std::list<TARIFF_IMPL>::const_iterator it = tariffs.begin();
for (; it != tariffs.end(); ++it)
{
- it->GetTariffData(&td);
- tdl->push_back(td);
+ tdl->push_back(it->GetTariffData());
}
}
//-----------------------------------------------------------------------------
$Author: faust $
*/
-#ifndef TARIFFS_H
-#define TARIFFS_H
+#ifndef TARIFFS_IMPL_H
+#define TARIFFS_IMPL_H
#include <pthread.h>
#include <list>
#include "tariff.h"
+#include "tariff_impl.h"
+#include "tariffs.h"
#include "tariff_conf.h"
#define TARIFF_DAY 0
class STG_LOGGER;
class ADMIN;
-//-----------------------------------------------------------------------------
-class TARIFFS
-{
+class TARIFFS_IMPL : public TARIFFS {
public:
- TARIFFS(BASE_STORE * store);
- ~TARIFFS();
+ TARIFFS_IMPL(BASE_STORE * store);
+ virtual ~TARIFFS_IMPL();
int ReadTariffs ();
const TARIFF * FindByName(const std::string & name) const;
const TARIFF * GetNoTariff() const { return &noTariff; };
const std::string & GetStrError() const { return strError; };
private:
- std::list<TARIFF> tariffs;
+ std::list<TARIFF_IMPL> tariffs;
BASE_STORE * store;
STG_LOGGER & WriteServLog;
mutable pthread_mutex_t mutex;
std::string strError;
- TARIFF noTariff;
+ TARIFF_IMPL noTariff;
};
-//-----------------------------------------------------------------------------
#endif
#include <cassert>
-#include "user.h"
+#include "user_impl.h"
+#include "users.h"
#include "common.h"
#include "settings.h"
#include "script_executer.h"
#include "tariffs.h"
#include "admin.h"
-USER::USER(const SETTINGS * s,
+USER_IMPL::USER_IMPL(const SETTINGS * s,
const BASE_STORE * st,
const TARIFFS * t,
const ADMIN & a,
- const map<uint32_t, user_iter> * ipIdx)
- : property(s),
+ const USERS * u)
+ : users(u),
+ property(s),
WriteServLog(GetStgLogger()),
login(),
id(0),
ipNotifier(this)
{
settings = s;
-ipIndex = ipIdx;
password = "*_EMPTY_PASSWORD_*";
tariffName = NO_TARIFF_NAME;
pthread_mutex_init(&mutex, &attr);
}
//-----------------------------------------------------------------------------
-USER::USER(const USER & u)
- : property(u.settings),
+USER_IMPL::USER_IMPL(const USER_IMPL & u)
+ : users(u.users),
+ property(u.settings),
WriteServLog(GetStgLogger()),
login(u.login),
id(u.id),
connected = 0;
-ipIndex = u.ipIndex;
-
deleted = u.deleted;
lastWriteStat = u.lastWriteStat;
pthread_mutex_init(&mutex, &attr);
}
//-----------------------------------------------------------------------------
-USER::~USER()
+USER_IMPL::~USER_IMPL()
{
property.passive.DelBeforeNotifier(&passiveNotifier);
property.tariffName.DelBeforeNotifier(&tariffNotifier);
pthread_mutex_destroy(&mutex);
}
//-----------------------------------------------------------------------------
-void USER::SetLogin(string const & l)
+void USER_IMPL::SetLogin(string const & l)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
assert(login.empty() && "Login is already set");
id = userIDGenerator.GetNextID();
}
//-----------------------------------------------------------------------------
-int USER::ReadConf()
+int USER_IMPL::ReadConf()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
USER_CONF conf;
return 0;
}
//-----------------------------------------------------------------------------
-int USER::ReadStat()
+int USER_IMPL::ReadStat()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
USER_STAT stat;
return 0;
}
//-----------------------------------------------------------------------------
-int USER::WriteConf()
+int USER_IMPL::WriteConf()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
USER_CONF conf(property.GetConf());
return 0;
}
//-----------------------------------------------------------------------------
-int USER::WriteStat()
+int USER_IMPL::WriteStat()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
USER_STAT stat(property.GetStat());
return 0;
}
//-----------------------------------------------------------------------------
-int USER::WriteMonthStat()
+int USER_IMPL::WriteMonthStat()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
time_t tt = stgTime - 3600;
return 0;
}
//-----------------------------------------------------------------------------
-int USER::Authorize(uint32_t ip, const string &, uint32_t dirs, const BASE_AUTH * auth)
+int USER_IMPL::Authorize(uint32_t ip, const string &, uint32_t dirs, const BASE_AUTH * auth)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
/*
return -1;
}
- map<uint32_t, user_iter>::const_iterator ci = ipIndex->find(ip);
- if (ci != ipIndex->end())
+ USER * u = NULL;
+ if (!users->FindByIPIdx(ip, &u))
{
// Address is already present in IP-index
// If it's not our IP - throw an error
- if (&(*ci->second) != this)
+ if (u != this)
{
errorStr = "IP address " + inet_ntostring(ip) + " alredy in use";
return -1;
}
else
{
- if (ipIndex->find(ip) != ipIndex->end())
+ if (users->IsIPInIndex(ip))
{
// Address is already present in IP-index
errorStr = "IP address " + inet_ntostring(ip) + " alredy in use";
return 0;
}
//-----------------------------------------------------------------------------
-void USER::Unauthorize(const BASE_AUTH * auth)
+void USER_IMPL::Unauthorize(const BASE_AUTH * auth)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
/*
}
}
//-----------------------------------------------------------------------------
-bool USER::IsAuthorizedBy(const BASE_AUTH * auth) const
+bool USER_IMPL::IsAuthorizedBy(const BASE_AUTH * auth) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
// Is this user authorized by specified authorizer?
return authorizedBy.find(auth) != authorizedBy.end();
}
//-----------------------------------------------------------------------------
-void USER::Connect(bool fakeConnect)
+void USER_IMPL::Connect(bool fakeConnect)
{
/*
* Connect user to Internet. This function is differ from Authorize() !!!
lastIPForDisconnect = currIP;
}
//-----------------------------------------------------------------------------
-void USER::Disconnect(bool fakeDisconnect, const std::string & reason)
+void USER_IMPL::Disconnect(bool fakeDisconnect, const std::string & reason)
{
/*
* Disconnect user from Internet. This function is differ from UnAuthorize() !!!
sessionDownload = zeroSesssion;
}
//-----------------------------------------------------------------------------
-void USER::PrintUser() const
+void USER_IMPL::PrintUser() const
{
//return;
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
cout << "============================================================" << endl;
}
//-----------------------------------------------------------------------------
-void USER::Run()
+void USER_IMPL::Run()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
}
//-----------------------------------------------------------------------------
-void USER::UpdatePingTime(time_t t)
+void USER_IMPL::UpdatePingTime(time_t t)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
//printfd(__FILE__, "UpdatePingTime(%d) %s\n", t, login.c_str());
pingTime = stgTime;
}
//-----------------------------------------------------------------------------
-bool USER::IsInetable()
+bool USER_IMPL::IsInetable()
{
//STG_LOCKER lock(&mutex, __FILE__, __LINE__);
return (cash - tariff->GetFee() >= -credit);
}
//-----------------------------------------------------------------------------
-string USER::GetEnabledDirs()
+string USER_IMPL::GetEnabledDirs()
{
//STG_LOCKER lock(&mutex, __FILE__, __LINE__);
}
//-----------------------------------------------------------------------------
#ifdef TRAFF_STAT_WITH_PORTS
-void USER::AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len)
+void USER_IMPL::AddTraffStatU(int dir, uint32_t ip, uint16_t port, uint32_t len)
#else
-void USER::AddTraffStatU(int dir, uint32_t ip, uint32_t len)
+void USER_IMPL::AddTraffStatU(int dir, uint32_t ip, uint32_t len)
#endif
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
cost = dc - freeMb.ConstData();
// Direct access to internal data structures via friend-specifier
- property.stat.freeMb -= dc;
- property.stat.cash -= cost;
+ property.Stat().freeMb -= dc;
+ property.Stat().cash -= cost;
cash.ModifyTime();
freeMb.ModifyTime();
}
}
//-----------------------------------------------------------------------------
#ifdef TRAFF_STAT_WITH_PORTS
-void USER::AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len)
+void USER_IMPL::AddTraffStatD(int dir, uint32_t ip, uint16_t port, uint32_t len)
#else
-void USER::AddTraffStatD(int dir, uint32_t ip, uint32_t len)
+void USER_IMPL::AddTraffStatD(int dir, uint32_t ip, uint32_t len)
#endif
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
else if (freeMb.ConstData() < dc) // FreeMb is partially exhausted
cost = dc - freeMb.ConstData();
- property.stat.freeMb -= dc;
- property.stat.cash -= cost;
+ property.Stat().freeMb -= dc;
+ property.Stat().cash -= cost;
cash.ModifyTime();
freeMb.ModifyTime();
}
}
}
//-----------------------------------------------------------------------------
-void USER::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
+void USER_IMPL::AddCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
currIP.AddBeforeNotifier(n);
}
//-----------------------------------------------------------------------------
-void USER::DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
+void USER_IMPL::DelCurrIPBeforeNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
currIP.DelBeforeNotifier(n);
}
//-----------------------------------------------------------------------------
-void USER::AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
+void USER_IMPL::AddCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
currIP.AddAfterNotifier(n);
}
//-----------------------------------------------------------------------------
-void USER::DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
+void USER_IMPL::DelCurrIPAfterNotifier(PROPERTY_NOTIFIER_BASE<uint32_t> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
currIP.DelAfterNotifier(n);
}
//-----------------------------------------------------------------------------
-void USER::OnAdd()
+void USER_IMPL::OnAdd()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
}
}
//-----------------------------------------------------------------------------
-void USER::OnDelete()
+void USER_IMPL::OnDelete()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
Run();
}
//-----------------------------------------------------------------------------
-int USER::WriteDetailStat(bool hard)
+int USER_IMPL::WriteDetailStat(bool hard)
{
printfd(__FILE__, "USER::WriteDetailedStat() - saved size = %d\n", traffStatSaved.second.size());
return 0;
}
//-----------------------------------------------------------------------------
-double USER::GetPassiveTimePart() const
+double USER_IMPL::GetPassiveTimePart() const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
return double(dt) / (secMonth);
}
//-----------------------------------------------------------------------------
-void USER::SetPassiveTimeAsNewUser()
+void USER_IMPL::SetPassiveTimeAsNewUser()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
passiveTime = (time_t)(pt * 24 * 3600 * daysCurrMon);
}
//-----------------------------------------------------------------------------
-void USER::MidnightResetSessionStat()
+void USER_IMPL::MidnightResetSessionStat()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
}
}
//-----------------------------------------------------------------------------
-void USER::ProcessNewMonth()
+void USER_IMPL::ProcessNewMonth()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
// Reset traff
}
}
//-----------------------------------------------------------------------------
-void USER::ProcessDayFeeSpread()
+void USER_IMPL::ProcessDayFeeSpread()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
ResetPassiveTime();
}
//-----------------------------------------------------------------------------
-void USER::ProcessDayFee()
+void USER_IMPL::ProcessDayFee()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
property.cash.Set(c - f, sysAdmin, login, store, "Subscriber fee charge");
}
//-----------------------------------------------------------------------------
-void USER::SetPrepaidTraff()
+void USER_IMPL::SetPrepaidTraff()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
property.freeMb.Set(tariff->GetFree(), sysAdmin, login, store, "Prepaid traffic");
}
//-----------------------------------------------------------------------------
-int USER::AddMessage(STG_MSG * msg)
+int USER_IMPL::AddMessage(STG_MSG * msg)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
return 0;
}
//-----------------------------------------------------------------------------
-int USER::SendMessage(STG_MSG & msg) const
+int USER_IMPL::SendMessage(STG_MSG & msg) const
{
// No lock `cause we are already locked from caller
int ret = -1;
return ret;
}
//-----------------------------------------------------------------------------
-void USER::ScanMessage()
+void USER_IMPL::ScanMessage()
{
// No lock `cause we are already locked from caller
// We need not check for the authorizedBy `cause it has already checked by caller
$Author: faust $
*/
-#ifndef USER_H
-#define USER_H
+#ifndef USER_IMPL_H
+#define USER_IMPL_H
#include <ctime>
#include <list>
#include <map>
#include <set>
+#include "user.h"
#include "os_int.h"
#include "stg_const.h"
#include "user_stat.h"
using namespace std;
//-----------------------------------------------------------------------------
-class USER;
class TARIFF;
class TARIFFS;
class ADMIN;
-typedef list<USER>::iterator user_iter;
-typedef list<USER>::const_iterator const_user_iter;
+class USER_IMPL;
//-----------------------------------------------------------------------------
-class USER_ID_GENERATOR
-{
-friend class USER;
+class USER_ID_GENERATOR {
+friend class USER_IMPL;
private:
USER_ID_GENERATOR() {}
int GetNextID() { static int id = 0; return id++; }
};
//-----------------------------------------------------------------------------
class CHG_PASSIVE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<int>,
- private NONCOPYABLE
-{
+ private NONCOPYABLE {
public:
- CHG_PASSIVE_NOTIFIER(USER * u) : user(u) {}
+ CHG_PASSIVE_NOTIFIER(USER_IMPL * u) : user(u) {}
void Notify(const int & oldPassive, const int & newPassive);
private:
- USER * user;
+ USER_IMPL * user;
};
//-----------------------------------------------------------------------------
class CHG_TARIFF_NOTIFIER : public PROPERTY_NOTIFIER_BASE<string>,
- private NONCOPYABLE
-{
+ private NONCOPYABLE {
public:
- CHG_TARIFF_NOTIFIER(USER * u) : user(u) {}
+ CHG_TARIFF_NOTIFIER(USER_IMPL * u) : user(u) {}
void Notify(const string & oldTariff, const string & newTariff);
private:
- USER * user;
+ USER_IMPL * user;
};
//-----------------------------------------------------------------------------
class CHG_CASH_NOTIFIER : public PROPERTY_NOTIFIER_BASE<double>,
- private NONCOPYABLE
-{
+ private NONCOPYABLE {
public:
- CHG_CASH_NOTIFIER(USER * u) : user(u) {}
+ CHG_CASH_NOTIFIER(USER_IMPL * u) : user(u) {}
void Notify(const double & oldCash, const double & newCash);
private:
- USER * user;
+ USER_IMPL * user;
};
//-----------------------------------------------------------------------------
class CHG_IP_NOTIFIER : public PROPERTY_NOTIFIER_BASE<uint32_t>,
- private NONCOPYABLE
-{
+ private NONCOPYABLE {
public:
- CHG_IP_NOTIFIER(USER * u) : user(u) {}
+ CHG_IP_NOTIFIER(USER_IMPL * u) : user(u) {}
void Notify(const uint32_t & oldCash, const uint32_t & newCash);
private:
- USER * user;
+ USER_IMPL * user;
};
//-----------------------------------------------------------------------------
-class USER
-{
+class USER_IMPL : public USER {
friend class CHG_PASSIVE_NOTIFIER;
friend class CHG_TARIFF_NOTIFIER;
friend class CHG_CASH_NOTIFIER;
friend class CHG_IP_NOTIFIER;
public:
- USER(const SETTINGS * settings,
- const BASE_STORE * store,
- const TARIFFS * tariffs,
- const ADMIN & sysAdmin,
- const map<uint32_t, user_iter> * ipIndex);
- USER(const USER & u);
- ~USER();
+ USER_IMPL(const SETTINGS * settings,
+ const BASE_STORE * store,
+ const TARIFFS * tariffs,
+ const ADMIN & sysAdmin,
+ const USERS * u);
+ USER_IMPL(const USER_IMPL & u);
+ virtual ~USER_IMPL();
int ReadConf();
int ReadStat();
int Authorize(uint32_t ip, const string & iface, uint32_t enabledDirs, const BASE_AUTH * auth);
void Unauthorize(const BASE_AUTH * auth);
bool IsAuthorizedBy(const BASE_AUTH * auth) const;
- void OnAdd();
- void OnDelete();
int AddMessage(STG_MSG * msg);
const string & GetStrError() const { return errorStr; }
- USER_PROPERTIES property;
+ USER_PROPERTIES & GetProperty() { return property; };
+ const USER_PROPERTIES & GetProperty() const { return property; };
void SetDeleted() { deleted = true; }
bool GetDeleted() const { return deleted; }
bool IsInetable();
string GetEnabledDirs();
+ void OnAdd();
+ void OnDelete();
+
private:
+ const USERS * users;
+ USER_PROPERTIES property;
STG_LOGGER & WriteServLog;
void Connect(bool fakeConnect = false);
int SendMessage(STG_MSG & msg) const;
void ScanMessage();
+
time_t lastScanMessages;
string login;
time_t pingTime;
- const ADMIN sysAdmin;
+ const ADMIN & sysAdmin;
const BASE_STORE * store;
const TARIFFS * tariffs;
set<const BASE_AUTH *> authorizedBy;
- const map<uint32_t, user_iter> * ipIndex;
-
list<STG_MSG> messages;
bool deleted;
#include <vector>
#include "settings.h"
-#include "users.h"
-#include "user.h"
+#include "users_impl.h"
#include "common.h"
#include "stg_timer.h"
//#define USERS_DEBUG 1
//-----------------------------------------------------------------------------
-USERS::USERS(SETTINGS * s, BASE_STORE * st, TARIFFS * t, const ADMIN & sa)
+USERS_IMPL::USERS_IMPL(SETTINGS * s, BASE_STORE * st, TARIFFS * t, const ADMIN & sa)
: users(),
usersToDelete(),
userIPNotifiersBefore(),
pthread_mutex_init(&mutex, &attr);
}
//-----------------------------------------------------------------------------
-USERS::~USERS()
+USERS_IMPL::~USERS_IMPL()
{
pthread_mutex_destroy(&mutex);
}
//-----------------------------------------------------------------------------
-int USERS::FindByNameNonLock(const string & login, user_iter * user) const
+int USERS_IMPL::FindByNameNonLock(const string & login, user_iter * user)
{
-map<string, user_iter>::const_iterator iter;
+map<string, user_iter>::iterator iter;
iter = loginIndex.find(login);
if (iter != loginIndex.end())
{
return -1;
}
//-----------------------------------------------------------------------------
-int USERS::FindByName(const string & login, user_iter * user) const
+int USERS_IMPL::FindByName(const string & login, USER_PTR * user)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-return FindByNameNonLock(login, user);
+user_iter u;
+int res = FindByNameNonLock(login, &u);
+if (res)
+ return -1;
+*user = &(*u);
+return 0;
}
//-----------------------------------------------------------------------------
-bool USERS::TariffInUse(const string & tariffName)
+bool USERS_IMPL::TariffInUse(const string & tariffName) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<USER>::iterator iter;
+list<USER_IMPL>::const_iterator iter;
iter = users.begin();
while (iter != users.end())
{
- if (iter->property.tariffName.Get() == tariffName)
+ if (iter->GetProperty().tariffName.Get() == tariffName)
return true;
++iter;
}
return false;
}
//-----------------------------------------------------------------------------
-int USERS::Add(const string & login, const ADMIN & admin)
+int USERS_IMPL::Add(const string & login, const ADMIN & admin)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
const PRIV * priv = admin.GetPriv();
}
//////
-USER u(settings, store, tariffs, sysAdmin, &ipIndex);
+USER_IMPL u(settings, store, tariffs, sysAdmin, this);
/*struct tm * tms;
time_t t = stgTime;
SetUserNotifiers(users.begin());
// õ×ÅÄÏÍÌÑÅÍ ×ÓÅÈ ÖÅÌÁÀÝÉÈ, ÞÔÏ ÄÏÂÁ×ÌÅÎ ÎÏ×ÙÊ ÐÏÌØÚÏ×ÁÔÅÌØ
-set<NOTIFIER_BASE<user_iter> *>::iterator ni = onAddNotifiers.begin();
+set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onAddNotifiers.begin();
while (ni != onAddNotifiers.end())
{
- (*ni)->Notify(users.begin());
+ (*ni)->Notify(&users.front());
++ni;
}
return 0;
}
//-----------------------------------------------------------------------------
-void USERS::Del(const string & login, const ADMIN & admin)
+void USERS_IMPL::Del(const string & login, const ADMIN & admin)
{
const PRIV * priv = admin.GetPriv();
user_iter u;
}
}
-set<NOTIFIER_BASE<user_iter> *>::iterator ni = onDelNotifiers.begin();
+set<NOTIFIER_BASE<USER_PTR> *>::iterator ni = onDelNotifiers.begin();
while (ni != onDelNotifiers.end())
{
- (*ni)->Notify(u);
+ (*ni)->Notify(&(*u));
++ni;
}
}
}
//-----------------------------------------------------------------------------
-int USERS::ReadUsers()
+int USERS_IMPL::ReadUsers()
{
vector<string> usersList;
usersList.clear();
for (unsigned int i = 0; i < usersList.size(); i++)
{
- USER u(settings, store, tariffs, sysAdmin, &ipIndex);
+ USER_IMPL u(settings, store, tariffs, sysAdmin, this);
u.SetLogin(usersList[i]);
users.push_front(u);
return 0;
}
//-----------------------------------------------------------------------------
-void * USERS::Run(void * d)
+void * USERS_IMPL::Run(void * d)
{
printfd(__FILE__, "=====================| pid: %d |===================== \n", getpid());
-USERS * us = (USERS*) d;
+USERS_IMPL * us = (USERS_IMPL*) d;
struct tm t;
time_t tt = stgTime;
//printfd(__FILE__,"New Minute. old = %02d current = %02d\n", min, t->tm_min);
//printfd(__FILE__,"New Day. old = %2d current = %2d\n", day, t->tm_mday);
- for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER::Run));
+ for_each(us->users.begin(), us->users.end(), mem_fun_ref(&USER_IMPL::Run));
tt = stgTime;
localtime_r(&tt, &t);
return NULL;
}
//-----------------------------------------------------------------------------
-void USERS::NewMinute(const struct tm & t)
+void USERS_IMPL::NewMinute(const struct tm & t)
{
//Write traff, reset session traff. Fake disconnect-connect
if (t.tm_hour == 23 && t.tm_min == 59)
{
printfd(__FILE__,"MidnightResetSessionStat\n");
- for_each(users.begin(), users.end(), mem_fun_ref(&USER::MidnightResetSessionStat));
+ for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::MidnightResetSessionStat));
}
if (TimeToWriteDetailStat(t))
int usersCnt = 0;
// ðÉÛÅÍ ÀÚÅÒÏ× ÞÁÓÔÑÍÉ. ÷ ÐÅÒÅÒÙ×ÁÈ ×ÙÚÙ×ÁÅÍ USER::Run
- list<USER>::iterator usr = users.begin();
+ list<USER_IMPL>::iterator usr = users.begin();
while (usr != users.end())
{
usersCnt++;
usr->WriteDetailStat();
usr++;
if (usersCnt % 10 == 0)
- for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run));
+ for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
}
}
RealDelUser();
}
//-----------------------------------------------------------------------------
-void USERS::NewDay(const struct tm & t)
+void USERS_IMPL::NewDay(const struct tm & t)
{
struct tm t1;
time_t tt = stgTime;
if (settings->GetSpreadFee())
{
printfd(__FILE__, "Spread DayFee\n");
- for_each(users.begin(), users.end(), mem_fun_ref(&USER::ProcessDayFeeSpread));
+ for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFeeSpread));
}
else
{
if (t.tm_mday == dayFee)
{
printfd(__FILE__, "DayFee\n");
- for_each(users.begin(), users.end(), mem_fun_ref(&USER::ProcessDayFee));
+ for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessDayFee));
}
}
}
}
//-----------------------------------------------------------------------------
-void USERS::DayResetTraff(const struct tm & t1)
+void USERS_IMPL::DayResetTraff(const struct tm & t1)
{
int dayResetTraff = settings->GetDayResetTraff();
if (dayResetTraff == 0)
if (t1.tm_mday == dayResetTraff)
{
printfd(__FILE__, "ResetTraff\n");
- for_each(users.begin(), users.end(), mem_fun_ref(&USER::ProcessNewMonth));
- for_each(users.begin(), users.end(), mem_fun_ref(&USER::SetPrepaidTraff));
+ for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::ProcessNewMonth));
+ for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::SetPrepaidTraff));
}
}
//-----------------------------------------------------------------------------
-int USERS::Start()
+int USERS_IMPL::Start()
{
if (ReadUsers())
{
return 0;
}
//-----------------------------------------------------------------------------
-int USERS::Stop()
+int USERS_IMPL::Stop()
{
printfd(__FILE__, "USERS::Stop()\n");
}
printfd(__FILE__, "Before USERS::Run()\n");
-for_each(users.begin(), users.end(), mem_fun_ref(&USER::Run));
+for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::Run));
// 'cause bind2st accepts only constant first param
-for (list<USER>::iterator it = users.begin();
+for (list<USER_IMPL>::iterator it = users.begin();
it != users.end();
++it)
it->WriteDetailStat(true);
-for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteStat));
-for_each(users.begin(), users.end(), mem_fun_ref(&USER::WriteConf));
+for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteStat));
+for_each(users.begin(), users.end(), mem_fun_ref(&USER_IMPL::WriteConf));
printfd(__FILE__, "USERS::Stop()\n");
return 0;
}
//-----------------------------------------------------------------------------
-void USERS::RealDelUser()
+void USERS_IMPL::RealDelUser()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
return;
}
//-----------------------------------------------------------------------------
-int USERS::GetUserNum()
+int USERS_IMPL::GetUserNum() const
{
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
return users.size();
}
//-----------------------------------------------------------------------------
-void USERS::AddToIPIdx(user_iter user)
+void USERS_IMPL::AddToIPIdx(user_iter user)
{
printfd(__FILE__, "USERS: Add IP Idx\n");
uint32_t ip = user->GetCurrIP();
ipIndex.insert(it, std::make_pair(ip, user));
}
//-----------------------------------------------------------------------------
-void USERS::DelFromIPIdx(uint32_t ip)
+void USERS_IMPL::DelFromIPIdx(uint32_t ip)
{
printfd(__FILE__, "USERS: Del IP Idx\n");
assert(ip && "User has non-null ip");
ipIndex.erase(it);
}
//-----------------------------------------------------------------------------
-int USERS::FindByIPIdx(uint32_t ip, user_iter * usr)
+int USERS_IMPL::FindByIPIdx(uint32_t ip, USER_PTR * usr) const
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-map<uint32_t, user_iter>::iterator it;
+map<uint32_t, user_iter>::const_iterator it;
it = ipIndex.find(ip);
if (it == ipIndex.end())
//printfd(__FILE__, "User NOT found in IP_Index!!!\n");
return -1;
}
-*usr = it->second;
+*usr = &(*it->second);
//printfd(__FILE__, "User found in IP_Index\n");
return 0;
}
//-----------------------------------------------------------------------------
-void USERS::AddNotifierUserAdd(NOTIFIER_BASE<user_iter> * n)
+bool USERS_IMPL::IsIPInIndex(uint32_t ip) const
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+map<uint32_t, user_iter>::const_iterator it;
+it = ipIndex.find(ip);
+
+return it != ipIndex.end();
+}
+//-----------------------------------------------------------------------------
+void USERS_IMPL::AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
onAddNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
-void USERS::DelNotifierUserAdd(NOTIFIER_BASE<user_iter> * n)
+void USERS_IMPL::DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
//printfd(__FILE__, "DelNotifierUserAdd\n");
onAddNotifiers.erase(n);
}
//-----------------------------------------------------------------------------
-void USERS::AddNotifierUserDel(NOTIFIER_BASE<user_iter> * n)
+void USERS_IMPL::AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
onDelNotifiers.insert(n);
}
//-----------------------------------------------------------------------------
-void USERS::DelNotifierUserDel(NOTIFIER_BASE<user_iter> * n)
+void USERS_IMPL::DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> * n)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
onDelNotifiers.erase(n);
}
//-----------------------------------------------------------------------------
-int USERS::OpenSearch()
+int USERS_IMPL::OpenSearch()
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
handle++;
return handle;
}
//-----------------------------------------------------------------------------
-int USERS::SearchNext(int h, user_iter * u)
+int USERS_IMPL::SearchNext(int h, USER_PTR * user)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
}
}
-*u = searchDescriptors[h];
+*user = &(*searchDescriptors[h]);
++searchDescriptors[h];
return 0;
}
//-----------------------------------------------------------------------------
-int USERS::CloseSearch(int h)
+int USERS_IMPL::CloseSearch(int h)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
if (searchDescriptors.find(h) != searchDescriptors.end())
return -1;
}
//-----------------------------------------------------------------------------
-void USERS::SetUserNotifiers(user_iter user)
+void USERS_IMPL::SetUserNotifiers(user_iter user)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
user->AddCurrIPAfterNotifier(&(*userIPNotifiersAfter.begin()));
}
//-----------------------------------------------------------------------------
-void USERS::UnSetUserNotifiers(user_iter user)
+void USERS_IMPL::UnSetUserNotifiers(user_iter user)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
}
}
//-----------------------------------------------------------------------------
-void USERS::AddUserIntoIndexes(user_iter user)
+void USERS_IMPL::AddUserIntoIndexes(user_iter user)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-loginIndex.insert(pair<string, user_iter>(user->GetLogin(), user));
+loginIndex.insert(make_pair(user->GetLogin(), user));
}
//-----------------------------------------------------------------------------
-void USERS::DelUserFromIndexes(user_iter user)
+void USERS_IMPL::DelUserFromIndexes(user_iter user)
{
STG_LOCKER lock(&mutex, __FILE__, __LINE__);
loginIndex.erase(user->GetLogin());
}
//-----------------------------------------------------------------------------
-bool USERS::TimeToWriteDetailStat(const struct tm & t)
+bool USERS_IMPL::TimeToWriteDetailStat(const struct tm & t)
{
int statTime = settings->GetDetailStatWritePeriod();
*/
-#ifndef USERS_H
-#define USERS_H
+#ifndef USERS_IMPL_H
+#define USERS_IMPL_H
#include <pthread.h>
#include "os_int.h"
+#include "base_store.h"
#include "settings.h"
+#include "users.h"
#include "user.h"
+#include "user_impl.h"
#include "tariffs.h"
#include "stg_logger.h"
#include "notifer.h"
const int userDeleteDelayTime = 120;
-using namespace std;
-class USERS;
+typedef std::list<USER_IMPL>::iterator user_iter;
+typedef std::list<USER_IMPL>::const_iterator const_user_iter;
+
+class USERS_IMPL;
//-----------------------------------------------------------------------------
-class PROPERTY_NOTIFER_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t>
-{
+class PROPERTY_NOTIFER_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
public:
- PROPERTY_NOTIFER_IP_BEFORE(USERS & us, user_iter u) : users(us), user(u) {};
+ PROPERTY_NOTIFER_IP_BEFORE(USERS_IMPL & us, user_iter u) : users(us), user(u) {}
void Notify(const uint32_t & oldValue, const uint32_t & newValue);
- user_iter GetUser() const { return user; };
+ user_iter GetUser() const { return user; }
private:
- USERS & users;
- user_iter user;
+ USERS_IMPL & users;
+ user_iter user;
};
//-----------------------------------------------------------------------------
-class PROPERTY_NOTIFER_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t>
-{
+class PROPERTY_NOTIFER_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
public:
- PROPERTY_NOTIFER_IP_AFTER(USERS & us, user_iter u) : users(us), user(u) {};
+ PROPERTY_NOTIFER_IP_AFTER(USERS_IMPL & us, user_iter u) : users(us), user(u) {}
void Notify(const uint32_t & oldValue, const uint32_t & newValue);
- user_iter GetUser() const { return user; };
+ user_iter GetUser() const { return user; }
private:
- USERS & users;
- user_iter user;
+ USERS_IMPL & users;
+ user_iter user;
};
//-----------------------------------------------------------------------------
-struct USER_TO_DEL
-{
+struct USER_TO_DEL {
USER_TO_DEL()
: iter(),
delTime(0)
-{};
+{}
-list<USER>::iterator iter;
+std::list<USER_IMPL>::iterator iter;
time_t delTime;
};
//-----------------------------------------------------------------------------
-class USERS : private NONCOPYABLE
- {
+class USERS_IMPL : private NONCOPYABLE, public USERS {
friend class PROPERTY_NOTIFER_IP_BEFORE;
friend class PROPERTY_NOTIFER_IP_AFTER;
public:
- USERS(SETTINGS * s, BASE_STORE * store, TARIFFS * tariffs, const ADMIN & sysAdmin);
- ~USERS();
+ USERS_IMPL(SETTINGS * s, BASE_STORE * store, TARIFFS * tariffs, const ADMIN & sysAdmin);
+ virtual ~USERS_IMPL();
- int FindByName(const string & login, user_iter * user) const;
- int FindByID(int id, user_iter * user);
+ int FindByName(const std::string & login, USER_PTR * user);
- bool TariffInUse(const string & tariffName);
+ bool TariffInUse(const std::string & tariffName) const;
- void AddNotifierUserAdd(NOTIFIER_BASE<user_iter> *);
- void DelNotifierUserAdd(NOTIFIER_BASE<user_iter> *);
+ void AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
+ void DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
- void AddNotifierUserDel(NOTIFIER_BASE<user_iter> *);
- void DelNotifierUserDel(NOTIFIER_BASE<user_iter> *);
+ void AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
+ void DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
- int Add(const string & login, const ADMIN & admin);
- void Del(const string & login, const ADMIN & admin);
+ int Add(const std::string & login, const ADMIN & admin);
+ void Del(const std::string & login, const ADMIN & admin);
int ReadUsers();
- int GetUserNum();
+ int GetUserNum() const;
- int FindByIPIdx(uint32_t ip, user_iter * user);
+ int FindByIPIdx(uint32_t ip, USER_PTR * user) const;
+ bool IsIPInIndex(uint32_t ip) const;
int OpenSearch();
- int SearchNext(int handler, user_iter * u);
+ int SearchNext(int handler, USER_PTR * user);
int CloseSearch(int handler);
int Start();
int Stop();
private:
- void AddToIPIdx(user_iter);
+ void AddToIPIdx(user_iter user);
void DelFromIPIdx(uint32_t ip);
- int FindByNameNonLock(const string & login, user_iter * user) const;
- int FindByIDNonLock(int id, user_iter * user);
+ int FindByNameNonLock(const std::string & login, user_iter * user);
void RealDelUser();
void ProcessActions();
bool TimeToWriteDetailStat(const struct tm & t);
- list<USER> users;
- list<USER_TO_DEL> usersToDelete;
- list<PROPERTY_NOTIFER_IP_BEFORE> userIPNotifiersBefore;
- list<PROPERTY_NOTIFER_IP_AFTER> userIPNotifiersAfter;
+ std::list<USER_IMPL> users;
+ std::list<USER_TO_DEL> usersToDelete;
+ std::list<PROPERTY_NOTIFER_IP_BEFORE> userIPNotifiersBefore;
+ std::list<PROPERTY_NOTIFER_IP_AFTER> userIPNotifiersAfter;
- map<uint32_t, user_iter> ipIndex;
- map<string, user_iter> loginIndex;
+ std::map<uint32_t, user_iter> ipIndex;
+ std::map<std::string, user_iter> loginIndex;
SETTINGS * settings;
TARIFFS * tariffs;
BASE_STORE * store;
- const ADMIN sysAdmin;
+ const ADMIN & sysAdmin;
STG_LOGGER & WriteServLog;
bool nonstop;
pthread_t thread;
mutable unsigned int handle;
- mutable map<int, user_iter> searchDescriptors;
+ mutable std::map<int, user_iter> searchDescriptors;
- set <NOTIFIER_BASE<user_iter>*> onAddNotifiers;
- set <NOTIFIER_BASE<user_iter>*> onDelNotifiers;
- };
+ std::set<NOTIFIER_BASE<USER_PTR>*> onAddNotifiers;
+ std::set<NOTIFIER_BASE<USER_PTR>*> onDelNotifiers;
+};
//-----------------------------------------------------------------------------
inline
void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
}
//-----------------------------------------------------------------------------
#endif
-