private void UpdateStores()
        {
            var stores = new List <BroadcastStoreMessage>();

            foreach (var trustSignature in _trustSignatures)
            {
                var store = _amoebaInterfaceManager.GetStore(trustSignature, CancellationToken.None).Result;

                if (store == null)
                {
                    if (_cacheStores.TryGetValue(trustSignature, out var cachedStore))
                    {
                        stores.Add(cachedStore);
                    }
                }
                else
                {
                    if (!_cacheStores.TryGetValue(trustSignature, out var cachedStore) ||
                        store.CreationTime > cachedStore.CreationTime)
                    {
                        _cacheStores[trustSignature] = store;
                        stores.Add(store);
                    }
                    else
                    {
                        stores.Add(cachedStore);
                    }
                }
            }

            lock (_cacheStores.LockObject)
            {
                _cacheStores.Clear();

                foreach (var store in stores)
                {
                    _cacheStores.Add(store.AuthorSignature, store);
                }
            }
        }
Exemple #2
0
        private void WatchThread(CancellationToken token)
        {
            for (; ;)
            {
                var subscribeStoreInfos = new List <StoreSignatureInfo>();

                try
                {
                    App.Current.Dispatcher.Invoke(() =>
                    {
                        if (token.IsCancellationRequested)
                        {
                            return;
                        }

                        var subscribeCategoryInfos = new List <StoreCategoryInfo>();
                        subscribeCategoryInfos.Add(this.TabViewModel.Value.Model);

                        for (int i = 0; i < subscribeCategoryInfos.Count; i++)
                        {
                            subscribeCategoryInfos.AddRange(subscribeCategoryInfos[i].CategoryInfos);
                            subscribeStoreInfos.AddRange(subscribeCategoryInfos[i].SignatureInfos);
                        }
                    }, DispatcherPriority.Background, token);
                }
                catch (OperationCanceledException)
                {
                    return;
                }

                foreach (var storeInfo in subscribeStoreInfos)
                {
                    if (token.IsCancellationRequested)
                    {
                        return;
                    }

                    var message = _amoebaInterfaceManager.GetStore(storeInfo.AuthorSignature, CancellationToken.None).Result;
                    if (message == null || storeInfo.UpdateTime == message.CreationTime)
                    {
                        continue;
                    }

                    var tempBoxInfos = new List <StoreBoxInfo>();

                    foreach (var targetBox in message.Value.Boxes)
                    {
                        tempBoxInfos.Add(CreateStoreBoxInfo(targetBox, storeInfo.BoxInfos.FirstOrDefault(n => n.Name == targetBox.Name)));
                    }

                    try
                    {
                        App.Current.Dispatcher.Invoke(() =>
                        {
                            if (token.IsCancellationRequested)
                            {
                                return;
                            }

                            storeInfo.UpdateTime = message.CreationTime;
                            storeInfo.IsUpdated  = true;
                            storeInfo.BoxInfos.Clear();
                            storeInfo.BoxInfos.AddRange(tempBoxInfos);

                            // Refresh
                            {
                                if (this.TabSelectedItem.Value is StoreSignatureViewModel selectedSignatureViewModel)
                                {
                                    if (selectedSignatureViewModel.Model == storeInfo)
                                    {
                                        this.Refresh();
                                    }
                                }
                                else if (this.TabSelectedItem.Value is StoreBoxViewModel selectedBoxViewModel)
                                {
                                    if (selectedBoxViewModel.GetAncestors().OfType <StoreSignatureViewModel>().First().Model == storeInfo)
                                    {
                                        this.Refresh();
                                    }
                                }
                            }
                        }, DispatcherPriority.Background, token);
                    }
                    catch (OperationCanceledException)
                    {
                        return;
                    }
                }

                {
                    var cacheMetadatas = new HashSet <Metadata>();
                    cacheMetadatas.UnionWith(_amoebaInterfaceManager.GetCacheContentReports().Select(n => n.Metadata));

                    var downloadingMetadatas = new HashSet <Metadata>();
                    downloadingMetadatas.UnionWith(_amoebaInterfaceManager.GetDownloadContentReports().Select(n => n.Metadata));

                    var downloadedMetadatas = new HashSet <Metadata>();
                    downloadedMetadatas.UnionWith(SettingsManager.Instance.DownloadedSeeds.Select(n => n.Metadata));

                    lock (_cacheStates.LockObject)
                    {
                        _cacheStates.Clear();

                        foreach (var metadata in cacheMetadatas)
                        {
                            _cacheStates.Add(metadata, SearchState.Cache);
                        }

                        foreach (var metadata in downloadingMetadatas)
                        {
                            _cacheStates.AddOrUpdate(metadata, SearchState.Downloading, (_, oldValue) => oldValue | SearchState.Downloading);
                        }

                        foreach (var metadata in downloadedMetadatas)
                        {
                            _cacheStates.AddOrUpdate(metadata, SearchState.Downloaded, (_, oldValue) => oldValue | SearchState.Downloaded);
                        }
                    }
                }

                if (token.WaitHandle.WaitOne(1000 * 30))
                {
                    return;
                }
            }
        }
Exemple #3
0
        private void Setting_ChechUpdate()
        {
            _checkUpdateTimer = new WatchTimer(() =>
            {
                try
                {
                    var updateInfo = SettingsManager.Instance.UpdateInfo;
                    if (!updateInfo.IsEnabled)
                    {
                        return;
                    }

                    var store = _serviceManager.GetStore(updateInfo.Signature, CancellationToken.None).Result;
                    if (store == null)
                    {
                        return;
                    }

                    var updateBox = store.Value.Boxes.FirstOrDefault(n => n.Name == "Update")?.Boxes.FirstOrDefault(n => n.Name == "Windows");
                    if (updateBox == null)
                    {
                        return;
                    }

                    Seed targetSeed = null;

                    {
                        var map   = new Dictionary <Seed, Version>();
                        var regex = new Regex(@"Amoeba.+?((\d*)\.(\d*)\.(\d*)).*?\.zip", RegexOptions.Compiled);

                        foreach (var seed in updateBox.Seeds)
                        {
                            var match = regex.Match(seed.Name);
                            if (!match.Success)
                            {
                                continue;
                            }

                            var version = new Version(match.Groups[1].Value);
                            if (version <= AmoebaEnvironment.Version)
                            {
                                continue;
                            }

                            map.Add(seed, version);
                        }

                        if (map.Count > 0)
                        {
                            var sortedList = map.ToList();
                            sortedList.Sort((x, y) => y.Value.CompareTo(x.Value));

                            targetSeed = sortedList.First().Key;
                        }
                    }

                    if (targetSeed == null)
                    {
                        return;
                    }

                    string fullPath = Path.GetFullPath(Path.Combine(AmoebaEnvironment.Paths.UpdatePath, targetSeed.Name));
                    if (File.Exists(fullPath))
                    {
                        return;
                    }

                    var downloadItemInfo = new DownloadItemInfo(targetSeed, fullPath);
                    SettingsManager.Instance.DownloadItemInfos.Add(downloadItemInfo);
                }
                catch (Exception e)
                {
                    Log.Error(e);
                }
            });
            _checkUpdateTimer.Start(new TimeSpan(0, 0, 0), new TimeSpan(0, 3, 0));
        }