예제 #1
0
        protected MessageDispatcherTestBase()
        {
            var clock = Substitute.For <IClock>();
            var replyQueueNameSetting = new ReplyQueueNameSetting(
                new ApplicationNameSetting {
                Value = "TestApplication"
            },
                new InstanceNameSetting {
                Value = "TestInstance"
            });

            TypeProvider = new TestHarnessTypeProvider(new[] { GetType().Assembly }, new[] { GetType().Namespace });
            var serializer = new DataContractSerializer(TypeProvider);

            HandlerMapper          = new HandlerMapper(TypeProvider);
            BrokeredMessageFactory = new BrokeredMessageFactory(new DefaultMessageTimeToLiveSetting(),
                                                                new MaxLargeMessageSizeSetting(),
                                                                new MaxSmallMessageSizeSetting(),
                                                                replyQueueNameSetting,
                                                                clock,
                                                                new NullCompressor(),
                                                                new DispatchContextManager(),
                                                                new UnsupportedLargeMessageBodyStore(),
                                                                serializer,
                                                                TypeProvider);
        }
        protected override async Task <CommandMessageDispatcher> Given()
        {
            BusId             = Guid.NewGuid();
            MethodCallCounter = MethodCallCounter.CreateInstance(BusId);

            var clock                 = new SystemClock();
            var logger                = new ConsoleLogger();
            var typeProvider          = new TestHarnessTypeProvider(new[] { GetType().Assembly }, new[] { GetType().Namespace });
            var serializer            = new DataContractSerializer(typeProvider);
            var replyQueueNameSetting = new ReplyQueueNameSetting(
                new ApplicationNameSetting {
                Value = "TestApplication"
            },
                new InstanceNameSetting {
                Value = "TestInstance"
            },
                new PathFactory(new GlobalPrefixSetting()));
            var propertyInjector = new StubPropertyInjector(BusId);

            var handlerMap = new HandlerMapper(typeProvider).GetFullHandlerMap(typeof(IHandleCommand <>));

            _nimbusMessageFactory = new NimbusMessageFactory(new DefaultMessageTimeToLiveSetting(),
                                                             replyQueueNameSetting,
                                                             clock,
                                                             new DispatchContextManager());

            return(new CommandMessageDispatcher(new DependencyResolver(typeProvider),
                                                new NullInboundInterceptorFactory(),
                                                new NullLogger(),
                                                handlerMap,
                                                propertyInjector));
        }
예제 #3
0
        internal UdpConnection(Socket socket,
                               MemoryPool <byte> memoryPool,
                               PipeScheduler scheduler,
                               IUdpConfiguration udpConfiguration,
                               ILogger logger,
                               HandlerMapper handlerMapper)
        {
            Debug.Assert(socket != null);
            Debug.Assert(memoryPool != null);
            Debug.Assert(logger != null);
            Debug.Assert(handlerMapper != null);

            _socket           = socket;
            MemoryPool        = memoryPool;
            Scheduler         = scheduler;
            _logger           = logger;
            _handlerMapper    = handlerMapper;
            _udpConfiguration = udpConfiguration;

            // On *nix platforms, Sockets already dispatches to the ThreadPool.
            // Yes, the IOQueues are still used for the PipeSchedulers. This is intentional.
            // https://github.com/aspnet/KestrelHttpServer/issues/2573
            var awaiterScheduler = IsWindows ? Scheduler : PipeScheduler.Inline;

            _pipe = new Pipe(new PipeOptions(MemoryPool, awaiterScheduler, awaiterScheduler, useSynchronizationContext: false));

            _receiver = new UdpReceiver(_socket, awaiterScheduler);
        }
        public static async Task CallDifferenceHandlers(int fileType, DifferenceType differenceType, object[] parameters)
        {
            // Get the handlers
            SortedList <int, MethodInfo> methodInfos = HandlerMapper.GetHandlers((int)fileType, differenceType);

            // Loop over every handler
            foreach (MethodInfo methodInfo in methodInfos.Values)
            {
                await DiscordBot.LoggingChannel.SendMessageAsync("**[BCAT]** Calling " + methodInfo.DeclaringType.Name + "." + methodInfo.Name + "()");

                try
                {
                    // Invoke the method
                    object returnValue = methodInfo.Invoke(null, parameters);

                    // Check the return value
                    if (returnValue != null && returnValue is Task)
                    {
                        await(Task) returnValue;
                    }
                }
                catch (Exception exception)
                {
                    // Notify the logging channel
                    await DiscordUtil.HandleException((exception is TargetInvocationException)?((TargetInvocationException)exception).InnerException : exception, $"in ``{methodInfo.DeclaringType.Name}.{methodInfo.Name}()``");
                }
            }
        }
