Beispiel #1
0
 public static void Clear(IEventBroadcaster eventBroadcaster)
 {
     if (subscribersDic.ContainsKey(eventBroadcaster))
     {
         subscribersDic.Remove(eventBroadcaster);
     }
 }
Beispiel #2
0
        internal static void Register <T>(IEventBroadcaster eventBroadcaster, string tag, Action <T> cb)
        {
            var type = typeof(T);

            if (!subscribersDic.TryGetValue(eventBroadcaster, out var dic))
            {
                dic = new Dictionary <Type, List <ICallBackBase> >();
                subscribersDic.Add(eventBroadcaster, dic);
            }

            if (!dic.TryGetValue(type, out var list))
            {
                list = new List <ICallBackBase>();
                dic.Add(type, list);
            }

            if (!CallBackExists(list, tag, cb))
            {
                ICallBackBase callBackBase = new MulCallbackContainer <T>(tag, cb);
                list.Add(callBackBase);
            }
            else
            {
                Log.Warning("repeated register....");
            }
        }
Beispiel #3
0
        internal static void UnRegister <T>(IEventBroadcaster eventBroadcaster, string tag, Action <T> cb)
        {
            var type = typeof(T);

            if (!subscribersDic.TryGetValue(eventBroadcaster, out var dic))
            {
                Log.Warning("try unRegister empty....");
                return;
            }

            if (!dic.TryGetValue(type, out var list))
            {
                Log.Warning("try unRegister empty....");
                return;
            }

            var callBackBase = GetCallBackBase(list, tag, cb);

            if (cb == null)
            {
                Log.Warning("try unRegister empty....");
                return;
            }

            list.Remove(callBackBase);
        }
Beispiel #4
0
        public void 局部事件()
        {
            IEventBroadcaster eventBroadcaster = Substitute.For <IEventBroadcaster>();

            eventBroadcaster.Register <AMsg>(AMsgCB);
            eventBroadcaster.TriggerEvent(new AMsg());
            Assert.IsTrue(IsCall);
        }
Beispiel #5
0
 public InputCircuitsManager(ICircuitsDalHelper circuitsDalHelper,
                             IPinMapper mapper,
                             IDateProvider dateProvider,
                             IEventBroadcaster eventBroadcaster)
 {
     _circuitsDalHelper = circuitsDalHelper;
     _mapper            = mapper;
     _dateProvider      = dateProvider;
     _eventBroadcaster  = eventBroadcaster;
 }
Beispiel #6
0
 public Scheduler(IMemory memory, ILog log, INetworkServicePool networkServicePool, IEventBroadcaster eventBroadcaster)
 {
     _memory                  = memory;
     _log                     = log;
     _networkServicePool      = networkServicePool;
     _eventBroadcaster        = eventBroadcaster;
     _objectDisposed          = false;
     _renderingLock           = new object();
     _extractionLock          = new object();
     _verificationLock        = new object();
     _cancellationTokenSource = new CancellationTokenSource();
 }
Beispiel #7
0
 public Spooler(
     string database,
     string login,
     string password,
     string event_table,
     IEventBroadcaster messageBroker,
     string applicationName = "event spooler",
     string host            = "localhost",
     int port = 5432)
 {
     this.connectionString = $"Host={host};Port={port};Username={login};Password={password};Application name={applicationName};Database={database}";
     this.messageBroker    = messageBroker;
     this.process          = Process.GetCurrentProcess();
 }
Beispiel #8
0
        public static void TriggerEvent <T>(IEventBroadcaster eventBroadcaster, string tag, T value)
        {
            var type = typeof(T);

            if (!_subscribersDic.TryGetValue(eventBroadcaster, out var dic))
            {
                return;
            }
            if (!dic.TryGetValue(type, out var list))
            {
                return;
            }
            // ReSharper disable once PossibleInvalidCastExceptionInForeachLoop
            foreach (MulCallbackContainer <T> callbackContainer in list)
            {
                callbackContainer.Trigger(tag, value);
            }
        }
