1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
//+=============================================================================
//
// file : StarterUtil.cpp
//
// description : C++ source for tools used by the Starter device server.
//
// project : TANGO Device Server
//
// $Author$
//
// Copyright (C) : 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,2015
// European Synchrotron Radiation Facility
// BP 220, Grenoble 38043
// FRANCE
//
// This file is part of Tango.
//
// Tango is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Tango is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Tango. If not, see <http://www.gnu.org/licenses/>.
//
// $Revision$
// $Date$
//
//-=============================================================================
#include <tango/tango.h>
#include "PingThread.h"
#include "Logging.h"
#include <utility>
#ifndef TIME_VAR
#ifndef _TG_WINDOWS_
# define TimeVal struct timeval
# define GetTime(t) gettimeofday(&t, nullptr);
# define Elapsed(before, after) \
1000.0*(after.tv_sec-before.tv_sec) + \
((double)after.tv_usec-before.tv_usec) / 1000
#else
# define TimeVal struct _timeb
# define GetTime(t) _ftime(&t);
# define Elapsed(before, after) \
1000*(after.time - before.time) + (after.millitm - before.millitm)
#endif /* _TG_WINDOWS_ */
#endif /* TIME_VAR */
namespace Starter_ns {
//+----------------------------------------------------------------------------
/**
* Constructor
*/
//+----------------------------------------------------------------------------
PingThreadData::PingThreadData(std::string sname) {
servname = std::move(sname);
stop_thread = false;
last_write_time = time(nullptr);
state = Tango::ON;
nbInstances = 0;
}
//+----------------------------------------------------------------------------
/**
* Get the server name
*/
//+----------------------------------------------------------------------------
std::string PingThreadData::get_server_name() {
omni_mutex_lock sync(*this);
return servname;
}
//+----------------------------------------------------------------------------
/**
* command to stop thread
*/
//+----------------------------------------------------------------------------
void PingThreadData::set_stop_thread() {
omni_mutex_lock sync(*this);
stop_thread = true;
signal();
}
//+----------------------------------------------------------------------------
/**
* get stop thread status
*/
//+----------------------------------------------------------------------------
bool PingThreadData::get_stop_thread() {
omni_mutex_lock sync(*this);
return stop_thread;
}
//+----------------------------------------------------------------------------
/**
* Return the elapsed time from last write.
*/
//+----------------------------------------------------------------------------
time_t PingThreadData::get_last_write_time() {
omni_mutex_lock sync(*this);
return last_write_time;
}
//+----------------------------------------------------------------------------
/**
* Set the ping result.
*/
//+----------------------------------------------------------------------------
void PingThreadData::set_state(Tango::DevState st) {
omni_mutex_lock sync(*this);
state = st;
last_write_time = time(nullptr);
}
//+----------------------------------------------------------------------------
/**
* Set the ping result.
*/
//+----------------------------------------------------------------------------
Tango::DevState PingThreadData::get_state() {
omni_mutex_lock sync(*this);
return state;
}
//+----------------------------------------------------------------------------
/**
* Force thread to update data.
*/
//+----------------------------------------------------------------------------
void PingThreadData::wake_up() {
omni_mutex_lock sync(*this);
signal();
}
//+----------------------------------------------------------------------------
//+----------------------------------------------------------------------------
void PingThreadData::setNbInstaces(int nb) {
omni_mutex_lock sync(*this);
nbInstances = nb;
}
//+----------------------------------------------------------------------------
//+----------------------------------------------------------------------------
int PingThreadData::getNbInstaces() {
omni_mutex_lock sync(*this);
return nbInstances;
}
//+----------------------------------------------------------------------------
//+----------------------------------------------------------------------------
//+----------------------------------------------------------------------------
/**
* Create a thread to ping server
*
* @param shared pointer on shared data between thread and DS.
* @param name The pinged server name
* @param timeout timeout value in milliseconds for ping command.
*/
//+----------------------------------------------------------------------------
PingThread::PingThread(PingThreadData *sd, const std::string& name, CheckProcessUtil *proc_util)
{
// Convert instance to lowercase
unsigned long idx = name.find('/');
std::string exeFile = name.substr(0, idx);
std::string instance = name.substr(++idx);
transform(instance.begin(), instance.end(),
instance.begin(), ::tolower);
servname = exeFile +'/'+instance;
shared = sd;
process_util = proc_util;
}
//+----------------------------------------------------------------------------
/**
* Execute the thread loop.
*/
//+----------------------------------------------------------------------------
void *PingThread::run_undetached(TANGO_UNUSED(void *ptr)) {
TimeVal before{}, after{};
Tango::DeviceProxy *pDevice = nullptr;
Tango::DevState state;
bool stop_thread = false;
std::string adm_devname("dserver/");
adm_devname += servname;
while (!stop_thread) {
GetTime(before);
// Make sure server running in one instance
int nbInstances = process_util->getNbServerInstances(servname);
shared->setNbInstaces(nbInstances);
// Check before if server running or failed
if (process_util->is_server_running(servname)) {
// try to build DeviceProxy
if (pDevice == nullptr) {
try {
pDevice = new Tango::DeviceProxy(adm_devname);
}
catch (Tango::DevFailed &e) {
Tango::Except::print_exception(e);
}
catch (...) {
TANGO_LOG_INFO << "============================================" << std::endl;
TANGO_LOG_INFO << " Exception catch !!!!!!" << std::endl;
TANGO_LOG_INFO << "============================================" << std::endl;
}
}
if (pDevice != nullptr) {
try {
pDevice->ping();
state = Tango::ON;
}
catch (Tango::DevFailed &) {
TANGO_LOG_INFO << servname << " is running but not responding !!!" << std::endl;
//Tango::Except::print_exception(e);
state = Tango::MOVING;
}
} else {
state = Tango::FAULT;
}
} else {
state = Tango::FAULT;
}
shared->set_state(state);
// Compute time to sleep
GetTime(after)
double dt = (double) Elapsed(before, after);
long time_to_sleep = 2000 - (int) dt;
if (time_to_sleep < 10)
time_to_sleep = 10;
// Check if thread must be stopped.
stop_thread = shared->get_stop_thread();
if (!stop_thread) {
// And wait for next ping
omni_mutex_lock sync(*shared);
shared->wait(time_to_sleep);
}
stop_thread = shared->get_stop_thread();
}
delete shared;
delete pDevice;
return nullptr;
}
//+----------------------------------------------------------------------------
//+----------------------------------------------------------------------------
} // namespace
|