BrowseCrawler
개요
BFS 큐를 사용하여 AddressSpace 서브트리 전체를 순회하며, 이중 보호 (max_depth + max_nodes)를 적용합니다. C 버전에서는콜백 방식: 노드가 하나 발견될 때마다 호출되며 on_node, 크롤링이 완료되면 함수가 반환되고, 결과 배열은 반환하지 않습니다.
API
| 함수 | 분류 | 읽기 및 쓰기 | 설명 |
|---|---|---|---|
| DarraUa_CrawlerConfig_Init(&cfg) | 구조 | — | 기본 설정 입력 |
| DarraUa_Session_CrawlAddressSpace(session, &cfg, on_node, ctx, &total) | 방법 | 읽기 | 동기화 BFS, 차단 호출 |
#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 = 기본값 10 사용 */
uint32_t max_nodes; /* 0 = 기본값 100000 사용 */
DarraUa_NodeClass node_class_filter; /* UA_NODE_CLASS_UNSPECIFIED = 필터링 없음 */
} 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; /* 루트 노드는 NULL */
uint32_t depth; /* root = 0 */
} DarraUa_CrawlNode;
/* 1 = 계속, 0 = 즉시 취소 */
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 = 모든 설정을 기본값으로 유지 */
DarraUa_OnCrawlNode on_node, /* 반드시 아니어야 한다 NULL */
void* user_context,
uint32_t* out_total); /* ~할 수 있다 NULL */
| 매개변수 | 기본값 | 설명 |
|---|---|---|
| session | — | 연결된 세션 핸들 (DarraUa_SessionHandle) |
| cfg | NULL → 모든 기본값 | 구성 정보 수집 |
| on_node | — | 노드 콜백은 NULL이 아니어야 합니다. |
| user_context | — | 사용자 컨텍스트 복원 |
| out_total | NULL 허용 | 출력: 총 포착된 노드 수 |
노드 필드의 수명 주기: DarraUa_CrawlNode 여기 있는 모든 문자열 필드는 borrowed이며, 수명 주기는 콜백 내부에서만 유지됩니다. 보존하려면 콜백 내에서 처리해야 합니다 strdup.
취소: 콜백이 0을 반환하면 즉시 취소되며, 함수가 반환됩니다 UA_STATUS_GOOD.out_total 그래도 캡처된 개수는 여전히 반환됩니다.
코드 예시
#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 : "");
/* 변수 노드만 캐시합니다 ( BFS 드릴다운에는 영향을 미치지 않음). */
if (node->node_class == UA_NODE_CLASS_VARIABLE) {
/* 문자열은 빌려온 것이므로, 유지하려면 직접 복사해야 합니다. */
cache_put(node->node_id, node->browse_name);
c->shown++;
}
/* 사용자가 "취소"를 클릭하면 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 + DarraUa_Session_Connect 생략 ... */
DarraUa_CrawlerConfig cfg;
DarraUa_CrawlerConfig_Init(&cfg); /* 깊이 10 / 노드 100000 / 필터링 없음 */
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);
/* 결과가 없는 배열을 해제해야 함: 콜백 내에서 노드를 그 자리에서 소비 */
return 0;
}
모범 사례
- root로 제한하며, i=84(Root)부터 시작하지 마십시오.
- 를 사용하여 콜백 횟수를
node_class_filter줄입니다 (주의: 필터링은 단순히 콜백을 작성하지 않는 것일 뿐, 모든 계층적 하위 노드에 대해서는 BFS 여전히 드릴다운이 수행됩니다). - 시작 시 한 번에 크롤링
- 콜백이 0을 반환하면 취소가 이루어지므로, 외부 플래그를 통해 폴링할 필요가 없습니다.
- 받은 문자열을 보관하려면
DarraUa_CrawlNode복사해야 합니다
다국어 대조
| C# | Python | Java | C++ | Rust | C |
|---|---|---|---|---|---|
| BrowseCrawler | BrowseCrawler | BrowseCrawler | BrowseCrawler | BrowseCrawler | DarraUa_CrawlerConfig + DarraUa_Session_CrawlAddressSpace |
| CrawlAsync | crawl_async | crawlAsync | CrawlAsync | crawl_async | DarraUa_Session_CrawlAddressSpace |