Beispiel #9
0
        public static void TriggerEvent <T>(IEventBroadcaster eventBroadcaster, string tag, T value)
        {
            var type = typeof(T);

            if (!subscribersDic.TryGetValue(eventBroadcaster, out var dic))
            {
                return;
            }
            if (!dic.TryGetValue(type, out var list))
            {
                return;
            }
            foreach (var callBackBase in list)
            {
                var callbackContainer = (MulCallbackContainer <T>)callBackBase;
                callbackContainer.Trigger(tag, value);
            }
        }
Beispiel #10
0
 public GameMoveManager(IGameRepository gameRepository,
                        IGameTeamRepository gameTeamRepository,
                        IGameStatusRepository gameStatusRepository,
                        IMoveValidatorFactory validationFactory,
                        IEventBroadcaster eventBroadcaster,
                        IGameMoveRepository gameMoveRepository,
                        IGameFieldRepository gameFieldRepository,
                        INextTeamProvider nextTeamProvider,
                        IEndGameChecker endGameChecker)
 {
     this.gameRepository       = gameRepository;
     this.validationFactory    = validationFactory;
     this.gameTeamRepository   = gameTeamRepository;
     this.gameStatusRepository = gameStatusRepository;
     this.eventBroadcaster     = eventBroadcaster;
     this.gameMoveRepository   = gameMoveRepository;
     this.gameFieldRepository  = gameFieldRepository;
     this.nextTeamProvider     = nextTeamProvider;
     this.endGameChecker       = endGameChecker;
 }
Beispiel #11
0
 public GameCreator(IGameCreatorValidator validator,
                    IDbTransactionProvider transactionProvider,
                    IGameRepository gameRepository,
                    IGameTeamRepository gameTeamRepository,
                    IGameStatusRepository gameStatusRepository,
                    IGameFieldsGenerator gameFieldsGenerator,
                    IRandomWordRepository randomWordRepository,
                    IGameFieldRepository gameFieldRepository,
                    IGameTeamsGenerator gameTeamsGenerator,
                    IEventBroadcaster eventBroadcaster)
 {
     this.validator            = validator;
     this.transactionProvider  = transactionProvider;
     this.gameRepository       = gameRepository;
     this.gameTeamRepository   = gameTeamRepository;
     this.gameStatusRepository = gameStatusRepository;
     this.gameFieldsGenerator  = gameFieldsGenerator;
     this.randomWordRepository = randomWordRepository;
     this.gameFieldRepository  = gameFieldRepository;
     this.gameTeamsGenerator   = gameTeamsGenerator;
     this.eventBroadcaster     = eventBroadcaster;
 }
 public HomeController(IHostingEnvironment env, IEventBroadcaster b)
 {
     f            = env.WebRootFileProvider;
     _broadcaster = b;
 }
Beispiel #13
0
 public static void UnRegister <T>(this IEventBroadcaster broadcaster, string tag, Action <T> cb)
 {
     LocalEventService.UnRegister(broadcaster, tag, cb);
 }
Beispiel #14
0
 public static void UnRegister <T>(this IEventBroadcaster broadcaster, Action <T> cb)
 {
     broadcaster.UnRegister(string.Empty, cb);
 }
