Skip to main content

BrowseCrawler

Overview​

BrowseCrawler Use a queue BFS to traverse the entire AddressSpace subtree, with double protection (max_depth + max_nodes).

API​

BrowseCrawler It is a class (darra::opcua) in the namespace browse_crawler.hpp; is passed during construction Session&.

MembersCategoryReading and WritingNote
BrowseCrawler(Session& ua, int max_depth = 10, int max_nodes = 100000, NodeClass filter = Unspecified)Structure—max_depth / max_nodes <= 0 will be raised to 1
CrawlAsync(root_node_id, progress = {}, cancel = {})MethodsReadReturns std::future<CrawlResult>
CrawlNode.node_id / display_name / browse_name / node_class / parent_node_idFieldReadSingle-Node Description
CrawlResult.all_nodesFieldReadstd::vector<CrawlNode>
CrawlResult.children_by_parentFieldReadstd::unordered_map<std::string, std::vector<std::string>>
CrawlResult.started_atFieldReadstd::chrono::steady_clock::time_point
CrawlResult.elapsedFieldReadstd::chrono::milliseconds, which is already the elapsed time
namespace darra::opcua {

struct CrawlNode {
std::string node_id;
std::string display_name;
std::string browse_name;
NodeClass node_class = NodeClass::Unspecified;
std::string parent_node_id;
};

struct CrawlResult {
std::vector<CrawlNode> all_nodes;
std::unordered_map<std::string, std::vector<std::string>> children_by_parent;
std::chrono::steady_clock::time_point started_at;
std::chrono::milliseconds elapsed{0};
};

class BrowseCrawler {
public:
using ProgressFn = std::function<void(int, std::string const&)>;

BrowseCrawler(Session& ua, int max_depth = 10, int max_nodes = 100000,
NodeClass node_class_filter = NodeClass::Unspecified);

std::future<CrawlResult> CrawlAsync(
std::string root_node_id,
ProgressFn progress = {},
std::shared_ptr<std::atomic<bool>> cancel = {});
};
}
ParametersDefaultNote
ua—Connected Session(borrowed; the caller guarantees that it will outlive the future)
max_depth10Maximum recursion depth;<= 0 set to 1
max_nodes100000Maximum number of nodes;<= 0 set to 1
node_class_filterUnspecifiedKeep only NodeClass the specified nodes; Unspecified = Keep all
root_node_id—Starting Point; NodeIdAutomatically use an empty string "i=85"(ObjectsFolder)
progressEmptyvoid(int count, std::string const& current_nid); Report once for every 100 nodes captured,I'll add it again at the end.(current_nid empty string). Called on a worker thread
cancelEmptystd::shared_ptr<std::atomic<bool>>; When set to true, the program checks once after each node is expanded and then exits

Key Points of Conduct: Traverse in BFS order and remove duplicates from the visited nodes (the server's node graph may contain cycles); each time Browse is traversed BrowseSafe,Skip a single failure without interrupting the process;filter Matches using either bitwise OR or equality.

Code Example​

#include <darra/opcua/browse_crawler.hpp>
#include <darra/opcua/client.hpp>
#include <darra/opcua/well_known_nodes.hpp>
#include <chrono>
#include <iostream>
#include <string>

using namespace darra::opcua;

Session s("opc.tcp://localhost:4840");
s.Connect();

// 1) Climb the entire tree ObjectsFolder
BrowseCrawler crawler(s, /*max_depth=*/8, /*max_nodes=*/50000);

auto progress = [](int count, std::string const& nid) {
std::cout << "\r已抓 " << count << " 个, 当前 " << nid << std::flush;
};

auto fut = crawler.CrawlAsync(WellKnownNodes::ObjectsFolder, progress);
CrawlResult result = fut.get();

std::cout << "\n共 " << result.all_nodes.size() << " 节点, 耗时 "
<< result.elapsed.count() << " ms\n"; // elapsed is already in milliseconds

// 2) Variable nodes only
BrowseCrawler c2(s, 10, 100000, NodeClass::Variable);
CrawlResult r2 = c2.CrawlAsync("ns=2;s=Boilers").get();
for (auto const& n : r2.all_nodes)
std::cout << " " << n.browse_name << " (" << n.node_id << ")\n";

Performance​

Crawl time grows linearly with the number of server nodes and the number of network round-trips — at least one Browse RPC per node. The SDK has no benchmark data, so measure it on your own server. max_nodes is a hard limit; when it is reached, crawling stops.

Best Practices​

  • Restriction root_node_id: Do not start crawling from "i=84"(RootFolder)
  • Use node_class_filter to reduce the number of landing nodes
  • During startup, crawl the entire site once and store it in the local cache; subsequent searches and autocomplete will no longer send requests. Browse RPC
  • Long-running tasks should be cancellable: Add a stop button to cancel UI.

Cross-Language Comparison​

C#PythonJavaC++RustC
BrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerDarraUa_Session_CrawlAddressSpace

The entry point for C is a single function DarraUa_Session_CrawlAddressSpace(session, config, on_node, ctx), accompanied by DarraUa_CrawlerConfig(DarraUa_CrawlerConfig_Init initialization) / DarraUa_CrawlNode / DarraUa_OnCrawlNode,There is no such DarraUa_BrowseCrawler_* family。

Next Step​