/// <summary> /// Returns the next child. /// </summary> private IReference NextChild() { var system = (UnderlyingSystem)SystemContext.SystemHandle; NodeId targetId = null; // check if a specific browse name is requested. if (!QualifiedName.IsNull(BrowseName)) { // check if match found previously. if (_position == int.MaxValue) { return(null); } // browse name must be qualified by the correct namespace. if (_source.BrowseName.NamespaceIndex != BrowseName.NamespaceIndex) { return(null); } // look for matching segment. if (_stage == Stage.Segments && _segments != null) { for (var ii = 0; ii < _segments.Count; ii++) { if (BrowseName.Name == _segments[ii].Name) { targetId = ModelUtils.ConstructIdForSegment(_segments[ii].Id, _source.NodeId.NamespaceIndex); break; } } } // look for matching block. if (_stage == Stage.Blocks && _blocks != null) { for (var ii = 0; ii < _blocks.Count; ii++) { var block = system.FindBlock(_blocks[ii]); if (block != null && BrowseName.Name == block.Name) { targetId = ModelUtils.ConstructIdForBlock(_blocks[ii], _source.NodeId.NamespaceIndex); break; } } } _position = int.MaxValue; } // return the child at the next position. else { // look for next segment. if (_stage == Stage.Segments && _segments != null) { if (_position >= _segments.Count) { return(null); } targetId = ModelUtils.ConstructIdForSegment(_segments[_position++].Id, _source.NodeId.NamespaceIndex); } // look for next block. else if (_stage == Stage.Blocks && _blocks != null) { if (_position >= _blocks.Count) { return(null); } targetId = ModelUtils.ConstructIdForBlock(_blocks[_position++], _source.NodeId.NamespaceIndex); } } // create reference. if (targetId != null) { return(new NodeStateReference(ReferenceTypeIds.Organizes, false, targetId)); } return(null); }
/// <summary> /// Verifies that the specified node exists. /// </summary> protected override NodeState ValidateNode( ServerSystemContext context, NodeHandle handle, IDictionary <NodeId, NodeState> cache) { // not valid if no root. if (handle == null) { return(null); } // check if previously validated. if (handle.Validated) { return(handle.Node); } NodeState target = null; // check if already in the cache. if (cache != null) { if (cache.TryGetValue(handle.NodeId, out target)) { // nulls mean a NodeId which was previously found to be invalid has been referenced again. if (target == null) { return(null); } handle.Node = target; handle.Validated = true; return(handle.Node); } target = null; } try { // check if the node id has been parsed. if (!(handle.ParsedNodeId is ParsedNodeId parsedNodeId)) { return(null); } NodeState root = null; // validate a segment. if (parsedNodeId.RootType == ModelUtils.Segment) { var segment = _system.FindSegment(parsedNodeId.RootId); // segment does not exist. if (segment == null) { return(null); } var rootId = ModelUtils.ConstructIdForSegment(segment.Id, NamespaceIndex); // create a temporary object to use for the operation. root = new SegmentState(context, rootId, segment); } // validate segment. else if (parsedNodeId.RootType == ModelUtils.Block) { // validate the block. var block = _system.FindBlock(parsedNodeId.RootId); // block does not exist. if (block == null) { return(null); } var rootId = ModelUtils.ConstructIdForBlock(block.Id, NamespaceIndex); // check for check for blocks that are being currently monitored. if (_blocks.TryGetValue(rootId, out var node)) { root = node; } // create a temporary object to use for the operation. else { root = new BlockState(this, rootId, block); } } // unknown root type. else { return(null); } // all done if no components to validate. if (string.IsNullOrEmpty(parsedNodeId.ComponentPath)) { handle.Validated = true; handle.Node = target = root; return(handle.Node); } // validate component. NodeState component = root.FindChildBySymbolicName(context, parsedNodeId.ComponentPath); // component does not exist. if (component == null) { return(null); } // found a valid component. handle.Validated = true; handle.Node = target = component; return(handle.Node); } finally { // store the node in the cache to optimize subsequent lookups. if (cache != null) { cache.Add(handle.NodeId, target); } } }