Merge branch 'MessageServer'

This commit is contained in:
Charl Wentzel
2019-07-15 17:41:04 +02:00
7 changed files with 86 additions and 86 deletions

View File

@@ -371,6 +371,40 @@ bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned cha
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
char const * BuildDateStr( unsigned char Day, unsigned char Month, unsigned Year, const char * DateSeparator )
{
// Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d",
Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day );
// Return value
return (ReturnStr);
}
//---------------------------------------------------------------------------
char const * BuildTimeStr( unsigned char Hours, unsigned char Minutes, unsigned char Seconds, const char * TimeSeparator )
{
// Build String
sprintf( ReturnStr, "%02d%s%02d%s%02d",
Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds );
return (ReturnStr);
}
//---------------------------------------------------------------------------
char const * BuildDateTimeStr( unsigned char Day, unsigned char Month, unsigned Year,
unsigned char Hours, unsigned char Minutes, unsigned char Seconds,
const char * DateSeparator, const char * TimeSeparator )
{
// Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d",
Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day,
Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds );
return (ReturnStr);
}
//---------------------------------------------------------------------------
// Get the current date in a string // Get the current date in a string
char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator ) char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator )
{ {
@@ -382,11 +416,7 @@ char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char *
ReadDate( EpochTime, LocalTime, Day, Month, Year ); ReadDate( EpochTime, LocalTime, Day, Month, Year );
// Build String // Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d", return BuildTimeStr( Year, Month, Day, DateSeparator );
Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day );
// Return value
return (ReturnStr);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -400,10 +430,7 @@ char const * BuildTimeStr( const time_t EpochTime, bool LocalTime, const char *
ReadTime( EpochTime, LocalTime, Hours, Minutes, Seconds ); ReadTime( EpochTime, LocalTime, Hours, Minutes, Seconds );
// Build String // Build String
sprintf( ReturnStr, "%02d%s%02d%s%02d", return BuildTimeStr( Hours, Minutes, Seconds, TimeSeparator );
Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds );
return (ReturnStr);
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
@@ -420,9 +447,5 @@ char const * BuildDateTimeStr( const time_t EpochTime, bool LocalTime, const cha
ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds ); ReadDateTime( EpochTime, LocalTime, Day, Month, Year, Hours, Minutes, Seconds );
// Build String // Build String
sprintf( ReturnStr, "%04d%s%02d%s%02d %02d%s%02d%s%02d", return BuildDateTimeStr( Year, Month, Day, Hours, Minutes, Seconds, DateSeparator, TimeSeparator );
Year, ((DateSeparator)? DateSeparator : ""), Month, ((DateSeparator)? DateSeparator : ""), Day,
Hours, ((TimeSeparator)? TimeSeparator : ""), Minutes, ((TimeSeparator)? TimeSeparator : ""), Seconds );
return (ReturnStr);
} }

View File

@@ -39,10 +39,16 @@ bool ReadDate( const time_t EpochTime, bool LocalTime, unsigned char &Day, un
bool ReadDateTime( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year, bool ReadDateTime( const time_t EpochTime, bool LocalTime, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds ); unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds );
bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ); bool ReadTimeStr( const char *DateTimeStr, unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime = false );
bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year ); bool ReadDateStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year );
bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year, bool ReadDateTimeStr( const char *DateTimeStr, unsigned char &Day, unsigned char &Month, unsigned &Year,
unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime ); unsigned char &Hours, unsigned char &Minutes, unsigned char &Seconds, bool Maxtime = false );
char const * BuildDateStr( unsigned char Day, unsigned char Month, unsigned Year, const char * DateSeparator = "/" );
char const * BuildTimeStr( unsigned char Hours, unsigned char Minutes, unsigned char Seconds, const char * TimeSeparator = ":" );
char const * BuildDateTimeStr( unsigned char Day, unsigned char Month, unsigned Year,
unsigned char Hours, unsigned char Minutes, unsigned char Seconds,
const char * DateSeparator = "/", const char * TimeSeparator = ":" );
char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator = "/" ); char const * BuildDateStr( const time_t EpochTime, bool LocalTime, const char * DateSeparator = "/" );
char const * BuildTimeStr( const time_t EpochTime, bool LocalTime, const char * TimeSeparator = ":" ); char const * BuildTimeStr( const time_t EpochTime, bool LocalTime, const char * TimeSeparator = ":" );

