Kaynağa Gözat

improve the server discovery

Julius Pfrommer 10 yıl önce
ebeveyn
işleme
cfc09e323f
2 değiştirilmiş dosya ile 59 ekleme ve 46 silme
  1. 21 24
      src/server/ua_server.c
  2. 38 22
      src/server/ua_services_discovery.c

+ 21 - 24
src/server/ua_server.c

@@ -231,30 +231,27 @@ UA_Server * UA_Server_new(void) {
         endpoint->securityMode = UA_MESSAGESECURITYMODE_NONE;
         endpoint->securityPolicyUri = UA_STRING_ALLOC("http://opcfoundation.org/UA/SecurityPolicy#None");
         endpoint->transportProfileUri = UA_STRING_ALLOC("http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary");
-        if(!endpoint->userIdentityTokens) {
-            UA_EndpointDescription_delete(endpoint);
-        } else {
-            endpoint->userIdentityTokensSize = 2;
-        	endpoint->userIdentityTokens = UA_Array_new(&UA_TYPES[UA_TYPES_USERTOKENPOLICY], 2);
-
-            UA_UserTokenPolicy_init(&endpoint->userIdentityTokens[0]);
-            endpoint->userIdentityTokens[0].tokenType = UA_USERTOKENTYPE_ANONYMOUS;
-            endpoint->userIdentityTokens[0].policyId = UA_STRING_ALLOC("my-anonymous-policy"); // defined per server
-
-            UA_UserTokenPolicy_init(&endpoint->userIdentityTokens[1]);
-            endpoint->userIdentityTokens[1].tokenType = UA_USERTOKENTYPE_USERNAME;
-            endpoint->userIdentityTokens[1].policyId = UA_STRING_ALLOC("my-username-policy"); // defined per server
-
-            /* UA_String_copy(endpointUrl, &endpoint->endpointUrl); */
-            /* /\* The standard says "the HostName specified in the Server Certificate is the */
-            /*    same as the HostName contained in the endpointUrl provided in the */
-            /*    EndpointDescription *\/ */
-            /* UA_String_copy(&server->serverCertificate, &endpoint->serverCertificate); */
-            UA_ApplicationDescription_copy(&server->description, &endpoint->server);
-
-            server->endpointDescriptions = endpoint;
-            server->endpointDescriptionsSize = 1;
-        }
+
+        endpoint->userIdentityTokensSize = 2;
+        endpoint->userIdentityTokens = UA_Array_new(&UA_TYPES[UA_TYPES_USERTOKENPOLICY], 2);
+
+        UA_UserTokenPolicy_init(&endpoint->userIdentityTokens[0]);
+        endpoint->userIdentityTokens[0].tokenType = UA_USERTOKENTYPE_ANONYMOUS;
+        endpoint->userIdentityTokens[0].policyId = UA_STRING_ALLOC("my-anonymous-policy"); // defined per server
+
+        UA_UserTokenPolicy_init(&endpoint->userIdentityTokens[1]);
+        endpoint->userIdentityTokens[1].tokenType = UA_USERTOKENTYPE_USERNAME;
+        endpoint->userIdentityTokens[1].policyId = UA_STRING_ALLOC("my-username-policy"); // defined per server
+
+        /* UA_String_copy(endpointUrl, &endpoint->endpointUrl); */
+        /* /\* The standard says "the HostName specified in the Server Certificate is the */
+        /*    same as the HostName contained in the endpointUrl provided in the */
+        /*    EndpointDescription *\/ */
+        /* UA_String_copy(&server->serverCertificate, &endpoint->serverCertificate); */
+        UA_ApplicationDescription_copy(&server->description, &endpoint->server);
+
+        server->endpointDescriptions = endpoint;
+        server->endpointDescriptionsSize = 1;
     }
 
 #define MAXCHANNELCOUNT 100

+ 38 - 22
src/server/ua_services_discovery.c

@@ -2,9 +2,7 @@
 #include "ua_services.h"
 #include "ua_util.h"
 
