Bladeren bron

Stack: Refactor server config to separated discovery and mDNS settings

Stefan Profanter 5 jaren geleden
bovenliggende
commit
f2a151bc01

+ 9 - 4
examples/discovery/server_lds.c

@@ -24,17 +24,22 @@ int main(void) {
     UA_Server *server = UA_Server_new();
     UA_ServerConfig *config = UA_Server_getConfig(server);
     UA_ServerConfig_setDefault(config);
-    
+
+    // NOTE:
+    // A server instance defined as DISCOVERYSERVER will not be shown in UaExpert.
+    // See also:
+	// https://forum.unified-automation.com/topic1987.html
+
     config->applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER;
     UA_String_clear(&config->applicationDescription.applicationUri);
     config->applicationDescription.applicationUri =
             UA_String_fromChars("urn:open62541.example.local_discovery_server");
-    config->mdnsServerName = UA_String_fromChars("LDS");
+    config->discovery.mdns.mdnsServerName = UA_String_fromChars("LDS");
     // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
-    config->serverCapabilitiesSize = 1;
+    config->discovery.mdns.serverCapabilitiesSize = 1;
     UA_String *caps = UA_String_new();
     *caps = UA_String_fromChars("LDS");
-    config->serverCapabilities = caps;
+    config->discovery.mdns.serverCapabilities = caps;
 
     /* timeout in seconds when to automatically remove a registered server from
      * the list, if it doesn't re-register within the given time frame. A value

+ 6 - 58
examples/discovery/server_multicast.c

@@ -27,43 +27,6 @@ static void stopHandler(int sign) {
     running = false;
 }
 
-static UA_StatusCode
-readInteger(UA_Server *server, const UA_NodeId *sessionId,
-            void *sessionContext, const UA_NodeId *nodeId,
-            void *nodeContext, UA_Boolean includeSourceTimeStamp,
-            const UA_NumericRange *range, UA_DataValue *value) {
-    UA_Int32 *myInteger = (UA_Int32*)nodeContext;
-    value->hasValue = true;
-    UA_Variant_setScalarCopy(&value->value, myInteger, &UA_TYPES[UA_TYPES_INT32]);
-
-    // we know the nodeid is a string
-    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Node read %.*s",
-                (int)nodeId->identifier.string.length,
-                nodeId->identifier.string.data);
-    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
-                "read value %i", *(UA_UInt32 *)myInteger);
-    return UA_STATUSCODE_GOOD;
-}
-
-static UA_StatusCode
-writeInteger(UA_Server *server, const UA_NodeId *sessionId,
-             void *sessionContext, const UA_NodeId *nodeId,
-             void *nodeContext, const UA_NumericRange *range,
-             const UA_DataValue *value) {
-    UA_Int32 *myInteger = (UA_Int32*)nodeContext;
-    if(value->hasValue && UA_Variant_isScalar(&value->value) &&
-       value->value.type == &UA_TYPES[UA_TYPES_INT32] && value->value.data)
-        *myInteger = *(UA_Int32 *)value->value.data;
-
-    // we know the nodeid is a string
-    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Node written %.*s",
-                (int)nodeId->identifier.string.length,
-                nodeId->identifier.string.data);
-    UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
-                "written value %i", *(UA_UInt32 *)myInteger);
-    return UA_STATUSCODE_GOOD;
-}
-
 char *discovery_url = NULL;
 
 static void
@@ -278,34 +241,19 @@ int main(int argc, char **argv) {
     UA_String_clear(&config->applicationDescription.applicationUri);
     config->applicationDescription.applicationUri =
         UA_String_fromChars("urn:open62541.example.server_multicast");
-    config->mdnsServerName = UA_String_fromChars("Sample Multicast Server");
+    config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Multicast Server");
     // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
     //config.serverCapabilitiesSize = 1;
     //UA_String caps = UA_String_fromChars("LDS");
     //config.serverCapabilities = ∩︀
 
-    /* add a variable node to the address space */
-    UA_Int32 myInteger = 42;
-    UA_NodeId myIntegerNodeId = UA_NODEID_STRING(1, "the.answer");
-    UA_QualifiedName myIntegerName = UA_QUALIFIEDNAME(1, "the answer");
-    UA_DataSource dateDataSource;
-    dateDataSource.read = readInteger;
-    dateDataSource.write = writeInteger;
-    UA_VariableAttributes attr = UA_VariableAttributes_default;
-    attr.description = UA_LOCALIZEDTEXT("en-US", "the answer");
-    attr.displayName = UA_LOCALIZEDTEXT("en-US", "the answer");
-
-    UA_Server_addDataSourceVariableNode(server, myIntegerNodeId,
-                                        UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
-                                        UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES),
-                                        myIntegerName, UA_NODEID_NULL, attr, dateDataSource,
-                                        &myInteger, NULL);
-
-    // callback which is called when a new server is detected through mDNS
-    UA_Server_setServerOnNetworkCallback(server, serverOnNetworkCallback, NULL);
-
     // Start the server and call iterate to wait for the multicast discovery of the LDS
     UA_StatusCode retval = UA_Server_run_startup(server);
