]> git.stg.codes - stg.git/blobdiff - stglibs/pinger.lib/pinger.cpp
Stupid comments erased
[stg.git] / stglibs / pinger.lib / pinger.cpp
index 9169b4e607f07400c9ac8968168c81357664d1d5..216b5977e5b7efbf3ddb4dc4a56be20972fcf3b9 100644 (file)
@@ -15,8 +15,8 @@
 #include <stdio.h>
 
 #include "pinger.h"
 #include <stdio.h>
 
 #include "pinger.h"
-#include "common.h"
-#include "stg_locker.h"
+#include "stg/common.h"
+#include "stg/locker.h"
 
 #ifdef STG_TIME
 extern volatile time_t stgTime;
 
 #ifdef STG_TIME
 extern volatile time_t stgTime;
@@ -24,10 +24,16 @@ extern volatile time_t stgTime;
 
 //-----------------------------------------------------------------------------
 STG_PINGER::STG_PINGER(time_t d)
 
 //-----------------------------------------------------------------------------
 STG_PINGER::STG_PINGER(time_t d)
+    : delay(d),
+      nonstop(false),
+      isRunningRecver(false),
+      isRunningSender(false),
+      sendSocket(-1),
+      recvSocket(-1),
+      pid(0)
 {
 {
-    delay = d;
     pthread_mutex_init(&mutex, NULL);
     pthread_mutex_init(&mutex, NULL);
-    pid = 0;
+    memset(&pmSend, 0, sizeof(pmSend));
 }
 //-----------------------------------------------------------------------------
 STG_PINGER::~STG_PINGER()
 }
 //-----------------------------------------------------------------------------
 STG_PINGER::~STG_PINGER()
@@ -71,8 +77,7 @@ int STG_PINGER::Stop()
     if (isRunningRecver)
         {
         //5 seconds to thread stops itself
     if (isRunningRecver)
         {
         //5 seconds to thread stops itself
-        int i;
-        for (i = 0; i < 25; i++)
+        for (size_t i = 0; i < 25; i++)
             {
             if (i % 5 == 0)
                 SendPing(0x0100007f);//127.0.0.1
             {
             if (i % 5 == 0)
                 SendPing(0x0100007f);//127.0.0.1
@@ -86,20 +91,18 @@ int STG_PINGER::Stop()
         //after 5 seconds waiting thread still running. now killing it
         if (isRunningRecver)
             {
         //after 5 seconds waiting thread still running. now killing it
         if (isRunningRecver)
             {
-            //if (pthread_kill(recvThread, SIGINT))
-            //    {
+            if (pthread_kill(recvThread, SIGINT))
+                {
                 errorStr = "Cannot kill thread.";
                 return -1;
                 errorStr = "Cannot kill thread.";
                 return -1;
-            //    }
-            //printf("recvThread killed\n");
+                }
             }
         }
 
     if (isRunningSender)
         {
         //5 seconds to thread stops itself
             }
         }
 
     if (isRunningSender)
         {
         //5 seconds to thread stops itself
-        int i;
-        for (i = 0; i < 25; i++)
+        for (size_t i = 0; i < 25; i++)
             {
             if (!isRunningSender)
                 break;
             {
             if (!isRunningSender)
                 break;
@@ -110,12 +113,11 @@ int STG_PINGER::Stop()
         //after 5 seconds waiting thread still running. now killing it
         if (isRunningSender)
             {
         //after 5 seconds waiting thread still running. now killing it
         if (isRunningSender)
             {
-            //if (pthread_kill(sendThread, SIGINT))
-            //    {
+            if (pthread_kill(sendThread, SIGINT))
+                {
                 errorStr = "Cannot kill thread.";
                 return -1;
                 errorStr = "Cannot kill thread.";
                 return -1;
-            //    }
-            //printf("sendThread killed\n");
+                }
             }
         }
 
             }
         }
 
@@ -126,14 +128,12 @@ int STG_PINGER::Stop()
 void STG_PINGER::AddIP(uint32_t ip)
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 void STG_PINGER::AddIP(uint32_t ip)
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-    //printf("AddIP\n");
     ipToAdd.push_back(ip);
 }
 //-----------------------------------------------------------------------------
 void STG_PINGER::DelIP(uint32_t ip)
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
     ipToAdd.push_back(ip);
 }
 //-----------------------------------------------------------------------------
 void STG_PINGER::DelIP(uint32_t ip)
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-    //printf("DelIP\n");
     ipToDel.push_back(ip);
 }
 //-----------------------------------------------------------------------------
     ipToDel.push_back(ip);
 }
 //-----------------------------------------------------------------------------
