Esempio n. 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();
        }
Esempio n. 2
0
        public Uri GetWebAppUri(AppLauncher launcher)
        {
            if (launcher is null)
            {
                throw new ArgumentNullException(nameof(launcher));
            }

            var node = CachedNodes.FirstOrDefault(x => x.NodeGuid == launcher.NodeId);

            if (node is null)
            {
                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}"));
        }
Esempio n. 3
0
        void OnCachedNodesChange()
        {
            lock (CachedNodes)
            {
                NodeInfoForPC[] nodes;
                nodes = CachedNodes.ToArray();
                foreach (var node in nodes)
                {
                    var cur = RootFS.ClientList.FirstOrDefault(x => {//find by node id
                        if (x.Value is TopFolderClient tpc)
                        {
                            if (tpc.NodeId == node.NodeGuid)
                            {
                                return(true);
                            }
                        }
                        return(false);
                    });
                    if (cur.Key == null)//not found
                    {
                        InsertRootFS(node);
                    }
                    else
                    {
                        if (cur.Value is TopFolderClient tpc)
                        {
                            if (tpc.TimeStamp >= node.PCTimeStamp)
                            {
                                continue;
                            }
                            var origname = tpc.Name;
                            tpc.hostUri   = new Uri(node.Url);
                            tpc.Name      = node.Name;
                            tpc.NodeId    = node.NodeGuid;
                            tpc.TimeStamp = node.PCTimeStamp;
                            if (origname != node.Name)
                            {
                                InsertRootFS(node, tpc);
                                RootFS.ClientList.TryRemove(origname, out _);
                            }
                            else
                            {// do nothing
                            }
                        }
                    }
                }

                var lis = RootFS.ClientList.Where(x => {
                    if (x.Value is TopFolderClient tpc)
                    {
                        lock (CachedNodes)
                        {
                            return(CachedNodes.FirstOrDefault(y => y.NodeGuid == tpc.NodeId) == null);
                        }
                    }
                    return(false);
                }).ToList();
                foreach (var item in lis)
                {
                    RootFS.ClientList.TryRemove(item.Key, out _);
                }
            }
        }
Esempio n. 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
                    {
                    }
                });
            }
        }