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
This commit is contained in:
Charl Wentzel
2017-07-16 20:29:01 +02:00
parent 8933ec1ae6
commit 434377f122
17 changed files with 809 additions and 205 deletions

View File

@@ -6,9 +6,8 @@
*/
// redA Libraries
#include "ApplicationCore.h"
#include "SelectableCore.h"
#include "TimingCore.h"
#include "LogCore.h"
// Standard C/C++ Libraries
#include <stdio.h>
@@ -30,14 +29,20 @@
//---------------------------------------------------------------------------
CSelectableCore::CSelectableCore( const char * pName, CSelect * pSelect, CLogCore * pLog ) :
CFunctionCore( pName, pLog )
// Global Vars
//extern char * ProcessName;
extern CApplication * Application;
//---------------------------------------------------------------------------
CSelectableCore::CSelectableCore( const char * pName, const char * pType ) :
CFunctionCore( pName, pType )
{
// Quick access
Selector = Application->Selector;
// Handles
FirstHandle = NULL;
// Select
Select = pSelect;
}
//---------------------------------------------------------------------------
@@ -73,13 +78,17 @@ bool CSelectableCore::LoadConfigData()
// Call Previous load config
CFunctionCore::LoadConfigData();
// Set debug output
TempMember = DataTree->GetFirstChild( DataTree->GetMember( BaseMember, "Handles", true ) );
// Load Handles
TempMember = DataTree->GetFirstChild( DataTree->GetMember( ConfigMember, "Handles", true ) );
while (TempMember)
{
// Check if name is valid
if (!TempMember->Name || !*TempMember->Name)
continue;
// Create Handle and channel link
Handle = CreateHandle( DataTree->GetStr( TempMember, "Name" ), false );
Handle->Channel = GetChannel( DataTree->GetStr( TempMember, "Name" ) );
Handle = CreateHandle( TempMember->Name, false );
Handle->Channel = GetChannel( DataTree->GetStr( TempMember, "Channel" ) );
Type = (char*)DataTree->GetStr( TempMember, "Type", "TCPclient", true );
if (!strcasecmp( Type, "Port" ))
@@ -94,7 +103,7 @@ bool CSelectableCore::LoadConfigData()
else if (!strcasecmp( Type, "TCPserver" ))
{
Address = (char*)DataTree->GetStr( TempMember, "Socket/Address", NULL ); // Get default Address value
Port = DataTree->GetInt( TempMember, "Socket/Port", 0, true ); // Get default Port value
Port = DataTree->GetInt( TempMember, "Socket/Port", 0 ); // Get default Port value
if ((Name = (char*)DataTree->GetStr( TempMember, "Socket/Name", NULL ))) {
sprintf( Path, "Address/%s/Address", Name );
Address = (char*)DataTree->GetStr( NULL, Path, Address, true ); // Get AddressList Address value
@@ -106,7 +115,7 @@ bool CSelectableCore::LoadConfigData()
else if (!strcasecmp( Type, "TCPclient" ))
{
Address = (char*)DataTree->GetStr( TempMember, "Socket/Address", NULL ); // Get default Address value
Port = DataTree->GetInt( TempMember, "Socket/Port", 0, true ); // Get default Port value
Port = DataTree->GetInt( TempMember, "Socket/Port", 0 ); // Get default Port value
if ((Name = (char*)DataTree->GetStr( TempMember, "Socket/Name", NULL ))) {
sprintf( Path, "Address/%s/Address", Name );
Address = (char*)DataTree->GetStr( NULL, Path, Address, true ); // Get AddressList Address value
@@ -435,8 +444,8 @@ int CSelectableCore::OpenPort( THandle * Handle )
if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - Port opened [%s]", Name, Handle->Name, Handle->Path );
// Add to Select Lists
if (Select) {
Select->Add( Handle->FD, true, false, this );
if (Selector) {
Selector->Add( Handle->FD, true, false, this );
}
// Set state
@@ -527,8 +536,8 @@ int CSelectableCore::OpenForkPipe( THandle * Handle )
}
// Add to Select Lists
if (Select) {
Select->Add( Handle->FD, false, true, this );
if (Selector) {
Selector->Add( Handle->FD, false, true, this );
}
// Set state
@@ -637,8 +646,8 @@ int CSelectableCore::OpenServerSocket( THandle * Handle )
if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - TCP Server binded and listening [%s:%d]", Name, Handle->Name, Handle->Address, Handle->PortNo );
// Add to Select Lists
if (Select) {
Select->Add( Handle->FD, true, false, this );
if (Selector) {
Selector->Add( Handle->FD, true, false, this );
}
// Set state
@@ -711,8 +720,8 @@ int CSelectableCore::OpenRemoteClientSocket( THandle * Handle )
if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - TCP Server accepted Remote TCP Client connection [%s]", Name, Handle->Name, ClientAddress );
// Add to Select Lists
if (Select) {
Select->Add( (*RemoteClient)->FD, true, true, this );
if (Selector) {
Selector->Add( (*RemoteClient)->FD, true, true, this );
}
return (*RemoteClient)->FD;
@@ -802,8 +811,8 @@ int CSelectableCore::OpenClientSocket( THandle * Handle )
if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - TCP Client connected [%s:%d]", Name, Handle->Name, Handle->Address, Handle->PortNo );
// Add to Select Lists
if (Select) {
Select->Add( Handle->FD, true, true, this );
if (Selector) {
Selector->Add( Handle->FD, true, true, this );
}
// Set status
@@ -816,8 +825,8 @@ int CSelectableCore::OpenClientSocket( THandle * Handle )
if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - TCP Client waiting to connect [%s:%d] (%s)", Name, Handle->Name, Handle->Address, Handle->PortNo, strerror(errno) );
// Add to Select Lists
if (Select) {
Select->Add( Handle->FD, true, true, this );
if (Selector) {
Selector->Add( Handle->FD, true, true, this );
}
// Set status
@@ -830,8 +839,8 @@ int CSelectableCore::OpenClientSocket( THandle * Handle )
if (Log) Log->Message( LogLevel, dlMedium, "%s: Handle '%s' - TCP Client could not connect [%s:%d] (%s)", Name, Handle->Name, Handle->Address, Handle->PortNo, strerror(errno) );
// Remove from Select List
if (Select) {
Select->Remove( Handle->FD, true, true );
if (Selector) {
Selector->Remove( Handle->FD, true, true );
}
// Close socket
@@ -950,8 +959,8 @@ bool CSelectableCore::Close( THandle * Handle, bool CloseChildren )
};
// Remove from Select List
if (!Fail && Select) {
Select->Remove( Handle->FD, true, true );
if (!Fail && Selector) {
Selector->Remove( Handle->FD, true, true );
}
// Reset FD
@@ -982,8 +991,8 @@ bool CSelectableCore::Read( THandle * Handle )
ClientFD = OpenRemoteClientSocket( Handle );
if (ClientFD != -1) {
// Add to Select Lists
if (Select) {
Select->Add( ClientFD, true, true, this );
if (Selector) {
Selector->Add( ClientFD, true, true, this );
}
return true;
}
@@ -1061,8 +1070,8 @@ bool CSelectableCore::Write( THandle * Handle )
}
// Remove from set for select write
if (Select) {
Select->Remove( Handle->FD, false, true );
if (Selector) {
Selector->Remove( Handle->FD, false, true );
}
return true;
}
@@ -1086,16 +1095,16 @@ bool CSelectableCore::Write( THandle * Handle )
// Check if Buffer emtpy
if (!Handle->OutBuffer->Len()) {
// Add to Select Write list
if (Select) {
Select->Remove( Handle->FD, false, true );
if (Selector) {
Selector->Remove( Handle->FD, false, true );
}
}
}
else
{
// No Output buffer to write from, so remove from Write list
if (Select) {
Select->Remove( Handle->FD, false, true );
if (Selector) {
Selector->Remove( Handle->FD, false, true );
}
}
return true;
@@ -1267,8 +1276,8 @@ int CSelectableCore::Input( const char * ChannelName, const char * Data, int Len
BytesWritten = ChildHandle->OutBuffer->Push( true, Data, Len );
// Add to select write list
if (BytesWritten) {
Select->Add( ChildHandle->FD, false, true, this );
if (BytesWritten && Selector) {
Selector->Add( ChildHandle->FD, false, true, this );
}
}
else
@@ -1296,8 +1305,8 @@ int CSelectableCore::Input( const char * ChannelName, const char * Data, int Len
BytesWritten = Handle->OutBuffer->Push( true, Data, Len );
// Add to select write list
if (BytesWritten) {
Select->Add( Handle->FD, false, true, this );
if (BytesWritten && Selector) {
Selector->Add( Handle->FD, false, true, this );
}
}
else