/// <summary>
        /// Updates the header of a service request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <param name="useDefaults">if set to <c>true</c> use defaults].</param>
        protected virtual void UpdateRequestHeader(IServiceRequest request, bool useDefaults)
        {
            lock (m_lock)
            {
                if (request.RequestHeader == null)
                {
                    request.RequestHeader = new RequestHeader();
                }

                if (useDefaults)
                {
                    request.RequestHeader.ReturnDiagnostics = (uint)(int)m_returnDiagnostics;
                }

                if (request.RequestHeader.RequestHandle == 0)
                {
                    request.RequestHeader.RequestHandle = (uint)Utils.IncrementIdentifier(ref m_nextRequestHandle);
                }

                if (NodeId.IsNull(request.RequestHeader.AuthenticationToken))
                {
                    request.RequestHeader.AuthenticationToken = m_authenticationToken;
                }

                request.RequestHeader.Timestamp    = DateTime.UtcNow;
                request.RequestHeader.AuditEntryId = CreateAuditLogEntry(request);
            }
        }
 /// <summary>
 /// Generates a unique request handle.
 /// </summary>
 public uint NewRequestHandle()
 {
     return((uint)Utils.IncrementIdentifier(ref m_nextRequestHandle));
 }
        /// <summary>
        /// Instantiates the hierarchy.
        /// </summary>
        private IList <ILocalNode> InstantiateHierarchy(
            ILocalNode root,
            InstanceDeclarationHierarchy hierarchy,
            ref long counter,
            ushort namespaceIndex)
        {
            List <ILocalNode>          nodesToAdd    = new List <ILocalNode>();
            List <HierarchyBrowsePath> nodesToUpdate = new List <HierarchyBrowsePath>();

            // create an instance for each browse path that does not already have one.
            foreach (HierarchyBrowsePath browsePath in hierarchy.BrowsePaths.Values)
            {
                bool updateReferences = browsePath.BrowsePath == "/";

                // find the instance.
                INode instance = m_nodes.Find(browsePath.InstanceId);

                if (instance == null)
                {
                    // do nothing if optional.
                    if (browsePath.IsOptional)
                    {
                        continue;
                    }

                    // find the declaration.
                    ILocalNode declaration = m_nodes.Find(browsePath.DeclarationId) as ILocalNode;

                    if (declaration == null)
                    {
                        continue;
                    }

                    instance = declaration;

                    // check the creation rule.
                    CreationRule creationRule = GetCreationRule(declaration.ModellingRule);

                    // create a new instance is the creation rule is new.
                    if (creationRule == CreationRule.New)
                    {
                        Node newNode = Node.Copy(declaration);
                        newNode.NodeId = new NodeId(Utils.IncrementIdentifier(ref counter), namespaceIndex);
                        instance       = newNode;
                        nodesToAdd.Add(newNode);
                        updateReferences = true;
                    }
                }

                // update the browse path.
                browsePath.InstanceId = instance.NodeId;
                hierarchy.Instances[(NodeId)instance.NodeId] = instance;

                // check if instance can be traced back to root.
                if (!updateReferences)
                {
                    continue;
                }

                nodesToUpdate.Add(browsePath);
            }

            // create the references.
            foreach (HierarchyBrowsePath browsePath in nodesToUpdate)
            {
                if (browsePath.InstanceId != null)
                {
                    InstantiateReferences(browsePath, hierarchy);
                }
            }

            return(nodesToAdd);
        }