-void Service_FindServers(UA_Server                    *server,
-                          const UA_FindServersRequest *request,
-                          UA_FindServersResponse      *response) {
+void Service_FindServers(UA_Server *server, const UA_FindServersRequest *request, UA_FindServersResponse *response) {
     response->servers = UA_malloc(sizeof(UA_ApplicationDescription));
     if(!response->servers) {
         response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
@@ -15,41 +13,59 @@ void Service_FindServers(UA_Server                    *server,
         response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
         return;
     }
+    UA_String_deleteMembers(response->servers->discoveryUrls);
+    UA_String_copy(&request->endpointUrl, response->servers->discoveryUrls);
 	response->serversSize = 1;
 }
 
-void Service_GetEndpoints(UA_Server                    *server,
-                          const UA_GetEndpointsRequest *request,
-                          UA_GetEndpointsResponse      *response) {
+void Service_GetEndpoints(UA_Server *server, const UA_GetEndpointsRequest *request, UA_GetEndpointsResponse *response) {
     /* test if the supported binary profile shall be returned */
-    UA_Boolean returnBinary = request->profileUrisSize == 0;
-    for(UA_Int32 i=0;i<request->profileUrisSize;i++) {
-        if(UA_String_equal(&request->profileUris[i], &server->endpointDescriptions->transportProfileUri)) {
-            returnBinary = UA_TRUE;
-            break;
+    UA_Boolean relevant_endpoints[server->endpointDescriptionsSize];
+    size_t relevant_count = 0;
+    for(UA_Int32 j = 0; j < server->endpointDescriptionsSize; j++) {
+        relevant_endpoints[j] = UA_FALSE;
+        if(request->profileUrisSize <= 0) {
+                relevant_endpoints[j] = UA_TRUE;
+                relevant_count++;
+                continue;
+        }
+        for(UA_Int32 i = 0; i < request->profileUrisSize; i++) {
+            if(UA_String_equal(&request->profileUris[i], &server->endpointDescriptions->transportProfileUri)) {
+                relevant_endpoints[j] = UA_TRUE;
+                relevant_count++;
+                break;
+            }
         }
     }
 
-    if(!returnBinary) {
+    if(relevant_count == 0) {
         response->endpointsSize = 0;
         return;
     }
 
-    response->endpoints = UA_malloc(sizeof(UA_EndpointDescription));
+    response->endpoints = UA_malloc(sizeof(UA_EndpointDescription) * relevant_count);
     if(!response->endpoints) {
         response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
         return;
     }
-    if(UA_EndpointDescription_copy(server->endpointDescriptions, response->endpoints) != UA_STATUSCODE_GOOD) {
-        UA_free(response->endpoints);
-        response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
-        return;
+
+    size_t k = 0;
+    UA_StatusCode retval = UA_STATUSCODE_GOOD;
+    for(UA_Int32 j = 0; j < server->endpointDescriptionsSize && retval == UA_STATUSCODE_GOOD; j++) {
+        if(relevant_endpoints[j] != UA_TRUE)
+            continue;
+        retval = UA_copy(&server->endpointDescriptions[j], &response->endpoints[j],
+                         &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]);
+        UA_String_deleteMembers(&response->endpoints[j].endpointUrl);
+        UA_String_copy(&request->endpointUrl, &response->endpoints[j].endpointUrl);
+        k++;
     }
-    UA_String_deleteMembers(&response->endpoints->endpointUrl);
-    if(UA_String_copy(&request->endpointUrl, &response->endpoints->endpointUrl) != UA_STATUSCODE_GOOD) {
-        UA_EndpointDescription_delete(response->endpoints);
-        response->responseHeader.serviceResult = UA_STATUSCODE_BADOUTOFMEMORY;
+
+    if(retval != UA_STATUSCODE_GOOD) {
+        response->responseHeader.serviceResult = retval;
+        UA_Array_delete(response->endpoints, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION], --k);
         return;
     }
-    response->endpointsSize = 1;
+    response->endpointsSize = relevant_count;
 }
+