Beispiel #15
0
        public NetworkServicePool(Configurations configurations, IEventBroadcaster eventBroadcaster, IHardwareMonitor hardwareMonitor,
                                  IMemory memory, ILog log, Func <IResourceExtractor> getResourceExtractor, Func <IResourceVerifier> getResourceVerifier,
                                  Func <IHtmlRenderer> getHtmlRenderer)
        {
            _log                   = log;
            _objectDisposed        = false;
            _statistics            = new Statistics();
            _eventBroadcaster      = eventBroadcaster;
            _resourceExtractorPool = new BlockingCollection <IResourceExtractor>();
            _resourceVerifierPool  = new BlockingCollection <IResourceVerifier>();
            _htmlRendererPool      = new BlockingCollection <IHtmlRenderer>();

            PreCreateServices();
            CreateAndDestroyHtmlRenderersAdaptively();

            void PreCreateServices()
            {
                PreCreateResourceExtractors();
                PreCreateResourceVerifiers();
                CreateHtmlRenderer();

                void PreCreateResourceExtractors()
                {
                    for (var resourceExtractorId = 0; resourceExtractorId < configurations.ResourceExtractorCount; resourceExtractorId++)
                    {
                        var resourceExtractor = getResourceExtractor();
                        _resourceExtractorPool.Add(resourceExtractor);
                        _statistics.CreatedResourceExtractorCount++;
                    }
                }

                void PreCreateResourceVerifiers()
                {
                    for (var resourceVerifierId = 0; resourceVerifierId < configurations.ResourceVerifierCount; resourceVerifierId++)
                    {
                        var resourceVerifier = getResourceVerifier();
                        _resourceVerifierPool.Add(resourceVerifier);
                        _statistics.CreatedResourceVerifierCount++;
                    }
                }
            }

            void CreateAndDestroyHtmlRenderersAdaptively()
            {
                hardwareMonitor.OnLowCpuAndMemoryUsage += (averageCpuUsage, memoryUsage) =>
                {
                    if (_htmlRendererPool.Count > 0 || _statistics.CreatedHtmlRendererCount == configurations.MaxHtmlRendererCount)
                    {
                        return;
                    }
                    CreateHtmlRenderer();

                    var createdHtmlRendererCount = _statistics.CreatedHtmlRendererCount;
                    _log.Info(
                        $"Low CPU usage ({averageCpuUsage}%) and low memory usage ({memoryUsage}%) detected. " +
                        $"Browser count increased from {createdHtmlRendererCount - 1} to {createdHtmlRendererCount}."
                        );
                };
                hardwareMonitor.OnHighCpuOrMemoryUsage += (averageCpuUsage, memoryUsage) =>
                {
                    if (_statistics.CreatedHtmlRendererCount == 1)
                    {
                        return;
                    }
                    _htmlRendererPool.Take().Dispose();
                    _statistics.CreatedHtmlRendererCount--;

                    if (averageCpuUsage == null && memoryUsage == null)
                    {
                        throw new ArgumentException(nameof(averageCpuUsage), nameof(memoryUsage));
                    }

                    var createdHtmlRendererCount = _statistics.CreatedHtmlRendererCount;
                    if (averageCpuUsage != null && memoryUsage != null)
                    {
                        _log.Info(
                            $"High CPU usage ({averageCpuUsage}%) and high memory usage ({memoryUsage}%) detected. " +
                            $"Browser count decreased from {createdHtmlRendererCount + 1} to {createdHtmlRendererCount}."
                            );
                    }
                    else if (averageCpuUsage != null)
                    {
                        _log.Info(
                            $"High CPU usage ({averageCpuUsage}%) detected. " +
                            $"Browser count decreased from {createdHtmlRendererCount + 1} to {createdHtmlRendererCount}."
                            );
                    }
                    else
                    {
                        _log.Info(
                            $"High memory usage ({memoryUsage}%) detected. " +
                            $"Browser count decreased from {createdHtmlRendererCount + 1} to {createdHtmlRendererCount}."
                            );
                    }
                };
            }

            void CreateHtmlRenderer()
            {
                var htmlRenderer = getHtmlRenderer();

                htmlRenderer.OnResourceCaptured += memory.MemorizeToBeVerifiedResource;
                _htmlRendererPool.Add(htmlRenderer);
                _statistics.CreatedHtmlRendererCount++;
            }
        }
Beispiel #16
0
 public static void TriggerEvent <T>(this IEventBroadcaster broadcaster, string tag, T value)
 {
     LocalEventService.TriggerEvent(broadcaster, tag, value);
 }
Beispiel #17
0
 public static void TriggerEvent <T>(this IEventBroadcaster broadcaster, T value)
 {
     broadcaster.TriggerEvent(string.Empty, value);
 }