Important Update:

- FunctionCore/SelectableCore:
  - Replace Channel->State with InState
  - Add Channel-OutState (ready for output) updated by ChannelStateEvent()
- TimingCore:
  - Add GetUpCounter() for calculating uptime string (in seconds)
This commit is contained in:
Charl Wentzel
2019-06-23 10:38:27 +02:00
parent 207caa696d
commit e9b50f1af9
4 changed files with 80 additions and 28 deletions

View File

@@ -186,11 +186,13 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const EChannelSt
(*Channel)->Name = strdup( ChannelName ); (*Channel)->Name = strdup( ChannelName );
(*Channel)->Ref = (char*)malloc( strlen(Name)+strlen(ChannelName)+2 ); (*Channel)->Ref = (char*)malloc( strlen(Name)+strlen(ChannelName)+2 );
sprintf( (*Channel)->Ref, "%s/%s", Name, ChannelName ); sprintf( (*Channel)->Ref, "%s/%s", Name, ChannelName );
(*Channel)->State = State; (*Channel)->InState = State;
(*Channel)->OutState = CH_off;
// Log Event // Log Event
if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Channel '%s' - Created, Ref:'%s', State:%s", if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Channel '%s' - Created, Ref:'%s', In:%s, Out:%s",
ProcessName, Name, ChannelName, (*Channel)->Ref, ChannelStateName[(*Channel)->State] ); ProcessName, Name, ChannelName, (*Channel)->Ref,
ChannelStateName[(*Channel)->InState], ChannelStateName[(*Channel)->OutState]);
} }
return *Channel; return *Channel;
} }
@@ -198,7 +200,7 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const EChannelSt
bool CFunctionCore::SetChannelState( TChannel * Channel, const EChannelState State ) bool CFunctionCore::SetChannelState( TChannel * Channel, const EChannelState State )
{ {
EChannelState OldState = Channel->State; EChannelState OldState = Channel->InState;
TChannelLink * LinkChannel; TChannelLink * LinkChannel;
// Validate // Validate
@@ -206,8 +208,8 @@ bool CFunctionCore::SetChannelState( TChannel * Channel, const EChannelState Sta
return false; return false;
// Update state // Update state
Channel->State = State; Channel->InState = State;
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Channel '%s' - State:%s", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Channel '%s' - In:%s",
ProcessName, Name, Channel->Name, ChannelStateName[State] ); ProcessName, Name, Channel->Name, ChannelStateName[State] );
// Update linked channels // Update linked channels
@@ -223,16 +225,34 @@ bool CFunctionCore::SetChannelState( TChannel * Channel, const EChannelState Sta
bool CFunctionCore::ChannelStateEvent( TChannel * Channel, const char * SourceRef, const EChannelState OldState, const EChannelState NewState ) bool CFunctionCore::ChannelStateEvent( TChannel * Channel, const char * SourceRef, const EChannelState OldState, const EChannelState NewState )
{ {
TChannelLink * UpdateChannel;
TChannelLink * LinkChannel; TChannelLink * LinkChannel;
EChannelState OutState;
// Validate
if (!Channel) if (!Channel)
return false; return false;
if (!(LinkChannel = GetLinkChannel( Channel, SourceRef ))) if (!(UpdateChannel = GetLinkChannel( Channel, SourceRef )))
return false; return false;
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Link Channel '%s'-->'%s' - State:%s", if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Link Channel '%s'-->'%s' - In:%s",
ProcessName, Name, Channel->Name, LinkChannel->Channel->Ref, ChannelStateName[NewState] ); ProcessName, Name, Channel->Name, UpdateChannel->Channel->Ref, ChannelStateName[NewState] );
// Check if channel Out state changed
OutState = CH_off;
LinkChannel = Channel->FirstLink;
while (LinkChannel) {
if (LinkChannel->Channel->InState > OutState)
OutState = LinkChannel->Channel->InState;
LinkChannel = LinkChannel->Next;
}
if (OutState != Channel->OutState) {
// Update output state
Channel->OutState = OutState;
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Channel '%s' - Out:%s",
ProcessName, Name, Channel->Name, ChannelStateName[OutState] );
}
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -240,22 +260,20 @@ bool CFunctionCore::ChannelStateEvent( TChannel * Channel, const char * SourceRe
EChannelState CFunctionCore::ChannelOutState( TChannel * Channel, const char * TargetRef ) EChannelState CFunctionCore::ChannelOutState( TChannel * Channel, const char * TargetRef )
{ {
TChannelLink * LinkChannel = NULL; TChannelLink * LinkChannel = NULL;
EChannelState State = CH_off;
// Validate // Validate
if (!Channel) if (!Channel) {
return State; return CH_off;
// Check if any linked channels are ready
LinkChannel = Channel->FirstLink;
while (LinkChannel) {
if (!TargetRef || !*TargetRef || !strcasecmp( TargetRef, LinkChannel->Channel->Ref )) {
if (LinkChannel->Channel->State > State)
State = LinkChannel->Channel->State;
} }
LinkChannel = LinkChannel->Next; else if (!TargetRef || !*TargetRef) {
return Channel->OutState;
}
else if ((LinkChannel = GetLinkChannel( Channel, TargetRef ))) {
return LinkChannel->Channel->InState;
}
else {
return CH_off;
} }
return State;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -373,9 +391,9 @@ int CFunctionCore::Input( const char * ChannelName, const char * SourceRef, cons
ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName );
return 0; return 0;
} }
else if (!Channel->State) { else if (!Channel->InState) {
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel %s", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel %s",
ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName, ChannelStateName[Channel->State] ); ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName, ChannelStateName[Channel->InState] );
return 0; return 0;
} }