View File

@@ -18,8 +18,8 @@
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
// Enumarate Types // Enumarate Types
typedef enum { CH_off = 0, CH_wait = 1, CH_ready = 2 } EChannelState; typedef enum { CH_off = 0, CH_standby = 1, CH_wait = 2, CH_ready = 3 } EChannelState;
const char ChannelStateName[][15] = { "Off", "Waiting", "Ready" }; const char ChannelStateName[][15] = { "Off", "Standby", "Waiting", "Ready" };
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------

View File

@@ -8,6 +8,7 @@
// Standard C/C++ Libraries // Standard C/C++ Libraries
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
// redA Libraries // redA Libraries
#include "JSONparseCore.h" #include "JSONparseCore.h"
@@ -145,7 +146,7 @@ bool CJSONparse::WriteToFile( const char * BasePath, const char * FilePath, cons
// Open file // Open file
if ((Handle = open( FilePath, O_CREAT|O_WRONLY|O_TRUNC, 0660 )) < 0) { if ((Handle = open( FilePath, O_CREAT|O_WRONLY|O_TRUNC, 0660 )) < 0) {
Error = true; Error = true;
sprintf( ErrorText, "Could not open file" ); sprintf( ErrorText, "Could not open file - [%d] %s", errno, strerror(errno) );
return false; return false;
} }
@@ -243,7 +244,7 @@ bool CJSONparse::ReadFromFile( const char * BasePath, const char * FilePath )
// Open file // Open file
if ((Handle = open( FilePath, O_RDONLY )) < 0) { if ((Handle = open( FilePath, O_RDONLY )) < 0) {
Error = true; Error = true;
sprintf( ErrorText, "Could not open file" ); sprintf( ErrorText, "Could not open file - [%d] %s", errno, strerror(errno) );
return false; return false;
} }

View File

@@ -175,7 +175,7 @@ bool CSelectableBare::HandleState( THandle * Handle, EConnectState State )
if (!Handle || (Handle->State == State)) if (!Handle || (Handle->State == State))
return false; return false;
// Set Call back // Execute call back for state
if (Handle->StateCallback[ (int)State ]) if (Handle->StateCallback[ (int)State ])
(Handle->StateCallback[ (int)State ])( this, Handle, State ); (Handle->StateCallback[ (int)State ])( this, Handle, State );
@@ -188,6 +188,8 @@ bool CSelectableBare::HandleState( THandle * Handle, EConnectState State )
ChannelState = CH_wait; ChannelState = CH_wait;
else if ((Handle->State == csOpen) || (Handle->State == csDataWaiting)) else if ((Handle->State == csOpen) || (Handle->State == csDataWaiting))
ChannelState = CH_ready; ChannelState = CH_ready;
else if (Handle->AutoManage)
ChannelState = CH_standby;
else else
ChannelState = CH_off; ChannelState = CH_off;
if (Handle->Channel->InState != ChannelState) if (Handle->Channel->InState != ChannelState)
@@ -677,9 +679,9 @@ int CSelectableBare::Input( const char * ChannelName, const char * SourceRef, co
ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName );
return 0; return 0;
} }
else if (Channel->InState != CH_ready) { else if (Channel->InState == CH_off) {
// Channel disabled // Channel disabled
if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel not Ready", if (Log) Log->Message( LogLevel, dlHigh, "%s/%s: Channel '%s'->'%s' - Input rejected, Channel off",
ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName ); ProcessName, Name, ((SourceRef && *SourceRef)? SourceRef : "(Any)"), ChannelName );
return 0; return 0;
} }
@@ -822,6 +824,10 @@ bool CSelectableBare::Process()
Open( Handle ); Open( Handle );
} }
} }
else if (Handle->Channel && (Handle->Channel->InState == CH_off) && Handle->AutoManage) {
// Set channel to standby
SetChannelInState( Handle->Channel, CH_standby );
}
// Check Input buffers // Check Input buffers
if (Handle->InBuffer && (Handle->InBuffer->Len() > 0)) { if (Handle->InBuffer && (Handle->InBuffer->Len() > 0)) {

View File

@@ -114,6 +114,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
short Parity; short Parity;
short FlowCtrl; short FlowCtrl;
short Queue; short Queue;
bool Persitent;
// Call Previous load config // Call Previous load config
if (!CFunctionCore::Init( FunctionConfig )) if (!CFunctionCore::Init( FunctionConfig ))
@@ -170,6 +171,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
SerialConfig->GetChInt( "DataBits", 8, true ), SerialConfig->GetChInt( "DataBits", 8, true ),
Parity, SerialConfig->GetChInt( "StopBits", 1, true ), Parity, SerialConfig->GetChInt( "StopBits", 1, true ),
FlowCtrl, SerialConfig->GetChInt( "DataWait", 0, true )); FlowCtrl, SerialConfig->GetChInt( "DataWait", 0, true ));
Persitent = true;
} }
} }
else if (!strcasecmp( Type, "LinePrinter" )) else if (!strcasecmp( Type, "LinePrinter" ))
@@ -180,6 +182,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value Address = (char*)HandleConfig->GetChStr( "Port/Address", NULL, true ); // Get default value
} }
SetLinePrinterHandle( Handle, Address ); SetLinePrinterHandle( Handle, Address );
Persitent = true;
} }
else if (!strcasecmp( Type, "UNIXserver" )) else if (!strcasecmp( Type, "UNIXserver" ))
{ {
@@ -190,6 +193,7 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
} }
Queue = HandleConfig->GetChInt( "Socket/Queue", 2, true ); Queue = HandleConfig->GetChInt( "Socket/Queue", 2, true );
SetUnixHandle( Handle, ctUNIXserver, Address, Queue ); SetUnixHandle( Handle, ctUNIXserver, Address, Queue );
Persitent = true;
} }
else if (!strcasecmp( Type, "UNIXclient" )) else if (!strcasecmp( Type, "UNIXclient" ))
{ {
@@ -199,69 +203,75 @@ bool CSelectableCore::Init( CDataMember * FunctionConfig )
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
} }
SetUnixHandle( Handle, ctUNIXclient, Address, 0 ); SetUnixHandle( Handle, ctUNIXclient, Address, 0 );
Persitent = false;
} }
else if (!strcasecmp( Type, "UDPserver" )) else if (!strcasecmp( Type, "UDPserver" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value
} }
SetSocketHandle( Handle, ctUDPserver, Address, strlcase(Port), 0 ); SetSocketHandle( Handle, ctUDPserver, Address, strlcase(Port), 0 );
Persitent = true;
} }
else if (!strcasecmp( Type, "UDPclient" )) else if (!strcasecmp( Type, "UDPclient" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value
} }
SetSocketHandle( Handle, ctUDPclient, Address, strlcase(Port), 0 ); SetSocketHandle( Handle, ctUDPclient, Address, strlcase(Port), 0 );
Persitent = false;
} }
else if (!strcasecmp( Type, "TCPserver" )) else if (!strcasecmp( Type, "TCPserver" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value
} }
Queue = HandleConfig->GetChInt( "Socket/Queue", 2, true ); Queue = HandleConfig->GetChInt( "Socket/Queue", 2, true );
SetSocketHandle( Handle, ctTCPserver, Address, strlcase(Port), Queue ); SetSocketHandle( Handle, ctTCPserver, Address, strlcase(Port), Queue );
Persitent = true;
} }
else if (!strcasecmp( Type, "TCPclient" )) else if (!strcasecmp( Type, "TCPclient" ))
{ {
if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) { if ((Name = (char*)HandleConfig->GetChStr( "Socket/Name", NULL )) && (AddressDef = GetHandleAddress( Handle, Name ))) {
Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value Address = (char*)AddressDef->GetChStr( "Address", NULL, true ); // Get address list value
Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value Port = (char*)AddressDef->GetChStr( "Port", "0", true ); // Get AddressList Port value
} else { } else {
Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value Address = (char*)HandleConfig->GetChStr( "Socket/Address", NULL, true ); // Get default Address value
Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value Port = (char*)HandleConfig->GetChStr( "Socket/Port", "0", true ); // Get default Port value
} }
SetSocketHandle( Handle, ctTCPclient, Address, strlcase(Port), 0 ); SetSocketHandle( Handle, ctTCPclient, Address, strlcase(Port), 0 );
Persitent = false;
} }
else if (!strcasecmp( Type, "ForkPipe" )) { else if (!strcasecmp( Type, "ForkPipe" )) {
Address = (char*)HandleConfig->GetChStr( "Fork/ExecPath", NULL, true ); // Get default value Address = (char*)HandleConfig->GetChStr( "Fork/ExecPath", NULL, true ); // Get default value
SetForkPipeHandle( Handle, Address ); SetForkPipeHandle( Handle, Address );
Persitent = true;
} }
// Set Auto Manage // Set Auto Manage
SetAutoManage( Handle, HandleConfig->GetChBool( "AutoManage/Enabled", true, true ), SetAutoManage( Handle, HandleConfig->GetChBool( "AutoManage/Enabled", true, true ),
HandleConfig->GetChBool( "AutoManage/Persistent", false, true ), HandleConfig->GetChBool( "AutoManage/Persistent", Persitent, true ),
HandleConfig->GetChInt( "AutoManage/ReopenDelay", 2000, true ), HandleConfig->GetChInt( "AutoManage/ReopenDelay", 2000, true ),
HandleConfig->GetChInt( "AutoManage/CloseTimeout", 2000, true )); HandleConfig->GetChInt( "AutoManage/CloseTimeout", 2000, true ));
// Input buffer // Input buffer
SetInBuffer( Handle, HandleConfig->GetChInt( "InputBuffer/Size", 0 ), SetInBuffer( Handle, HandleConfig->GetChInt( "InputBuffer/Size", 0 ),
HandleConfig->GetChInt( "InputBuffer/Timeout", 250 ), HandleConfig->GetChInt( "InputBuffer/Timeout", 250 ),
HandleConfig->GetChStr( "InputBuffer/Marker", "" ), HandleConfig->GetChStr( "InputBuffer/Marker", "" ),
HandleConfig->GetChInt( "InputBuffer/MarkerLen", 0 ) ); HandleConfig->GetChInt( "InputBuffer/MarkerLen", 0 ) );
SetOutBuffer( Handle, HandleConfig->GetChInt( "OutputBuffer/Size", 0 ) ); SetOutBuffer( Handle, HandleConfig->GetChInt( "OutputBuffer/Size", 0 ) );
// Next // Next
HandleConfig = HandleConfig->GetNextPeer(); HandleConfig = HandleConfig->GetNextPeer();
@@ -2330,51 +2340,6 @@ int CSelectableCore::OutputHandle( THandle * Handle, const char * Data, int Len
} }
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
bool CSelectableCore::Process()
{
THandle * Handle = NULL;
// Process all Handles
Handle = FirstHandle;
while (Handle)
{
// Auto manage handles
if (Handle->State == csPrepared) {
// Proceed to open
Open( Handle );
}
else if ((Handle->State != csOpen) && Handle->AutoManage && Handle->Persistent) {
// Try to re-open port after delay
if (Timeout( Handle->LastAction, Handle->ReopenDelay )) {
Open( Handle );
}
}
// Check Input buffers
if (Handle->InBuffer && (Handle->InBuffer->Len() > 0)) {
// Check duration since last PortIn
if (Timeout( Handle->InStart, Handle->InTimeout )) {
// Process Input
ProcessInputBuffer( Handle, true );
// Reset timer
ClearStartTime( &(Handle->InStart) );
}
}
// Check for auto close (but not on servers)
if ((Handle->State == csOpen) && Handle->AutoManage && !Handle->Persistent && (Handle->Type != ctTCPserver) && (Handle->Type != ctUNIXserver)) {
// Close port after timeout
if (Timeout( Handle->LastAction, Handle->CloseTimeout )) {
Close( Handle, true );
}
}
Handle = Handle->Next;
}
return true;
}
//---------------------------------------------------------------------------
// Set serial port configuration parameters // Set serial port configuration parameters
bool CSelectableCore::WriteSerialConfig( THandle * Handle ) bool CSelectableCore::WriteSerialConfig( THandle * Handle )
{ {

View File

@@ -365,7 +365,6 @@ public:
// Function Interface // Function Interface
virtual int OutputHandle( THandle * Handle, const char * Data, int Len ); virtual int OutputHandle( THandle * Handle, const char * Data, int Len );
virtual bool Process();
friend void ResolveHandler( int Signal, siginfo_t * SignalInfo, void * Context ); friend void ResolveHandler( int Signal, siginfo_t * SignalInfo, void * Context );
}; };