Skip to content

Commit

Permalink
skip used NodeId's (FreeOpcUa#241)
Browse files Browse the repository at this point in the history
* skip used NodeId's

When you create a Node with an explicitly set NodeId - lets say 2003 -
and after that create three Node's using the automatic id allocation,
you run into error stating "NodeId 2003 already exists". This patch
avoids such errors by skipping existing NodeId's in automatic id
allocation.

* fixed stupid copy paste bug

* combine CreateUniqueNodeId and GetNewNodeId

* add range test to automatic node id allocation
  • Loading branch information
ralf1070 authored and oroulet committed May 30, 2017
1 parent 6970ab6 commit 710a16c
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/server/address_space_internal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,17 +570,37 @@ namespace OpcUa

NodeId AddressSpaceInMemory::GetNewNodeId(const NodeId& id)
{
uint32_t idx;
if (id == ObjectId::Null || id.IsNull())
{
return OpcUa::NumericNodeId(++MaxNodeIdNum, DefaultIdx);
idx = DefaultIdx;
} else {
if (id.HasNullIdentifier())
{
idx = id.GetNamespaceIndex();
} else {
return id;
}
}

if (id.HasNullIdentifier())
// skip over already assigned node id's
// this should be a very seldom operation - it only happens when
// we actively assign static node id's to some nodes and after that
// create nodes using automatic id allocation and even then it
// only happens once. So we don't care about optimiziation here.
for (;;)
{
return OpcUa::NumericNodeId(++MaxNodeIdNum, id.GetNamespaceIndex());
NodeId result = OpcUa::NumericNodeId(++MaxNodeIdNum, idx);
if (Nodes.find(result) == Nodes.end())
{
return result;
}
// completly unlikly - we would have to allocate 4gig nodes to
// fullfill this condition
if (MaxNodeIdNum == std::numeric_limits<uint32_t>::max()) {
throw std::runtime_error("AddressSpaceInternal | unable to assign new NodeId: range exceeded");
}
}

return id;
}
}

Expand Down

0 comments on commit 710a16c

Please sign in to comment.