Skip to content

Commit

Permalink
remove void* extension and put serverContext, nodecontainer in own st…
Browse files Browse the repository at this point in the history
…ruct
  • Loading branch information
matkonnerth committed Dec 13, 2023
1 parent f9ef032 commit c0ddf53
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 29 deletions.
53 changes: 33 additions & 20 deletions backends/open62541/src/import.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, &lt, &qn, &description, ServerContext_getServerObject(serverContext));
&parentReferenceId, &lt, &qn, &description, ServerContext_getServerObject(context->serverContext));
break;

case NODECLASS_METHOD:
addedNodeStatus = handleMethodNode((const NL_MethodNode *)node, &id, &parentId,
&parentReferenceId, &lt, &qn, &description, ServerContext_getServerObject(serverContext));
&parentReferenceId, &lt, &qn, &description, ServerContext_getServerObject(context->serverContext));
break;

case NODECLASS_OBJECTTYPE:
addedNodeStatus = handleObjectTypeNode((const NL_ObjectTypeNode *)node, &id, &parentId,
&parentReferenceId, &lt, &qn, &description,
ServerContext_getServerObject(serverContext));
ServerContext_getServerObject(context->serverContext));
break;

case NODECLASS_REFERENCETYPE:
addedNodeStatus = handleReferenceTypeNode((const NL_ReferenceTypeNode *)node, &id,
&parentId, &parentReferenceId, &lt, &qn,
&description, ServerContext_getServerObject(serverContext));
&description, ServerContext_getServerObject(context->serverContext));
break;

case NODECLASS_VARIABLETYPE:
addedNodeStatus = handleVariableTypeNode((const NL_VariableTypeNode *)node, &id, &parentId,
&parentReferenceId, &lt, &qn, &description,
ServerContext_getServerObject(serverContext));
ServerContext_getServerObject(context->serverContext));
break;

case NODECLASS_VARIABLE:
addedNodeStatus = handleVariableNode((const NL_VariableNode *)node, &id, &parentId,
&parentReferenceId, &lt, &qn, &description, serverContext);
&parentReferenceId, &lt, &qn, &description, context->serverContext);
break;
case NODECLASS_DATATYPE:
addedNodeStatus = handleDataTypeNode((const NL_DataTypeNode *)node, &id, &parentId,
&parentReferenceId, &lt, &qn, &description, ServerContext_getServerObject(serverContext));
&parentReferenceId, &lt, &qn, &description, ServerContext_getServerObject(context->serverContext));
break;
case NODECLASS_VIEW:
addedNodeStatus = handleViewNode((const NL_ViewNode *)node, &id, &parentId,
&parentReferenceId, &lt, &qn, &description, ServerContext_getServerObject(serverContext));
&parentReferenceId, &lt, &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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion backends/stdout/examples/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions include/NodesetLoader/NodesetLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/Nodeset.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
2 changes: 1 addition & 1 deletion src/Nodeset.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/NodesetLoader.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion tests/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit c0ddf53

Please sign in to comment.