aboutsummaryrefslogtreecommitdiffstats
path: root/src/server_register.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/server_register.c')
-rw-r--r--src/server_register.c89
1 files changed, 61 insertions, 28 deletions
diff --git a/src/server_register.c b/src/server_register.c
index 80bed20..ec5045f 100644
--- a/src/server_register.c
+++ b/src/server_register.c
@@ -22,16 +22,58 @@
#include <string.h>
#include <time.h>
-UA_Boolean running = true;
+volatile UA_Boolean running = true;
static void
stopHandler (int sign)
{
- UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
running = false;
}
/* ========================================================================
+ * LDS Client Config Helper
+ * ======================================================================== */
+
+/**
+ * Parameters for building a client config that connects to the LDS.
+ * Gathered once in main, then reused for every register/deregister call.
+ */
+typedef struct
+{
+ const char *appUri;
+ const char *certPath;
+ const char *keyPath;
+ char **trustPaths;
+ size_t trustSize;
+ UA_MessageSecurityMode securityMode;
+ const char *securityPolicyUri;
+ int logLevel;
+ const char *username;
+ const char *password;
+} LdsClientParams;
+
+/**
+ * Builds a fresh UA_ClientConfig from LdsClientParams.
+ *
+ * UA_Server_registerDiscovery / deregisterDiscovery consume (clear) the
+ * client config, so a new one must be created before every call.
+ */
+static UA_StatusCode
+makeLdsClientConfig (UA_ClientConfig *cc, const LdsClientParams *p)
+{
+ memset (cc, 0, sizeof (UA_ClientConfig));
+ UA_StatusCode rv = createSecureClientConfig (
+ cc, p->appUri, p->certPath, p->keyPath, p->trustPaths, p->trustSize,
+ p->securityMode, p->securityPolicyUri);
+ if (rv != UA_STATUSCODE_GOOD)
+ return rv;
+ cc->logging->context = (void *)(uintptr_t)p->logLevel;
+ if (p->username)
+ UA_ClientConfig_setAuthenticationUsername (cc, p->username, p->password);
+ return UA_STATUSCODE_GOOD;
+}
+
+/* ========================================================================
* Main
* ======================================================================== */
@@ -196,26 +238,32 @@ main (int argc, char **argv)
serverConfig->applicationDescription.applicationType
= UA_APPLICATIONTYPE_SERVER;
+ LdsClientParams ldsParams = {
+ .appUri = clientAppUri,
+ .certPath = clientCertPath,
+ .keyPath = clientKeyPath,
+ .trustPaths = clientTrustPaths,
+ .trustSize = clientTrustSize,
+ .securityMode = securityMode,
+ .securityPolicyUri = securityPolicyUri,
+ .logLevel = logLevel,
+ .username = clientUsername,
+ .password = clientPassword,
+ };
+
/* Use run_startup + manual event loop (instead of UA_Server_run) so we
can periodically re-register with the LDS between iterations. */
UA_Server_run_startup (server);
/* UA_Server_registerDiscovery consumes (clears) the client config,
- so a fresh zero-initialized config is needed for every call. */
+ so makeLdsClientConfig builds a fresh one for every call. */
UA_ClientConfig clientConfig;
- memset (&clientConfig, 0, sizeof (UA_ClientConfig));
- retval = createSecureClientConfig (
- &clientConfig, clientAppUri, clientCertPath, clientKeyPath,
- clientTrustPaths, clientTrustSize, securityMode, securityPolicyUri);
+ retval = makeLdsClientConfig (&clientConfig, &ldsParams);
if (retval != UA_STATUSCODE_GOOD)
{
UA_Server_run_shutdown (server);
goto cleanup;
}
- clientConfig.logging->context = (void *)(uintptr_t)logLevel;
- if (clientUsername)
- UA_ClientConfig_setAuthenticationUsername (&clientConfig, clientUsername,
- clientPassword);
UA_String discoveryUrl = UA_STRING_ALLOC (discoveryEndpoint);
retval = UA_Server_registerDiscovery (server, &clientConfig, discoveryUrl,
@@ -237,17 +285,9 @@ main (int argc, char **argv)
time_t now = time (NULL);
if (now - lastRegister >= registerInterval)
{
- memset (&clientConfig, 0, sizeof (UA_ClientConfig));
- retval = createSecureClientConfig (&clientConfig, clientAppUri,
- clientCertPath, clientKeyPath,
- clientTrustPaths, clientTrustSize,
- securityMode, securityPolicyUri);
+ retval = makeLdsClientConfig (&clientConfig, &ldsParams);
if (retval == UA_STATUSCODE_GOOD)
{
- clientConfig.logging->context = (void *)(uintptr_t)logLevel;
- if (clientUsername)
- UA_ClientConfig_setAuthenticationUsername (
- &clientConfig, clientUsername, clientPassword);
UA_String reregUrl = UA_STRING_ALLOC (discoveryEndpoint);
retval = UA_Server_registerDiscovery (server, &clientConfig,
reregUrl, UA_STRING_NULL);
@@ -263,16 +303,9 @@ main (int argc, char **argv)
/* Deregister from the LDS before shutting down so the LDS removes
our entry immediately rather than waiting for the cleanup timeout. */
- memset (&clientConfig, 0, sizeof (UA_ClientConfig));
- retval = createSecureClientConfig (
- &clientConfig, clientAppUri, clientCertPath, clientKeyPath,
- clientTrustPaths, clientTrustSize, securityMode, securityPolicyUri);
+ retval = makeLdsClientConfig (&clientConfig, &ldsParams);
if (retval == UA_STATUSCODE_GOOD)
{
- clientConfig.logging->context = (void *)(uintptr_t)logLevel;
- if (clientUsername)
- UA_ClientConfig_setAuthenticationUsername (
- &clientConfig, clientUsername, clientPassword);
UA_String deregUrl = UA_STRING_ALLOC (discoveryEndpoint);
retval = UA_Server_deregisterDiscovery (server, &clientConfig, deregUrl);
UA_String_clear (&deregUrl);