@@ -141,14 +141,12 @@ void STG_PINGER::RealAddIP()
     {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
     {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
-    list<uint32_t>::iterator iter;
+    std::list<uint32_t>::iterator iter;
     iter = ipToAdd.begin();
     while (iter != ipToAdd.end())
         {
     iter = ipToAdd.begin();
     while (iter != ipToAdd.end())
         {
-        /*packets.insert(pair<RAW_PACKET, PACKET_EXTRA_DATA>(rawPacket, ed));*/
-        //pingIP[*iter] = 0;
-        pingIP.insert(pair<uint32_t, time_t>(*iter, 0));
-        iter++;
+        pingIP.insert(std::make_pair(*iter, 0));
+        ++iter;
         }
     ipToAdd.erase(ipToAdd.begin(), ipToAdd.end());
     }
         }
     ipToAdd.erase(ipToAdd.begin(), ipToAdd.end());
     }
@@ -157,52 +155,46 @@ void STG_PINGER::RealDelIP()
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 
-    list<uint32_t>::iterator iter;
-    multimap<uint32_t, time_t>::iterator treeIter;
+    std::list<uint32_t>::iterator iter;
+    std::multimap<uint32_t, time_t>::iterator treeIter;
     iter = ipToDel.begin();
     while (iter != ipToDel.end())
         {
         treeIter = pingIP.find(*iter);
     iter = ipToDel.begin();
     while (iter != ipToDel.end())
         {
         treeIter = pingIP.find(*iter);
-        //printf("Found %X\n", *iter);
         if (treeIter != pingIP.end())
             pingIP.erase(treeIter);
 
         if (treeIter != pingIP.end())
             pingIP.erase(treeIter);
 
-        iter++;
+        ++iter;
         }
     ipToDel.erase(ipToDel.begin(), ipToDel.end());
 }
 //-----------------------------------------------------------------------------
         }
     ipToDel.erase(ipToDel.begin(), ipToDel.end());
 }
 //-----------------------------------------------------------------------------
-int STG_PINGER::GetPingIPNum()
+int STG_PINGER::GetPingIPNum() const
 {
     return pingIP.size();
 }
 //-----------------------------------------------------------------------------
 {
     return pingIP.size();
 }
 //-----------------------------------------------------------------------------
-void STG_PINGER::GetAllIP(vector<PING_IP_TIME> *)
-{
-    //STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-}
-//-----------------------------------------------------------------------------
 void STG_PINGER::PrintAllIP()
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 void STG_PINGER::PrintAllIP()
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-    multimap<uint32_t, time_t>::iterator iter;
+    std::multimap<uint32_t, time_t>::iterator iter;
     iter = pingIP.begin();
     while (iter != pingIP.end())
         {
         uint32_t ip = iter->first;
         time_t t = iter->second;
     iter = pingIP.begin();
     while (iter != pingIP.end())
         {
         uint32_t ip = iter->first;
         time_t t = iter->second;
-        string s;
+        std::string s;
         x2str(t, s);
         printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str());
         x2str(t, s);
         printf("ip = %s, time = %9s\n", inet_ntostring(ip).c_str(), s.c_str());
-        iter++;
+        ++iter;
         }
 
 }
 //-----------------------------------------------------------------------------
         }
 
 }
 //-----------------------------------------------------------------------------
