]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/other/ping/ping.cpp
Use common filtering algorithm with predicate specialization
[stg.git] / projects / stargazer / plugins / other / ping / ping.cpp
index f9fc387aa11f27dc1810e95f89e2c4629bbbaf16..0fb5d30bda075db38a5e69840bd6b1553fd66715 100644 (file)
@@ -1,14 +1,13 @@
 #include <stdio.h>
 #include <stdio.h>
-#include <unistd.h>
 #include <signal.h>
 
 #include <signal.h>
 
+#include <ctime>
 #include <algorithm>
 
 #include <algorithm>
 
+#include "stg/user.h"
+#include "stg/locker.h"
+#include "stg/user_property.h"
 #include "ping.h"
 #include "ping.h"
-#include "user.h"
-#include "stg_locker.h"
-#include "user_property.h"
-#include "../../../settings.h"
 
 class PING_CREATOR
 {
 
 class PING_CREATOR
 {
@@ -178,12 +177,13 @@ if (!isRunning)
 pinger.Stop();
 nonstop = false;
 //5 seconds to thread stops itself
 pinger.Stop();
 nonstop = false;
 //5 seconds to thread stops itself
+struct timespec ts = {0, 200000000};
 for (int i = 0; i < 25; i++)
     {
     if (!isRunning)
         break;
 
 for (int i = 0; i < 25; i++)
     {
     if (!isRunning)
         break;
 
-    usleep(200000);
+    nanosleep(&ts, NULL);
     }
 
 //after 5 seconds waiting thread still running. now kill it
     }
 
 //after 5 seconds waiting thread still running. now kill it
@@ -220,22 +220,22 @@ return isRunning;
 //-----------------------------------------------------------------------------
 void * PING::Run(void * d)
 {
 //-----------------------------------------------------------------------------
 void * PING::Run(void * d)
 {
-PING * ping = (PING*)d;
+PING * ping = (PING *)d;
 ping->isRunning = true;
 ping->isRunning = true;
-list<USER_PTR>::iterator iter;
-uint32_t ip;
-time_t t;
 
 
+long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
 while (ping->nonstop)
     {
 while (ping->nonstop)
     {
-    iter = ping->usersList.begin();
+    list<USER_PTR>::iterator iter = ping->usersList.begin();
         {
         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
         while (iter != ping->usersList.end())
             {
             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
                 {
         {
         STG_LOCKER lock(&ping->mutex, __FILE__, __LINE__);
         while (iter != ping->usersList.end())
             {
             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
                 {
-                ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
+                uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
+                time_t t;
                 if (ping->pinger.GetIPTime(ip, &t) == 0)
                     {
                     if (t)
                 if (ping->pinger.GetIPTime(ip, &t) == 0)
                     {
                     if (t)
@@ -244,9 +244,10 @@ while (ping->nonstop)
                 }
             else
                 {
                 }
             else
                 {
-                ip = (*iter)->GetCurrIP();
+                uint32_t ip = (*iter)->GetCurrIP();
                 if (ip)
                     {
                 if (ip)
                     {
+                    time_t t;
                     if (ping->pinger.GetIPTime(ip, &t) == 0)
                         {
                         if (t)
                     if (ping->pinger.GetIPTime(ip, &t) == 0)
                         {
                         if (t)
@@ -257,14 +258,16 @@ while (ping->nonstop)
             ++iter;
             }
         }
             ++iter;
             }
         }
+    struct timespec ts = {delay / 1000000000, delay % 1000000000};
     for (int i = 0; i < 100; i++)
         {
         if (ping->nonstop)
             {
     for (int i = 0; i < 100; i++)
         {
         if (ping->nonstop)
             {
-            usleep((10000*ping->pingSettings.GetPingDelay())/3 + 50000);
+            nanosleep(&ts, NULL);
             }
         }
     }
             }
         }
     }
+
 ping->isRunning = false;
 return NULL;
 }
 ping->isRunning = false;
 return NULL;
 }