Skip to main content

BrowseCrawler

Overview​

BFS The queue traverses the entire AddressSpace subtree, with double protection (max_depth+max_nodes). The C version isCallback-based: Call this function each time a node is found on_node; the function returns when the crawl is complete, but does not return an array of results.

API​

FunctionCategoryReading and WritingNote
DarraUa_CrawlerConfig_Init(&cfg)Structure—Fill in the default settings
DarraUa_Session_CrawlAddressSpace(session, &cfg, on_node, ctx, &total)MethodsReadSynchronous BFS, Blocking Call
#define UA_CRAWLER_DEFAULT_MAX_DEPTH  10
#define UA_CRAWLER_DEFAULT_MAX_NODES 100000

typedef struct {
const char* root_node_id; /* NULL = "i=85" (ObjectsFolder) */
uint32_t max_depth; /* 0 = Use the default value of 10 */
uint32_t max_nodes; /* 0 = Use the default value of 100000 */
DarraUa_NodeClass node_class_filter; /* UA_NODE_CLASS_UNSPECIFIED = No filter */
} DarraUa_CrawlerConfig;

typedef struct {
const char* node_id;
const char* display_name;
const char* browse_name;
DarraUa_NodeClass node_class;
const char* parent_node_id; /* The root node is NULL */
uint32_t depth; /* root = 0 */
} DarraUa_CrawlNode;

/* Return 1 = Continue, 0 = Cancel immediately */
typedef int32_t (*DarraUa_OnCrawlNode)(
const DarraUa_CrawlNode* node,
uint32_t running_count,
void* user_context);

DarraUa_Status DarraUa_Session_CrawlAddressSpace(
DarraUa_SessionHandle session,
const DarraUa_CrawlerConfig* cfg, /* NULL = All defaults */
DarraUa_OnCrawlNode on_node, /* Must not be NULL */
void* user_context,
uint32_t* out_total); /* can be NULL */
ParametersDefaultNote
session—Connected session handle (DarraUa_SessionHandle)
cfgNULL → All defaultsCrawling Configuration
on_node—Node callback; must not be NULL
user_context—Restore the User Context
out_totalNULL allowedOutput: Total number of nodes captured

Node Field Lifecycle: All string fields here DarraUa_CrawlNode are "borrowed" and exist only within the callback; to retain them, you must do so within the callback strdup.

Cancel: If the callback returns 0, the operation is canceled immediately UA_STATUS_GOOD,out_total and the function returns. The number of items captured is still returned.

Code Example​

#include <opcua.h>
#include <opcua_browse_crawler.h>
#include <stdio.h>

typedef struct {
uint32_t shown;
int32_t cancel;
} CrawlCtx;

static int32_t on_node(const DarraUa_CrawlNode* node,
uint32_t running_count, void* ctx)
{
CrawlCtx* c = (CrawlCtx*)ctx;
printf("\r已抓 %u 个, 当前 %s", running_count,
node->node_id ? node->node_id : "");

/* Cache only variable nodes (does not affect drill-down BFS ) */
if (node->node_class == UA_NODE_CLASS_VARIABLE) {
/* The string is borrowed; to keep it, you must copy it yourself. */
cache_put(node->node_id, node->browse_name);
c->shown++;
}

/* If the user clicks "Cancel," the function returns 0. */
return c->cancel ? 0 : 1;
}

int main(void)
{
DarraUa_SessionConfig scfg;
DarraUa_SessionConfig_Init(&scfg);
scfg.endpoint_url = "opc.tcp://localhost:4840";
/* ... DarraUa_Session_Create + (omitted DarraUa_Session_Connect ) ... */

DarraUa_CrawlerConfig cfg;
DarraUa_CrawlerConfig_Init(&cfg); /* Depth 10 / Nodes 100000 / No filtering */
cfg.root_node_id = "i=85"; /* ObjectsFolder */
cfg.max_depth = 8;
cfg.max_nodes = 50000;
cfg.node_class_filter = UA_NODE_CLASS_UNSPECIFIED;

CrawlCtx ctx = { 0, 0 };
uint32_t total = 0;
DarraUa_Status st = DarraUa_Session_CrawlAddressSpace(
session, &cfg, on_node, &ctx, &total);

printf("\n共 %u 节点 (变量 %u), status=0x%08X\n",
total, ctx.shown, st);
/* Free the array if it contains no results: Consume nodes in-place within the callback */
return 0;
}

Best Practices​

  • Restricted to root; do not start from i=84 (Root)
  • Use this to reduce node_class_filter the number of callbacks (Note: Filtering simply omits the callback; all hierarchical child nodes BFS will still be drilled down).
  • Crawl all at once on startup
  • A callback returning 0 indicates cancellation; there is no need to poll an external flag.
  • If you want to keep the string DarraUa_CrawlNode you've obtained, you'll need to copy it.

Cross-Language Comparison​

C#PythonJavaC++RustC
BrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerBrowseCrawlerDarraUa_CrawlerConfig + DarraUa_Session_CrawlAddressSpace
CrawlAsynccrawl_asynccrawlAsyncCrawlAsynccrawl_asyncDarraUa_Session_CrawlAddressSpace

Next Step​