FindServers
- Once you have the server, use GetEndpoints List the available endpoints for this server.
- Then use Constructor True Connection.
Signature
namespace darra::opcua {
class Discovery {
public:
static std::vector<ApplicationDescription> FindServers(
std::string const& endpoint_url,
uint32_t timeout_ms = 10000);
};
}
Query the Local Discovery Server (LDS) for all OPC UA Server. registered within the same domain.
| Parameters | Default | Note |
|---|---|---|
| endpoint_url | — | LDS / Discovery Server endpoint URL; Passing an empty string throws Exception("endpoint_url 不能为空", Status::DarraInvalidArgument) |
| timeout_ms | 10000 | Total query timeout (milliseconds) |
Must return a value before the call Stack::Init() std::vector<ApplicationDescription>; if the query fails, throw an exception Exception("FindServers '<url>' failed", <原始状态码>)—read the status code ex.status_code(), then proceed with the readable name Stack::StatusName(ex.status_code()).
ApplicationDescription Field
struct ApplicationDescription {
std::string application_uri;
std::string product_uri;
std::string application_name;
int32_t application_type = 0; // 0 Server / 1 Client / 2 ClientAndServer / 3 DiscoveryServer
std::string gateway_server_uri;
std::string discovery_profile_uri;
std::vector<std::string> discovery_urls;
};
| Field | Type | Note |
|---|---|---|
| application_uri | std::string | Application URI (Globally Unique Identifier) |
| product_uri | std::string | Products URI |
| application_name | std::string | App Display Name |
| application_type | int32_t | Server (0) / Client (1) / ClientAndServer (2) / DiscoveryServer (3) |
| gateway_server_uri | std::string | Gateway (optional) |
| discovery_profile_uri | std::string | Discovery Profile |
| discovery_urls | std::vector<std::string> | Endpoint URLs for this server |
Usage
#include <darra/opcua/discovery.hpp>
#include <iostream>
using namespace darra::opcua;
Stack::Init();
auto servers = Discovery::FindServers("opc.tcp://lds-host:4840");
std::cout << "Domain has " << servers.size() << " servers:\n";
for (auto const& s : servers) {
std::cout << "\n App: " << s.application_name
<< "\n AppUri: " << s.application_uri
<< "\n Type: " << s.application_type
<< "\n Discovery URLs:\n";
for (auto const& url : s.discovery_urls) {
std::cout << " - " << url << "\n";
}
}
Stack::Shutdown();
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.
FindServersOnNetwork (Not provided)
OPC UA LDS There are also services FindServersOnNetwork that are discovered across subnets via mDNS. Currently SDK Not available: There is only GetEndpoints / on the C++ side FindServers, and only DarraUa_Discovery_GetEndpoints / on the C side DarraUa_Discovery_FindServers; there are no corresponding entry points on either side, so there is no way around it.
Use Case: Automatic Server Failover (Redundancy)
In a redundant scenario, both the primary and standby servers are registered with the same client LDS. The client periodically checks their status FindServers, and if the primary server becomes unreachable, it switches to the standby server (continued from the previous text #include; same compilation unit):
auto servers = Discovery::FindServers("opc.tcp://lds:4840");
ApplicationDescription const* primary = nullptr;
ApplicationDescription const* backup = nullptr;
for (auto const& s : servers) {
if (s.application_uri == "urn:plant:server-primary") primary = &s;
if (s.application_uri == "urn:plant:server-backup") backup = &s;
}
std::string url;
if (primary && !primary->discovery_urls.empty()) {
url = primary->discovery_urls.front();
} else if (backup && !backup->discovery_urls.empty()) {
url = backup->discovery_urls.front();
}