예제 #5
0
        protected override async Task <CommandMessageDispatcher> Given()
        {
            var clock                 = new SystemClock();
            var logger                = new ConsoleLogger();
            var typeProvider          = new TestHarnessTypeProvider(new[] { GetType().Assembly }, new[] { GetType().Namespace });
            var serializer            = new DataContractSerializer(typeProvider);
            var replyQueueNameSetting = new ReplyQueueNameSetting(
                new ApplicationNameSetting {
                Value = "TestApplication"
            },
                new InstanceNameSetting {
                Value = "TestInstance"
            });

            var handlerMap = new HandlerMapper(typeProvider).GetFullHandlerMap(typeof(IHandleCommand <>));

            _nimbusMessageFactory = new NimbusMessageFactory(new DefaultMessageTimeToLiveSetting(),
                                                             replyQueueNameSetting,
                                                             clock,
                                                             new DispatchContextManager());

            return(new CommandMessageDispatcher(new DependencyResolver(typeProvider),
                                                new NullInboundInterceptorFactory(),
                                                new NullLogger(),
                                                handlerMap,
                                                Substitute.For <IPropertyInjector>()));
        }
예제 #6
0
        private async Task Run(bool fastShutdown)
        {
            Console.WriteLine($"{(fastShutdown ? "Fast" : "Slow")} shutdown started");

            if (!fastShutdown)
            {
                // Shutdown the Scheduler
                await QuartzScheduler.Dispose();

                // Shutdown the DiscordBot
                await DiscordBot.Dispose();
            }
            else
            {
                // Create a backup of the current config.json just in case
                File.Copy(Boot.LOCAL_CONFIGURATION, Boot.LOCAL_CONFIGURATION_AUTOMATIC_BACKUP, true);
            }

            // Shutdown Twitter
            TwitterManager.Dispose();

            // Shutdown DigitalOcean
            DoApi.Dispose();

            // Shutdown S3
            S3Api.Dispose();

            // Shutdown BCAT
            BcatApi.Dispose();

            // Shutdown DAuth
            DAuthApi.Dispose();

            // Shutdown the HandlerMapper
            HandlerMapper.Dispose();

            // Shutdown the KeysetManager
            KeysetManager.Dispose();

            // Shutdown anything app-specific
            ShutdownAppSpecificItems();

            // Save the configuration
            Configuration.LoadedConfiguration.Write();

            if (!fastShutdown)
            {
                // Wait a little while
                await Task.Delay(1000 *ShutdownWaitTime);
            }

            Console.WriteLine("Shutdown complete");
        }