-int STG_PINGER::GetIPTime(uint32_t ip, time_t * t)
+int STG_PINGER::GetIPTime(uint32_t ip, time_t * t) const
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
 {
     STG_LOCKER lock(&mutex, __FILE__, __LINE__);
-    multimap<uint32_t, time_t>::iterator treeIter;
+    std::multimap<uint32_t, time_t>::const_iterator treeIter;
 
     treeIter = pingIP.find(ip);
     if (treeIter == pingIP.end())
 
     treeIter = pingIP.find(ip);
     if (treeIter == pingIP.end())
@@ -212,21 +204,6 @@ int STG_PINGER::GetIPTime(uint32_t ip, time_t * t)
     return 0;
 }
 //-----------------------------------------------------------------------------
     return 0;
 }
 //-----------------------------------------------------------------------------
-void STG_PINGER::SetDelayTime(time_t d)
-{
-    delay = d;
-}
-//-----------------------------------------------------------------------------
-time_t STG_PINGER::GetDelayTime()
-{
-    return delay;
-}
-//-----------------------------------------------------------------------------
-string STG_PINGER::GetStrError()
-{
-    return errorStr;
-}
-//-----------------------------------------------------------------------------
 uint16_t STG_PINGER::PingCheckSum(void * data, int len)
 {
     unsigned short * buf = (unsigned short *)data;
 uint16_t STG_PINGER::PingCheckSum(void * data, int len)
 {
     unsigned short * buf = (unsigned short *)data;
@@ -248,7 +225,6 @@ uint16_t STG_PINGER::PingCheckSum(void * data, int len)
 int STG_PINGER::SendPing(uint32_t ip)
 {
     struct sockaddr_in addr;
 int STG_PINGER::SendPing(uint32_t ip)
 {
     struct sockaddr_in addr;
-    //printf("SendPing %X \n", ip);
     memset(&addr, 0, sizeof(addr));
     addr.sin_family = AF_INET;
     addr.sin_port = 0;
     memset(&addr, 0, sizeof(addr));
     addr.sin_family = AF_INET;
     addr.sin_port = 0;
@@ -263,7 +239,7 @@ int STG_PINGER::SendPing(uint32_t ip)
 
     if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 )
         {
 
     if (sendto(sendSocket, &pmSend, sizeof(pmSend), 0, (sockaddr *)&addr, sizeof(addr)) <= 0 )
         {
-        errorStr = "Send ping error: " + string(strerror(errno));
+        errorStr = "Send ping error: " + std::string(strerror(errno));
         return -1;
         }
 
         return -1;
         }
 
@@ -282,13 +258,11 @@ uint32_t STG_PINGER::RecvPing()
     socklen_t len = sizeof(addr);
 
     bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len);
     socklen_t len = sizeof(addr);
 
     bytes = recvfrom(recvSocket, &buf, sizeof(buf), 0, (struct sockaddr*)&addr, &len);
-    //printf("recvfrom\n");
     if (bytes > 0)
         {
         struct IP_HDR * ip = (struct IP_HDR *)buf;
         struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4);
 
     if (bytes > 0)
         {
         struct IP_HDR * ip = (struct IP_HDR *)buf;
         struct ICMP_HDR *icmp = (struct ICMP_HDR *)(buf + ip->ihl * 4);
 
-        //printf("icmp->un.echo.id=%d,  pid=%d, tid: %d\n", icmp->un.echo.id, pid);
         if (icmp->un.echo.id != pid)
             return 0;
 
         if (icmp->un.echo.id != pid)
             return 0;
 
@@ -300,7 +274,7 @@ uint32_t STG_PINGER::RecvPing()
 //-----------------------------------------------------------------------------
 void * STG_PINGER::RunSendPing(void * d)
 {
 //-----------------------------------------------------------------------------
 void * STG_PINGER::RunSendPing(void * d)
 {
-    STG_PINGER * pinger = (STG_PINGER*)d;
+    STG_PINGER * pinger = static_cast<STG_PINGER *>(d);
 
     pinger->isRunningSender = true;
     time_t lastPing = 0;
 
     pinger->isRunningSender = true;
     time_t lastPing = 0;
@@ -309,13 +283,12 @@ void * STG_PINGER::RunSendPing(void * d)
         pinger->RealAddIP();
         pinger->RealDelIP();
 
         pinger->RealAddIP();
         pinger->RealDelIP();
 
-        multimap<uint32_t, time_t>::iterator iter;
+        std::multimap<uint32_t, time_t>::iterator iter;
         iter = pinger->pingIP.begin();
         while (iter != pinger->pingIP.end())
             {
         iter = pinger->pingIP.begin();
         while (iter != pinger->pingIP.end())
             {
-            uint32_t ip = iter->first;
-            pinger->SendPing(ip);
-            iter++;
+            pinger->SendPing(iter->first);
+            ++iter;
             }
 
         time_t currTime;
             }
 
         time_t currTime;
@@ -336,7 +309,6 @@ void * STG_PINGER::RunSendPing(void * d)
             #endif
             usleep(20000);
             }
             #endif
             usleep(20000);
             }
-        //printf("new ping cycle\n");
         }
 
     pinger->isRunningSender = false;
         }
 
     pinger->isRunningSender = false;
@@ -346,37 +318,26 @@ void * STG_PINGER::RunSendPing(void * d)
 //-----------------------------------------------------------------------------
 void * STG_PINGER::RunRecvPing(void * d)
 {
 //-----------------------------------------------------------------------------
 void * STG_PINGER::RunRecvPing(void * d)
 {
-    STG_PINGER * pinger = (STG_PINGER*)d;
+    STG_PINGER * pinger = static_cast<STG_PINGER *>(d);
 
     pinger->isRunningRecver = true;
 
 
     pinger->isRunningRecver = true;
 
-    uint32_t ip;
-    multimap<uint32_t, time_t>::iterator treeIterLower;
-    multimap<uint32_t, time_t>::iterator treeIterUpper;
-
     while (pinger->nonstop)
         {
     while (pinger->nonstop)
         {
-        ip = pinger->RecvPing();
+        uint32_t ip = pinger->RecvPing();
 
         if (ip)
             {
 
         if (ip)
             {
-            //printf("RecvPing %X\n", ip);
-            treeIterUpper = pinger->pingIP.upper_bound(ip);
-            treeIterLower = pinger->pingIP.lower_bound(ip);
-            int i = 0;
+            std::multimap<uint32_t, time_t>::iterator treeIterUpper = pinger->pingIP.upper_bound(ip);
+            std::multimap<uint32_t, time_t>::iterator treeIterLower = pinger->pingIP.lower_bound(ip);
             while (treeIterUpper != treeIterLower)
             while (treeIterUpper != treeIterLower)
-            //treeIterUpper = pinger->pingIP.find(ip);
-            //if (treeIterUpper != pinger->pingIP.end())
                 {
                 {
-                //printf("+++! time=%d %X i=%d !+++\n", time(NULL), ip, i);
-                //printf("--- time=%d ---\n", time(NULL));
                 #ifdef STG_TIME
                 treeIterLower->second = stgTime;
                 #else
                 treeIterLower->second = time(NULL);
                 #endif
                 ++treeIterLower;
                 #ifdef STG_TIME
                 treeIterLower->second = stgTime;
                 #else
                 treeIterLower->second = time(NULL);
                 #endif
                 ++treeIterLower;
-                i++;
                 }
             }
 
                 }
             }
 
@@ -385,4 +346,3 @@ void * STG_PINGER::RunRecvPing(void * d)
     return NULL;
 }
 //-----------------------------------------------------------------------------
     return NULL;
 }
 //-----------------------------------------------------------------------------
-