FindServers (C)
- Once you have the server, use GetEndpoints List the available endpoints for this server.
- Then use Configuration and Creation True Connection.
API
OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Discovery_FindServers(
const char* endpoint_url,
uint32_t timeout_ms,
DarraUa_FindServersResult** out_result);
OPCUA_API uint32_t OPCUA_CALL
DarraUa_FindServersResult_GetCount(const DarraUa_FindServersResult* r);
OPCUA_API const DarraUa_ApplicationDescription* OPCUA_CALL
DarraUa_FindServersResult_GetServer(
const DarraUa_FindServersResult* r, uint32_t index);
OPCUA_API void OPCUA_CALL
DarraUa_FindServersResult_Delete(DarraUa_FindServersResult* r);
Query the Local Discovery Server (LDS) for all OPC UA Server. registered within the same domain.
ApplicationDescription Field
typedef struct {
char* application_uri;
char* product_uri;
char* application_name; /* LocalizedText.text */
int32_t application_type; /* 0=Server 1=Client 2=ClientAndServer 3=DiscoveryServer */
char* gateway_server_uri;
char* discovery_profile_uri;
int32_t discovery_url_count;
char** discovery_urls;
} DarraUa_ApplicationDescription;
All pointer fields are FindServers allocated internally by Stack and are released uniformly DarraUa_FindServersResult_Delete by Stack.
Complete Example
#include <opcua/opcua.h>
#include <stdio.h>
int main(int argc, char** argv)
{
const char* lds = (argc >= 2) ? argv[1] : "opc.tcp://lds-host:4840";
DarraUa_Initialize();
DarraUa_FindServersResult* res = NULL;
DarraUa_Status st = DarraUa_Discovery_FindServers(lds, 5000, &res);
if (!UA_STATUS_IS_GOOD(st)) {
fprintf(stderr, "FindServers failed: 0x%08X (%s)\n",
(unsigned)st, DarraUa_StatusName(st));
DarraUa_Shutdown();
return 1;
}
uint32_t n = DarraUa_FindServersResult_GetCount(res);
printf("Domain has %u servers:\n", (unsigned)n);
for (uint32_t i = 0; i < n; ++i) {
const DarraUa_ApplicationDescription* a =
DarraUa_FindServersResult_GetServer(res, i);
if (!a) continue;
printf("\n App: %s\n", a->application_name);
printf(" AppUri: %s\n", a->application_uri);
printf(" Type: %d\n", (int)a->application_type);
printf(" URLs:\n");
for (int u = 0; u < a->discovery_url_count; ++u)
printf(" - %s\n", a->discovery_urls[u]);
}
DarraUa_FindServersResult_Delete(res);
DarraUa_Shutdown();
return 0;
}
LDS
LDS = Local Discovery Server, provided OPC Foundation by. When a server on the same subnet starts up, it can register with LDS, and clients can FindServers discover all of them via.
LDS By default, it listens on port 4840 (the same port as a standard server). It is typically installed on a dedicated host, but can also be deployed alongside the server.
Use Case: Primary-Standby Server Failover
In a redundant configuration, both the primary and standby servers are registered with the same client LDS. The client periodically checks their status FindServersand switches to the standby server if the primary server becomes unreachable:
const char* primary_uri = "urn:plant:server-primary";
const char* backup_uri = "urn:plant:server-backup";
DarraUa_FindServersResult* res = NULL;
DarraUa_Discovery_FindServers("opc.tcp://lds:4840", 5000, &res);
const char* chosen_url = NULL;
uint32_t n = DarraUa_FindServersResult_GetCount(res);
for (uint32_t i = 0; i < n; ++i) {
const DarraUa_ApplicationDescription* a =
DarraUa_FindServersResult_GetServer(res, i);
if (strcmp(a->application_uri, primary_uri) == 0 &&
a->discovery_url_count > 0) {
chosen_url = a->discovery_urls[0];
break;
}
}
if (!chosen_url) {
/* Fall back to standby */
for (uint32_t i = 0; i < n; ++i) {
const DarraUa_ApplicationDescription* a =
DarraUa_FindServersResult_GetServer(res, i);
if (strcmp(a->application_uri, backup_uri) == 0 &&
a->discovery_url_count > 0) {
chosen_url = a->discovery_urls[0];
break;
}
}
}
/* ... Use chosen_url to cfg.endpoint_url create a conversation ... */
DarraUa_FindServersResult_Delete(res);