From 889df6c7ded98d68571bbf555653e8a1bb57f98e Mon Sep 17 00:00:00 2001 From: Charl Wentzel Date: Sat, 9 Mar 2019 20:41:25 +0200 Subject: [PATCH] Important Update: - DeviceCore: - Added PollCycle variable (not used here) - WatchdogCore: - Return "true" on in Process() - SelectableCore: - Minor update (code compacted) - Application/Function: - Added CApplication as friend of CFunction - Added param WaitToTerminate on Function - Application Run() only exists if all (WaitToTerminate) functions has terminated cleanly. --- ApplicationCore.cpp | 22 +++++++++++++--------- DeviceCore.cpp | 1 + DeviceCore.h | 5 +++-- FunctionCore.h | 3 +++ SelectableCore.cpp | 18 ++++++------------ WatchdogCore.cpp | 2 +- 6 files changed, 27 insertions(+), 24 deletions(-) diff --git a/ApplicationCore.cpp b/ApplicationCore.cpp index e441d53..3f6a1b4 100644 --- a/ApplicationCore.cpp +++ b/ApplicationCore.cpp @@ -17,6 +17,7 @@ //--------------------------------------------------------------------------- // Global Vars +extern bool Terminate; extern char * ProcessName; //--------------------------------------------------------------------------- @@ -405,7 +406,8 @@ CFunctionCore * CApplication::GetFunction( const char * Name ) bool CApplication::Run( bool TerminateOnError ) { - bool AllGood = true; + bool CleanTerminate = true; + bool ProcessTerminate = false; TFunctionItem * FunctionItem; // Check for FD Events/States @@ -416,15 +418,17 @@ bool CApplication::Run( bool TerminateOnError ) // Process Functions for (FunctionItem = FirstFunction; FunctionItem; FunctionItem = FunctionItem->Next ) { - if (!FunctionItem->Function->Process()) - { - if (TerminateOnError) - return false; - else { - AllGood = false; - } + ProcessTerminate = !FunctionItem->Function->Process(); + if (TerminateOnError) { + if (ProcessTerminate) + Terminate = true; + if (FunctionItem->Function->WaitToTerminate && !ProcessTerminate) + CleanTerminate = false; } } - return AllGood; + if (Terminate && TerminateOnError && CleanTerminate) + return false; + else + return true; } //--------------------------------------------------------------------------- diff --git a/DeviceCore.cpp b/DeviceCore.cpp index 3515a6f..5beb5f9 100644 --- a/DeviceCore.cpp +++ b/DeviceCore.cpp @@ -30,6 +30,7 @@ extern CApplication * Application; CDeviceCore::CDeviceCore( const char * pName, const char * pType ) : CFunctionCore( pName, pType ) { // Polling + PollCycle = 0; PollStep = 0; PollInterval = 250; SetStartTime( &PollWait ); diff --git a/DeviceCore.h b/DeviceCore.h index 2cd7347..9f43f95 100644 --- a/DeviceCore.h +++ b/DeviceCore.h @@ -31,8 +31,8 @@ const char DataTypeName[][20] = { "None", "Unsigned16", "Signed16", "Unsigned32_ // Structure prototypes typedef struct SDevice TDevice; typedef struct SDeviceParam TDeviceParam; -typedef struct SDeviceParamGroup TDeviceParamGroup; -typedef struct SDeviceParamItem TDeviceParamItem; +typedef struct SDeviceParamGroup TDeviceParamGroup; // Parameter group, e.g. used with polling parameters +typedef struct SDeviceParamItem TDeviceParamItem; // Placeholder parameters in Parameter groups //--------------------------------------------------------------------------- @@ -138,6 +138,7 @@ protected: TChannel * EventChannel = NULL; // Poll + int PollCycle; // Device Polling state, e.g. Init, Connect, Run, Disonnect, Shutdown int PollStep; // Position in polling sequence timeval PollWait; // Time at which last poll was done long PollInterval; // Minimum delay between polls diff --git a/FunctionCore.h b/FunctionCore.h index 37c75f0..b04266a 100644 --- a/FunctionCore.h +++ b/FunctionCore.h @@ -60,6 +60,7 @@ protected: // Function Definition const char * Type = NULL; char * Name = NULL; + bool WaitToTerminate = false; // Channels TChannel * FirstChannel = NULL; @@ -138,6 +139,8 @@ public: virtual bool LinkInputChannel( const char * ChannelName, const char * OutFunctionName, const char * OutChannelName, bool Bidirectional ); virtual bool LinkOutputChannel( const char * ChannelName, const char * InFunctionName, const char * InChannelName, bool Bidirectional ); virtual bool Process() = 0; + + friend class CApplication; }; //--------------------------------------------------------------------------- diff --git a/SelectableCore.cpp b/SelectableCore.cpp index f7dd274..2214ca5 100644 --- a/SelectableCore.cpp +++ b/SelectableCore.cpp @@ -2564,20 +2564,18 @@ bool CSelectableCore::Process() { THandle * Handle = NULL; - // Check all handles + // Process all Handles Handle = FirstHandle; while (Handle) { // Auto manage handles - if ((Handle->State == csOpenRequest)) - { + if ((Handle->State == csOpenRequest)) { // Resolve then open socket if (Timeout( Handle->LastAction, Handle->ResolveDelay )) { Open( Handle, false ); } } - else if (((Handle->State != csOpen) && Handle->AutoManage && Handle->Persistent) ) - { + else if (((Handle->State != csOpen) && Handle->AutoManage && Handle->Persistent) ) { // Try to re-open port after delay if (Timeout( Handle->LastAction, Handle->ReopenDelay )) { Open( Handle, false ); @@ -2585,11 +2583,9 @@ bool CSelectableCore::Process() } // Check Input buffers - if (Handle->InBuffer && (Handle->InBuffer->Len() > 0)) - { + if (Handle->InBuffer && (Handle->InBuffer->Len() > 0)) { // Check duration since last PortIn - if (Timeout( Handle->InStart, Handle->InTimeout )) - { + if (Timeout( Handle->InStart, Handle->InTimeout )) { // Process Input ProcessInputBuffer( Handle, true ); @@ -2599,14 +2595,12 @@ bool CSelectableCore::Process() } // Check for auto close (but not on servers) - if ((Handle->State == csOpen) && (Handle->Type != ctTCPserver) && (Handle->Type != ctUNIXserver) && Handle->AutoManage && !Handle->Persistent) - { + if ((Handle->State == csOpen) && Handle->AutoManage && !Handle->Persistent && (Handle->Type != ctTCPserver) && (Handle->Type != ctUNIXserver)) { // Close port after timeout if (Timeout( Handle->LastAction, Handle->CloseTimeout )) { Close( Handle, true ); } } - Handle = Handle->Next; } return true; diff --git a/WatchdogCore.cpp b/WatchdogCore.cpp index af11199..cebd535 100644 --- a/WatchdogCore.cpp +++ b/WatchdogCore.cpp @@ -88,7 +88,7 @@ bool CWatchdogCore::Process() SetStartTime( &PingTimer ); } - return false; + return true; } //---------------------------------------------------------------------------