Files
redAcore/SelectableCore.h
Charl Wentzel 2f81d1fbbe Important Update:
- Library Clean up:
  - Removed all unused C/C++ libraries from source
  - First C/C++ libraries then redA libraries
- Library changes:
  - renamed BufferCore -> CharBufferCore
  - added ItemBufferCore
- CharBufferCore:
  - Derive RollingBuffer & ShiftBuffer from common class CharBuffer
  - CharBuffer is mostly a virtual class (interface)
2017-07-22 17:46:05 +02:00

290 lines
8.9 KiB
C++

/*
* SelectableCore.h
*
* Created on: 13 May 2016
* Author: wentzelc
*/
#ifndef REDACORE_SELECTABLECORE_H_
#define REDACORE_SELECTABLECORE_H_
// Standard C/C++ Libraries
/* none */
// redA Libraries
#include "FunctionCore.h"
//---------------------------------------------------------------------------
// Types required for connections
typedef enum { ctNone = 0, ctPort = 1, ctForkPipe = 2, ctServer = 3, ctRemoteClient = 4, ctClient = 5 } EConnectType;
const char ConnectTypeName[][15] = { "None", "Port", "ForkPipe", "Server", "RemoteClient", "Client" };
typedef enum { csNone = 0, csWaitingtoOpen = 1, csOpen = 2, csDataWaiting = 3, csClosed = 4, csFailed = 5 } EConnectState;
const char ConnectStateName[][15] = { "None", "WaitingToOpen", "Open", "DataWaiting", "Closed", "Failed" };
//---------------------------------------------------------------------------
// Defines required to configure port
#define NO_PARITY 0
#define ODD_PARITY 1
#define EVEN_PARITY 2
#define MARK_PARITY 3
#define SPACE_PARITY 4
#define NO_FLOWCTRL 0
#define HW_FLOWCTRL 1
#define SW_FLOWCTRL 2
//---------------------------------------------------------------------------
// Previews
typedef struct SSelectHandle TSelectHandle;
typedef struct SHandle THandle;
class CSelect;
class CSelectableCore;
// Callback function for handle events
typedef void (*FHandleCallback)( CSelectableCore * Function, THandle * Handle, EConnectState State );
//---------------------------------------------------------------------------
// List of Handles for Select Object
struct SSelectHandle {
// File Descriptor
int FD;
bool Read;
bool Write;
// Event Object
CSelectableCore * Function;
// List
TSelectHandle * Next;
};
//---------------------------------------------------------------------------
// List or Handles for Selectable Function Object
struct SHandle {
// Description
char * Name;
EConnectType Type;
// State
int FD;
EConnectState State;
bool AutoManage;
// Callback functions
FHandleCallback StateCallback[ 6 ];
// Type specific parameters
char * Path; // Port (file)name or Exec path
pid_t ChildPID; // Forked child PID
char * HostName; // Host name or IP adddress
char * PortName; // Socket port no or protocol, e.g. "80" or "HTTP"
struct addrinfo * AddressList; // List of resolved IP Addresses for host name
struct addrinfo * AddressInfo; // Current selected IP Address
bool KeepAlive; // Socket keep alive
// Buffers
CRollingBuffer * InBuffer;
CRollingBuffer * OutBuffer;
// Input Markers
char * InMarker;
int InMarkerLen;
// Input Timer
timeval InStart;
long InTimeout; // millisecs
// Reopen Timer
timeval ReopenStart;
long ReopenDelay; // millisecs
// List / Tree
TChannel * Channel;
THandle * Parent;
THandle * Next;
};
//---------------------------------------------------------------------------
class CSelect
{
protected:
// List
TSelectHandle * FirstHandle;
// Select Variables
fd_set ReadTestFDS;
fd_set WriteTestFDS;
fd_set ReadFDS;
fd_set WriteFDS;
// Configuration
int MaxFD;
timeval Timeout;
// Output
CLogCore * Log;
EDebugLevel LogLevel;
public:
// Life Cycle
CSelect( long SelectTimeout, EDebugLevel pLogLevel );
~CSelect();
// Parameters
bool SetLogLevel( EDebugLevel pDebugLevel );
// Manage FDs
void Clear();
void Add( int FD, bool Read, bool Write, CSelectableCore * Function = NULL);
void Remove( int FD, bool Read, bool Write );
// Testing FDs
bool Test();
bool Check( int FD, bool &Read, bool &Write );
};
//---------------------------------------------------------------------------
class CSelectableCore : public CFunctionCore
{
protected:
// FDs
THandle * FirstHandle;
// Select interface
CSelect * Selector;
// Configuration
virtual bool LoadConfigData();
// Managing File Handles
bool RemoveHandle( THandle * Handle );
bool DestroyHandle( THandle * Handle );
// Get Parameters
inline int GetFD( const char * HandleName ) {
THandle * Handle = GetHandle( HandleName );
return ((Handle)? Handle->FD : -1);
};
// General fucntions
inline bool ChangeState( THandle * Handle, EConnectState State ) {
if (!Handle || (Handle->State == State)) return false;
if (Handle->StateCallback[ (int)State ])
(Handle->StateCallback[ (int)State ])( this, Handle, State );
Handle->State = State;
return true;
}
// Port Operations
virtual int OpenPort( THandle * Handle );
// ForkPipe Operations
virtual int OpenForkPipe( THandle * Handle );
// Socket Operations
bool ResolveAddress( THandle * Handle );
virtual int OpenServerSocket( THandle * Handle );
virtual int OpenRemoteClientSocket( THandle * Handle );
virtual int OpenClientSocket( THandle * Handle );
// Mutual Operations
int ReadFromFD( int FD, char * Data, int MaxLen );
int WriteToFD( int FD, const char * Data, int Len, bool Force );
// Buffer operations
virtual bool ProcessBuffer( THandle * Handle, bool Force );
// Specific operations
bool BuildArgs( const char * ExecPath, int &Count, char * Args[] );
// Convert string to lower case
inline char * strlcase( char * Str ) {
for (char * Ch = Str; *Ch; Ch++ )
*Ch = tolower(*Ch);
return Str;
}
public:
// Life Cycle
CSelectableCore( const char * Name, const char * pType = "Selectable" );
virtual ~CSelectableCore();
// Finding Handles
inline THandle * GetHandle( const char * HandleName )
{
if (!HandleName) return NULL;
THandle * Handle = FirstHandle;
while ( Handle && strcmp( HandleName, Handle->Name ))
Handle = Handle->Next;
return Handle;
}
inline THandle * GetHandle( int FD )
{
if (FD < 0) return NULL;
THandle * Handle = FirstHandle;
while ( Handle && (FD != Handle->FD))
Handle = Handle->Next;
return Handle;
}
THandle * CreateHandle( const char * HandleName, bool CreateChannel );
bool SetCallback( THandle * Handle, EConnectState pState, FHandleCallback pCallback );
bool SetAutoManage( THandle * Handle, bool AutoManage, int ReopenTime = 0 );
bool SetBuffers( THandle * Handle, int InBufSize, int OutBufSize, int InTimeout, const char * InMarker, int InMarkerLen );
bool SerialConfig( THandle * Handle, int Baud, short DataBits, short StopBits, short Parity, short FlowCtrl, int Wait );
// File Interface
bool SetPortHandle( THandle * Handle, const char * FileName );
bool SetForkPipeHandle( THandle * Handle, const char * ExecPath );
bool SetSocketHandle( THandle * Handle, EConnectType Type, const char * HostName, const char * PortName, bool KeepAlive );
bool ClearHandle( THandle * Handle );
// FD Operations
virtual int Open( THandle * Handle );
virtual bool Close( THandle * Handle, bool CloseChildren = false );
virtual bool Read( THandle * Handle );
virtual bool Write( THandle * Handle );
// FD operations
inline virtual int Open( const char * HandleName ) { return (Open( GetHandle( HandleName ))); };
inline virtual bool Close( const char * HandleName, bool CloseChildren = false ) { return (Close( GetHandle( HandleName ), CloseChildren )); };
inline virtual bool Close( int FD, bool CloseChildren = false ) { return (Close( GetHandle( FD ), CloseChildren )); };
inline virtual bool Read( const char * HandleName ) { return (Read( GetHandle( HandleName ))); };
inline virtual bool Read( int FD ) { return (Read( GetHandle( FD ))); };
inline virtual bool Write( const char * HandleName ) { return (Write( GetHandle( HandleName ))); };
inline virtual bool Write( int FD ) { return (Write( GetHandle( FD ))); };
// Info
inline EConnectType GetType( const char * HandleName ) {
THandle * Handle = GetHandle( HandleName );
return ((Handle)? Handle->Type : ctNone);
};
inline EConnectState GetState( const char * HandleName ) {
THandle * Handle = GetHandle( HandleName );
return ((Handle)? Handle->State : csNone);
};
// Function Interface
virtual int Input( const char *ChannelName, const char * Buffer, int BufLen = -1 );
virtual bool Process();
};
//---------------------------------------------------------------------------
#endif /* REDACORE_SELECTABLECORE_H_ */