]> git.stg.codes - stg.git/blobdiff - projects/stargazer/traffcounter_impl.h
Fix build on Darwin.
[stg.git] / projects / stargazer / traffcounter_impl.h
index 78027cb57d3d7d5eb8b08448b3b4e81843c9d65a..03c118dfb94a1c1c6bb9965a6b99c84f18e31761 100644 (file)
 #include "stg/traffcounter.h"
 #include "stg/logger.h"
 #include "stg/raw_ip_packet.h"
-#include "stg/noncopyable.h"
-#include "stg/notifer.h"
-#include "actions.h"
-#include "eventloop.h"
+#include "stg/subscriptions.h"
 #include "user_impl.h"
 
-#include <ctime>
 #include <list>
+#include <vector>
+#include <tuple>
 #include <map>
 #include <string>
+#include <mutex>
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wshadow"
+#include <jthread.hpp>
+#pragma GCC diagnostic pop
 #include <cstdint>
-
-#include <pthread.h>
+#include <ctime>
 
 #define PROTOMAX    (5)
 
@@ -80,92 +82,7 @@ struct PacketExtraData {
     uint32_t    lenD;               // Download length
 };
 //-----------------------------------------------------------------------------
-class TraffCounterImpl;
-//-----------------------------------------------------------------------------
-class TRF_IP_BEFORE: public PropertyNotifierBase<uint32_t> {
-public:
-                TRF_IP_BEFORE(TraffCounterImpl & t, UserImpl * u)
-                    : PropertyNotifierBase<uint32_t>(),
-                      traffCnt(t),
-                      user(u)
-                {}
-                TRF_IP_BEFORE(const TRF_IP_BEFORE & rvalue)
-                    : PropertyNotifierBase<uint32_t>(),
-                      traffCnt(rvalue.traffCnt),
-                      user(rvalue.user)
-                {}
-    void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void        SetUser(UserImpl * u) { user = u; }
-    UserImpl * GetUser() const { return user; }
-
-private:
-    TRF_IP_BEFORE & operator=(const TRF_IP_BEFORE & rvalue);
-
-    TraffCounterImpl & traffCnt;
-    UserImpl * user;
-};
-//-----------------------------------------------------------------------------
-class TRF_IP_AFTER: public PropertyNotifierBase<uint32_t> {
-public:
-                TRF_IP_AFTER(TraffCounterImpl & t, UserImpl * u)
-                    : PropertyNotifierBase<uint32_t>(),
-                      traffCnt(t),
-                      user(u)
-                {}
-                TRF_IP_AFTER(const TRF_IP_AFTER & rvalue)
-                    : PropertyNotifierBase<uint32_t>(),
-                      traffCnt(rvalue.traffCnt),
-                      user(rvalue.user)
-                {}
-    void        Notify(const uint32_t & oldValue, const uint32_t & newValue);
-    void        SetUser(UserImpl * u) { user = u; }
-    UserImpl * GetUser() const { return user; }
-private:
-    TRF_IP_AFTER & operator=(const TRF_IP_AFTER & rvalue);
-
-    TraffCounterImpl & traffCnt;
-    UserImpl * user;
-};
-
-using UserImplPtr = UserImpl*;
-//-----------------------------------------------------------------------------
-class ADD_USER_NONIFIER: public NotifierBase<UserImplPtr> {
-public:
-            explicit ADD_USER_NONIFIER(TraffCounterImpl & t) :
-                NotifierBase<UserImplPtr>(),
-                traffCnt(t)
-            {}
-    virtual ~ADD_USER_NONIFIER() {}
-    void    Notify(const UserImplPtr & user);
-
-private:
-    ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
-    ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
-
-    TraffCounterImpl & traffCnt;
-};
-//-----------------------------------------------------------------------------
-class DEL_USER_NONIFIER: public NotifierBase<UserImplPtr> {
-public:
-            explicit DEL_USER_NONIFIER(TraffCounterImpl & t) :
-                NotifierBase<UserImplPtr>(),
-                traffCnt(t)
-            {}
-    virtual ~DEL_USER_NONIFIER() {}
-    void    Notify(const UserImplPtr & user);
-
-private:
-    DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
-    DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
-
-    TraffCounterImpl & traffCnt;
-};
-//-----------------------------------------------------------------------------
 class TraffCounterImpl : public TraffCounter {
-    friend class ADD_USER_NONIFIER;
-    friend class DEL_USER_NONIFIER;
-    friend class TRF_IP_BEFORE;
-    friend class TRF_IP_AFTER;
     public:
         TraffCounterImpl(UsersImpl * users, const std::string & rulesFileName);
         ~TraffCounterImpl();
@@ -185,7 +102,7 @@ class TraffCounterImpl : public TraffCounter {
         void        FreeRules();
         bool        ReadRules(bool test = false);
 
-        static void * Run(void * data);
+        void        Run(std::stop_token token);
 
         void        DeterminateDir(const RawPacket & packet,
                                    int * dirU, // Direction for upload
@@ -195,8 +112,8 @@ class TraffCounterImpl : public TraffCounter {
 
         void        AddUser(UserImpl * user);
         void        DelUser(uint32_t uip);
-        void        SetUserNotifiers(UserImpl * user);
-        void        UnSetUserNotifiers(UserImpl * user);
+        void        SetUserNotifiers(UserImpl* user);
+        void        UnSetUserNotifiers(UserImpl* user);
 
         typedef std::list<Rule>::iterator rule_iter;
 
@@ -223,49 +140,18 @@ class TraffCounterImpl : public TraffCounter {
 
         UsersImpl *             users;
 
-        bool                     running;
         bool                     stopped;
-        pthread_mutex_t          mutex;
-        pthread_t                thread;
+        std::mutex               m_mutex;
+        std::jthread             m_thread;
 
-        std::list<TRF_IP_BEFORE> ipBeforeNotifiers;
-        std::list<TRF_IP_AFTER>  ipAfterNotifiers;
+        ScopedConnection m_onAddUserConn;
+        ScopedConnection m_onDelUserConn;
 
-        ADD_USER_NONIFIER        addUserNotifier;
-        DEL_USER_NONIFIER        delUserNotifier;
+        using OnIPConns = std::tuple<int, ScopedConnection, ScopedConnection>;
+        std::vector<OnIPConns> m_onIPConns;
+        void beforeIPChange(uint32_t oldVal);
+        void afterIPChange(UserImpl* user, uint32_t newVal);
 };
-//-----------------------------------------------------------------------------
-inline
-void TRF_IP_BEFORE::Notify(const uint32_t & oldValue, const uint32_t &)
-{
-// User changes his address. Remove old IP
-if (!oldValue)
-    return;
-
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, oldValue);
-}
-//-----------------------------------------------------------------------------
-inline
-void TRF_IP_AFTER::Notify(const uint32_t &, const uint32_t & newValue)
-{
-// User changes his address. Add new IP
-if (!newValue)
-    return;
 
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::AddUser, user);
-}
-//-----------------------------------------------------------------------------
-inline
-void ADD_USER_NONIFIER::Notify(const UserImplPtr & user)
-{
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::SetUserNotifiers, user);
 }
 //-----------------------------------------------------------------------------
-inline
-void DEL_USER_NONIFIER::Notify(const UserImplPtr & user)
-{
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::UnSetUserNotifiers, user);
-EVENT_LOOP_SINGLETON::GetInstance().Enqueue(traffCnt, &TraffCounterImpl::DelUser, user->GetCurrIP());
-}
-//-----------------------------------------------------------------------------
-}