Files
redAcore/SelectCore.cpp
Charl Wentzel 434377f122 Major Update:
- Implement new ApplicationCore:
  - Manage Tools: Log, DataTree, JSONparser & Selector
  - Load configuration, Manage FunctionBlocks
- FunctionCore & dirived classes, SignalCore:
  - affects: SelectableCore, DeviceCore, FileCore, WatchdogCore
  - Do not pass Log()
  - Define and pass Type
  - Update/reduce included headers
  - Use ProcessName and Application global vars
  - Get Log, DataTree from Application
  - Use virtual Init() function to set must have Channels/Handles
- DataTreeCore:
  - Bug fix: Check if child members exist and destroy in SetValuePtr()
  - Add method GetFirstChild with BaseMember & Path
  - Bug fix: do not use RootMember if no Parent in GetChildxxx() methods
- SignalCore, SelectCore:
  - Use Application->Log()
- FunctionCore:
  - Add itself to Application (function list)
  - Add parameter: LinkConfigMember, Type
  - Use virtual LoadConfigData() to configure from DataTree
  - Rename methods: LoadConfig() -> InitConfig(),
      InitLogging() -> SetLogParam(), SetDebugLevel() -> SetLogLevel()
  - Add method: GetType(), LoadChannelLinkData(), InitChannelLinks()
  - Modify LinkIn/OutputChannel() to use name for InFunction, not pointer
- SelectableCore & Dirived classes:
  - Affects DeviceCore & WatchdogClient
  - Rename parameter: Select -> Selector, BaseMember -> ConfigMember
  - Get Selector from Application->Selector
  - Change Handles JSON structure from Array to Key:Value pairs
  - Bug fix: check if Selector exist during Input()
  - Bug fix: do not set PortNo if not exist
2017-07-16 20:29:01 +02:00

239 lines
6.3 KiB
C++

/*
* SelectCore.cpp
*
* Created on: 13 May 2016
* Author: wentzelc
*/
// redA Libraries
#include "ApplicationCore.h"
#include "SelectableCore.h"
// Standard C/C++ Libraries
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
//---------------------------------------------------------------------------
// Global Vars
extern char * ProcessName;
extern CApplication * Application;
//---------------------------------------------------------------------------
// Create Select
CSelect::CSelect( long SelectTimeout, EDebugLevel pDebugLevel )
{
// Clear List
FirstHandle = NULL;
// Clear Select sets
FD_ZERO( &ReadTestFDS );
FD_ZERO( &WriteTestFDS );
// Reset maximum File Descriptor
MaxFD = 0;
// Set Timeout
SetInterval( &Timeout, SelectTimeout );
// Output
Log = Application->Log;
LogLevel = pDebugLevel;
// Show status
if (Log) Log->Message( LogLevel, dlLow, "%s: Selector - Created", ProcessName );
}
//---------------------------------------------------------------------------
// Destroy Select
CSelect::~CSelect()
{
TSelectHandle * NextHandle;
// Destroy handles
while (FirstHandle)
{
NextHandle = FirstHandle->Next;
free( FirstHandle );
FirstHandle = NextHandle;
}
// Show status
if (Log) Log->Message( LogLevel, dlLow, "%s: Selector - Destroyed", ProcessName );
return;
}
//---------------------------------------------------------------------------
bool CSelect::SetDebugLevel( EDebugLevel pDebugLevel )
{
LogLevel = pDebugLevel;
return true;
}
//---------------------------------------------------------------------------
// Clear Select File Descriptors
void CSelect::Clear()
{
// Clear Select sets
FD_ZERO( &ReadTestFDS );
FD_ZERO( &WriteTestFDS );
// Reset maximum File Descriptor
MaxFD = 0;
}
//---------------------------------------------------------------------------
// Add Select File Descriptor
void CSelect::Add( int FD, bool Read, bool Write, CSelectableCore * Function )
{
TSelectHandle ** Handle = NULL;
// Check if Handle already exists
Handle = &FirstHandle;
while (*Handle && ((*Handle)->FD != FD)) {
Handle = &((*Handle)->Next);
}
// Create if not exist
if (!*Handle) {
// Create
*Handle = (TSelectHandle*)malloc( sizeof(TSelectHandle) );
memset( *Handle, 0, sizeof(TSelectHandle) );
// Set Parameters
(*Handle)->FD = FD;
(*Handle)->Function = Function;
}
// Add Read select
if (Read && !(*Handle)->Read) {
(*Handle)->Read = true;
FD_SET( FD, &ReadTestFDS );
// Log event
if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Add Read", FD );
}
// Add Write Select
if (Write && !(*Handle)->Write) {
(*Handle)->Write = true;
FD_SET( FD, &WriteTestFDS );
// Log event
if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Add Write", FD );
}
// Check Maximum File Handle
if (MaxFD <= FD)
MaxFD = FD+1;
}
//---------------------------------------------------------------------------
void CSelect::Remove( int FD, bool Read, bool Write )
{
TSelectHandle ** Handle = NULL;
// Check if Handle already exists
Handle = &FirstHandle;
while (*Handle && ((*Handle)->FD != FD)) {
Handle = &((*Handle)->Next);
}
// Check if found
if (!*Handle)
return;
// Remove from set for select read check
if (Read && (*Handle)->Read) {
(*Handle)->Read = false;
FD_CLR( FD, &ReadTestFDS);
// Log event
if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Remove Read", FD );
}
// Remove from set for select write check
if (Write && (*Handle)->Write) {
(*Handle)->Write = false;
FD_CLR( FD, &WriteTestFDS);
// Log event
if (Log) Log->Message( LogLevel, dlHigh, "Selector: FD [%d] - Remove Write", FD );
}
// Handle will be removed in Test() if both Read & Write flags are false
}
//---------------------------------------------------------------------------
bool CSelect::Test()
{
TSelectHandle * Handle = NULL;
TSelectHandle ** HandlePtr = NULL;
int TestFD = 0;
int Events = 0;
timeval STimeout = Timeout;
// Set Test sets
ReadFDS = ReadTestFDS;
WriteFDS = WriteTestFDS;
// Perform select
Events = select( MaxFD, &ReadFDS, &WriteFDS, (fd_set*)NULL, &STimeout );
if (Events < 0)
{
if (Log) Log->Message( LogLevel, dlHigh, "Selector: Select operation failed" );
return false;
}
// Check all descriptors for events
if (Events) {
Handle = FirstHandle;
while (Handle)
{
// Check if to remove from list
if (!Handle->Read && !Handle->Write)
{
// Update Maximum Test FD
if (Handle->FD == MaxFD-1) {
for (TestFD = MaxFD-1; TestFD >= 0; TestFD--) {
if (FD_ISSET( TestFD, &ReadTestFDS ) || FD_ISSET( TestFD, &WriteTestFDS )) {
break;
}
}
MaxFD = TestFD+1;
}
// Remove from list
HandlePtr = &FirstHandle;
while (*HandlePtr && (*HandlePtr != Handle))
HandlePtr = &((*HandlePtr)->Next);
*HandlePtr = (*HandlePtr)->Next;
// Destroy and go to next
free( Handle );
Handle = *HandlePtr;
continue;
}
else
{
// Check read Event
if (FD_ISSET( Handle->FD, &ReadFDS ) && Handle->Function) {
Handle->Function->Read( Handle->FD );
}
// Check Write Event
if (FD_ISSET( Handle->FD, &WriteFDS ) && Handle->Function) {
Handle->Function->Write( Handle->FD );
}
}
// Next
Handle = Handle->Next;
}
}
// return success
return (bool)Events;
}
//---------------------------------------------------------------------------