Files
redAcore/FunctionCore.cpp
Charl Wentzel b4073a166a Major Update:
- Updated all Logged messages, standardised DebugLevel:
  - dlNone   - Show startup and stop
  - dlLow    - Show creation/destruction of Function Blocks and Local IO
  - dlMedium - Show connection events, eg. open/close
  - dlHigh   - Show data flow events
-LogCore:
  - LogMessage and ShowOutput uses va_list
    only run printf if DebugLevel correct
  - Remove global LogStr[] variable
- SelectableCore:
  - Implemented Auto-management of handles
    auto open on startup/fail/close
  - Changed simple Open/Close/Read/Write methods to inline
  - Do not set Select Write trigger for server socket
  - Memory leak, setting address twice on RemoteClient
  - Bug fix, only send handle out data if DebugLevel = dlHigh
  - Bug fix, do not Read/ProcessBuffer if no InputBuffer
2016-05-27 13:32:54 +02:00

256 lines
6.7 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[];
//---------------------------------------------------------------------------
// 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
LogMessage( dlLow, "%s: Function '%s' - Created", ProcessName, FunctionName );
}
//---------------------------------------------------------------------------
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
LogMessage( dlLow, "%s: Function '%s' - Destroyed", ProcessName, Name );
// 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
LogMessage( dlLow, "%s: Local IO '%s' - Created", Name, IOName );
}
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
LogMessage( dlLow, "%s: Input Linked - '%s'/'%s' <-- '%s'/'%s'", Name, Name, IOName, OutFunction->GetName(), OutputName );
}
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
LogMessage( dlLow, "%s: Output Linked - '%s'/'%s' --> '%s'/'%s'", Name, Name, IOName, InFunction->GetName(), InputName );
}
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
LogMessage( dlHigh, "%s: Local IO '%s' - Input rejected, Local IO not found", Name, IOName );
return 0;
}
// Log event
ShowOutput( dlHigh, OUT_NORMAL, Data, MaxLen, "%s: Local IO '%s' - IN:", Name, IOName );
// 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
LogMessage( dlHigh, "%s: Local IO '%s' - Output rejected, Local IO not found", Name, IOName );
return 0;
}
// Log event
ShowOutput( dlHigh, OUT_NORMAL, Data, Len, "%s: Local IO '%s' - OUT:", Name, IOName );
// Return processed bytes
return Len;
}
//---------------------------------------------------------------------------