Exemplo n.º 1
0
        public CoinKernelSet(INTMinerRoot root)
        {
            _root = root;
            _root.ServerContextWindow <AddCoinKernelCommand>("添加币种内核", LogEnum.DevConsole,
                                                             action: (message) => {
                InitOnece();
                if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (!_root.CoinSet.Contains(message.Input.CoinId))
                {
                    throw new ValidationException("there is no coin with id" + message.Input.CoinId);
                }
                if (_dicById.ContainsKey(message.Input.GetId()))
                {
                    return;
                }
                if (_dicById.Values.Any(a => a.CoinId == message.Input.CoinId && a.KernelId == message.Input.KernelId))
                {
                    return;
                }
                CoinKernelData entity = new CoinKernelData().Update(message.Input);
                _dicById.Add(entity.Id, entity);
                var repository = NTMinerRoot.CreateServerRepository <CoinKernelData>();
                repository.Add(entity);

                VirtualRoot.Happened(new CoinKernelAddedEvent(entity));

                ICoin coin;
                if (root.CoinSet.TryGetCoin(message.Input.CoinId, out coin))
                {
                    IPool[] pools = root.PoolSet.Where(a => a.CoinId == coin.GetId()).ToArray();
                    foreach (IPool pool in pools)
                    {
                        Guid poolKernelId = Guid.NewGuid();
                        var poolKernel    = new PoolKernelData()
                        {
                            Id       = poolKernelId,
                            Args     = string.Empty,
                            KernelId = message.Input.KernelId,
                            PoolId   = pool.GetId()
                        };
                        VirtualRoot.Execute(new AddPoolKernelCommand(poolKernel));
                    }
                }
            });
            _root.ServerContextWindow <UpdateCoinKernelCommand>("更新币种内核", LogEnum.DevConsole,
                                                                action: (message) => {
                InitOnece();
                if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (!_root.CoinSet.Contains(message.Input.CoinId))
                {
                    throw new ValidationException("there is no coin with id" + message.Input.CoinId);
                }
                if (!_dicById.ContainsKey(message.Input.GetId()))
                {
                    return;
                }
                CoinKernelData entity = _dicById[message.Input.GetId()];
                if (ReferenceEquals(entity, message.Input))
                {
                    return;
                }
                entity.Update(message.Input);
                var repository = NTMinerRoot.CreateServerRepository <CoinKernelData>();
                repository.Update(entity);

                VirtualRoot.Happened(new CoinKernelUpdatedEvent(entity));
            });
            _root.ServerContextWindow <RemoveCoinKernelCommand>("移除币种内核", LogEnum.DevConsole,
                                                                action: (message) => {
                InitOnece();
                if (message == null || message.EntityId == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (!_dicById.ContainsKey(message.EntityId))
                {
                    return;
                }
                CoinKernelData entity = _dicById[message.EntityId];
                _dicById.Remove(entity.Id);
                var repository = NTMinerRoot.CreateServerRepository <CoinKernelData>();
                repository.Remove(entity.Id);

                VirtualRoot.Happened(new CoinKernelRemovedEvent(entity));
                ICoin coin;
                if (root.CoinSet.TryGetCoin(entity.CoinId, out coin))
                {
                    List <Guid> toRemoves = new List <Guid>();
                    IPool[] pools         = root.PoolSet.Where(a => a.CoinId == coin.GetId()).ToArray();
                    foreach (IPool pool in pools)
                    {
                        foreach (PoolKernelData poolKernel in root.PoolKernelSet.Where(a => a.PoolId == pool.GetId() && a.KernelId == entity.KernelId))
                        {
                            toRemoves.Add(poolKernel.Id);
                        }
                    }
                    foreach (Guid poolKernelId in toRemoves)
                    {
                        VirtualRoot.Execute(new RemovePoolKernelCommand(poolKernelId));
                    }
                }
            });
            _root.ServerContextOn <FileWriterRemovedEvent>("移除文件书写器后移除引用关系", LogEnum.DevConsole,
                                                           action: message => {
                var repository = NTMinerRoot.CreateServerRepository <CoinKernelData>();
                var entities   = _dicById.Values.Where(a => a.FileWriterIds.Contains(message.Source.GetId())).ToArray();
                foreach (var entity in entities)
                {
                    entity.FileWriterIds = new List <Guid>(entity.FileWriterIds.Where(a => a != message.Source.GetId()));
                    repository.Update(entity);
                    VirtualRoot.Happened(new CoinKernelUpdatedEvent(entity));
                }
            });
            _root.ServerContextOn <FragmentWriterRemovedEvent>("移除命令行片段书写器后移除引用关系", LogEnum.DevConsole,
                                                               action: message => {
                var repository = NTMinerRoot.CreateServerRepository <CoinKernelData>();
                var entities   = _dicById.Values.Where(a => a.FragmentWriterIds.Contains(message.Source.GetId())).ToArray();
                foreach (var entity in entities)
                {
                    entity.FragmentWriterIds = new List <Guid>(entity.FragmentWriterIds.Where(a => a != message.Source.GetId()));
                    repository.Update(entity);
                    VirtualRoot.Happened(new CoinKernelUpdatedEvent(entity));
                }
            });
        }
Exemplo n.º 2
0
        public KernelOutputTranslaterSet(INTMinerRoot root, bool isUseJson)
        {
            _root      = root;
            _isUseJson = isUseJson;
            _root.ServerContextWindow <AddKernelOutputTranslaterCommand>("添加内核输出翻译器", LogEnum.DevConsole,
                                                                         action: (message) => {
                InitOnece();
                if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (string.IsNullOrEmpty(message.Input.RegexPattern))
                {
                    throw new ValidationException($"{nameof(message.Input.RegexPattern)} can't be null or empty");
                }
                if (_dicById.ContainsKey(message.Input.GetId()))
                {
                    return;
                }
                KernelOutputTranslaterData entity = new KernelOutputTranslaterData().Update(message.Input);
                _dicById.Add(entity.Id, entity);
                if (!_dicByKernelOutputId.ContainsKey(entity.KernelOutputId))
                {
                    _dicByKernelOutputId.Add(entity.KernelOutputId, new List <KernelOutputTranslaterData>());
                }
                _dicByKernelOutputId[entity.KernelOutputId].Add(entity);
                var repository = NTMinerRoot.CreateServerRepository <KernelOutputTranslaterData>(isUseJson);
                repository.Add(entity);

                VirtualRoot.Happened(new KernelOutputTranslaterAddedEvent(entity));
            });
            _root.ServerContextWindow <UpdateKernelOutputTranslaterCommand>("更新内核输出翻译器", LogEnum.DevConsole,
                                                                            action: (message) => {
                InitOnece();
                if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (string.IsNullOrEmpty(message.Input.RegexPattern))
                {
                    throw new ValidationException($"{nameof(message.Input.RegexPattern)} can't be null or empty");
                }
                if (!_dicById.ContainsKey(message.Input.GetId()))
                {
                    return;
                }
                KernelOutputTranslaterData entity = _dicById[message.Input.GetId()];
                if (ReferenceEquals(entity, message.Input))
                {
                    return;
                }
                string regexPattern = entity.RegexPattern;
                string color        = entity.Color;
                entity.Update(message.Input);
                if (entity.RegexPattern != regexPattern)
                {
                    _regexDic.Remove(entity);
                }
                if (entity.Color != color)
                {
                    _colorDic.Remove(entity);
                }
                _dicByKernelOutputId[entity.KernelOutputId].Sort(new SortNumberComparer());
                var repository = NTMinerRoot.CreateServerRepository <KernelOutputTranslaterData>(isUseJson);
                repository.Update(entity);

                VirtualRoot.Happened(new KernelOutputTranslaterUpdatedEvent(entity));
            });
            _root.ServerContextWindow <RemoveKernelOutputTranslaterCommand>("移除内核输出翻译器", LogEnum.DevConsole,
                                                                            action: (message) => {
                InitOnece();
                if (message == null || message.EntityId == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (!_dicById.ContainsKey(message.EntityId))
                {
                    return;
                }
                KernelOutputTranslaterData entity = _dicById[message.EntityId];
                _dicById.Remove(entity.Id);
                _dicByKernelOutputId[entity.KernelOutputId].Remove(entity);
                _colorDic.Remove(entity);
                _regexDic.Remove(entity);
                _dicByKernelOutputId[entity.KernelOutputId].Sort(new SortNumberComparer());
                var repository = NTMinerRoot.CreateServerRepository <KernelOutputTranslaterData>(isUseJson);
                repository.Remove(entity.Id);

                VirtualRoot.Happened(new KernelOutputTranslaterRemovedEvent(entity));
            });
            _root.ServerContextOn <SysDicItemUpdatedEvent>($"{Consts.LogColorDicKey}字典项更新后刷新翻译器内存", LogEnum.DevConsole,
                                                           action: message => {
                if (!_root.SysDicSet.TryGetSysDic(Consts.LogColorDicKey, out ISysDic dic))
                {
                    return;
                }
                if (message.Source.DicId != dic.GetId())
                {
                    return;
                }
                foreach (var entity in _dicById.Values)
                {
                    if (entity.Color == message.Source.Code)
                    {
                        _colorDic.Remove(entity);
                    }
                }
            });
        }