diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e1eec3..dacedc4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,3 @@ PROJECT(lib_redAcore) -ADD_LIBRARY(redAcore TimingCore.cpp DateTimeCore.cpp LogCore.cpp SignalCore.cpp DataTreeCore.cpp JSONparseCore.cpp BufferCore.cpp LiteProtocolCore.cpp FunctionCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp) +ADD_LIBRARY(redAcore TimingCore.cpp DateTimeCore.cpp LogCore.cpp SignalCore.cpp DataTreeCore.cpp JSONparseCore.cpp BufferCore.cpp LiteProtocolCore.cpp FunctionCore.cpp WatchdogCore.cpp DeviceCore.cpp FileCore.cpp SelectCore.cpp SelectableCore.cpp) diff --git a/SignalCore.cpp b/SignalCore.cpp index 6cac983..6a08755 100644 --- a/SignalCore.cpp +++ b/SignalCore.cpp @@ -83,22 +83,36 @@ void SignalTerminate( int sig ) Terminate = true; TermCount++; - // Check for Terminate Limit - // Show signal received if not already terminated - std::cerr << "\r\n" << ProcessName << ": ***********************************\n"; + std::cerr << "\r\n" << ProcessName << ": ***********************************" << std::endl; // Create Log Entry - if (Log) Log->Message( dlNone, dlNone, "%s: ** %s signal received [%d] **", ProcessName, SigName, TermCount ); - if (TermCount < MaxTermCount) { - if (Log) Log->Message( dlNone, dlNone, "%s: ** Terminating normally... **", ProcessName ); + if (Log) + { + Log->Message( dlNone, dlNone, "%s: ** %s signal received [%d] **", ProcessName, SigName, TermCount ); + // Check for Terminate Limit + if (TermCount < MaxTermCount) { + Log->Message( dlNone, dlNone, "%s: ** Terminating normally... **", ProcessName ); + } else { + Log->Message( dlNone, dlNone, "%s: ** Terminating immediately! **", ProcessName ); + } } - else { - if (Log) Log->Message( dlNone, dlNone, "%s: ** Terminating immediately! **", ProcessName ); + else + { + std::cerr << ProcessName << ": ** " << SigName << " signal received [" << TermCount << "] **" << std::endl; + // Check for Terminate Limit + if (TermCount < MaxTermCount) { + std::cerr << ProcessName << ": ** Terminating normally... **" << std::endl; + } else { + std::cerr << ProcessName << ": ** Terminating immediately! **" << std::endl; + } } - std::cerr << ProcessName << ": ***********************************\n\n"; - if (TermCount >= MaxTermCount) + std::cerr << ProcessName << ": ***********************************" << std::endl << std::endl; + + // Check for Terminate Limit + if (TermCount >= MaxTermCount) { exit( sig ); + } return; }//--------------------------------------------------------------------------- @@ -120,13 +134,19 @@ void SignalAbort( int sig ) } // Show signal received - std::cerr << "\n" << ProcessName << ": ********************************\n"; + std::cerr << "\n" << ProcessName << ": ********************************" << std::endl; // Create Log Entry - but don't post - if (Log) Log->Message( dlNone, dlNone, "%s: ** %s signal received **", ProcessName, SigName ); - if (Log) Log->Message( dlNone, dlNone, "%s: ** Terminating immediately! **", ProcessName ); + if (Log) { + Log->Message( dlNone, dlNone, "%s: ** %s signal received **", ProcessName, SigName ); + Log->Message( dlNone, dlNone, "%s: ** Terminating immediately! **", ProcessName ); + } + else { + std::cerr << ProcessName << ": ** " << SigName << " signal received **" << std::endl; + std::cerr << ProcessName << ": ** Terminating immediately! **" << std::endl; + } - std::cerr << ProcessName << ": ********************************\n\n"; + std::cerr << ProcessName << ": ********************************" << std::endl << std::endl; // Terminate with signal exit( sig ); diff --git a/WatchdogCore.cpp b/WatchdogCore.cpp new file mode 100644 index 0000000..2757ed5 --- /dev/null +++ b/WatchdogCore.cpp @@ -0,0 +1,67 @@ +/* + * WatchdogCore.cpp + * + * Created on: July 2017 + * Author: wentzelc + */ + +// redA Libraries +#include "WatchdogCore.h" +#include "TimingCore.h" + +// Standard C/C++ Libraries +#include +#include +#include +#include +#include + +//--------------------------------------------------------------------------- + +// Global vars +extern char * ProcessName; +char LogStr[1000]; // Temporary var to create log messages, globally available to save on memory operations + +//--------------------------------------------------------------------------- + +CWatchdogCore::CWatchdogCore( const char * WatchdogName, const int pInterval, CLogCore * pLog, EDebugLevel pDebugLevel ) : + CFunctionCore( Name, pLog, pDebugLevel, OUT_NORMAL ) +{ + // Create protocol + Protocol = new CLiteProtocol( 50, '\x01', '\x02', '\x00' ); + Protocol->AppendParam( Name, strlen(Name) ); + Protocol->AppendParam( ProcessName, strlen(ProcessName) ); + Protocol->AppendParam( "ping", 4 ); + + // Start timer + PingTimeout = 500; + SetStartTime( &PingTimer ); + + // Create Channel + Ping = AddChannel( "Ping" ); +} +//--------------------------------------------------------------------------- + +CWatchdogCore::~CWatchdogCore() +{ + // Destroy Protocol + if (Protocol) + delete Protocol; +} +//--------------------------------------------------------------------------- + +bool CWatchdogCore::Process() +{ + if (Timeout( PingTimer, PingTimeout )) + { + // Send command + Output( Ping, Protocol->GetCommandStr(), Protocol->GetCommandLen() ); + + // Reset timer + SetStartTime( &PingTimer ); + } + + return false; +} +//--------------------------------------------------------------------------- + diff --git a/WatchdogCore.h b/WatchdogCore.h new file mode 100644 index 0000000..a4ddbb8 --- /dev/null +++ b/WatchdogCore.h @@ -0,0 +1,41 @@ +/* + * WatchdogCore.h + * + * Created on: July 2017 + * Author: wentzelc + */ + +#ifndef REDACORE_WATCHDOGCORE_H_ +#define REDACORE_WATCHDOGCORE_H_ + +// redA Libraries +#include "FunctionCore.h" +#include "LiteProtocolCore.h" + +// Standard C/C++ Libraries +/*none*/ + +//--------------------------------------------------------------------------- + +class CWatchdogCore : public CFunctionCore +{ +private: + // Command + CLiteProtocol * Protocol; + + // Timing + timeval PingTimer; + int PingTimeout; + + // Channel + TChannel * Ping; + +public: + CWatchdogCore( const char * WatchdogName, const int pInterval, CLogCore * pLog, EDebugLevel pDebugLevel ); + virtual ~CWatchdogCore(); + + virtual bool Process(); +}; +//--------------------------------------------------------------------------- + +#endif /* REDACORE_WATCHDOGCORE_H_ */