Important Update:

- Implement Handle Address renaming:
- Restructure JSON config, with address list and rename list
- SelectableCore:
  - Add method GetHandleAddress() - lookup renamed handle
  - Add init values to all struct and classes declarations
- Replace malloc()/strcpy() sequences with strdup()
This commit is contained in:
Charl Wentzel
2019-01-08 08:35:21 +02:00
parent c5da842ac1
commit 3e40f7a86d
7 changed files with 128 additions and 232 deletions

View File

@@ -44,9 +44,6 @@ CSelectableCore::CSelectableCore( const char * pName, const char * pType ) : CFu
{
// Quick access
Selector = Application->Selector;
// Handles
FirstHandle = NULL;
}
//---------------------------------------------------------------------------
@@ -98,6 +95,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
Handle = CreateHandle( HandleConfig->GetName(), false );
Handle->Channel = GetChannel( HandleConfig->GetChStr( "Channel" ) );
// Load handle specifics
Type = (char*)HandleConfig->GetChStr( "Type", "TCPclient", true );
if (!strcasecmp( Type, "Serial" ))
{
@@ -142,7 +140,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "LinePrinter" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Port/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
} else {
Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value
@@ -151,7 +149,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "UNIXserver" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
} else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
@@ -161,7 +159,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "UNIXclient" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
} else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
@@ -170,7 +168,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "UDPserver" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else {
@@ -182,7 +180,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "UDPclient" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else {
@@ -194,7 +192,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "TCPserver" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else {
@@ -207,7 +205,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
else if (!strcasecmp( Type, "TCPclient" ))
{
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = Application->AddressList->GetChild( Name ))) {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else {
@@ -243,6 +241,27 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
}
//---------------------------------------------------------------------------
CDataMember * CSelectableCore::GetHandleAddress( THandle * Handle, const char * HandleRef )
{
CDataMember * AddressDef = NULL;
char NamePath[100];
char * Address;
// Handle renamed?
sprintf( NamePath, "Application/Addresses/Rename/%s", HandleRef );
if (!(Address = (char*)Application->Config->GetChStr( NamePath, NULL, false ))) {
Address = (char*)HandleRef;
}
// Get address def
if ((AddressDef = Application->AddressList->GetChild( Address ))) {
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Use address '%s' ('%s')",
ProcessName, Name, Handle->Name, Address, HandleRef );
}
return AddressDef;
}
//---------------------------------------------------------------------------
THandle * CSelectableCore::CreateHandle( const char * HandleName, bool CreateChannel )
{
THandle ** Handle = NULL;
@@ -260,8 +279,7 @@ THandle * CSelectableCore::CreateHandle( const char * HandleName, bool CreateCh
// Set name
if (HandleName) {
(*Handle)->Name = (char*)malloc( strlen(HandleName)+1 );
strcpy( (*Handle)->Name, HandleName );
(*Handle)->Name = strdup( HandleName );
}
// Set File Descriptor
@@ -354,14 +372,9 @@ bool CSelectableCore::SetSerialHandle( THandle * Handle, const char * FileName )
// Set Type
Handle->Type = ctSerial;
// Clear File Name
if (Handle->Path) {
free( Handle->Path );
}
// Set name
Handle->Path = (char*)malloc( strlen(FileName)+1 );
strcpy( Handle->Path, FileName );
// Set path
if (Handle->Path) free( Handle->Path );
Handle->Path = strdup( FileName );
// Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as Port [%s]",
@@ -399,14 +412,9 @@ bool CSelectableCore::SetLinePrinterHandle( THandle * Handle, const char * FileN
// Set Type
Handle->Type = ctLinePrinter;
// Clear File Name
if (Handle->Path) {
free( Handle->Path );
}
// Set name
Handle->Path = (char*)malloc( strlen(FileName)+1 );
strcpy( Handle->Path, FileName );
// Set Path
if (Handle->Path) free( Handle->Path );
Handle->Path = strdup( FileName );
// Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as Port [%s]",
@@ -425,14 +433,9 @@ bool CSelectableCore::SetForkPipeHandle( THandle * Handle, const char * ExecPath
// Set Type
Handle->Type = ctForkPipe;
// Clear File Name
if (Handle->Path) {
free( Handle->Path );
}
// Set name
Handle->Path = (char*)malloc( strlen(ExecPath)+1 );
strcpy( Handle->Path, ExecPath );
// Set path
if (Handle->Path) free( Handle->Path );
Handle->Path = strdup( ExecPath );
// Log event
if (Log) Log->Message( LogLevel, dlMedium, "%s/%s: Handle '%s' - Set as ForkPipe [%s]",
@@ -459,8 +462,7 @@ bool CSelectableCore::SetUnixHandle( THandle * Handle, EConnectType Type, const
}
// Set name
Handle->Path = (char*)malloc( strlen(FileName)+1 );
strcpy( Handle->Path, FileName );
Handle->Path = strdup( FileName );
Handle->Queue = Queue;
// Log event
@@ -492,11 +494,8 @@ bool CSelectableCore::SetSocketHandle( THandle * Handle, EConnectType Type, con
freeaddrinfo( Handle->AddressList );
// Set HostName & Port
Handle->HostName = (char*)malloc( strlen(HostName)+1 );
strcpy( Handle->HostName, HostName );
Handle->PortName = (char*)malloc( strlen(PortName)+1 );
strcpy( Handle->PortName, PortName );
Handle->HostName = strdup( HostName );
Handle->PortName = strdup( PortName );
Handle->AddressList = NULL;
Handle->AddressInfo = NULL;
@@ -597,8 +596,7 @@ bool CSelectableCore::SetInBuffer( THandle * Handle, int InBufSize, int InTimeou
// Set Input Markers
if (InMarkerLen && InMarker) {
Handle->InMarkerLen = InMarkerLen;
Handle->InMarker = (char *)malloc( InMarkerLen+1 );
memcpy( Handle->InMarker, InMarker, InMarkerLen );
Handle->InMarker = strndup( InMarker, InMarkerLen );
Handle->InMarker[InMarkerLen] = 0;
}
@@ -1954,7 +1952,7 @@ bool CSelectableCore::Read( THandle * Handle )
// Read incoming message and address
errno = 0;
UDPbuffer = (char*)malloc( BytesWaiting+1 );
UDPbuffer = (char*)malloc( BytesWaiting+1 );
BytesRead = ReadFromUDP( Handle, (char*)UDPaddress, (char*)UDPport, UDPbuffer, BytesWaiting );
if (!errno && (Handle->Type == ctUDPserver))
@@ -2906,8 +2904,7 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[
// 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] = strndup( StartPos, Len );
Args[Count][Len] = 0;
Count++;
ParamStarted = false;
@@ -2919,8 +2916,7 @@ bool CSelectableCore::BuildArgs( const char * ExecPath, int &Count, char * Args[
// Whitespace ends parameter
if ((*MatchPos == ' ') || (*MatchPos == 0)) {
Len = MatchPos-StartPos;
Args[Count] = (char*)malloc( Len+1 );
strncpy( Args[Count], StartPos, Len );
Args[Count] = strndup( StartPos, Len );
Args[Count][Len] = 0;
Count++;
ParamStarted = false;