#include <string>
#include <list>
+#include "notifer.h"
+
class ADMIN;
class TARIFF;
struct TARIFF_DATA;
class TARIFFS {
public:
- virtual int ReadTariffs () = 0;
+ virtual int ReadTariffs () = 0;
virtual const TARIFF * FindByName(const std::string & name) const = 0;
virtual const TARIFF * GetNoTariff() const = 0;
- virtual int Del(const std::string & name, const ADMIN * admin) = 0;
- virtual int Add(const std::string & name, const ADMIN * admin) = 0;
- virtual int Chg(const TARIFF_DATA & td, const ADMIN * admin) = 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 AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * notifier) = 0;
+ virtual void DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * notifier) = 0;
+
+ virtual void AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * notifier) = 0;
+ virtual void DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * notifier) = 0;
- virtual void GetTariffsData(std::list<TARIFF_DATA> * tdl) = 0;
+ virtual void GetTariffsData(std::list<TARIFF_DATA> * tdl) = 0;
- virtual size_t Count() const = 0;
+ virtual size_t Count() const = 0;
virtual const std::string & GetStrError() const = 0;
};
store(st),
WriteServLog(GetStgLogger()),
strError(),
- noTariff(NO_TARIFF_NAME)
+ noTariff(NO_TARIFF_NAME),
+ onAddNotifiers(),
+ onDelNotifiers()
{
pthread_mutex_init(&mutex, NULL);
ReadTariffs();
return -1;
}
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-
-list<TARIFF_IMPL>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+TARIFF_DATA td;
-if (ti == tariffs.end())
{
- strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
- WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
- return -1;
+ STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+
+ list<TARIFF_IMPL>::iterator ti;
+ ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+
+ if (ti == tariffs.end())
+ {
+ strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist.";
+ WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
+ return -1;
+ }
+
+ if (store->DelTariff(name))
+ {
+ WriteServLog("Cannot delete tariff %s.", name.c_str());
+ WriteServLog("%s", store->GetStrError().c_str());
+ return -1;
+ }
+
+ td = ti->GetTariffData();
+
+ tariffs.erase(ti);
}
-if (store->DelTariff(name))
+std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onDelNotifiers.begin();
+while (ni != onDelNotifiers.end())
{
- WriteServLog("Cannot delete tariff %s.", name.c_str());
- WriteServLog("%s", store->GetStrError().c_str());
- return -1;
+ (*ni)->Notify(td);
+ ++ni;
}
-tariffs.erase(ti);
-
WriteServLog("%s Tariff \'%s\' deleted.",
admin->GetLogStr().c_str(),
name.c_str());
return -1;
}
-STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+ {
+ STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-list<TARIFF_IMPL>::iterator ti;
-ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
+ list<TARIFF_IMPL>::iterator ti;
+ ti = find(tariffs.begin(), tariffs.end(), TARIFF_IMPL(name));
-if (ti != tariffs.end())
- {
- strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
- WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
- return -1;
- }
+ if (ti != tariffs.end())
+ {
+ strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist.";
+ WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
+ return -1;
+ }
-tariffs.push_back(TARIFF_IMPL(name));
+ tariffs.push_back(TARIFF_IMPL(name));
+ }
if (store->AddTariff(name) < 0)
{
return -1;
}
+// Fire all "on add" notifiers
+std::set<NOTIFIER_BASE<TARIFF_DATA> *>::iterator ni = onAddNotifiers.begin();
+while (ni != onAddNotifiers.end())
+ {
+ (*ni)->Notify(tariffs.back().GetTariffData());
+ ++ni;
+ }
+
WriteServLog("%s Tariff \'%s\' added.",
admin->GetLogStr().c_str(), name.c_str());
}
}
//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onAddNotifiers.insert(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onAddNotifiers.erase(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onDelNotifiers.insert(n);
+}
+//-----------------------------------------------------------------------------
+void TARIFFS_IMPL::DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * n)
+{
+STG_LOCKER lock(&mutex, __FILE__, __LINE__);
+onDelNotifiers.erase(n);
+}
+//-----------------------------------------------------------------------------
- /*
- $Revision: 1.7 $
- $Date: 2010/10/07 18:43:21 $
- */
-
/*
* 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
* Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
*/
-/*
- $Revision: 1.7 $
- $Date: 2010/10/07 18:43:21 $
- $Author: faust $
- */
-
#ifndef TARIFFS_IMPL_H
#define TARIFFS_IMPL_H
#include <string>
#include <list>
+#include <set>
#include "stg/tariff.h"
#include "stg/tariffs.h"
int Add(const std::string & name, const ADMIN * admin);
int Chg(const TARIFF_DATA & td, const ADMIN * admin);
+ void AddNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * notifier);
+ void DelNotifierAdd(NOTIFIER_BASE<TARIFF_DATA> * notifier);
+
+ void AddNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * notifier);
+ void DelNotifierDel(NOTIFIER_BASE<TARIFF_DATA> * notifier);
+
void GetTariffsData(std::list<TARIFF_DATA> * tdl);
const std::string & GetStrError() const { return strError; }
mutable pthread_mutex_t mutex;
std::string strError;
TARIFF_IMPL noTariff;
+
+ std::set<NOTIFIER_BASE<TARIFF_DATA>*> onAddNotifiers;
+ std::set<NOTIFIER_BASE<TARIFF_DATA>*> onDelNotifiers;
};
#endif