View File

@@ -38,7 +38,8 @@ struct SChannel
TChannelLink * FirstLink = NULL; // List of channels linked for input/output TChannelLink * FirstLink = NULL; // List of channels linked for input/output
EChannelState State = CH_off; // Channel ready to receive input EChannelState InState = CH_off; // Channel ready to receive input (determined by parent function)
EChannelState OutState = CH_off; // Channel ready to send output (determined by InStates of linked channels)
TChannel * Next = NULL; TChannel * Next = NULL;
}; };

View File

@@ -190,7 +190,7 @@ bool CSelectableBare::HandleState( THandle * Handle, EConnectState State )
ChannelState = CH_ready; ChannelState = CH_ready;
else else
ChannelState = CH_off; ChannelState = CH_off;
if (Handle->Channel->State != ChannelState) if (Handle->Channel->InState != ChannelState)
SetChannelState( Handle->Channel, ChannelState ); SetChannelState( Handle->Channel, ChannelState );
} }
return true; return true;
@@ -677,7 +677,7 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co
ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName );
return 0; return 0;
} }
else if (Channel->State != CH_ready) { else if (Channel->InState != CH_ready) {
// Channel disabled // Channel disabled
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel not Ready", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel not Ready",
ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName );

View File

@@ -12,6 +12,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <limits.h> #include <limits.h>
#include <stdio.h>
// redA Libraries // redA Libraries
/* none */ /* none */
@@ -23,19 +24,22 @@ inline void SetInterval( timeval * Time, long MilliSeconds ) {
Time->tv_sec = MilliSeconds / 1000; Time->tv_sec = MilliSeconds / 1000;
Time->tv_usec = (MilliSeconds % 1000) * 1000; Time->tv_usec = (MilliSeconds % 1000) * 1000;
}; };
//---------------------------------------------------------------------------
// Mark start time // Mark start time
inline void SetStartTime( timeval *StartTime ) { inline void SetStartTime( timeval *StartTime ) {
gettimeofday( StartTime, NULL ); gettimeofday( StartTime, NULL );
}; };
//---------------------------------------------------------------------------
// Clear timer // Clear timer
inline void ClearStartTime( timeval * StartTime ) { inline void ClearStartTime( timeval * StartTime ) {
StartTime->tv_sec = 0; StartTime->tv_sec = 0;
StartTime->tv_usec = 0; StartTime->tv_usec = 0;
}; };
//---------------------------------------------------------------------------
// Time passed since start time // Milli-seconds passed since start time
inline long TimePassed( timeval StartTime ) { inline long TimePassed( timeval StartTime ) {
timeval CurrTime; timeval CurrTime;
long Duration; long Duration;
@@ -47,17 +51,46 @@ inline long TimePassed( timeval StartTime ) {
Duration = LONG_MAX; Duration = LONG_MAX;
return Duration; return Duration;
}; };
//---------------------------------------------------------------------------
// Time remaining from Start time to given time out // Time remaining from Start time to given time out
inline long TimeLeft( timeval StartTime, long MilliSeconds ) { inline long TimeLeft( timeval StartTime, long MilliSeconds ) {
return (MilliSeconds - TimePassed(StartTime)); return (MilliSeconds - TimePassed(StartTime));
}; };
//---------------------------------------------------------------------------
// Has give time expired after start time // Has give time expired after start time
inline bool Timeout( timeval StartTime, long MilliSeconds ) { inline bool Timeout( timeval StartTime, long MilliSeconds ) {
return ((TimePassed(StartTime) > MilliSeconds)? true : false); return ((TimePassed(StartTime) > MilliSeconds)? true : false);
}; };
//---------------------------------------------------------------------------
// Get Upcount in seconds from start time (with string output)
inline long GetUpCounter( timeval StartTime, char * TextStr ) {
long Duration;
int Days, Hours, Minutes, Seconds;
timeval CurrTime;
// Get duration
gettimeofday( &CurrTime, NULL );
Duration = (!StartTime.tv_sec)? 0 : (CurrTime.tv_sec - StartTime.tv_sec); // Handle zero start
// Create string
if (TextStr) {
// Get Parts
Minutes = Duration / 60;
Hours = Minutes / 60;
Days = Hours / 24;
// Get modulus
Seconds = Duration % 60;
Minutes = Minutes % 60;
Hours = Hours % 24;
sprintf( TextStr, "%dd %02d:%02d:%02d", Days, Hours, Minutes, Seconds );
}
return Duration;
}
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#endif /* REDACORE_TIMINGCORE_H_ */ #endif /* REDACORE_TIMINGCORE_H_ */