Files
redAcore/FunctionCore.cpp
Charl Wentzel c01c8f5e9b Major Update:
- Implemented global var DebugLevel
- Update LogCore to check DebugLevel
- Added many log messages and standadised all log messages
- Further improved validation checks on all methods
- Updated SelectCore, only remove SelectHandle from list during Test()
- Close Handles in SelectableCore destructor
Bug fixes:
- Non-blocking Client Socket Connection now working correctly
- Remove FD from Select lists at the correct time
2016-05-26 15:03:13 +02:00

267 lines
7.1 KiB
C++

/*
* FunctionCore.cpp
*
* Created on: 18 May 2016
* Author: wentzelc
*/
// redA Libraries
#include "FunctionCore.h"
#include "TimingCore.h"
#include "LogCore.h"
// Standard C/C++ Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//---------------------------------------------------------------------------
extern char ProcessName[];
extern char LogStr[]; // Access to global temporary log messages string
//---------------------------------------------------------------------------
// Life cycle
CFunctionCore::CFunctionCore( const char * FunctionName )
{
// Set name
if (FunctionName) {
Name = (char*)malloc( strlen(FunctionName)+1 );
strcpy( Name, FunctionName );
}
else {
Name = NULL;
}
// IO Functions
FirstIO = NULL;
// Report status
sprintf( LogStr, "Function '%s' - Created", FunctionName );
LogMessage( ProcessName, dlLow, LogStr );
}
//---------------------------------------------------------------------------
CFunctionCore::~CFunctionCore()
{
TLocalIO * NextIO = NULL;
TLinkedIO * NextLinkedIO = NULL;
// Destroy IO
while (FirstIO)
{
// Destroy Parameters
if (FirstIO->Name) {
free( FirstIO->Name );
}
// Destroy Linked Inputs
while (FirstIO->FirstInput) {
if (FirstIO->FirstInput->IOName) {
free( FirstIO->FirstInput->IOName );
}
NextLinkedIO = FirstIO->FirstInput->Next;
free( FirstIO->FirstInput );
FirstIO->FirstInput = NextLinkedIO;
}
// Destroy Linked Outputs
while (FirstIO->FirstOutput) {
if (FirstIO->FirstOutput->IOName) {
free( FirstIO->FirstOutput->IOName );
}
NextLinkedIO = FirstIO->FirstOutput->Next;
free( FirstIO->FirstOutput );
FirstIO->FirstOutput = NextLinkedIO;
}
// Destroy Local IO
// Destroy Local IO
NextIO = FirstIO->Next;
free( FirstIO );
FirstIO = NextIO;
}
// Report status
sprintf( LogStr, "Function '%s' - Destroyed", Name );
LogMessage( ProcessName, dlLow, LogStr );
// Destroy Name
if (Name) {
free( Name );
}
}
//---------------------------------------------------------------------------
TLocalIO * CFunctionCore::AddLocalIO( const char * IOName )
{
TLocalIO ** LocalIO = NULL;
// Validate
if (!IOName) {
return NULL;
}
// Check if exists
LocalIO = &FirstIO;
while (*LocalIO && strcmp( IOName, (*LocalIO)->Name )) {
LocalIO = &((*LocalIO)->Next);
}
// Create if not exist
if (!*LocalIO) {
// Create
*LocalIO = (TLocalIO*)malloc( sizeof(TLocalIO) );
memset( *LocalIO, 0, sizeof(TLocalIO) );
// Set Name
(*LocalIO)->Name = (char*)malloc( strlen(IOName)+1 );
strcpy( (*LocalIO)->Name, IOName );
// Log Event
sprintf( LogStr, "Local IO '%s' - Created", IOName );
LogMessage( Name, dlMedium, LogStr );
}
return *LocalIO;
}
//---------------------------------------------------------------------------
// Automated Data Input/Output
bool CFunctionCore::AddInput( const char * IOName, CFunctionCore * OutFunction, const char * OutputName )
{
TLocalIO * LocalIO = NULL;
TLinkedIO ** LinkedIO = NULL;
// Get IO
if (!OutFunction || !OutFunction || !(LocalIO = GetLocalIO( IOName ))) {
return false;
}
// Check if linked IO exists
LinkedIO = &(LocalIO->FirstInput);
while (*LinkedIO && (((*LinkedIO)->Function != OutFunction) || strcmp( (*LinkedIO)->IOName, OutputName ) )) {
LinkedIO = &((*LinkedIO)->Next);
}
// Create if not found
if (!*LinkedIO)
{
// Create
*LinkedIO = (TLinkedIO*)malloc( sizeof(TLinkedIO) );
memset( *LinkedIO, 0, sizeof( TLinkedIO ));
// Set Parameters
(*LinkedIO)->Function = OutFunction;
(*LinkedIO)->IOName = (char*)malloc( strlen(OutputName)+1 );
strcpy( (*LinkedIO)->IOName, OutputName );
// Log Event
sprintf( LogStr, "Input Linked - '%s'/'%s' <-- '%s'/'%s'", Name, IOName, OutFunction->GetName(), OutputName );
LogMessage( Name, dlMedium, LogStr );
}
return true;
}
//---------------------------------------------------------------------------
bool CFunctionCore::AddOutput( const char * IOName, CFunctionCore * InFunction, const char * InputName )
{
TLocalIO * LocalIO = NULL;
TLinkedIO ** LinkedIO = NULL;
// Get IO
if (!InFunction || !InputName || !(LocalIO = GetLocalIO( IOName ))) {
return false;
}
// Check if linked IO exists
LinkedIO = &(LocalIO->FirstOutput);
while (*LinkedIO && (((*LinkedIO)->Function != InFunction) || strcmp( (*LinkedIO)->IOName, InputName ) )) {
LinkedIO = &((*LinkedIO)->Next);
}
// Create if not found
if (!*LinkedIO)
{
// Create
*LinkedIO = (TLinkedIO*)malloc( sizeof(TLinkedIO) );
memset( *LinkedIO, 0, sizeof( TLinkedIO ));
// Set Parameters
(*LinkedIO)->Function = InFunction;
(*LinkedIO)->IOName = (char*)malloc( strlen(InputName)+1 );
strcpy( (*LinkedIO)->IOName, InputName );
// Log Event
sprintf( LogStr, "Output Linked - '%s'/'%s' --> '%s'/'%s'", Name, IOName, InFunction->GetName(), InputName );
LogMessage( Name, dlMedium, LogStr );
}
return true;
}
//---------------------------------------------------------------------------
// Manual Data Input/Output
int CFunctionCore::Input( const char * IOName, const char * Data, int MaxLen )
{
TLocalIO * LocalIO = NULL;
// Validate
if (!IOName || !Data) {
return 0;
}
else if (MaxLen == -1) {
MaxLen = strlen( Data );
}
// Get IO
if (!(LocalIO = GetLocalIO( IOName )))
{
// Log event
sprintf( LogStr, "%s: Local IO '%s' - Input rejected, Local IO not found", Name, IOName );
LogMessage( Name, dlMedium, LogStr );
return 0;
}
// Log event
sprintf( LogStr, "%s: Local IO '%s' - IN:", Name, IOName );
ShowOutput( LogStr, dlMedium, OUT_NORMAL, Data, MaxLen );
// Return processed bytes
return MaxLen;
}
//---------------------------------------------------------------------------
int CFunctionCore::Output( const char * IOName, const char * Data, int Len )
{
TLocalIO * LocalIO = NULL;
// Validate
if (!IOName || !Data) {
return 0;
} else if (Len == -1) {
Len = strlen( Data );
}
// Get IO
if (!(LocalIO = GetLocalIO( IOName )))
{
// Log Event
sprintf( LogStr, "%s: Local IO '%s' - Output rejected, Local IO not found", Name, IOName );
LogMessage( Name, dlMedium, LogStr );
return 0;
}
// Log event
sprintf( LogStr, "%s: Local IO '%s' - OUT:", Name, IOName );
ShowOutput( LogStr, dlMedium, OUT_NORMAL, Data, Len );
// Return processed bytes
return Len;
}
//---------------------------------------------------------------------------