]> git.stg.codes - stg.git/blobdiff - projects/stargazer/traffcounter_impl.h
[NY] Made NETUNIT private.
[stg.git] / projects / stargazer / traffcounter_impl.h
index c30f6be4f01754b3dcc45a4873f4bf07b5b92390..7e552f82c186e2eab2c179c72ecb2d0d972a43ca 100644 (file)
 #include "stg/os_int.h"
 #include "stg/logger.h"
 #include "stg/raw_ip_packet.h"
 #include "stg/os_int.h"
 #include "stg/logger.h"
 #include "stg/raw_ip_packet.h"
-#include "stg/users.h"
 #include "stg/noncopyable.h"
 #include "stg/noncopyable.h"
+#include "stg/notifer.h"
 #include "actions.h"
 #include "eventloop.h"
 #include "actions.h"
 #include "eventloop.h"
+#include "user_impl.h"
 
 #define PROTOMAX    (5)
 
 
 #define PROTOMAX    (5)
 
-class TARIFFS;
+class USERS_IMPL;
 
 //-----------------------------------------------------------------------------
 struct RULE {
 
 //-----------------------------------------------------------------------------
 struct RULE {
@@ -62,9 +63,9 @@ struct PACKET_EXTRA_DATA {
 PACKET_EXTRA_DATA()
     : flushTime(0),
       updateTime(0),
 PACKET_EXTRA_DATA()
     : flushTime(0),
       updateTime(0),
-      userU(),
+      userU(NULL),
       userUPresent(false),
       userUPresent(false),
-      userD(),
+      userD(NULL),
       userDPresent(false),
       dirU(DIR_NUM),
       dirD(DIR_NUM),
       userDPresent(false),
       dirU(DIR_NUM),
       dirD(DIR_NUM),
@@ -87,9 +88,9 @@ PACKET_EXTRA_DATA(const PACKET_EXTRA_DATA & pp)
 
 time_t      flushTime;          // Last flush time
 time_t      updateTime;         // Last update time
 
 time_t      flushTime;          // Last flush time
 time_t      updateTime;         // Last update time
-USER_PTR    userU;              // Uploader
+USER_IMPL * userU;              // Uploader
 bool        userUPresent;       // Uploader is registered user
 bool        userUPresent;       // Uploader is registered user
-USER_PTR    userD;              // Downloader
+USER_IMPL * userD;              // Downloader
 bool        userDPresent;       // Downloader is registered user
 int         dirU;               // Upload direction
 int         dirD;               // Download direction
 bool        userDPresent;       // Downloader is registered user
 int         dirU;               // Upload direction
 int         dirD;               // Download direction
@@ -101,56 +102,78 @@ class TRAFFCOUNTER_IMPL;
 //-----------------------------------------------------------------------------
 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
 public:
 //-----------------------------------------------------------------------------
 class TRF_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
 public:
-                    TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_PTR u)
-                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
-                          traffCnt(t),
-                          user(u)
-                    {}
-    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void            SetUser(USER_PTR u) { user = u; }
-    USER_PTR        GetUser() const { return user; }
+                TRF_IP_BEFORE(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
+                    : PROPERTY_NOTIFIER_BASE<uint32_t>(),
+                      traffCnt(t),
+                      user(u)
+                {}
+                TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
+                    : PROPERTY_NOTIFIER_BASE<uint32_t>(),
+                      traffCnt(rvalue.traffCnt),
+                      user(rvalue.user)
+                {}
+    void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void        SetUser(USER_IMPL * u) { user = u; }
+    USER_IMPL * GetUser() const { return user; }
 
 private:
 
 private:
+    TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
+
     TRAFFCOUNTER_IMPL & traffCnt;
     TRAFFCOUNTER_IMPL & traffCnt;
-    USER_PTR            user;
+    USER_IMPL * user;
 };
 //-----------------------------------------------------------------------------
 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
 public:
 };
 //-----------------------------------------------------------------------------
 class TRF_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
 public:
-                    TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_PTR u)
-                        : PROPERTY_NOTIFIER_BASE<uint32_t>(),
-                          traffCnt(t),
-                          user(u)
-                    {}
-    void            Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void            SetUser(USER_PTR u) { user = u; }
-    USER_PTR        GetUser() const { return user; }
+                TRF_IP_AFTER(TRAFFCOUNTER_IMPL & t, USER_IMPL * u)
+                    : PROPERTY_NOTIFIER_BASE<uint32_t>(),
+                      traffCnt(t),
+                      user(u)
+                {}
+                TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
+                    : PROPERTY_NOTIFIER_BASE<uint32_t>(),
+                      traffCnt(rvalue.traffCnt),
+                      user(rvalue.user)
+                {}
+    void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
+    void        SetUser(USER_IMPL * u) { user = u; }
+    USER_IMPL * GetUser() const { return user; }
 private:
 private:
+    TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
+
     TRAFFCOUNTER_IMPL & traffCnt;
     TRAFFCOUNTER_IMPL & traffCnt;
-    USER_PTR            user;
+    USER_IMPL * user;
 };
 //-----------------------------------------------------------------------------
 };
 //-----------------------------------------------------------------------------
-class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
+class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
 public:
             ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
 public:
             ADD_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
-                NOTIFIER_BASE<USER_PTR>(),
+                NOTIFIER_BASE<USER_IMPL_PTR>(),
                 traffCnt(t)
             {}
     virtual ~ADD_USER_NONIFIER() {}
                 traffCnt(t)
             {}
     virtual ~ADD_USER_NONIFIER() {}
-    void    Notify(const USER_PTR & user);
+    void    Notify(const USER_IMPL_PTR & user);
+
 private:
 private:
