Пример #1
0
        /// <summary>
        /// Deletes the child.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="name">The name.</param>
        public void DeleteChild(IDriverNode node, string name)
        {
            // We first make sure node exists.
            WeakReference childRef;

            if (children.TryGetValue(name, out childRef))
            {
                // We must make sure it is not managed or transparent.
                ManagedCommonNode n = childRef.Target as ManagedCommonNode;
                if (n != null)
                {
                    if (n.CurrentVersion.IsMountPoint || n.CurrentVersion.IsTransparentNode)
                    {
                        throw new InvalidOperationException("Cannot delete a managed node " + name +
                                                            ", it is needed for mounting.");
                    }

                    // Dispose all versions.
                    n.Dispose();
                }

                // Remove name.
                children.Remove(name);
            }
            // We check if not cached.
            else if (node.Find(name) == null)
            {
                throw new InvalidOperationException("The child with name " + name + " does not exist.");
            }

            // We can now delete it.
            node.DeleteChild(name);
        }
Пример #2
0
 protected override void ChildrenCreated(IDriverNode child)
 {
     if (child is T tNode)
     {
         ChildrenT.Add(tNode);
     }
 }
Пример #3
0
        /// <summary>
        /// Creates the child.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="name">The name.</param>
        /// <param name="defaultType">Type of the default.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public ManagedNode CreateChild(IDriverNode node, string name,
                                       string defaultType, StreamOptions options)
        {
            // We first check if it exists already.
            WeakReference childRef;

            if (children.TryGetValue(name, out childRef))
            {
                if (childRef.IsAlive)
                {
                    throw new InvalidNameException("The child with name " + name + " already exists.");
                }
            }

            // We check node for such child.
            IDriverNode child = node.Find(name);

            if (child != null)
            {
                // Will not use child anymore.
                throw new InvalidNameException("The child with name " + name + " already exists.");
            }

            // We can create new child.
            IDriverNode       driverChild     = node.CreateChild(name, defaultType, options);
            ManagedCommonNode commonChildNode = new ManagedCommonNode(this, name, driverChild, manager);

            children.Add(name, new WeakReference(commonChildNode));

            // We return current version
            return(commonChildNode.CurrentVersion);
        }
Пример #4
0
        /// <summary>
        /// We delete a specific version.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <param name="version">The version.</param>
        public void DeleteVersion(IDriverNode node, ulong version)
        {
            if (currentVersion.Version == version)
            {
                throw new InvalidOperationException("Cannot delete current version.");
            }

            // We make sure we lose references.
            WeakReference versionRef;

            if (versions.TryGetValue(version, out versionRef))
            {
                ManagedNode v = versionRef.Target as ManagedNode;
                if (v != null)
                {
                    // Cannot be used anymore!
                    v.Dispose();
                }

                versions.Remove(version);
            }

            // And issue delete.
            node.DeleteVersion(version);
        }
Пример #5
0
 private void RemoveNode(IDriverNode node)
 {
     _store.Remove(node);
     foreach (var child in node.Children)
     {
         RemoveNode(child);
     }
 }
Пример #6
0
 /// <summary>
 /// Creates host database with extensions.
 /// </summary>
 /// <param name="rootDir"></param>
 /// <param name="extensions"></param>
 public HostDatabase(string rootDir, params KeyValuePair <string, ILoadableFactory>[] extensions)
 {
     this.directory = rootDir;
     this.root      = NodeFromDirectory(null, rootDir);
     foreach (KeyValuePair <string, ILoadableFactory> ex in extensions)
     {
         this.extensions.Add(ex.Key, ex.Value);
     }
 }
Пример #7
0
 /// <summary>
 /// Sets as mount node.
 /// </summary>
 /// <param name="root">The root, can be null if reseting to non-mount node.</param>
 internal void SetAsMountNode(IDriverNode root)
 {
     lock (SyncRoot)
     {
         AssertNotDisposed();
         common.IsMountPoint = root != null;
         node = root;
     }
 }
Пример #8
0
 public void Dispose()
 {
     lock (SyncRoot)
     {
         isDisposed = true;
         node.Dispose();
         node = null;
     }
 }
Пример #9
0
        public override IDriverNode CreateDriverNode(IDriverContext ctx)
        {
            IDriverNode driverNode = null;

            switch (ctx.NodeInstance.This2NodeTemplateNavigation.Key)
            {
            case "light-bulb-switch":
                driverNode = new SwitchNode(ctx,
                                            Accessory.Services[1].Characteristics.Single(a => a.Type == CharacteristicBase.OnType), Driver);
                break;

            case "light-bulb-brightness":
                var brigthness = Accessory.CreateBrightness();
                driverNode = new LightSwitch(ctx, Driver, brigthness);
                break;

            case "light-bulb-hue":
                var hue = Accessory.CreateHue();
                driverNode = new LightSwitch(ctx, Driver, hue);
                break;

            case "power-outlet-switch":
                driverNode = new SwitchNode(ctx,
                                            Accessory.Services[1].Characteristics.Single(a => a.Type == CharacteristicBase.OnType), Driver);
                break;

            case "contact-sensor":
                driverNode = new BaseNode(ctx,
                                          Accessory.Services[1].Characteristics
                                          .Single(a => a.Type == CharacteristicBase.ContactSensorStateType), Driver);
                break;

            case "switch":
                driverNode = new SwitchNode(ctx,
                                            Accessory.Services[1].Characteristics.Single(a => a.Type == CharacteristicBase.OnType), Driver);
                break;

            case "temperature-sensor":
                driverNode = new BaseNode(ctx,
                                          Accessory.Services[1].Characteristics
                                          .Single(a => a.Type == CharacteristicBase.CurrentTemperatureType), Driver);
                break;

            case "switch-status":
            case "power-outlet-status":
            case "light-bulb-status":
                driverNode = new StateNode(ctx,
                                           Accessory.Services[1].Characteristics.Single(a => a.Type == CharacteristicBase.OnType), Driver);
                break;
            }

            return(driverNode);
        }
