Important Update:

- Remove unused: FunctionCore-ChannelBuffer
- DataTreeCore:
  - Bug fix: Set Len=0 on element Clear()
- SelectableBare/Core:
  - Move move BuildArgs() to SelectableBare
  - Make OutputHandle() to virtual method
- UtilCore:
  - Bug fix: Handle NoCrLF correctly in BytesToSafeStr()
This commit is contained in:
Charl Wentzel
2019-06-02 16:12:42 +02:00
parent ac649bf4fb
commit 7459763eb6
6 changed files with 91 additions and 312 deletions

View File

@@ -131,10 +131,8 @@ CDataMember * CDataMember::CreateChild( const char * Name, const int Len )
bool CDataMember::Clear() bool CDataMember::Clear()
{ {
// Clear value // Clear value
if (Value) { if (Value)
free( Value ); free( Value );
Value = NULL;
}
// Clear children // Clear children
while (FirstChild) { while (FirstChild) {
@@ -142,7 +140,9 @@ bool CDataMember::Clear()
} }
// Reset value // Reset value
Type = jtNull; Type = jtNull;
Value = NULL;
Len = 0;
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@@ -1,222 +0,0 @@
/*
* FunctionCore.h
*
* Created on: 18 May 2016
* Author: wentzelc
*/
#ifndef REDACORE_FUNCTIONCORE_H_
#define REDACORE_FUNCTIONCORE_H_
// Standard C/C++ Libraries
/* none */
// redA Libraries
#include "LogCore.h"
#include "DataTreeCore.h"
#include "CharBufferCore.h"
#include "ItemBufferCore.h"
//---------------------------------------------------------------------------
// Preview
typedef struct SChannel TChannel;
typedef struct SChannelLink TChannelLink;
typedef struct SProcessBuffer TProcessBuffer;
typedef struct SProcessItem TProcessItem;
class CFunctionCore;
//---------------------------------------------------------------------------
struct SChannel
{
char * Name;
TChannelLink * FirstInput;
TChannelLink * FirstOutput;
bool InputEnabled;
bool OutputEnabled;
TProcessBuffer * InputBuffer;
TChannel * Next;
};
//---------------------------------------------------------------------------
struct SChannelLink
{
CFunctionCore * Function;
char * Name;
SChannelLink * Next;
};
//---------------------------------------------------------------------------
struct SProcessBuffer
{
// Process buffer
char * Name; // Name of buffer;
CItemBufferCore Buffer; // Message buffer
unsigned long LastRefNo; // Last RefNo for item
// Input parameters
bool PassthroughCallback; // Send Callback once processed message has been passed on, else once processed
// Output parameters
TChannel * OutputChannel; // Channel to which to pass buffer output
bool OutputCallback; // Expect callback on output?
TProcessBuffer * Next; // Next buffer in list
};
//---------------------------------------------------------------------------
struct SProcessItem
{
// Input Parameters
TChannel * SourceChannel;
char * SourceRef;
bool SourceCallback;
// Processing Parameters
bool Processed;
bool Completed;
// Output Parameters
timeval CallbackTimer;
TChannelLink FirstOutput;
// Input Data
void * DataIn;
int DataInLen;
// Output Data
void * DataOut;
int DateOutLen;
};
//---------------------------------------------------------------------------
class CFunctionCore_not_used
{
protected:
// Function Definition
char * Name;
char * Type;
// Configuration
CDataTree * DataTree;
TDataMember * ConfigMember;
TDataMember * LinkConfigMember;
// Channels
TChannel * FirstChannel;
// Processing Queues
TProcessBuffer * FirstProcessBuffer;
// Logging
CLogCore * Log;
EDebugLevel LogLevel;
int LogOutput;
// Stored Output
char * StoredOutput;
int StoredOutputLen;
// Manage Channel
inline TChannel * GetChannel( const char * Name ) {
if (!Name) return NULL;
TChannel * Channel = FirstChannel;
while (Channel && strcmp( Name, Channel->Name ))
Channel = Channel->Next;
return Channel;
}
// Load configuration
virtual bool LoadConfigData();
virtual bool LoadChannelLinkData();
// Data Input/Output
virtual int Output( const TChannel * Channel, const char * Data, int Len );
virtual bool PullInput( TChannel * Channel );
// Processing Queue
TProcessItem * CreateProcessItem();
public:
// Life cycle
CFunctionCore_not_used( const char * pName, const char * Type );
virtual ~CFunctionCore_not_used();
// Load Configuration
virtual bool Init();
bool InitConfig( const char * pConfigPath );
bool InitConfig( TDataMember * pConfigMember );
bool InitChannelLinks( const char * pLinkConfigPath );
bool InitChannelLinks( TDataMember *pLinkConfigMember );
// Set Parameters Manually
bool SetLogParam( EDebugLevel pDebugLevel, int pOutputDisplay );
bool SetLogLevel( EDebugLevel pDebugLevel );
// Miscellaneous
inline const char * GetName() { return Name; };
inline const char * GetType() { return Type; };
// Manage Channels
virtual TChannel * AddChannel( const char * ChannelName, const bool pInputEnable = true, const bool pOutputEnabled = true );
inline bool SetChannelOutEnable( const char * ChannelName, const bool pOutputEnable ) {
TChannel * Channel = GetChannel( ChannelName );
if (!Channel) return false;
Channel->OutputEnabled = pOutputEnable;
return true;
}
inline bool SetChannelInEnable( const char * ChannelName, const bool pInputEnable ) {
TChannel * Channel = GetChannel( ChannelName );
if (!Channel) return false;
Channel->InputEnabled = pInputEnable;
return true;
}
inline bool isInputEnabled( const char * ChannelName ) {
TChannel * Channel = GetChannel( ChannelName );
return ((Channel)? Channel->InputEnabled : false);
}
inline bool isOutputEnabled( const char * ChannelName ) {
TChannel * Channel = GetChannel( ChannelName );
return ((Channel)? Channel->OutputEnabled : false);
}
// Pushing Data Output -> Input
virtual int Output( const char * ChannelName, const char * Data, int Len = -1 );
virtual int Input( const char * ChannelName, const char * Data, int Len = -1 );
// Pulling Data Input <- Output
virtual bool PullInput( const char * ChannelName );
virtual bool PullOutput( const char * ChannelName, char ** Data, int * Len = NULL );
// Manages Process Buffers
TProcessBuffer * CreateProcessBuffer( const char * Name );
TProcessBuffer * GetProcessBuffer( const char * Name );
TProcessItem * CreateProcessItem( const char * BufferName );
TProcessItem * GetProcessItem( const char BufferName, const int Ref );
bool DestroyProcessItem( const char * BufferName, const int Ref );
// Queued Output
virtual bool OutputMessage( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo, bool Callback, const char * Data, int Len = -1 );
virtual bool OutputMessageCallback( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo, const bool Success, const char * Error );
virtual bool InputMessage( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo, bool Callback, const char * Data, int Len = -1 );
virtual bool InputMessageCancel( TChannel * SourceChannel, const char * TargetChannelName, const int RefNo );
// Automated Data Input/Output
virtual bool LinkInputChannel( const char * ChannelName, const char * OutFunctionName, const char * OutChannelName, bool Bidirectional );
virtual bool LinkOutputChannel( const char * ChannelName, const char * InFunctionName, const char * InChannelName, bool Bidirectional );
virtual bool Process() = 0;
};
//---------------------------------------------------------------------------
#endif /* REDACORE_FUNCTIONCORE_H_ */

View File

@@ -873,82 +873,3 @@ bool CSelectableBare::Process()
return true; return true;
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CSelectableBare::BuildArgs( const char * ExecPath, int &Count, char * Args[] )
{
bool ParamStarted = false;
bool OpenQuotes = false;
char * MatchPos = NULL;
char * StartPos = NULL;
int Len;
// Validate
if (!ExecPath || !*ExecPath) {
return false;
}
// Split params
MatchPos = (char*)ExecPath;
for (;;)
{
// Look for whitespace
if (!ParamStarted)
{
// Quotes starts quoted parameter
if (*MatchPos == '"') {
ParamStarted = true;
OpenQuotes = true;
StartPos = MatchPos+1; // Skip starting quote
}
// Non-whitespace starts normal parameter
else if ((*MatchPos != ' ') && (*MatchPos != 0)) {
ParamStarted = true;
StartPos = MatchPos;
}
}
else if (OpenQuotes)
{
// Another quote ends parameter
if (*MatchPos == '"') {
Len = MatchPos-StartPos-1; // Skip end quote
Args[Count] = (char*)malloc( Len+1 );
strncpy( Args[Count], StartPos, Len );
Args[Count][Len] = 0;
Count++;
ParamStarted = false;
OpenQuotes = false;
}
}
else
{
// Whitespace ends parameter
if ((*MatchPos == ' ') || (*MatchPos == 0)) {
Len = MatchPos-StartPos;
Args[Count] = (char*)malloc( Len+1 );
strncpy( Args[Count], StartPos, Len );
Args[Count][Len] = 0;
Count++;
ParamStarted = false;
}
}
// Next char, unless NULL
if (*MatchPos)
MatchPos++;
else
break;
}
// Check all parameters closed
if (ParamStarted) {
Count = 0;
Args[0] = NULL;
return false;
}
// Set last Param to NULL
Args[Count] = NULL;
return true;
}
//---------------------------------------------------------------------------

View File

@@ -1254,7 +1254,6 @@ THandle * CSelectableCore::OpenTCPremoteSocket( THandle * Handle )
strcpy( ClientAddress, inet_ntoa(address.sin_addr) ); strcpy( ClientAddress, inet_ntoa(address.sin_addr) );
sprintf( ClientPort, "%d", ntohs(address.sin_port) ); sprintf( ClientPort, "%d", ntohs(address.sin_port) );
// Get end of client list // Get end of client list
RemoteClient = &FirstHandle; RemoteClient = &FirstHandle;
while (*RemoteClient) { while (*RemoteClient) {
@@ -2455,3 +2454,80 @@ bool CSelectableCore::ReadSerialConfig( THandle * Handle )
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[] )
{
bool ParamStarted = false;
bool OpenQuotes = false;
char * MatchPos = NULL;
char * StartPos = NULL;
int Len;
// Validate
if (!ExecPath || !*ExecPath) {
return false;
}
// Split params
MatchPos = (char*)ExecPath;
for (;;)
{
// Look for whitespace
if (!ParamStarted)
{
// Quotes starts quoted parameter
if (*MatchPos == '"') {
ParamStarted = true;
OpenQuotes = true;
StartPos = MatchPos+1; // Skip starting quote
}
// Non-whitespace starts normal parameter
else if ((*MatchPos != ' ') && (*MatchPos != 0)) {
ParamStarted = true;
StartPos = MatchPos;
}
}
else if (OpenQuotes)
{
// Another quote ends parameter
if (*MatchPos == '"') {
Len = MatchPos-StartPos-1; // Skip end quote
Args[Count] = (char*)malloc( Len+1 );
strncpy( Args[Count], StartPos, Len );
Args[Count][Len] = 0;
Count++;
ParamStarted = false;
OpenQuotes = false;
}
}
else
{
// Whitespace ends parameter
if ((*MatchPos == ' ') || (*MatchPos == 0)) {
Len = MatchPos-StartPos;
Args[Count] = (char*)malloc( Len+1 );
strncpy( Args[Count], StartPos, Len );
Args[Count][Len] = 0;
Count++;
ParamStarted = false;
}
}
// Next char, unless NULL
if (*MatchPos)
MatchPos++;
else
break;
}
// Check all parameters closed
if (ParamStarted) {
Count = 0;
Args[0] = NULL;
return false;
}
// Set last Param to NULL
Args[Count] = NULL;
return true;
}
//---------------------------------------------------------------------------

View File

@@ -217,9 +217,6 @@ protected:
// Buffer operations // Buffer operations
virtual bool ProcessInputBuffer( THandle * Handle, bool Force ); virtual bool ProcessInputBuffer( THandle * Handle, bool Force );
// Specific operations
bool BuildArgs( const char * ExecPath, int &Count, char * Args[] );
// Convert string to lower case // Convert string to lower case
inline char * strlcase( char * Str ) { inline char * strlcase( char * Str ) {
for (char * Ch = Str; *Ch; Ch++ ) for (char * Ch = Str; *Ch; Ch++ )
@@ -295,7 +292,7 @@ public:
// Function Interface // Function Interface
virtual int Input( const char * ChannelName, const char * Buffer, int BufLen = -1 ); virtual int Input( const char * ChannelName, const char * Buffer, int BufLen = -1 );
int OutputHandle( THandle * Handle, const char * Data, int Len ); virtual int OutputHandle( THandle * Handle, const char * Data, int Len );
virtual bool Process(); virtual bool Process();
}; };
@@ -319,6 +316,7 @@ protected:
// Socket Operations // Socket Operations
bool ResolveAddress( THandle * Handle, bool DelayResolve ); bool ResolveAddress( THandle * Handle, bool DelayResolve );
THandle * OpenUDPserverSocket( THandle * Handle, bool DelayResolve ); THandle * OpenUDPserverSocket( THandle * Handle, bool DelayResolve );
THandle * OpenUDPremoteSocket( THandle * Handle, char * RemoteAddr, char * RemotePort ); THandle * OpenUDPremoteSocket( THandle * Handle, char * RemoteAddr, char * RemotePort );
THandle * OpenUDPclientSocket( THandle * Handle, bool DelayResolve ); THandle * OpenUDPclientSocket( THandle * Handle, bool DelayResolve );
@@ -330,6 +328,9 @@ protected:
int ReadFromUDP( THandle * Handle, char * RemoteAddr, char * RemotePort, char * Data, int MaxLen ); int ReadFromUDP( THandle * Handle, char * RemoteAddr, char * RemotePort, char * Data, int MaxLen );
int WriteToUDP( THandle * Handle, const char * Data, int Len, bool Force ); int WriteToUDP( THandle * Handle, const char * Data, int Len, bool Force );
// Specific operations
bool BuildArgs( const char * ExecPath, int &Count, char * Args[] );
public: public:
// Life Cycle // Life Cycle
CSelectableCore( const char * Name, const char * Type = TYPE_SELECTABLE ); CSelectableCore( const char * Name, const char * Type = TYPE_SELECTABLE );
@@ -353,7 +354,7 @@ public:
virtual bool Write( THandle * Handle ); virtual bool Write( THandle * Handle );
// Function Interface // Function Interface
int OutputHandle( THandle * Handle, const char * Data, int Len ); virtual int OutputHandle( THandle * Handle, const char * Data, int Len );
virtual bool Process(); virtual bool Process();
}; };

View File

@@ -40,7 +40,10 @@ char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf, con
// Remove special char // Remove special char
for (int i=0; i<Len; i++) { for (int i=0; i<Len; i++) {
if (((Bytes[i] < 32) || (Bytes[i] > 126)) || if ((Bytes[i] == '\r') || (Bytes[i] == '\n')) {
*BufPos = (NoCrLf)? SpecChar : Bytes[i];
}
else if (((Bytes[i] < 32) || (Bytes[i] > 126)) ||
(NoCrLf && ((Bytes[i] == '\r') || (Bytes[i] == '\n')))) { (NoCrLf && ((Bytes[i] == '\r') || (Bytes[i] == '\n')))) {
*BufPos = SpecChar; *BufPos = SpecChar;
} }