+    ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
+    ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
+
     TRAFFCOUNTER_IMPL & traffCnt;
 };
 //-----------------------------------------------------------------------------
     TRAFFCOUNTER_IMPL & traffCnt;
 };
 //-----------------------------------------------------------------------------
-class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
+class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_IMPL_PTR> {
 public:
             DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
 public:
             DEL_USER_NONIFIER(TRAFFCOUNTER_IMPL & t) :
-                NOTIFIER_BASE<USER_PTR>(),
+                NOTIFIER_BASE<USER_IMPL_PTR>(),
                 traffCnt(t)
             {}
     virtual ~DEL_USER_NONIFIER() {}
                 traffCnt(t)
             {}
     virtual ~DEL_USER_NONIFIER() {}
-    void    Notify(const USER_PTR & user);
+    void    Notify(const USER_IMPL_PTR & user);
+
 private:
 private:
+    DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
+    DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
+
     TRAFFCOUNTER_IMPL & traffCnt;
 };
 //-----------------------------------------------------------------------------
     TRAFFCOUNTER_IMPL & traffCnt;
 };
 //-----------------------------------------------------------------------------
@@ -160,7 +183,7 @@ friend class DEL_USER_NONIFIER;
 friend class TRF_IP_BEFORE;
 friend class TRF_IP_AFTER;
 public:
 friend class TRF_IP_BEFORE;
 friend class TRF_IP_AFTER;
 public:
-    TRAFFCOUNTER_IMPL(USERS * users, const TARIFFS * tariffs, const std::string & rulesFileName);
+    TRAFFCOUNTER_IMPL(USERS_IMPL * users, const std::string & rulesFileName);
     ~TRAFFCOUNTER_IMPL();
 
     void        SetRulesFile(const std::string & rulesFileName);
     ~TRAFFCOUNTER_IMPL();
 
     void        SetRulesFile(const std::string & rulesFileName);
@@ -172,7 +195,12 @@ public:
     void        Process(const RAW_PACKET & rawPacket);
     void        SetMonitorDir(const std::string & monitorDir);
 
     void        Process(const RAW_PACKET & rawPacket);
     void        SetMonitorDir(const std::string & monitorDir);
 
+    size_t      RulesCount() const { return rules.size(); }
+
 private:
 private:
+    TRAFFCOUNTER_IMPL(const TRAFFCOUNTER_IMPL & rvalue);
+    TRAFFCOUNTER_IMPL & operator=(const TRAFFCOUNTER_IMPL & rvalue);
+
     bool        ParseAddress(const char * ta, RULE * rule) const;
     uint32_t    CalcMask(uint32_t msk) const;
     void        FreeRules();
     bool        ParseAddress(const char * ta, RULE * rule) const;
     uint32_t    CalcMask(uint32_t msk) const;
     void        FreeRules();
@@ -187,21 +215,24 @@ private:
 
     void        FlushAndRemove();
 
 
     void        FlushAndRemove();
 
-    void        AddUser(USER_PTR user);
+    void        AddUser(USER_IMPL * user);
     void        DelUser(uint32_t uip);
     void        DelUser(uint32_t uip);
-    void        SetUserNotifiers(USER_PTR user);
-    void        UnSetUserNotifiers(USER_PTR user);
+    void        SetUserNotifiers(USER_IMPL * user);
+    void        UnSetUserNotifiers(USER_IMPL * user);
 
     typedef std::list<RULE>::iterator rule_iter;
 
     typedef std::list<RULE>::iterator rule_iter;
-    typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA>::iterator pp_iter;
-    typedef std::multimap<uint32_t, pp_iter>::iterator ip2p_iter;
-    typedef std::multimap<uint32_t, pp_iter>::const_iterator ip2p_citer;
 
     std::list<RULE>          rules;
 
 
     std::list<RULE>          rules;
 
-    std::map<RAW_PACKET, PACKET_EXTRA_DATA> packets; // Packets tree
+    typedef std::map<RAW_PACKET, PACKET_EXTRA_DATA> Packets;
+    typedef Packets::iterator pp_iter;
+    typedef std::multimap<uint32_t, pp_iter> Index;
+    typedef Index::iterator ip2p_iter;
+    typedef Index::const_iterator ip2p_citer;
+
+    Packets packets; // Packets tree
 
 
-    std::multimap<uint32_t, pp_iter> ip2packets; // IP-to-Packet index
+    Index ip2packets; // IP-to-Packet index
 
     std::string              dirName[DIR_NUM + 1];
 
 
     std::string              dirName[DIR_NUM + 1];
 
@@ -210,8 +241,9 @@ private:
 
     std::string              monitorDir;
     bool                     monitoring;
 
     std::string              monitorDir;
     bool                     monitoring;
+    time_t                   touchTimeP;
 
 
-    USERS *                  users;
+    USERS_IMPL *             users;
 
     bool                     running;
     bool                     stopped;
 
     bool                     running;
     bool                     stopped;
@@ -246,13 +278,13 @@ EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::AddUse
 }
 //-----------------------------------------------------------------------------
 inline
 }
 //-----------------------------------------------------------------------------
 inline
-void ADD_USER_NONIFIER::Notify(const USER_PTR & user)
+void ADD_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
 {
 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
 }
 //-----------------------------------------------------------------------------
 inline
 {
 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::SetUserNotifiers, user);
 }
 //-----------------------------------------------------------------------------
 inline
-void DEL_USER_NONIFIER::Notify(const USER_PTR & user)
+void DEL_USER_NONIFIER::Notify(const USER_IMPL_PTR & user)
 {
 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());
 {
 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::UnSetUserNotifiers, user);
 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TRAFFCOUNTER_IMPL::DelUser, user->GetCurrIP());