diff --git a/FunctionCore.cpp b/FunctionCore.cpp index f56f82f..1b0de8a 100644 --- a/FunctionCore.cpp +++ b/FunctionCore.cpp @@ -186,11 +186,13 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const EChannelSt (*Channel)->Name = strdup( ChannelName ); (*Channel)->Ref = (char*)malloc( strlen(Name)+strlen(ChannelName)+2 ); sprintf( (*Channel)->Ref, "%s/%s", Name, ChannelName ); - (*Channel)->State = State; + (*Channel)->InState = State; + (*Channel)->OutState = CH_off; // Log Event - if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Channel '%s' - Created, Ref:'%s', State:%s", - ProcessName, Name, ChannelName, (*Channel)->Ref, ChannelStateName[(*Channel)->State] ); + if (Log) Log->Message( LogLevel, dlLow, "%s/%s: Channel '%s' - Created, Ref:'%s', In:%s, Out:%s", + ProcessName, Name, ChannelName, (*Channel)->Ref, + ChannelStateName[(*Channel)->InState], ChannelStateName[(*Channel)->OutState]); } return *Channel; } @@ -198,7 +200,7 @@ TChannel * CFunctionCore::AddChannel( const char * ChannelName, const EChannelSt bool CFunctionCore::SetChannelState( TChannel * Channel, const EChannelState State ) { - EChannelState OldState = Channel->State; + EChannelState OldState = Channel->InState; TChannelLink * LinkChannel; // Validate @@ -206,8 +208,8 @@ bool CFunctionCore::SetChannelState( TChannel * Channel, const EChannelState Sta return false; // Update state - Channel->State = State; - if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Channel '%s' - State:%s", + Channel->InState = State; + if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Channel '%s' - In:%s", ProcessName, Name, Channel->Name, ChannelStateName[State] ); // 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 ) { + TChannelLink * UpdateChannel; TChannelLink * LinkChannel; + EChannelState OutState; + // Validate if (!Channel) return false; - if (!(LinkChannel = GetLinkChannel( Channel, SourceRef ))) + if (!(UpdateChannel = GetLinkChannel( Channel, SourceRef ))) return false; - if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Link Channel '%s'-->'%s' - State:%s", - ProcessName, Name, Channel->Name, LinkChannel->Channel->Ref, ChannelStateName[NewState] ); + if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Update Link Channel '%s'-->'%s' - In:%s", + 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; } //--------------------------------------------------------------------------- @@ -240,22 +260,20 @@ bool CFunctionCore::ChannelStateEvent( TChannel * Channel, const char * SourceRe EChannelState CFunctionCore::ChannelOutState( TChannel * Channel, const char * TargetRef ) { TChannelLink * LinkChannel = NULL; - EChannelState State = CH_off; // Validate - if (!Channel) - return State; - - // 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; + if (!Channel) { + return CH_off; + } + 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 ); 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", - ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName, ChannelStateName[Channel->State] ); + ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName, ChannelStateName[Channel->InState] ); return 0; } diff --git a/FunctionCore.h b/FunctionCore.h index 6f77962..807414b 100644 --- a/FunctionCore.h +++ b/FunctionCore.h @@ -38,7 +38,8 @@ struct SChannel 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; }; diff --git a/SelectableBare.cpp b/SelectableBare.cpp index 265e691..b0f2134 100644 --- a/SelectableBare.cpp +++ b/SelectableBare.cpp @@ -190,7 +190,7 @@ bool CSelectableBare::HandleState( THandle * Handle, EConnectState State ) ChannelState = CH_ready; else ChannelState = CH_off; - if (Handle->Channel->State != ChannelState) + if (Handle->Channel->InState != ChannelState) SetChannelState( Handle->Channel, ChannelState ); } return true; @@ -677,7 +677,7 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); return 0; } - else if (Channel->State != CH_ready) { + else if (Channel->InState != CH_ready) { // Channel disabled if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel not Ready", ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); diff --git a/TimingCore.h b/TimingCore.h index 8718f6e..ce97f65 100644 --- a/TimingCore.h +++ b/TimingCore.h @@ -12,6 +12,7 @@ #include #include #include +#include // redA Libraries /* none */ @@ -23,19 +24,22 @@ inline void SetInterval( timeval * Time, long MilliSeconds ) { Time->tv_sec = MilliSeconds / 1000; Time->tv_usec = (MilliSeconds % 1000) * 1000; }; +//--------------------------------------------------------------------------- // Mark start time inline void SetStartTime( timeval *StartTime ) { gettimeofday( StartTime, NULL ); }; +//--------------------------------------------------------------------------- // Clear timer inline void ClearStartTime( timeval * StartTime ) { StartTime->tv_sec = 0; StartTime->tv_usec = 0; }; +//--------------------------------------------------------------------------- -// Time passed since start time +// Milli-seconds passed since start time inline long TimePassed( timeval StartTime ) { timeval CurrTime; long Duration; @@ -47,17 +51,46 @@ inline long TimePassed( timeval StartTime ) { Duration = LONG_MAX; return Duration; }; +//--------------------------------------------------------------------------- // Time remaining from Start time to given time out inline long TimeLeft( timeval StartTime, long MilliSeconds ) { return (MilliSeconds - TimePassed(StartTime)); }; +//--------------------------------------------------------------------------- // Has give time expired after start time inline bool Timeout( timeval StartTime, long MilliSeconds ) { 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_ */