8 #include "stg/locker.h"
 
   9 #include "stg/user_property.h"
 
  10 #include "stg/plugin_creator.h"
 
  15 PLUGIN_CREATOR<PING> pc;
 
  17 //-----------------------------------------------------------------------------
 
  18 //-----------------------------------------------------------------------------
 
  19 //-----------------------------------------------------------------------------
 
  20 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
 
  21 template <typename varType>
 
  22 class IS_CONTAINS_USER: public std::binary_function<varType, USER_PTR, bool>
 
  25     IS_CONTAINS_USER(const USER_PTR & u) : user(u) {}
 
  26     bool operator()(varType notifier) const
 
  28         return notifier.GetUser() == user;
 
  31     const USER_PTR & user;
 
  35 extern "C" PLUGIN * GetPlugin();
 
  36 //-----------------------------------------------------------------------------
 
  37 //-----------------------------------------------------------------------------
 
  38 //-----------------------------------------------------------------------------
 
  41 return pc.GetPlugin();
 
  43 //-----------------------------------------------------------------------------
 
  44 //-----------------------------------------------------------------------------
 
  45 //-----------------------------------------------------------------------------
 
  46 int PING_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
 
  49 std::vector<PARAM_VALUE>::const_iterator pvi;
 
  51 pv.param = "PingDelay";
 
  52 pvi = std::find(s.moduleParams.begin(), s.moduleParams.end(), pv);
 
  53 if (pvi == s.moduleParams.end() || pvi->value.empty())
 
  55     errorStr = "Parameter \'PingDelay\' not found.";
 
  56     printfd(__FILE__, "Parameter 'PingDelay' not found\n");
 
  59 if (ParseIntInRange(pvi->value[0], 5, 3600, &pingDelay))
 
  61     errorStr = "Cannot parse parameter \'PingDelay\': " + errorStr;
 
  62     printfd(__FILE__, "Canot parse parameter 'PingDelay'\n");
 
  68 //-----------------------------------------------------------------------------
 
  73       onAddUserNotifier(*this),
 
  74       onDelUserNotifier(*this),
 
  75       logger(GetPluginLogger(GetStgLogger(), "ping"))
 
  77 pthread_mutex_init(&mutex, NULL);
 
  79 //-----------------------------------------------------------------------------
 
  82 pthread_mutex_destroy(&mutex);
 
  84 //-----------------------------------------------------------------------------
 
  85 int PING::ParseSettings()
 
  87 int ret = pingSettings.ParseSettings(settings);
 
  89     errorStr = pingSettings.GetStrError();
 
  92 //-----------------------------------------------------------------------------
 
  97 users->AddNotifierUserAdd(&onAddUserNotifier);
 
  98 users->AddNotifierUserDel(&onDelUserNotifier);
 
 102 pinger.SetDelayTime(pingSettings.GetPingDelay());
 
 105 if (pthread_create(&thread, NULL, Run, this))
 
 107     errorStr = "Cannot start thread.";
 
 108     logger("Cannot create thread.");
 
 109     printfd(__FILE__, "Cannot start thread\n");
 
 115 //-----------------------------------------------------------------------------
 
 118 STG_LOCKER lock(&mutex);
 
 125 //5 seconds to thread stops itself
 
 126 struct timespec ts = {0, 200000000};
 
 127 for (int i = 0; i < 25; i++)
 
 132     nanosleep(&ts, NULL);
 
 135 users->DelNotifierUserAdd(&onAddUserNotifier);
 
 136 users->DelNotifierUserDel(&onDelUserNotifier);
 
 138 std::list<USER_PTR>::iterator users_iter;
 
 139 users_iter = usersList.begin();
 
 140 while (users_iter != usersList.end())
 
 142     UnSetUserNotifiers(*users_iter);
 
 151 //-----------------------------------------------------------------------------
 
 152 bool PING::IsRunning()
 
 156 //-----------------------------------------------------------------------------
 
 157 void * PING::Run(void * d)
 
 160 sigfillset(&signalSet);
 
 161 pthread_sigmask(SIG_BLOCK, &signalSet, NULL);
 
 163 PING * ping = static_cast<PING *>(d);
 
 164 ping->isRunning = true;
 
 166 long delay = (10000000 * ping->pingSettings.GetPingDelay()) / 3 + 50000000;
 
 168 while (ping->nonstop)
 
 170     std::list<USER_PTR>::iterator iter = ping->usersList.begin();
 
 172         STG_LOCKER lock(&ping->mutex);
 
 173         while (iter != ping->usersList.end())
 
 175             if ((*iter)->GetProperty().ips.ConstData().OnlyOneIP())
 
 177                 uint32_t ip = (*iter)->GetProperty().ips.ConstData()[0].ip;
 
 179                 if (ping->pinger.GetIPTime(ip, &t) == 0)
 
 182                         (*iter)->UpdatePingTime(t);
 
 187                 uint32_t ip = (*iter)->GetCurrIP();
 
 191                     if (ping->pinger.GetIPTime(ip, &t) == 0)
 
 194                             (*iter)->UpdatePingTime(t);
 
 201     struct timespec ts = {delay / 1000000000, delay % 1000000000};
 
 202     for (int i = 0; i < 100; i++)
 
 206             nanosleep(&ts, NULL);
 
 211 ping->isRunning = false;
 
 214 //-----------------------------------------------------------------------------
 
 215 void PING::SetUserNotifiers(USER_PTR u)
 
 217 CHG_CURRIP_NOTIFIER_PING ChgCurrIPNotifier(*this, u);
 
 218 CHG_IPS_NOTIFIER_PING ChgIPNotifier(*this, u);
 
 220 ChgCurrIPNotifierList.push_front(ChgCurrIPNotifier);
 
 221 ChgIPNotifierList.push_front(ChgIPNotifier);
 
 223 u->AddCurrIPAfterNotifier(&(*ChgCurrIPNotifierList.begin()));
 
 224 u->GetProperty().ips.AddAfterNotifier(&(*ChgIPNotifierList.begin()));
 
 226 //-----------------------------------------------------------------------------
 
 227 void PING::UnSetUserNotifiers(USER_PTR u)
 
 230 IS_CONTAINS_USER<CHG_CURRIP_NOTIFIER_PING> IsContainsUserCurrIP(u);
 
 231 IS_CONTAINS_USER<CHG_IPS_NOTIFIER_PING> IsContainsUserIP(u);
 
 233 std::list<CHG_CURRIP_NOTIFIER_PING>::iterator currIPter;
 
 234 std::list<CHG_IPS_NOTIFIER_PING>::iterator IPIter;
 
 236 currIPter = find_if(ChgCurrIPNotifierList.begin(),
 
 237                     ChgCurrIPNotifierList.end(),
 
 238                     IsContainsUserCurrIP);
 
 240 if (currIPter != ChgCurrIPNotifierList.end())
 
 242     currIPter->GetUser()->DelCurrIPAfterNotifier(&(*currIPter));
 
 243     ChgCurrIPNotifierList.erase(currIPter);
 
 245 // ---         CurrIP end          ---
 
 248 IPIter = find_if(ChgIPNotifierList.begin(),
 
 249                  ChgIPNotifierList.end(),
 
 252 if (IPIter != ChgIPNotifierList.end())
 
 254     IPIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*IPIter));
 
 255     ChgIPNotifierList.erase(IPIter);
 
 259 //-----------------------------------------------------------------------------
 
 260 void PING::GetUsers()
 
 262 STG_LOCKER lock(&mutex);
 
 265 int h = users->OpenSearch();
 
 266 assert(h && "USERS::OpenSearch is always correct");
 
 268 while (users->SearchNext(h, &u) == 0)
 
 270     usersList.push_back(u);
 
 272     if (u->GetProperty().ips.ConstData().OnlyOneIP())
 
 274         pinger.AddIP(u->GetProperty().ips.ConstData()[0].ip);
 
 278         uint32_t ip = u->GetCurrIP();
 
 286 users->CloseSearch(h);
 
 288 //-----------------------------------------------------------------------------
 
 289 void PING::AddUser(USER_PTR u)
 
 291 STG_LOCKER lock(&mutex);
 
 294 usersList.push_back(u);
 
 296 //-----------------------------------------------------------------------------
 
 297 void PING::DelUser(USER_PTR u)
 
 299 STG_LOCKER lock(&mutex);
 
 301 UnSetUserNotifiers(u);
 
 303 std::list<USER_PTR>::iterator users_iter;
 
 304 users_iter = usersList.begin();
 
 306 while (users_iter != usersList.end())
 
 308     if (u == *users_iter)
 
 310         usersList.erase(users_iter);
 
 316 //-----------------------------------------------------------------------------
 
 317 void CHG_CURRIP_NOTIFIER_PING::Notify(const uint32_t & oldIP, const uint32_t & newIP)
 
 319 ping.pinger.DelIP(oldIP);
 
 322     ping.pinger.AddIP(newIP);
 
 325 //-----------------------------------------------------------------------------
 
 326 void CHG_IPS_NOTIFIER_PING::Notify(const USER_IPS & oldIPS, const USER_IPS & newIPS)
 
 328 if (oldIPS.OnlyOneIP())
 
 330     ping.pinger.DelIP(oldIPS[0].ip);
 
 333 if (newIPS.OnlyOneIP())
 
 335     ping.pinger.AddIP(newIPS[0].ip);
 
 338 //-----------------------------------------------------------------------------
 
 339 void ADD_USER_NONIFIER_PING::Notify(const USER_PTR & user)
 
 343 //-----------------------------------------------------------------------------
 
 344 void DEL_USER_NONIFIER_PING::Notify(const USER_PTR & user)
 
 348 //-----------------------------------------------------------------------------