]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/smux/smux.cpp
Make tariff table based on tariff list
[stg.git] / projects / stargazer / plugins / other / smux / smux.cpp
index 79e9d208587b0398fa85ac86ee84f717b054b368..23d5f05fdc71bae163c9e759af08f90dd1a15f55 100644 (file)
@@ -6,6 +6,7 @@
 #include <cerrno>
 #include <ctime>
 #include <csignal>
+#include <cassert>
 
 #include <vector>
 #include <algorithm>
 
 #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 "stg/traffcounter.h"*/
 
 #include "smux.h"
 #include "utils.h"
@@ -89,9 +96,19 @@ SMUX::SMUX()
     : PLUGIN(),
       users(NULL),
       tariffs(NULL),
+      admins(NULL),
+      services(NULL),
+      corporations(NULL),
+      traffcounter(NULL),
       running(false),
       stopped(true),
-      sock(-1)
+      sock(-1),
+      smuxHandlers(),
+      pdusHandlers(),
+      sensors(),
+      tables(),
+      notifiers(),
+      addDelNotifier(*this)
 {
 pthread_mutex_init(&mutex, NULL);
 
@@ -128,6 +145,13 @@ return smuxSettings.ParseSettings(settings);
 
 int SMUX::Start()
 {
+assert(users != NULL && "users must not be NULL");
+assert(tariffs != NULL && "tariffs must not be NULL");
+assert(admins != NULL && "admins must not be NULL");
+assert(services != NULL && "services must not be NULL");
+assert(corporations != NULL && "corporations must not be NULL");
+assert(traffcounter != NULL && "traffcounter must not be NULL");
+
 if (PrepareNet())
     return -1;
 
@@ -145,11 +169,22 @@ 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);
+// Traffcounter
+sensors[OID(".1.3.6.1.4.1.38313.1.6.1")] = new TotalRulesSensor(*traffcounter);
 
 // 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);
+tables[".1.3.6.1.4.1.38313.1.1.6"] = new TariffUsersTable(".1.3.6.1.4.1.38313.1.1.6", *tariffs, *users);
 
 UpdateTables();
+SetNotifiers();
+users->AddNotifierUserAdd(&addDelNotifier);
+users->AddNotifierUserDel(&addDelNotifier);
 
 #ifdef DEBUG
 Sensors::const_iterator it(sensors.begin());
@@ -180,6 +215,10 @@ int SMUX::Stop()
 printfd(__FILE__, "SMUX::Stop() - Before\n");
 running = false;
 
+users->DelNotifierUserDel(&addDelNotifier);
+users->DelNotifierUserAdd(&addDelNotifier);
+ResetNotifiers();
+
 if (!stopped)
     {
     //5 seconds to thread stops itself
@@ -226,13 +265,17 @@ SendOpenPDU(sock);
 SendRReqPDU(sock);
 running = true;
 stopped = false;
+
 while(running)
     {
     if (WaitPackets(sock))
         {
         SMUX_PDUs_t * pdus = RecvSMUXPDUs(sock);
         if (pdus)
+            {
             DispatchPDUs(pdus);
+            ASN_STRUCT_FREE(asn_DEF_SMUX_PDUs, pdus);
+            }
         }
     if (!running)
         break;
@@ -351,3 +394,38 @@ sensors.insert(newSensors.begin(), newSensors.end());
 
 return true;
 }
+
+void SMUX::SetNotifiers()
+{
+USER_PTR u;
+int h = users->OpenSearch();
+assert(h && "USERS::OpenSearch is always correct");
+
+while (!users->SearchNext(h, &u))
+    {
+    notifiers.push_back(CHG_AFTER_NOTIFIER(*this, u));
+    u->GetProperty().tariffName.AddAfterNotifier(&notifiers.back());
+    }
+
+users->CloseSearch(h);
+}
+
+void SMUX::ResetNotifiers()
+{
+std::list<CHG_AFTER_NOTIFIER>::iterator it = notifiers.begin();
+while (it != notifiers.end())
+    {
+    it->GetUserPtr()->GetProperty().tariffName.DelAfterNotifier(&(*it));
+    ++it;
+    }
+}
+
+void CHG_AFTER_NOTIFIER::Notify(const std::string &, const std::string &)
+{
+smux.UpdateTables();
+}
+
+void ADD_DEL_USER_NOTIFIER::Notify(const USER_PTR &)
+{
+smux.UpdateTables();
+}