예제 #7
0
        static void Main(string[] args)
        {
            var logRepository = LogManager.GetRepository(Assembly.GetEntryAssembly());

            XmlConfigurator.Configure(logRepository, new FileInfo("log4net.config"));
            var log           = log4net.LogManager.GetLogger(typeof(Program));
            var settings      = GetSettings();
            var mongoDbClient = new MongoDbClient(settings.MongoConnectionString, TimeSpan.FromSeconds(2));

            ThreadPool.SetMaxThreads(32767, 32767);
            ThreadPool.SetMinThreads(2048, 2048);

            var(authorizer, sourceStorage) = new StateRestorer(mongoDbClient).Restore().GetAwaiter().GetResult();

            var addUserInfoHandler = new AddSourceInfoHandler(mongoDbClient, authorizer, sourceStorage, log);
            var sseClient          = new SseClient();
            var subscriber         = new Subscriber(authorizer, sourceStorage, sseClient);
            var messagSender       = new MessageSender();
            var sendMessageHandler = new SendMessageHandler(messagSender, mongoDbClient, sourceStorage, authorizer, settings.dataTtl);
            var handlerMapper      = new HandlerMapper();

            var expDaemon = new ExpirationDaemon(sourceStorage, settings.dataTtl);

            handlerMapper.Add("/addUserInfo", HttpMethod.Post, addUserInfoHandler);
            handlerMapper.Add("/subscribe", HttpMethod.Get, new SubscribeOnSourceHandler(subscriber));
            handlerMapper.Add("/sendMessage", HttpMethod.Post, sendMessageHandler);


            var routingHandler = new RoutingHandler(handlerMapper, log);

            var host = WebHost.CreateDefaultBuilder(args)
                       .ConfigureServices(s =>
            {
                s.AddSingleton(routingHandler);
                s.AddHttpContextAccessor();
            })
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       .UseKestrel()
                       .UseStartup <Startup>()
                       .ConfigureKestrel((context, options) =>
            {
                //options.Listen(new IPAddress(new byte[] { 10, 33, 54, 120 }), 5000);
                options.Listen(new IPAddress(new byte[] { 0, 0, 0, 0 }), 5000);
            })
                       .Build();

            host.Run();
        }
예제 #8
0
        public UdpTransport(
            IHostApplicationLifetime applicationLifetime,
            IUdpConfiguration udpConfiguration,
            HandlerMapper handlerMapper,
            ILogger <UdpTransport> logger)
        {
            Debug.Assert(applicationLifetime != null);
            Debug.Assert(udpConfiguration != null);
            Debug.Assert(handlerMapper != null);
            Debug.Assert(logger != null);

            _udpConfiguration = udpConfiguration;
            _appLifetime      = applicationLifetime;
            _handlerMapper    = handlerMapper;
            _logger           = logger;

            _memoryPool = Extensions.GetMemoryPoolFactory();

            var configuration = _udpConfiguration.ThreadExecution;

            if (udpConfiguration.IOQueueCount <= 0)
            {
                configuration = HandlerThreadExecution.ThreadPool;
            }

            switch (configuration)
            {
            case HandlerThreadExecution.Inline:
                _numSchedulers = ThreadPoolSchedulerArray_Inline.Length;
                _schedulers    = ThreadPoolSchedulerArray_Inline;
                break;

            case HandlerThreadExecution.ThreadPool:
                _numSchedulers = ThreadPoolSchedulerArray_Pool.Length;
                _schedulers    = ThreadPoolSchedulerArray_Pool;
                break;

            case HandlerThreadExecution.Dedicated:
                _numSchedulers = _udpConfiguration.IOQueueCount;
                _schedulers    = new IOQueue[_numSchedulers];

                for (var i = 0; i < _numSchedulers; i++)
                {
                    _schedulers[i] = new IOQueue();
                }
                break;
            }
        }
예제 #9
0
        protected MessageDispatcherTestBase()
        {
            var clock = Substitute.For <IClock>();
            var replyQueueNameSetting = new ReplyQueueNameSetting(
                new ApplicationNameSetting {
                Value = "TestApplication"
            },
                new InstanceNameSetting {
                Value = "TestInstance"
            });

            TypeProvider = new TestHarnessTypeProvider(new[] { GetType().Assembly }, new[] { GetType().Namespace });
            var serializer = new DataContractSerializer(TypeProvider);

            HandlerMapper        = new HandlerMapper(TypeProvider);
            NimbusMessageFactory = new NimbusMessageFactory(new DefaultMessageTimeToLiveSetting(),
                                                            replyQueueNameSetting,
                                                            clock,
                                                            new DispatchContextManager());
        }
