コード例 #1
0
        internal void OnNodeAdded(NodeShareInfo ninfo, string nodename, long pctimeStamp)
        {
            var newinf = new NodeInfoForPC {
                NodeGuid    = ninfo.NodeId,
                PCVersion   = ninfo.PCVersion,
                PCTimeStamp = pctimeStamp,
                Url         = ninfo.Url,
                Name        = nodename
            };

            lock (CachedNodes)
            {
                CachedNodes.Add(newinf);
            }
            OnCachedNodesChange();
        }
コード例 #2
0
        public Uri GetWebAppUri(AppLauncher launcher)
        {
            if (launcher is null)
            {
                throw new ArgumentNullException(nameof(launcher));
            }

            NodeInfoForPC node = GetNodeById(launcher.NodeId);

            if (node is null)
            {
                logger.LogError($"Couldn't find {launcher.NodeId}. Cached nodes is {CachedNodes.Count}");
                return(null);
            }

            var nu = node.Url[node.Url.Length - 1] == '/' ? node.Url.Substring(0, node.Url.Length - 1) : node.Url;

            return(new Uri($"{nu}{launcher.WebAddress}?AccKey={launcher.AccessKey}"));
        }
コード例 #3
0
        private void InsertRootFS(NodeInfoForPC node, TopFolderClient cli = null)
        {
            string nm     = node.Name;
            string key    = nm;
            var    loccli = cli;

            if (RootFS.ClientList.ContainsKey(nm))
            {
                //user input duplicated name
                for (int i = 2; i < int.MaxValue; i++)
                {
                    key = $"{nm} ({i})";
                    if (RootFS.ClientList.ContainsKey(key))
                    {
                        continue;
                    }
                    else
                    {
                        break;
                    }
                }

                if (loccli == null)
                {
#pragma warning disable CA2000 // Collection elements are disposed elsewhere.
                    loccli = new TopFolderClient(node.Url, MasterKey, Id)
                    {
                        Name      = node.Name,
                        NodeId    = node.NodeGuid,
                        TimeStamp = node.PCTimeStamp
                    };
#pragma warning restore CA2000
                }
                var ret = RootFS.ClientList.TryAdd(key, loccli);
                if ((!ret) && (cli == null))
                {
                    loccli.Dispose();
                }

                loccli = null;
            }
            else
            {
                if (loccli == null)
                {
#pragma warning disable CA2000 // Collection elements are disposed elsewhere.
                    loccli = new TopFolderClient(node.Url, MasterKey, Id)
                    {
                        Name      = node.Name,
                        NodeId    = node.NodeGuid,
                        TimeStamp = node.PCTimeStamp
                    };
#pragma warning restore CA2000
                }
                var ret = RootFS.ClientList.TryAdd(key, loccli);
                if ((!ret) && (cli == null))
                {
                    loccli.Dispose();
                }

                loccli = null;
            }
        }
コード例 #4
0
        internal async Task OnNodeUpdate(NodeInfo ninfo, List <SSDPPCInfo> ssdpinfo)
        {
            bool updated = false;
            bool deleted = false;

            lock (CachedNodes)
            {
                var ssdpin = ssdpinfo.FirstOrDefault(x => x.Id == Id);
                if (ssdpin != null)
                {
                    var name = DecryptName(ssdpin.EN);
                    if (name == null)
                    {
                        logger.LogError($"Invalid node from {ninfo.Url}");
                        return;
                    }

                    NodeInfoForPC info = CachedNodes.FirstOrDefault(x => x.NodeGuid == ninfo.NodeGuid);
                    if (info != null)
                    {
                        if (info.PCTimeStamp < ssdpin.TimeStamp)
                        {
                            CachedNodes.Remove(info);
                        }
                        else
                        {
                            return;
                        }
                    }
                    var newinf = new NodeInfoForPC {
                        NodeGuid    = ninfo.NodeGuid,
                        PCVersion   = ninfo.PCVersion,
                        PCTimeStamp = ssdpin.TimeStamp,
                        Url         = ninfo.Url,
                        Name        = name
                    };
                    CachedNodes.Add(newinf);
                    OnCachedNodesChange();
                    updated = true;
                }
                else
                {
                    int nremoved = 0;
                    if (CachedNodes.FirstOrDefault(x => x.NodeGuid == ninfo.NodeGuid) != null)
                    {
                        nremoved = CachedNodes.RemoveAll(x => x.NodeGuid == ninfo.NodeGuid);
                    }
                    if (nremoved > 0)
                    {
                        OnCachedNodesChange();
                        deleted = true;
                    }
                }
            }
            if (updated || deleted)
            {
                if (!deleted)
                {
                    var pci = await GetPeerPCInfo(this, ninfo).ConfigureAwait(false);

                    if ((pci != null) && (pci.StorageProviders != null))
                    {
                        bool needResync = false;
                        foreach (var sp in pci.StorageProviders)
                        {
                            if (sp.Visibility != StorageProviderVisibility.Public)
                            {
                                continue;
                            }
                            if (sp.Type == StorageProviderInstance.TypeAliYun)
                            {
                                lock (StorageProviderInstances)
                                {
                                    if (!StorageProviderInstances.Any(x => sp.Id.Equals(x.RuntimeId)))
                                    {
                                        var instance = new StorageProviderInstance_AliyunOSS(new StorageProviderInfo {
                                            Id         = sp.Id,
                                            Type       = StorageProviderInstance.TypeAliYun,
                                            Name       = sp.Name,
                                            Visibility = sp.Visibility,
                                            Settings   = sp.Settings
                                        });
                                        StorageProviderInstances.Add(instance);
                                        needResync = true;
                                    }
                                }
                            }
                            if (sp.Type == StorageProviderInstance.TypeAzure)
                            {
                                lock (StorageProviderInstances)
                                {
                                    if (!StorageProviderInstances.Any(x => sp.Id.Equals(x.RuntimeId)))
                                    {
                                        var instance = new StorageProviderInstance_AzureBlob(new StorageProviderInfo {
                                            Id         = sp.Id,
                                            Type       = StorageProviderInstance.TypeAzure,
                                            Name       = sp.Name,
                                            Visibility = sp.Visibility,
                                            Settings   = sp.Settings
                                        });
                                        StorageProviderInstances.Add(instance);
                                        needResync = true;
                                    }
                                }
                            }
                        }
                        if (needResync)
                        {
                            ResyncClientListToStorageProviderInstances();
                        }
                    }
                    if ((pci != null) && (pci.Apps != null))
                    {
                        foreach (var ai in pci.Apps)
                        {
                            NodeInfoForPC n = null;
                            lock (CachedNodes)
                            {
                                n = CachedNodes.FirstOrDefault(x => x.NodeGuid == ai.NodeId);
                            }
                            if (n == null)
                            {
                                return;
                            }
                            var a = Apps.FirstOrDefault(x => (x.NodeId == ai.NodeId) && (x.AppId == ai.AppId) && (x.Name == ai.Name));
                            if (a == null)
                            {
                                lock (Apps)
                                {
                                    Apps.Add(ai);
                                }
                            }
                        }
                    }
                }
                _ = Task.Run(() => {
                    try
                    {
                        OnNodeChangedEvent?.Invoke(this, EventArgs.Empty);
                    }
                    catch
                    {
                    }
                });
            }
        }