Пример #1
0
        public CoinKernelSet(INTMinerRoot root)
        {
            _root = root;
            Global.Access <AddCoinKernelCommand>(
                Guid.Parse("6345c411-4860-433b-ad5e-3a743bcebfa8"),
                "添加币种内核",
                LogEnum.Log,
                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);

                Global.Happened(new CoinKernelAddedEvent(entity));
            });
            Global.Access <UpdateCoinKernelCommand>(
                Guid.Parse("b3dfdf09-f732-4b3b-aeeb-25de7b83d30c"),
                "更新币种内核",
                LogEnum.Log,
                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()];
                entity.Update(message.Input);
                var repository = NTMinerRoot.CreateServerRepository <CoinKernelData>();
                repository.Update(entity);

                Global.Happened(new CoinKernelUpdatedEvent(entity));
            });
            Global.Access <RemoveCoinKernelCommand>(
                Guid.Parse("ee34113f-e616-421d-adcc-c2e810723035"),
                "移除币种内核",
                LogEnum.Log,
                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);

                Global.Happened(new CoinKernelRemovedEvent(entity));
            });
            Global.Logger.InfoDebugLine(this.GetType().FullName + "接入总线");
        }
Пример #2
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));
                }
            });
        }
Пример #3
0
        public CoinKernelSet(IServerContext context)
        {
            _context = context;
            context.AddCmdPath <AddCoinKernelCommand>(LogEnum.DevConsole,
                                                      action: (message) => {
                InitOnece();
                if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (!context.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 = context.CreateServerRepository <CoinKernelData>();
                repository.Add(entity);

                VirtualRoot.RaiseEvent(new CoinKernelAddedEvent(message.MessageId, entity));

                if (context.CoinSet.TryGetCoin(message.Input.CoinId, out ICoin coin))
                {
                    IPool[] pools = context.PoolSet.AsEnumerable().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));
                    }
                }
            }, location: this.GetType());
            context.AddCmdPath <UpdateCoinKernelCommand>(LogEnum.DevConsole,
                                                         action: (message) => {
                InitOnece();
                if (message == null || message.Input == null || message.Input.GetId() == Guid.Empty)
                {
                    throw new ArgumentNullException();
                }
                if (!context.CoinSet.Contains(message.Input.CoinId))
                {
                    throw new ValidationException("there is no coin with id" + message.Input.CoinId);
                }
                if (!_dicById.TryGetValue(message.Input.GetId(), out CoinKernelData entity))
                {
                    return;
                }
                if (ReferenceEquals(entity, message.Input))
                {
                    return;
                }
                entity.Update(message.Input);
                var repository = context.CreateServerRepository <CoinKernelData>();
                repository.Update(entity);

                VirtualRoot.RaiseEvent(new CoinKernelUpdatedEvent(message.MessageId, entity));
            }, location: this.GetType());
            context.AddCmdPath <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 = context.CreateServerRepository <CoinKernelData>();
                repository.Remove(entity.Id);

                VirtualRoot.RaiseEvent(new CoinKernelRemovedEvent(message.MessageId, entity));
                if (context.CoinSet.TryGetCoin(entity.CoinId, out ICoin coin))
                {
                    List <Guid> toRemoves = new List <Guid>();
                    IPool[] pools         = context.PoolSet.AsEnumerable().Where(a => a.CoinId == coin.GetId()).ToArray();
                    foreach (IPool pool in pools)
                    {
                        foreach (PoolKernelData poolKernel in context.PoolKernelSet.AsEnumerable().Where(a => a.PoolId == pool.GetId() && a.KernelId == entity.KernelId).ToArray())
                        {
                            toRemoves.Add(poolKernel.Id);
                        }
                    }
                    foreach (Guid poolKernelId in toRemoves)
                    {
                        VirtualRoot.Execute(new RemovePoolKernelCommand(poolKernelId));
                    }
                }
            }, location: this.GetType());
            context.AddEventPath <FileWriterRemovedEvent>("移除文件书写器后移除引用关系", LogEnum.DevConsole, location: this.GetType(), PathPriority.Normal,
                                                          action: message => {
                var repository = context.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.RaiseEvent(new CoinKernelUpdatedEvent(message.MessageId, entity));
                }
            });
            context.AddEventPath <FragmentWriterRemovedEvent>("移除命令行片段书写器后移除引用关系", LogEnum.DevConsole, location: this.GetType(), PathPriority.Normal,
                                                              action: message => {
                var repository = context.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.RaiseEvent(new CoinKernelUpdatedEvent(message.MessageId, entity));
                }
            });
        }