Пример #10
0
        /// <summary>
        /// Creates the new version.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="options">The options.</param>
        /// <returns></returns>
        public ManagedNode CreateNewVersion(IDriverNode node, string type, StreamOptions options)
        {
            IDriverNode newVersionNode = node.CreateNewVersion(type, options);

            // We now add it to current.
            ulong ver = currentVersion.Version + 1;

            currentVersion = new ManagedNode(newVersionNode, ver, this);
            versions.Add(ver, new WeakReference(currentVersion));

            // And return it.
            return(currentVersion);
        }
Пример #11
0
        /// <summary>
        /// Creates a driver node from a directory path
        /// </summary>
        /// <param name="parent">Parent node to associate</param>
        /// <param name="path">Host directory path to create the node from</param>
        /// <returns>Driver node for the host path</returns>
        internal IDriverNode NodeFromDirectory(IDriverNode parent, string path)
        {
            FileAttributes f = File.GetAttributes(path);

            if ((f & FileAttributes.Directory) != 0)
            {
                return(new DirectoryNode(this, path));
            }
            else
            {
                return(new FileNode(this, path));
            }
        }
Пример #12
0
        public ManagedCommonNode(ManagedCommonNode parent, string name,
                                 IDriverNode driverNode, ManagedDatabaseManager manager)
        {
            ulong version = 1;

            if (driverNode != null)
            {
                version = driverNode.Version;
            }

            this.parent            = parent;
            this.name              = name;
            this.manager           = manager;
            this.currentVersion    = new ManagedNode(driverNode, version, this);
            this.versions[version] = new WeakReference(this.currentVersion);
        }
Пример #13
0
        /// <summary>
        /// Obtains child of this node.
        /// </summary>
        /// <param name="n">Internal node if not in cache, can be null for some nodes.</param>
        /// <param name="name">The name of child.</param>
        /// <returns></returns>
        public ManagedNode GetChild(IDriverNode n, string name)
        {
            ManagedCommonNode child;

            // We first search in children.
            WeakReference childRef;

            if (children.TryGetValue(name, out childRef))
            {
                // We extract child.
                child = childRef.Target as ManagedCommonNode;

                // It may still be null, if discared meanwhile.
                if (child != null)
                {
                    return(child.currentVersion);
                }
            }

            if (n == null)
            {
                return(null);
            }

            // We have to do the search in node.
            IDriverNode childNode = n.Find(name);

            // We cannot return it.
            if (childNode == null)
            {
                return(null);
            }

            // Create the child, with the same name reference.
            child = new ManagedCommonNode(this, name, childNode, manager);

            // Add it to cache.
            children[name] = new WeakReference(child);

            // Return most current version.
            return(child.currentVersion);
        }
Пример #14
0
        private void AddDriverRecursive(IDriverNode driver)
        {
            if (driver.Children == null)
            {
                return;
            }
            foreach (var dr in driver.Children)
            {
                _driverNodeStore.Add(dr.Id, dr);

                _licenseContract.IncrementDriverCount();

                if (_licenseContract.DriverLicenseCountExceeded())
                {
                    _logger.LogError("Cannot instantiate more data-points, license exceeded");
                    return;
                }

                AddDriverRecursive(dr);
            }
        }
Пример #15
0
        /// <summary>
        /// Finds a version.
        /// </summary>
        /// <param name="id">The version id.</param>
        /// <returns>Managed node around the version.</returns>
        public ManagedNode GetVersion(IDriverNode n, ulong id)
        {
            // We search in cachd first:
            WeakReference versionRef;

            if (versions.TryGetValue(id, out versionRef))
            {
                ManagedNode version = versionRef.Target as ManagedNode;

                // Can be dereferenced in between.
                if (version != null)
                {
                    return(version);
                }
            }

            // We cannot create it if node does not exist.
            if (n == null)
            {
                return(null);
            }

            // We read it from driver.
            IDriverNode vn = n.GetVersion(id);

            if (vn == null)
            {
                return(null);
            }

            // We create it, add to cache and return it.
            ManagedNode versionNode = new ManagedNode(vn, id, this);

            versions[id] = new WeakReference(versionNode);
            return(versionNode);
        }
 public void Add(IDriverNode node)
 {
     _store.Add(node.Id, node);
 }
Пример #17
0
 /// <summary>
 /// A version constructor.
 /// </summary>
 /// <param name="node">The node.</param>
 /// <param name="version">The version.</param>
 /// <param name="common">The common.</param>
 internal ManagedNode(IDriverNode node, ulong version, ManagedCommonNode common)
 {
     this.node    = node;
     this.version = version;
     this.common  = common;
 }
Пример #18
0
 /// <summary>
 /// Creates a host database with only default raw loadable
 /// (always loading data as deserialization or byte[]).
 /// </summary>
 /// <param name="rootDir"></param>
 public HostDatabase(string rootDir)
 {
     this.directory = rootDir;
     this.root      = NodeFromDirectory(null, rootDir);
 }