#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <opcua_proxystub.h>
#include <opcua_trace.h>
#include "demoserver.h"
static char g_szHostname[256] = "";
int GetFQHostname( char *szHostname, int len )
{
struct hostent *pEnt = 0;
int ret = gethostname( szHostname, len );
if ( ret != 0 ) return ret;
pEnt = gethostbyname( szHostname );
if ( pEnt == 0 ) return -1;
OpcUa_StrlCpyA( szHostname, pEnt->h_name, len );
szHostname[len - 1] = 0;
return 0;
}
void PrintUsage(const char *szAppName)
{
printf( "Usage: %s [-h] [-v] [-n <hostname>] [-c <config file>] [-s <shutdown delay>]\n", szAppName );
printf( " -h: Shows this help\n" );
printf( " -v: Shows detailed version information\n" );
printf( " -n: Sets the hostname to bind on\n" );
printf( " -c: Sets the configuration file to use (default=settings.conf)\n" );
printf( " -s: Sets the shutdown delay in seconds (default=1)\n" );
}
int main( int argc, char *argv[] )
{
int ret = EXIT_SUCCESS;
int opt;
char szConfigurationFile[UABASE_PATH_MAX] = "settings.conf";
int shutdownDelay = 1;
while ( ( opt = getopt( argc, argv, "hvn:c:s:" ) ) != -1 )
{
switch ( opt )
{
case 'h':
PrintUsage( argv[0] );
exit( EXIT_SUCCESS );
break;
case 'v':
PrintVersionInfo();
exit( EXIT_SUCCESS );
break;
case 'n':
OpcUa_StrlCpyA( g_szHostname, optarg, sizeof(g_szHostname) );
break;
case 'c':
OpcUa_StrlCpyA( szConfigurationFile, optarg, sizeof(szConfigurationFile) );
break;
case 's':
shutdownDelay = atoi(optarg);
if (shutdownDelay < 0) shutdownDelay = 0;
break;
default:
PrintUsage( argv[0] );
exit( EXIT_FAILURE );
}
}
uStatus = InitializeOpcUaStack();
if (OpcUa_IsNotGood(uStatus)) return EXIT_FAILURE;
if ( g_szHostname[0] == 0 )
{
GetFQHostname( g_szHostname, sizeof( g_szHostname ) );
}
do {
if (OpcUa_IsNotGood(uStatus))
{
printf("Could not open configuration file '%s'\n", szConfigurationFile);
ret = EXIT_FAILURE;
break;
}
uStatus = ServerMain(&g_settings, g_szHostname, shutdownDelay);
} while (uStatus == OpcUa_BadContinue);
if (OpcUa_IsBad(uStatus) && uStatus != OpcUa_BadShutdown && uStatus != OpcUa_BadContinue) {ret = EXIT_FAILURE;}
CleanupOpcUaStack();
return ret;
}