From c0ddf535b619f919f0909883f0ada283054b709f Mon Sep 17 00:00:00 2001 From: mkonnerth Date: Wed, 13 Dec 2023 06:33:37 +0100 Subject: [PATCH] remove void* extension and put serverContext, nodecontainer in own struct --- backends/open62541/src/import.c | 53 +++++++++++++++++---------- backends/stdout/examples/main.c | 2 +- include/NodesetLoader/NodesetLoader.h | 4 +- src/Nodeset.c | 4 +- src/Nodeset.h | 2 +- src/NodesetLoader.c | 4 +- tests/parser.c | 2 +- 7 files changed, 42 insertions(+), 29 deletions(-) diff --git a/backends/open62541/src/import.c b/backends/open62541/src/import.c index 67d66b28..95f74063 100644 --- a/backends/open62541/src/import.c +++ b/backends/open62541/src/import.c @@ -376,7 +376,15 @@ static UA_StatusCode handleDataTypeNode(const NL_DataTypeNode *node, UA_NodeId * attr, node->extension, NULL); } -static void addNodeImpl(ServerContext *serverContext, NL_Node *node, void *extension) +struct AddNodeContext +{ + ServerContext* serverContext; + NodeContainer* problemNodes; +}; + +typedef struct AddNodeContext AddNodeContext; + +static void addNodeImpl(AddNodeContext *context, NL_Node *node) { UA_NodeId id = node->id; UA_NodeId parentReferenceId = UA_NODEID_NULL; @@ -393,51 +401,50 @@ static void addNodeImpl(ServerContext *serverContext, NL_Node *node, void *exten { case NODECLASS_OBJECT: addedNodeStatus = handleObjectNode((const NL_ObjectNode *)node, &id, &parentId, - &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(serverContext)); + &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(context->serverContext)); break; case NODECLASS_METHOD: addedNodeStatus = handleMethodNode((const NL_MethodNode *)node, &id, &parentId, - &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(serverContext)); + &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(context->serverContext)); break; case NODECLASS_OBJECTTYPE: addedNodeStatus = handleObjectTypeNode((const NL_ObjectTypeNode *)node, &id, &parentId, &parentReferenceId, <, &qn, &description, - ServerContext_getServerObject(serverContext)); + ServerContext_getServerObject(context->serverContext)); break; case NODECLASS_REFERENCETYPE: addedNodeStatus = handleReferenceTypeNode((const NL_ReferenceTypeNode *)node, &id, &parentId, &parentReferenceId, <, &qn, - &description, ServerContext_getServerObject(serverContext)); + &description, ServerContext_getServerObject(context->serverContext)); break; case NODECLASS_VARIABLETYPE: addedNodeStatus = handleVariableTypeNode((const NL_VariableTypeNode *)node, &id, &parentId, &parentReferenceId, <, &qn, &description, - ServerContext_getServerObject(serverContext)); + ServerContext_getServerObject(context->serverContext)); break; case NODECLASS_VARIABLE: addedNodeStatus = handleVariableNode((const NL_VariableNode *)node, &id, &parentId, - &parentReferenceId, <, &qn, &description, serverContext); + &parentReferenceId, <, &qn, &description, context->serverContext); break; case NODECLASS_DATATYPE: addedNodeStatus = handleDataTypeNode((const NL_DataTypeNode *)node, &id, &parentId, - &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(serverContext)); + &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(context->serverContext)); break; case NODECLASS_VIEW: addedNodeStatus = handleViewNode((const NL_ViewNode *)node, &id, &parentId, - &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(serverContext)); + &parentReferenceId, <, &qn, &description, ServerContext_getServerObject(context->serverContext)); break; } // If a node was not added to the server due to an error, we add such a node // to a special node container. We can then try to add such nodes later. - if(extension != NULL && UA_StatusCode_isBad(addedNodeStatus)) + if(context->problemNodes != NULL && UA_StatusCode_isBad(addedNodeStatus)) { - NodeContainer *problemNodes = (NodeContainer*)extension; - NodeContainer_add(problemNodes, node); + NodeContainer_add(context->problemNodes, node); } } @@ -483,7 +490,7 @@ struct DataTypeImportCtx UA_Server *server; }; -static void addDataType(struct DataTypeImportCtx *ctx, NL_Node *node, void *extension) +static void addDataType(struct DataTypeImportCtx *ctx, NL_Node *node) { // add only the types const NL_BiDirectionalReference *r = ctx->hasEncodingRef; @@ -518,14 +525,14 @@ static void importDataTypes(NodesetLoader *loader, UA_Server *server) ctx.hasEncodingRef = hasEncodingRef; ctx.server = server; ctx.importer = importer; - NodesetLoader_forEachNode(loader, NODECLASS_DATATYPE, &ctx, NULL, + NodesetLoader_forEachNode(loader, NODECLASS_DATATYPE, &ctx, (NodesetLoader_forEachNode_Func)addDataType); DataTypeImporter_initMembers(importer); DataTypeImporter_delete(importer); } -static void addNonHierachicalRefs(UA_Server *server, NL_Node *node, void *extension) +static void addNonHierachicalRefs(UA_Server *server, NL_Node *node) { NL_Reference *ref = node->nonHierachicalRefs; while (ref) @@ -568,11 +575,13 @@ static size_t secondChanceAddNodes(ServerContext *serverContext, { NodeContainer *local_badStatusNodes = NodeContainer_new((*badStatusNodes)->size, false); + AddNodeContext context; + context.problemNodes = local_badStatusNodes; + context.serverContext = serverContext; for (size_t counter = 0; counter < (*badStatusNodes)->size; counter++) { // Import to server again - addNodeImpl(serverContext, (*badStatusNodes)->nodes[counter], - local_badStatusNodes); + addNodeImpl(&context, (*badStatusNodes)->nodes[counter]); } size_t counterOfAdddedNodesForOneAttempt = (*badStatusNodes)->size - local_badStatusNodes->size; @@ -604,12 +613,16 @@ static void addNodes(NodesetLoader *loader, ServerContext *serverContext, // will always be adding new bad nodes to one list, and we have to calculate // the real number of bad status nodes on every single cycle. size_t previous_loop_badStatusNodes_size = 0; + + AddNodeContext context; + context.problemNodes = badStatusNodes; + context.serverContext = serverContext; for (size_t i = 0; i < NL_NODECLASS_COUNT; i++) { const NL_NodeClass classToImport = order[i]; size_t cnt = - NodesetLoader_forEachNode(loader, classToImport, serverContext, - badStatusNodes, (NodesetLoader_forEachNode_Func)addNodeImpl); + NodesetLoader_forEachNode(loader, classToImport, &context, + (NodesetLoader_forEachNode_Func)addNodeImpl); if (classToImport == NODECLASS_DATATYPE) { importDataTypes(loader, ServerContext_getServerObject(serverContext)); @@ -643,7 +656,7 @@ static void addNodes(NodesetLoader *loader, ServerContext *serverContext, const NL_NodeClass classToImport = order[i]; NodesetLoader_forEachNode( loader, classToImport, ServerContext_getServerObject(serverContext), - NULL, (NodesetLoader_forEachNode_Func)addNonHierachicalRefs); + (NodesetLoader_forEachNode_Func)addNonHierachicalRefs); } } diff --git a/backends/stdout/examples/main.c b/backends/stdout/examples/main.c index 4279e01c..7199fc4f 100644 --- a/backends/stdout/examples/main.c +++ b/backends/stdout/examples/main.c @@ -38,7 +38,7 @@ int main(int argc, char *argv[]) for (int i = 0; i < NL_NODECLASS_COUNT; i++) { - NodesetLoader_forEachNode(loader, (NL_NodeClass)i, NULL, NULL, (NodesetLoader_forEachNode_Func)dumpNode); + NodesetLoader_forEachNode(loader, (NL_NodeClass)i, NULL, (NodesetLoader_forEachNode_Func)dumpNode); } NodesetLoader_delete(loader); diff --git a/include/NodesetLoader/NodesetLoader.h b/include/NodesetLoader/NodesetLoader.h index 16850aec..4e70e5e9 100644 --- a/include/NodesetLoader/NodesetLoader.h +++ b/include/NodesetLoader/NodesetLoader.h @@ -264,10 +264,10 @@ LOADER_EXPORT void NodesetLoader_delete(NodesetLoader *loader); LOADER_EXPORT const NL_BiDirectionalReference * NodesetLoader_getBidirectionalRefs(const NodesetLoader *loader); LOADER_EXPORT bool NodesetLoader_sort(NodesetLoader *loader); -typedef void (*NodesetLoader_forEachNode_Func)(void *context, NL_Node *node, void *extension); +typedef void (*NodesetLoader_forEachNode_Func)(void *context, NL_Node *node); LOADER_EXPORT size_t NodesetLoader_forEachNode(NodesetLoader *loader, NL_NodeClass nodeClass, - void *context, void *extension, NodesetLoader_forEachNode_Func fn); + void *context, NodesetLoader_forEachNode_Func fn); LOADER_EXPORT bool NodesetLoader_isInstanceNode (const NL_Node *baseNode); #ifdef __cplusplus } diff --git a/src/Nodeset.c b/src/Nodeset.c index c44d1aae..853baffd 100644 --- a/src/Nodeset.c +++ b/src/Nodeset.c @@ -632,13 +632,13 @@ void Nodeset_InverseNameFinish(const Nodeset *nodeset, NL_Node *node, } size_t Nodeset_forEachNode(Nodeset *nodeset, NL_NodeClass nodeClass, - void *context, void *extension, + void *context, NodesetLoader_forEachNode_Func fn) { NodeContainer *c = nodeset->nodes[nodeClass]; for (NL_Node **node = c->nodes; node != c->nodes + c->size; node++) { - fn(context, *node, extension); + fn(context, *node); } return c->size; } diff --git a/src/Nodeset.h b/src/Nodeset.h index 82600ad5..bc67733f 100644 --- a/src/Nodeset.h +++ b/src/Nodeset.h @@ -71,5 +71,5 @@ void Nodeset_InverseNameFinish(const Nodeset *nodeset, NL_Node *node, char *text const NL_BiDirectionalReference * Nodeset_getBiDirectionalRefs(const Nodeset *nodeset); size_t Nodeset_forEachNode(Nodeset *nodeset, NL_NodeClass nodeClass, - void *context, void *extension, NodesetLoader_forEachNode_Func fn); + void *context, NodesetLoader_forEachNode_Func fn); #endif diff --git a/src/NodesetLoader.c b/src/NodesetLoader.c index cec2a398..4e80577b 100644 --- a/src/NodesetLoader.c +++ b/src/NodesetLoader.c @@ -567,8 +567,8 @@ NodesetLoader_getBidirectionalRefs(const NodesetLoader *loader) } size_t NodesetLoader_forEachNode(NodesetLoader *loader, NL_NodeClass nodeClass, - void *context, void *extension, + void *context, NodesetLoader_forEachNode_Func fn) { - return Nodeset_forEachNode(loader->nodeset, nodeClass, context, extension, fn); + return Nodeset_forEachNode(loader->nodeset, nodeClass, context, fn); } diff --git a/tests/parser.c b/tests/parser.c index e3ba848e..fe904570 100644 --- a/tests/parser.c +++ b/tests/parser.c @@ -39,7 +39,7 @@ START_TEST(Server_ImportBasicNodeClassTest) for (int i = 0; i < NL_NODECLASS_COUNT; i++) { - NodesetLoader_forEachNode(loader, (NL_NodeClass)i, &nodeCount, NULL, + NodesetLoader_forEachNode(loader, (NL_NodeClass)i, &nodeCount, (NodesetLoader_forEachNode_Func)addNode); }