예제 #10
0
        protected override async Task <CommandMessageDispatcher> Given()
        {
            var clock                 = new SystemClock();
            var logger                = new ConsoleLogger();
            var typeProvider          = new TestHarnessTypeProvider(new[] { GetType().Assembly }, new[] { GetType().Namespace });
            var serializer            = new DataContractSerializer(typeProvider);
            var replyQueueNameSetting = new ReplyQueueNameSetting(
                new ApplicationNameSetting {
                Value = "TestApplication"
            },
                new InstanceNameSetting {
                Value = "TestInstance"
            });

            var handlerMap = new HandlerMapper(typeProvider).GetFullHandlerMap(typeof(IHandleCommand <>));

            _brokeredMessageFactory = new BrokeredMessageFactory(new DefaultMessageTimeToLiveSetting(),
                                                                 new MaxLargeMessageSizeSetting(),
                                                                 new MaxSmallMessageSizeSetting(),
                                                                 replyQueueNameSetting,
                                                                 clock,
                                                                 new NullCompressor(),
                                                                 new DispatchContextManager(),
                                                                 new UnsupportedLargeMessageBodyStore(),
                                                                 serializer,
                                                                 typeProvider);

            return(new CommandMessageDispatcher(_brokeredMessageFactory,
                                                new SystemClock(),
                                                new DependencyResolver(typeProvider),
                                                new NullInboundInterceptorFactory(),
                                                new NullLogger(),
                                                handlerMap,
                                                new DefaultMessageLockDurationSetting(),
                                                new NimbusTaskFactory(new MaximumThreadPoolThreadsSetting(), new MinimumThreadPoolThreadsSetting(), logger),
                                                Substitute.For <IPropertyInjector>()));
        }
예제 #11
0
        static async Task Main(string[] args)
        {
            // Wait for the debugger to attach if requested
            if (args.Length > 0 && args[0].ToLower() == "--waitfordebugger")
            {
                Console.WriteLine("Waiting for debugger...");

                while (!Debugger.IsAttached)
                {
                    await Task.Delay(1000);
                }

                Console.WriteLine("Debugger attached!");
            }

            // Get the type of the Configuration
            Type configType = TypeUtils.GetSubclassOfType <Configuration>();

            // Declare variable to hold the configuration
            Configuration configuration;

            // Check if the config file exists
            if (!File.Exists(LOCAL_CONFIGURATION))
            {
                // Create a new dummy Configuration
                configuration = (Configuration)Activator.CreateInstance(TypeUtils.GetSubclassOfType <Configuration>());
                configuration.SetDefaults();

                // Write out the default config
                configuration.Write(LOCAL_CONFIGURATION);

                Console.WriteLine("Wrote default configuration to " + LOCAL_CONFIGURATION);

                return;
            }

            // Create the Exception logs directory
            System.IO.Directory.CreateDirectory(LOCAL_EXCEPTION_LOGS_DIRECTORY);

            // Load the Configuration
            Configuration.Load(configType, LOCAL_CONFIGURATION);

            // Initialize the Localizer
            Localizer.Initialize();

            // Initialize the HandlerMapper
            HandlerMapper.Initialize();

            // Initialize the KeysetManager
            KeysetManager.Initialize();

            // Initialize DAuth
            DAuthApi.Initialize();

            // Initialize BCAT
            BcatApi.Initialize();

            // Initialize S3
            S3Api.Initialize();

            // Initialize DigitalOcean
            DoApi.Initialize();

            // Initialize Twitter
            TwitterManager.Initialize();

            // Initialize the DiscordBot
            await DiscordBot.Initialize();

            // Initialize the Scheduler
            await QuartzScheduler.Initialize();

            // Wait for the bot to fully initialize
            while (!DiscordBot.IsReady)
            {
                await Task.Delay(1000);
            }

            // Print out to the logging channel that we're initialized
            await DiscordBot.LoggingChannel.SendMessageAsync("\\*\\*\\* **Initialized**");

            // Schedule the BootHousekeepingJob
            await QuartzScheduler.ScheduleJob(TypeUtils.GetSubclassOfType <BootHousekeepingJob>(), "Immediate");

            // Register the SIGTERM handler
            AssemblyLoadContext.Default.Unloading += async x =>
            {
                // Run Shutdown in fast mode
                await Shutdown.CreateAndRun(true);
            };

            await Task.Delay(-1);
        }
예제 #12
0
 internal RoutingHandler(HandlerMapper handlerMapper, ILog log)
 {
     this.handlerMapper = handlerMapper;
     this.log           = log;
 }