+
+	// callback which is called when a new server is detected through mDNS
+	// needs to be set after UA_Server_run_startup or UA_Server_run
+	UA_Server_setServerOnNetworkCallback(server, serverOnNetworkCallback, NULL);
+
     if(retval != UA_STATUSCODE_GOOD) {
         UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER,
                      "Could not start the server. StatusCode %s",

+ 1 - 1
examples/discovery/server_register.c

@@ -72,7 +72,7 @@ int main(int argc, char **argv) {
     UA_String_clear(&config->applicationDescription.applicationUri);
     config->applicationDescription.applicationUri =
         UA_String_fromChars("urn:open62541.example.server_register");
-    config->mdnsServerName = UA_String_fromChars("Sample Server");
+    config->discovery.mdns.mdnsServerName = UA_String_fromChars("Sample Server");
     // See http://www.opcfoundation.org/UA/schemas/1.03/ServerCapabilities.csv
     //config.serverCapabilitiesSize = 1;
     //UA_String caps = UA_String_fromChars("LDS");

+ 20 - 14
include/open62541/server_config.h

@@ -60,6 +60,25 @@ typedef struct {
     UA_Duration max;
 } UA_DurationRange;
 
+#ifdef UA_ENABLE_DISCOVERY
+typedef struct {
+
+	/* Timeout in seconds when to automatically remove a registered server from
+	 * the list, if it doesn't re-register within the given time frame. A value
+	 * of 0 disables automatic removal. Default is 60 Minutes (60*60). Must be
+	 * bigger than 10 seconds, because cleanup is only triggered approximately
+	 * every 10 seconds. The server will still be removed depending on the
+	 * state of the semaphore file. */
+	UA_UInt32 cleanupTimeout;
+
+#ifdef UA_ENABLE_DISCOVERY_MULTICAST
+	UA_MdnsDiscoveryConfiguration mdns;
+#endif
+
+} UA_ServerConfig_Discovery;
+
+#endif
+
 struct UA_ServerConfig {
     UA_UInt16 nThreads; /* only if multithreading is enabled */
     UA_Logger logger;
@@ -73,13 +92,6 @@ struct UA_ServerConfig {
     UA_RuleHandling verifyRequestTimestamp; /* Verify that the server sends a
                                              * timestamp in the request header */
 
-    /* MDNS Discovery */
-#ifdef UA_ENABLE_DISCOVERY
-    UA_String mdnsServerName;
-    size_t serverCapabilitiesSize;
-    UA_String *serverCapabilities;
-#endif
-
     /* Custom DataTypes. Attention! Custom datatypes are not cleaned up together
      * with the configuration. So it is possible to allocate them on ROM. */
     const UA_DataTypeArray *customDataTypes;
@@ -173,13 +185,7 @@ struct UA_ServerConfig {
 
     /* Discovery */
 #ifdef UA_ENABLE_DISCOVERY
-    /* Timeout in seconds when to automatically remove a registered server from
-     * the list, if it doesn't re-register within the given time frame. A value
-     * of 0 disables automatic removal. Default is 60 Minutes (60*60). Must be
-     * bigger than 10 seconds, because cleanup is only triggered approximately
-     * every 10 seconds. The server will still be removed depending on the
-     * state of the semaphore file. */
-    UA_UInt32 discoveryCleanupTimeout;
+	UA_ServerConfig_Discovery discovery;
 #endif
 
 #ifdef UA_ENABLE_SUBSCRIPTIONS

+ 3 - 5
plugins/ua_config_default.c

@@ -133,10 +133,8 @@ setDefaultConfig(UA_ServerConfig *conf) {
     /* conf->applicationDescription.discoveryUrlsSize = 0; */
     /* conf->applicationDescription.discoveryUrls = NULL; */
 
-#ifdef UA_ENABLE_DISCOVERY
-    /* conf->mdnsServerName = UA_STRING_NULL; */
-    /* conf->serverCapabilitiesSize = 0; */
-    /* conf->serverCapabilities = NULL; */
+#ifdef UA_ENABLE_DISCOVERY_MULTICAST
+    UA_MdnsDiscoveryConfiguration_init(&conf->discovery.mdns);
 #endif
 
     /* Custom DataTypes */
@@ -194,7 +192,7 @@ setDefaultConfig(UA_ServerConfig *conf) {
     conf->queueSizeLimits = UA_UINT32RANGE(1, 100);
 
 #ifdef UA_ENABLE_DISCOVERY
-    conf->discoveryCleanupTimeout = 60 * 60;
+    conf->discovery.cleanupTimeout = 60 * 60;
 #endif
 
 #ifdef UA_ENABLE_HISTORIZING

+ 2 - 6
src/server/ua_server_config.c

@@ -15,12 +15,8 @@ UA_ServerConfig_clean(UA_ServerConfig *config) {
     /* Server Description */
     UA_BuildInfo_deleteMembers(&config->buildInfo);
     UA_ApplicationDescription_deleteMembers(&config->applicationDescription);
-#ifdef UA_ENABLE_DISCOVERY
-    UA_String_deleteMembers(&config->mdnsServerName);
-    UA_Array_delete(config->serverCapabilities, config->serverCapabilitiesSize,
-                    &UA_TYPES[UA_TYPES_STRING]);
-    config->serverCapabilities = NULL;
-    config->serverCapabilitiesSize = 0;
+#ifdef UA_ENABLE_DISCOVERY_MULTICAST
+    UA_MdnsDiscoveryConfiguration_clear(&config->discovery.mdns);
 #endif
 
     /* Custom DataTypes */

+ 2 - 8
src/server/ua_server_discovery.c

@@ -61,19 +61,13 @@ register_server_with_discovery_server(UA_Server *server,
         request.server.discoveryUrls[config_discurls + i] = nl->discoveryUrl;
     }
 
-    UA_MdnsDiscoveryConfiguration mdnsConfig;
-    UA_MdnsDiscoveryConfiguration_init(&mdnsConfig);
-
     request.discoveryConfigurationSize = 1;
     request.discoveryConfiguration = UA_ExtensionObject_new();
     UA_ExtensionObject_init(&request.discoveryConfiguration[0]);
+    // Set to NODELETE so that we can just use a pointer to the mdns config
     request.discoveryConfiguration[0].encoding = UA_EXTENSIONOBJECT_DECODED_NODELETE;
     request.discoveryConfiguration[0].content.decoded.type = &UA_TYPES[UA_TYPES_MDNSDISCOVERYCONFIGURATION];
-    request.discoveryConfiguration[0].content.decoded.data = &mdnsConfig;
-
-    mdnsConfig.mdnsServerName = server->config.mdnsServerName;
-    mdnsConfig.serverCapabilities = server->config.serverCapabilities;
-    mdnsConfig.serverCapabilitiesSize = server->config.serverCapabilitiesSize;
+    request.discoveryConfiguration[0].content.decoded.data = &server->config.discovery.mdns;
 
     // First try with RegisterServer2, if that isn't implemented, use RegisterServer
     UA_RegisterServer2Response response;

+ 3 - 3
src/server/ua_services_discovery.c

@@ -492,8 +492,8 @@ void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic) {
     UA_DateTime timedOut = nowMonotonic;
     // registration is timed out if lastSeen is older than 60 minutes (default
     // value, can be modified by user).
-    if(server->config.discoveryCleanupTimeout)
-        timedOut -= server->config.discoveryCleanupTimeout*UA_DATETIME_SEC;
+    if(server->config.discovery.cleanupTimeout)
+        timedOut -= server->config.discovery.cleanupTimeout*UA_DATETIME_SEC;
 
     registeredServer_list_entry* current, *temp;
     LIST_FOREACH_SAFE(current, &server->discoveryManager.registeredServers, pointers, temp) {
@@ -517,7 +517,7 @@ void UA_Discovery_cleanupTimedOut(UA_Server *server, UA_DateTime nowMonotonic) {
         }
 #endif
 
-        if(semaphoreDeleted || (server->config.discoveryCleanupTimeout &&
+        if(semaphoreDeleted || (server->config.discovery.cleanupTimeout &&
                                 current->lastSeen < timedOut)) {
             if(semaphoreDeleted) {
                 UA_LOG_INFO(&server->config.logger, UA_LOGCATEGORY_SERVER,

+ 4 - 4
src/server/ua_services_discovery_multicast.c

@@ -90,13 +90,13 @@ addMdnsRecordForNetworkLayer(UA_Server *server, const UA_String *appName,
     }
     UA_Discovery_addRecord(server, appName, &hostname, port,
                            &path, UA_DISCOVERY_TCP, true,
-                           server->config.serverCapabilities,
-                           &server->config.serverCapabilitiesSize);
+                           server->config.discovery.mdns.serverCapabilities,
+                           &server->config.discovery.mdns.serverCapabilitiesSize);
     return UA_STATUSCODE_GOOD;
 }
 
 void startMulticastDiscoveryServer(UA_Server *server) {
-    UA_String *appName = &server->config.mdnsServerName;
+    UA_String *appName = &server->config.discovery.mdns.mdnsServerName;
     for(size_t i = 0; i < server->config.networkLayersSize; i++)
         addMdnsRecordForNetworkLayer(server, appName, &server->config.networkLayers[i]);
 
@@ -113,7 +113,7 @@ stopMulticastDiscoveryServer(UA_Server *server) {
     char hostname[256];
     if(UA_gethostname(hostname, 255) == 0) {
         UA_String hnString = UA_STRING(hostname);
-        UA_Discovery_removeRecord(server, &server->config.mdnsServerName,
+        UA_Discovery_removeRecord(server, &server->config.discovery.mdns.mdnsServerName,
                                   &hnString, 4840, true);
     } else {
         UA_LOG_ERROR(&server->config.logger, UA_LOGCATEGORY_SERVER,

+ 5 - 5
tests/server/check_discovery.c

@@ -53,12 +53,12 @@ static void setup_lds(void) {
     UA_LocalizedText_deleteMembers(&config_lds->applicationDescription.applicationName);
     config_lds->applicationDescription.applicationName
         = UA_LOCALIZEDTEXT_ALLOC("en", "LDS Server");
-    config_lds->mdnsServerName = UA_String_fromChars("LDS_test");
-    config_lds->serverCapabilitiesSize = 1;
+    config_lds->discovery.mdns.mdnsServerName = UA_String_fromChars("LDS_test");
+    config_lds->discovery.mdns.serverCapabilitiesSize = 1;
     UA_String *caps = UA_String_new();
     *caps = UA_String_fromChars("LDS");
-    config_lds->serverCapabilities = caps;
-    config_lds->discoveryCleanupTimeout = registerTimeout;
+    config_lds->discovery.mdns.serverCapabilities = caps;
+    config_lds->discovery.cleanupTimeout = registerTimeout;
 
     UA_Server_run_startup(server_lds);
     THREAD_CREATE(server_thread_lds, serverloop_lds);
@@ -103,7 +103,7 @@ static void setup_register(void) {
     UA_LocalizedText_deleteMembers(&config_register->applicationDescription.applicationName);
     config_register->applicationDescription.applicationName =
         UA_LOCALIZEDTEXT_ALLOC("de", "Anmeldungsserver");
-    config_register->mdnsServerName = UA_String_fromChars("Register_test");
+    config_register->discovery.mdns.mdnsServerName = UA_String_fromChars("Register_test");
 
     UA_Server_run_startup(server_register);
     THREAD_CREATE(server_thread_register, serverloop_register);