public virtual void TearDown()
 {
     Queue.Stop();
     Queue = null;
     Consumer.Dispose();
     Consumer = null;
 }
Esempio n. 2
0
        private void SetupMessaging(ISubscriber subscriber)
        {
            var storageReaderBus = new InMemoryBus("StorageReaderBus");

            storageReaderBus.Subscribe <SystemMessage.SystemInit>(this);
            storageReaderBus.Subscribe <SystemMessage.BecomeShuttingDown>(this);
            storageReaderBus.Subscribe <ClientMessage.ReadEvent>(this);
            storageReaderBus.Subscribe <ClientMessage.ReadStreamEventsBackward>(this);
            storageReaderBus.Subscribe <ClientMessage.ReadStreamEventsForward>(this);
            storageReaderBus.Subscribe <ClientMessage.ReadAllEventsForward>(this);
            storageReaderBus.Subscribe <ClientMessage.ReadAllEventsBackward>(this);
            storageReaderBus.Subscribe <ClientMessage.ListStreams>(this);

            subscriber.Subscribe(this.WidenFrom <SystemMessage.SystemInit, Message>());
            subscriber.Subscribe(this.WidenFrom <SystemMessage.BecomeShuttingDown, Message>());
            subscriber.Subscribe(this.WidenFrom <ClientMessage.ReadEvent, Message>());
            subscriber.Subscribe(this.WidenFrom <ClientMessage.ReadStreamEventsBackward, Message>());
            subscriber.Subscribe(this.WidenFrom <ClientMessage.ReadStreamEventsForward, Message>());
            subscriber.Subscribe(this.WidenFrom <ClientMessage.ReadAllEventsForward, Message>());
            subscriber.Subscribe(this.WidenFrom <ClientMessage.ReadAllEventsBackward, Message>());
            subscriber.Subscribe(this.WidenFrom <ClientMessage.ListStreams, Message>());

            _storageReaderQueues = new QueuedHandler[_threadCount];
            for (int i = 0; i < _threadCount; ++i)
            {
                var queue = new QueuedHandler(storageReaderBus, string.Format("StorageReaderQueue #{0}", i));
                _storageReaderQueues[i] = queue;
                queue.Start();
            }
        }
Esempio n. 3
0
        static void Main(string[] args)
        {
            var outputBus = new InMemoryBus("OutputBus");
            var controller = new NodeController(outputBus);
            var mainQueue = new QueuedHandler(controller, "Main Queue");
            controller.SetMainQueue(mainQueue);

            // Hello world service
            var hello = new HelloWorldService(mainQueue);
            outputBus.Subscribe<SystemMessage.SystemInit>(hello);
            outputBus.Subscribe<SystemMessage.StartShutdown>(hello);
            outputBus.Subscribe<HelloWorldMessage.Hi>(hello);

            // TIMER
            var timer = new TimerService(new ThreadBasedScheduler(new RealTimeProvider()));
            outputBus.Subscribe<TimerMessage.Schedule>(timer);

            Console.WriteLine("Starting everything. Press enter to initiate shutdown");

            mainQueue.Start();

            mainQueue.Publish(new SystemMessage.SystemInit());
            Console.ReadLine();
            mainQueue.Publish(new SystemMessage.StartShutdown());
            Console.ReadLine();
        }
Esempio n. 4
0
        protected override void Create()
        {
            base.Create();
            var db = this.TfDb;

            _coreInputBus = new InMemoryBus("bus");
            _coreQueue    = new QueuedHandler(_coreInputBus, "ProjectionCoreQueue");

            _readerInputBus = new InMemoryBus("Reader Input");

            _projectionNode = new ProjectionWorkerNode(db, _coreQueue, Node.HttpService);
            _projectionNode.SetupMessaging(_coreInputBus, _readerInputBus);

            _forwarder = new RequestResponseQueueForwarder(inputQueue: _coreQueue, externalRequestQueue: Node.MainQueue);
            // forwarded messages
            _projectionNode.CoreOutput.Subscribe <ClientMessage.ReadEvent>(_forwarder);
            _projectionNode.CoreOutput.Subscribe <ClientMessage.ReadEventsBackwards>(_forwarder);
            _projectionNode.CoreOutput.Subscribe <ClientMessage.ReadEventsForward>(_forwarder);
            _projectionNode.CoreOutput.Subscribe <ClientMessage.ReadEventsFromTF>(_forwarder);
            _projectionNode.CoreOutput.Subscribe <ClientMessage.WriteEvents>(_forwarder);
            _coreInputBus.Subscribe(new UnwrapEnvelopeHandler());

            Node.Bus.Subscribe(Forwarder.Create <SystemMessage.BecomeShuttingDown>(_coreQueue));
            Node.Bus.Subscribe(Forwarder.Create <SystemMessage.SystemInit>(_coreQueue));
            Node.Bus.Subscribe(Forwarder.Create <SystemMessage.SystemStart>(_coreQueue));

            _projectionNode.CoreOutput.Subscribe(Node.TimerService);
            _coreInputBus.Subscribe <SystemMessage.SystemStart>(this);
        }
Esempio n. 5
0
        public void can_filter_out_message_types()
        {
            // server side
            var serverInbound = new QueuedHandler(
                new AdHocHandler <IMessage>(_tcs.SetResult),
                "InboundMessageQueuedHandler",
                true,
                TimeSpan.FromMilliseconds(1000));

            var tcpBusServerSide = new TcpBusServerSide(
                _hostAddress,
                port,
                inboundNondiscardingMessageTypes: new[] { typeof(WoftamEvent) },
                inboundNondiscardingMessageQueuedHandler: serverInbound);

            serverInbound.Start();

            // client side
            var tcpBusClientSide = new TcpBusClientSide(_hostAddress, port);

            // wait for tcp connection to be established
            AssertEx.IsOrBecomesTrue(() => tcpBusClientSide.IsConnected, 200);

            // put disallowed message into client
            tcpBusClientSide.Handle(new WoftamCommand("abc"));

            // expect to receive it in the server but drop it on the floor
            var gotMessage = _tcs.Task.Wait(TimeSpan.FromMilliseconds(1000));

            Assert.False(gotMessage);

            tcpBusClientSide.Dispose();
            tcpBusServerSide.Dispose();
        }
Esempio n. 6
0
        public StorageWriter(IPublisher bus, ISubscriber subscriber, TFChunkWriter writer, IReadIndex readIndex)
        {
            Ensure.NotNull(bus, "bus");
            Ensure.NotNull(subscriber, "subscriber");
            Ensure.NotNull(writer, "writer");
            Ensure.NotNull(readIndex, "readIndex");

            Bus         = bus;
            _subscriber = subscriber;
            ReadIndex   = readIndex;

            _flushDelay = 0;
            _lastFlush  = _watch.ElapsedTicks;

            Writer = writer;
            Writer.Open();

            _writerBus          = new InMemoryBus("StorageWriterBus", watchSlowMsg: true, slowMsgThresholdMs: 500);
            _storageWriterQueue = new QueuedHandler(_writerBus, "StorageWriterQueue", watchSlowMsg: false);
            _storageWriterQueue.Start();

            SubscribeToMessage <SystemMessage.SystemInit>();
            SubscribeToMessage <SystemMessage.BecomeShuttingDown>();
            SubscribeToMessage <StorageMessage.WritePrepares>();
            SubscribeToMessage <StorageMessage.WriteDelete>();
            SubscribeToMessage <StorageMessage.WriteTransactionStart>();
            SubscribeToMessage <StorageMessage.WriteTransactionData>();
            SubscribeToMessage <StorageMessage.WriteTransactionPrepare>();
            SubscribeToMessage <StorageMessage.WriteCommit>();
        }
Esempio n. 7
0
 public Projections(
     TFChunkDb db, QueuedHandler mainQueue, InMemoryBus mainBus, TimerService timerService,
     HttpService httpService, int projectionWorkerThreadCount)
 {
     _projectionWorkerThreadCount = projectionWorkerThreadCount;
     SetupMessaging(db, mainQueue, mainBus, timerService, httpService);
 }
Esempio n. 8
0
        static void Main()
        {
            var bus             = new TopicBasedPubSub();
            var messageListener = new MessageListener(bus);
            var startables      = new List <IStartable>();

            var midgetFactory = new MidgetFactory();
            var midgetHouse   = new MidgetHouse(bus, midgetFactory);

            var consolePrinter = new QueuedHandler <OrderPaid>(Messages.Paid, new ConsolePrintingOrderHandler(bus));

            bus.Subscribe(consolePrinter);

            startables.Add(consolePrinter);
            var cashier       = new Cashier(bus);
            var queuedCashier = new QueuedHandler <TakePayment>(Messages.OrderBilled, cashier);

            bus.Subscribe(queuedCashier);
            startables.Add(queuedCashier);
            startables.Add(cashier);

            var assistantManager = new QueuedHandler <PriceOrder>(Messages.OrderPrepared, new AssistantManager(bus));

            bus.Subscribe(assistantManager);
            startables.Add(assistantManager);

            var chefs = new List <QueuedHandler <CookFood> >();
            var rand  = new Random();

            for (int i = 0; i < NumberOfChefs; i++)
            {
                var chef          = new TimeToLiveDispatcher <CookFood>(new Chef(bus, rand.Next(1000)));
                var queuedHandler = new QueuedHandler <CookFood>(string.Format("Chef {0}", i), chef);
                chefs.Add(queuedHandler);
                startables.Add(queuedHandler);
            }

            var distributionStrategy = new QueuedDispatcher <CookFood>(bus, chefs);

            startables.Add(distributionStrategy);

            foreach (var startable in startables)
            {
                startable.Start();
            }
            var monitor = new Monitor(startables);

            monitor.Start();

            var waiter = new Waiter(bus);

            for (int i = 0; i < 10; i++)
            {
                var correlationId = Guid.NewGuid();

                var orderId = waiter.PlaceOrder(correlationId);
            }

            Console.ReadKey();
        }
Esempio n. 9
0
        private void SetupMessaging(ISubscriber subscriber)
        {
            var storageWriterBus = new InMemoryBus("StorageWriterBus", watchSlowMsg: true, slowMsgThresholdMs: 500);

            storageWriterBus.Subscribe <SystemMessage.SystemInit>(this);
            storageWriterBus.Subscribe <SystemMessage.BecomeShuttingDown>(this);
            storageWriterBus.Subscribe <ReplicationMessage.WritePrepares>(this);
            storageWriterBus.Subscribe <ReplicationMessage.WriteDelete>(this);
            storageWriterBus.Subscribe <ReplicationMessage.WriteTransactionStart>(this);
            storageWriterBus.Subscribe <ReplicationMessage.WriteTransactionData>(this);
            storageWriterBus.Subscribe <ReplicationMessage.WriteTransactionPrepare>(this);
            storageWriterBus.Subscribe <ReplicationMessage.WriteCommit>(this);
            storageWriterBus.Subscribe <ReplicationMessage.LogBulk>(this);

            _storageWriterQueue = new QueuedHandler(storageWriterBus, "StorageWriterQueue", watchSlowMsg: false);
            _storageWriterQueue.Start();

            subscriber.Subscribe(this.WidenFrom <SystemMessage.SystemInit, Message>());
            subscriber.Subscribe(this.WidenFrom <SystemMessage.BecomeShuttingDown, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.WritePrepares, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.WriteDelete, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.WriteTransactionStart, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.WriteTransactionData, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.WriteTransactionPrepare, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.WriteCommit, Message>());
            subscriber.Subscribe(this.WidenFrom <ReplicationMessage.LogBulk, Message>());
        }
Esempio n. 10
0
        public void Register(StandardComponents standardComponents)
        {
            _leaderInputQueue = QueuedHandler.CreateQueuedHandler(_leaderMainBus, "Projections Leader",
                                                                  standardComponents.QueueStatsManager);
            _leaderOutputBus = new InMemoryBus("ProjectionManagerAndCoreCoordinatorOutput");

            _leaderMainBus.Subscribe <ProjectionSubsystemMessage.RestartSubsystem>(this);
            _leaderMainBus.Subscribe <ProjectionSubsystemMessage.ComponentStarted>(this);
            _leaderMainBus.Subscribe <ProjectionSubsystemMessage.ComponentStopped>(this);
            _leaderMainBus.Subscribe <ProjectionSubsystemMessage.IODispatcherDrained>(this);
            _leaderMainBus.Subscribe <SystemMessage.SystemCoreReady>(this);
            _leaderMainBus.Subscribe <SystemMessage.StateChangeMessage>(this);

            var projectionsStandardComponents = new ProjectionsStandardComponents(
                _projectionWorkerThreadCount,
                _runProjections,
                _leaderOutputBus,
                _leaderInputQueue,
                _leaderMainBus, _faultOutOfOrderProjections);

            CreateAwakerService(standardComponents);
            _coreQueues =
                ProjectionCoreWorkersNode.CreateCoreWorkers(standardComponents, projectionsStandardComponents);
            _queueMap = _coreQueues.ToDictionary(v => v.Key, v => (IPublisher)v.Value);

            ProjectionManagerNode.CreateManagerService(standardComponents, projectionsStandardComponents, _queueMap,
                                                       _projectionsQueryExpiry);
            projectionsStandardComponents.LeaderMainBus.Subscribe <CoreProjectionStatusMessage.Stopped>(this);
            projectionsStandardComponents.LeaderMainBus.Subscribe <CoreProjectionStatusMessage.Started>(this);
        }
        private void DispatchSingleMessage(IMessage message, QueueMessageDisptaching queueMessageDispatching)
        {
            var messageHandlerDataList = _handlerProvider.GetHandlers(message.GetType());

            if (!messageHandlerDataList.Any())
            {
                queueMessageDispatching.OnMessageHandled(message);
                return;
            }

            foreach (var messageHandlerData in messageHandlerDataList)
            {
                var singleMessageDispatching = new SingleMessageDisptaching(message, queueMessageDispatching, messageHandlerData.AllHandlers, _typeNameProvider);

                if (messageHandlerData.ListHandlers != null && messageHandlerData.ListHandlers.IsNotEmpty())
                {
                    foreach (var handler in messageHandlerData.ListHandlers)
                    {
                        DispatchSingleMessageToHandlerAsync(singleMessageDispatching, handler, null, 0);
                    }
                }
                if (messageHandlerData.QueuedHandlers != null && messageHandlerData.QueuedHandlers.IsNotEmpty())
                {
                    var queueHandler = new QueuedHandler <IMessageHandlerProxy1>(messageHandlerData.QueuedHandlers, (queuedHandler, nextHandler) => DispatchSingleMessageToHandlerAsync(singleMessageDispatching, nextHandler, queuedHandler, 0));
                    DispatchSingleMessageToHandlerAsync(singleMessageDispatching, queueHandler.DequeueHandler(), queueHandler, 0);
                }
            }
        }
Esempio n. 12
0
        public void SetMainQueue(QueuedHandler mainQueue)
        {
            Ensure.NotNull(mainQueue, "mainQueue");

            _mainQueue       = mainQueue;
            _publishEnvelope = new PublishEnvelope(mainQueue);
        }
        public void while_queue_is_busy_should_crash_with_timeout()
        {
            var consumer  = new WaitingConsumer(1);
            var busyQueue = QueuedHandler.CreateQueuedHandler(consumer, "busy_test_queue", new QueueStatsManager(), watchSlowMsg: false,
                                                              threadStopWaitTimeout: TimeSpan.FromMilliseconds(100));
            var waitHandle   = new ManualResetEvent(false);
            var handledEvent = new ManualResetEvent(false);

            try {
                busyQueue.Start();
                busyQueue.Publish(new DeferredExecutionTestMessage(() => {
                    handledEvent.Set();
                    waitHandle.WaitOne();
                }));

                handledEvent.WaitOne();
                Assert.Throws <TimeoutException>(() => busyQueue.Stop());
            } finally {
                waitHandle.Set();
                consumer.Wait();

                busyQueue.Stop();
                waitHandle.Dispose();
                handledEvent.Dispose();
                consumer.Dispose();
            }
        }
 public TcpOutboundMessageHandler(
     IDispatcher messageBus,
     QueuedHandler outboundMessageQueuedHandler)
 {
     _messageBus = messageBus;
     _outboundMessageQueuedHandler = outboundMessageQueuedHandler;
     _messageBus.Subscribe(this);
 }
 public TcpOutboundMessageHandler(
     IGeneralBus messageBus,
     QueuedHandler outboundMessageQueuedHandler)
 {
     _messageBus = messageBus;
     _outboundMessageQueuedHandler = outboundMessageQueuedHandler;
     _messageBus.Subscribe(this);
 }
Esempio n. 16
0
 public void Register(
     TFChunkDb db, QueuedHandler mainQueue, ISubscriber mainBus, TimerService timerService,
     ITimeProvider timeProvider, IHttpForwarder httpForwarder, HttpService[] httpServices, IPublisher networkSendService)
 {
     _projections = new EventStore.Projections.Core.Projections(
         db, mainQueue, mainBus, timerService, timeProvider, httpForwarder, httpServices, networkSendService,
         projectionWorkerThreadCount: _projectionWorkerThreadCount, runProjections: _runProjections);
 }
Esempio n. 17
0
        public ProjectionWorkerNode(TFChunkDb db, QueuedHandler inputQueue)
        {
            Ensure.NotNull(db, "db");

            _coreOutput = new InMemoryBus("Core Output");

            _projectionCoreService = new ProjectionCoreService(CoreOutput, inputQueue, 10, db.Config.WriterCheckpoint);
        }
        public void stoped_not_starting_server()
        {
            //GIVEN
            var controller = new SleepTestController1();
            var mainQueue  = new QueuedHandler(controller, "Main Queue", 1000);

            //WHEN
            mainQueue.Stop();
        }
Esempio n. 19
0
 protected ReadModelBase(string name, Func <IListener> getListener)
 {
     Ensure.NotNull(getListener, nameof(getListener));
     _getListener = getListener;
     _listeners   = new List <IListener>();
     _bus         = new InMemoryBus($"{nameof(ReadModelBase)}:{name} bus", false);
     _queue       = new QueuedHandler(_bus, $"{nameof(ReadModelBase)}:{name} queue");
     _queue.Start();
 }
        public static ProjectionManagerNode Create(TFChunkDb db, QueuedHandler inputQueue, HttpService httpService, IPublisher[] queues)
        {
            var projectionManagerNode =
                new ProjectionManagerNode(inputQueue, queues, db.Config.WriterCheckpoint);

            httpService.SetupController(new ProjectionsController(inputQueue));

            return(projectionManagerNode);
        }
Esempio n. 21
0
 public Projections(
     TFChunkDb db, QueuedHandler mainQueue, ISubscriber mainBus, TimerService timerService, ITimeProvider timeProvider,
     IHttpForwarder httpForwarder, HttpService[] httpServices, IPublisher networkSendQueue,
     int projectionWorkerThreadCount, RunProjections runProjections)
 {
     _projectionWorkerThreadCount = projectionWorkerThreadCount;
     SetupMessaging(
         db, mainQueue, mainBus, timerService, timeProvider, httpForwarder, httpServices, networkSendQueue,
         runProjections);
 }
Esempio n. 22
0
 /// <summary>
 /// Creates a read model using the provided stream store connection. Reads existing events using a
 /// reader, then transitions to a listener for live events.
 /// </summary>
 /// <param name="name">The name of the read model. Also used as the name of the listener and reader.</param>
 /// <param name="connection">A connection to a stream store.</param>
 protected ReadModelBase(string name, IConfiguredConnection connection)
 {
     Ensure.NotNull(connection, nameof(connection));
     _getListener = () => connection.GetListener(name);
     _getReader   = () => connection.GetReader(name, Handle);
     _listeners   = new List <IListener>();
     _bus         = new InMemoryBus($"{nameof(ReadModelBase)}:{name} bus", false);
     _queue       = new QueuedHandler(_bus, $"{nameof(ReadModelBase)}:{name} queue");
     _queue.Start();
 }
 public SynchronizableStreamListener(
     string name,
     ICatchupSteamSubscriber subscriptionTarget,
     bool sync      = false,
     string busName = null) :
     base(name, subscriptionTarget, busName)
 {
     Sync      = sync;
     SyncQueue = new QueuedHandler(this, "SyncListenerQueue");
 }
        public void multiple_start()
        {
            //GIVEN
            var mainQueue = new QueuedHandler(null, "Main Queue");

            mainQueue.Start();

            //WHEN
            mainQueue.Start();
        }
Esempio n. 25
0
        static void Main()
        {
            var bus = new TopicBasedPubSub();
            var messageListener = new MessageListener(bus);
            var startables = new List<IStartable>();
            
            var midgetFactory = new MidgetFactory();
            var midgetHouse = new MidgetHouse(bus, midgetFactory);

            var consolePrinter = new QueuedHandler<OrderPaid>(Messages.Paid, new ConsolePrintingOrderHandler(bus));
            bus.Subscribe(consolePrinter);

            startables.Add(consolePrinter);
            var cashier = new Cashier(bus);
            var queuedCashier = new QueuedHandler<TakePayment>(Messages.OrderBilled, cashier);
            bus.Subscribe(queuedCashier);
            startables.Add(queuedCashier);
            startables.Add(cashier);

            var assistantManager = new QueuedHandler<PriceOrder>(Messages.OrderPrepared, new AssistantManager(bus));
            bus.Subscribe(assistantManager);
            startables.Add(assistantManager);
          
            var chefs = new List<QueuedHandler<CookFood>>();
            var rand = new Random();
            for (int i = 0; i < NumberOfChefs; i++)
            {
                var chef = new TimeToLiveDispatcher<CookFood>(new Chef(bus, rand.Next(1000)));
                var queuedHandler = new QueuedHandler<CookFood>(string.Format("Chef {0}", i), chef);
                chefs.Add(queuedHandler);
                startables.Add(queuedHandler);
            }

            var distributionStrategy = new QueuedDispatcher<CookFood>(bus, chefs);
            startables.Add(distributionStrategy);

            foreach (var startable in startables)
            {
                startable.Start();
            }
            var monitor = new Monitor(startables);
            monitor.Start();

            var waiter = new Waiter(bus);
            
            for (int i = 0; i < 10; i++)
            {
                var correlationId = Guid.NewGuid();

                var orderId = waiter.PlaceOrder(correlationId);    
            }            

            Console.ReadKey();
        }
Esempio n. 26
0
        public virtual void TestFixtureSetUp()
        {
            var _queue = QueuedHandler.CreateQueuedHandler(_bus, "TestQueuedHandler", new QueueStatsManager());

            _ioDispatcher = new IODispatcher(_bus, new PublishEnvelope(_queue));
            IODispatcherTestHelpers.SubscribeIODispatcher(_ioDispatcher, _bus);
            _bus.Subscribe <ClientMessage.ReadStreamEventsForward>(this);
            _bus.Subscribe <ClientMessage.ReadStreamEventsBackward>(this);
            _bus.Subscribe <TimerMessage.Schedule>(this);
            _queue.Start();
        }
        public void starting_stoped_server()
        {
            //GIVEN
            var mainQueue = new QueuedHandler(null, "Main Queue");

            mainQueue.Start();
            Thread.Sleep(1000);
            mainQueue.Stop();

            //WHEN
            mainQueue.Start();
        }
Esempio n. 28
0
 public QueuedStreamListener(
     string name,
     IStreamStoreConnection connection,
     IStreamNameBuilder streamNameBuilder,
     IEventSerializer serializer,
     string busName = null,
     Action <Unit> liveProcessingStarted = null,
     Action <SubscriptionDropReason, Exception> subscriptionDropped = null) :
     base(name, connection, streamNameBuilder, serializer, busName, liveProcessingStarted, subscriptionDropped)
 {
     SyncQueue = new QueuedHandler(this, "SyncListenerQueue");
 }
Esempio n. 29
0
        public static ProjectionManagerNode Create(
            TFChunkDb db, QueuedHandler inputQueue, IHttpForwarder httpForwarder, HttpService[] httpServices, IPublisher networkSendQueue,
            IPublisher[] queues, RunProjections runProjections)
        {
            var projectionManagerNode = new ProjectionManagerNode(inputQueue, queues, runProjections);
            var projectionsController = new ProjectionsController(httpForwarder, inputQueue, networkSendQueue);

            foreach (var httpService in httpServices)
            {
                httpService.SetupController(projectionsController);
            }
            return(projectionManagerNode);
        }
        public void throw_exception_on_timeout()
        {
            //GIVEN
            var controller = new SleepTestController1();
            var mainQueue  = new QueuedHandler(controller, "Main Queue", 1000);

            mainQueue.Start();
            mainQueue.Enqueue(new QueuedTestMessage1());

            //WHEN
            Assert.AreEqual(false, controller.MessageHandled());
            mainQueue.Stop();
        }
Esempio n. 31
0
 public ProjectionsStandardComponents(
     int projectionWorkerThreadCount,
     ProjectionType runProjections,
     InMemoryBus masterOutputBus,
     QueuedHandler masterInputQueue,
     InMemoryBus masterMainBus)
 {
     _projectionWorkerThreadCount = projectionWorkerThreadCount;
     _runProjections   = runProjections;
     _masterOutputBus  = masterOutputBus;
     _masterInputQueue = masterInputQueue;
     _masterMainBus    = masterMainBus;
 }
        public TcpBusServerSide(
            IPAddress hostIp,
            int commandPort,
            IEnumerable <Type> inboundDiscardingMessageTypes = null,
            QueuedHandlerDiscarding inboundDiscardingMessageQueuedHandler = null,
            IEnumerable <Type> inboundNondiscardingMessageTypes           = null,
            QueuedHandler inboundNondiscardingMessageQueuedHandler        = null,
            Dictionary <Type, IMessageSerializer> messageSerializers      = null)
            : base(
                hostIp,
                commandPort,
                inboundDiscardingMessageTypes,
                inboundDiscardingMessageQueuedHandler,
                inboundNondiscardingMessageTypes,
                inboundNondiscardingMessageQueuedHandler,
                messageSerializers)
        {
            Log.Debug($"Configuring TCP Listener at {CommandEndpoint.AddressFamily}, {CommandEndpoint}.");

            var listener = new TcpServerListener(CommandEndpoint);

            listener.StartListening(
                (endPoint, socket) =>
            {
                var connectionId = Guid.NewGuid();
                var conn         = TcpConnection.CreateAcceptedTcpConnection(connectionId, endPoint, socket, verbose: true);

                var framer = new LengthPrefixMessageFramer();
                framer.RegisterMessageArrivedCallback(TcpMessageArrived);

                Action <ITcpConnection, IEnumerable <ArraySegment <byte> > > callback = null;
                callback = (x, data) =>
                {
                    try
                    {
                        framer.UnFrameData(x.ConnectionId, data);
                    }
                    catch (PackageFramingException exc)
                    {
                        Log.ErrorException(exc, "LengthPrefixMessageFramer.UnFrameData() threw an exception:");
                        return;
                    }
                    x.ReceiveAsync(callback);
                };
                conn.ReceiveAsync(callback);
                AddConnection(conn);
            },
                "Standard");
            Log.Debug($"TCP Listener at {CommandEndpoint.AddressFamily}, {CommandEndpoint} successfully configured.");
            _commandPortListener = listener;
        }
Esempio n. 33
0
        public static NodeEntryPoint StartWithOptions(NodeOptions options, Action<int> termination)
        {
            var slim = new ManualResetEventSlim(false);
            var list = String.Join(Environment.NewLine,
                options.GetPairs().Select(p => String.Format("{0} : {1}", p.Key, p.Value)));

            Log.Info(list);

            var bus = new InMemoryBus("OutputBus");
            var controller = new NodeController(bus);
            var mainQueue = new QueuedHandler(controller, "Main Queue");
            controller.SetMainQueue(mainQueue);
            Application.Start(i =>
                {
                    slim.Set();
                    termination(i);
                });

            var http = new PlatformServerApiService(mainQueue, String.Format("http://{0}:{1}/", options.LocalHttpIp, options.HttpPort));

            bus.Subscribe<SystemMessage.Init>(http);
            bus.Subscribe<SystemMessage.StartShutdown>(http);

            var timer = new TimerService(new ThreadBasedScheduler(new RealTimeProvider()));
            bus.Subscribe<TimerMessage.Schedule>(timer);

            // switch, based on configuration
            AzureStoreConfiguration azureConfig;
            if (AzureStoreConfiguration.TryParse(options.StoreLocation, out azureConfig))
            {
                var storageService = new AzureStorageService(azureConfig, mainQueue);
                bus.Subscribe<ClientMessage.AppendEvents>(storageService);
                bus.Subscribe<SystemMessage.Init>(storageService);
                bus.Subscribe<ClientMessage.ImportEvents>(storageService);
                bus.Subscribe<ClientMessage.RequestStoreReset>(storageService);
            }
            else
            {
                var storageService = new FileStorageService(options.StoreLocation, mainQueue);
                bus.Subscribe<ClientMessage.AppendEvents>(storageService);
                bus.Subscribe<SystemMessage.Init>(storageService);
                bus.Subscribe<ClientMessage.ImportEvents>(storageService);
                bus.Subscribe<ClientMessage.RequestStoreReset>(storageService);
            }

            mainQueue.Start();

            mainQueue.Enqueue(new SystemMessage.Init());
            return new NodeEntryPoint(mainQueue,slim);
        }
Esempio n. 34
0
        static void Main(string[] args)
        {
            var bus = new Bus(new Dispatcher.Dispatcher());
            var printers = new[] { new Printer(bus, 1), new Printer(bus, 2), new Printer(bus, 3), new Printer(bus, 4) };

            var printerPool = printers
                .Select(printer => new WideningHandler<PrintJob, Message>(printer))
                .Select(printer => new QueuedHandler(printer, bus) { MaxQueueLength = 1 })
                .ToArray();

            var refillPool = printers
                .Select(printer => new WideningHandler<RefillPaper, Message>(printer))
                .Select(printer => new QueuedHandler(printer, bus) { MaxQueueLength = 1 })
                .ToArray();

            foreach (var printer in refillPool)
            {
                bus.Subscribe<RefillPaper>(new NarrowingHandler<RefillPaper, Message>(printer));
                // subscribe the printer directly to RefillPaper as we don't want to distribute that with the print jobs
            }

            var office = Enumerable.Range(0, 50)
                .Select(i => new Employee(bus, i))
                .ToArray();

            var loadBalancer = new RoundRobinLoadBalancer(printerPool);
            var printerRetryHandler = new RetryHandler(loadBalancer, bus);
            var retryManager = new RetryManager(bus);
            var timerService = new TimerService(bus);

            bus.Subscribe<FutureMessage>(timerService);
            bus.Subscribe<RetryMessage>(retryManager);
            bus.Subscribe<SuccessMessage>(retryManager);

            bus.Subscribe(new NarrowingHandler<PrintJob, Message>(printerRetryHandler));

            var console = new QueuedHandler(new WideningHandler<LogMessage, Message>(new ConsoleHandler()), bus);
            bus.Subscribe<LogMessage>(new NarrowingHandler<LogMessage, Message>(console));

            var porter = new Porter(bus);
            bus.Subscribe(porter);

            var converter = new LogConverter(bus);
            bus.Subscribe<PrintJob>(converter);
            bus.Subscribe<OutOfPaper>(converter);
            bus.Subscribe<PagePrinted>(converter);
            bus.Subscribe<RetryMessage>(converter);
        }
        public ReconnectionHandler Build()
        {
            var outputBus = new InMemoryBus("OutputBus");
            var mainQueue = new QueuedHandler(outputBus, "Main Queue");

            // TIMER
            var timer = new TimerService(new ThreadBasedScheduler(new RealTimeProvider()));
            outputBus.Subscribe(timer);

            //ALERTER
            var alerter = new Alerter();
            outputBus.Subscribe<AlertReconnectingForTooLong>(alerter);
            outputBus.Subscribe<AlertFalseAlarm>(alerter);

            var connectionHandler = new ReconnectionHandler(mainQueue);
            outputBus.Subscribe(connectionHandler);
            mainQueue.Start();
            return connectionHandler;
        }
Esempio n. 36
0
        static void Main()
        {
            // this Client App uses a standard "Windows Forms" application as its host
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            
            

            #region This WinForms host uses its own in-memory message bus to manage the UI...
            // It uses this in-memory bus to wire user-interface "UI" elements ("controls") to the
            // logic (in controllers) that will be triggered when the user interacts with those elements.
            // This allows us to send messages to the uiBus so that interested UI classes can 
            // subscribe to the messages they care about, and react to them
            // when the bus publishes the messages to tell the UI controls what happened.
            #endregion
            var uiBus = new InMemoryBus("UI");

            // .dat file to write Domain Events to this session
            var fileToStoreEventsIn = 
                new FileAppendOnlyStore(new DirectoryInfo(Directory.GetCurrentDirectory()));
            fileToStoreEventsIn.Initialize();

            // provide serialization stuff for our file storage
            var messageStore = new MessageStore(fileToStoreEventsIn);
            messageStore.LoadDataContractsFromAssemblyOf(typeof(ActionDefined));

            // this WinForm App's own local file-based event storage
            var appEventStore = new AppEventStore(messageStore);

            #region Does App care about this msg? This class controls the App's "main message loop"...
            // It reads all msgs from in-memory queue (mainQueue below) and determines which messages 
            // the App will/will not handle at a given time (based on specific app state).
            // For each queued message that it determines should be handled by
            // uiBus subscribers, it passes the messages through to our in-memory uiBus,
            // so bus subscribers can be called to react to the current message when the bus publishes it.
            #endregion
            var appController = new AppController(uiBus, appEventStore);

            #region In-memory structure that all events we defined will go through...
            // All domain & system messages we defined are captured 
            // and accumulated in this queue until some code picks up
            // each message and processes it.
            // (ex: AppController, declared above, will do that processing in this case).
            #endregion
            var mainQueue = new QueuedHandler(appController, "Main Queue");

            appController.SetMainQueue(mainQueue);
            appEventStore.SetDispatcher(mainQueue);

            var provider = new ClientPerspective();

            ClientModelController.WireTo(appEventStore, provider, uiBus, mainQueue);
            
            // create services and bind them to the bus

            // we wire all controls together in a native way.
            // then we add adapters on top of that
            var form = new MainForm();

            var navigation = new NavigationView();
            form.NavigationRegion.RegisterDock(navigation, "nav");
            form.NavigationRegion.SwitchTo("nav");
            
            LogController.Wire(form, uiBus);

            #region View Controllers - Decoupling (UI) Views from their Controllers...
            // The intent with this design was to enable us to
            // write components or UI elements in a separated manner.
            // Provide the ability to develop new functionality independently and
            // it will sit in its own kind of "sandbox" so you can work on your stuff
            // without impacting everyone else.
            // It also sets us up for potential controller/code reuse and sharing in the future.

            // The UI is broken down into kinda "SEDA standalone controllers"
            // that communicate with each other via events.  This event-driven
            // separation allows for cleanly implementing logic like:
            // "If the Inbox is selected, then only display these two menu items,
            // but if a project is displayed, then display these additional menu items."
            // See the Handle methods inside of MainFormController.cs for an example.

            // Event-centric approaches are one of the nicest ways to build
            // plug-in systems because plug-ins have services and contracts which are linked
            // to behaviors (and in our case, these are events).
            // Example:  All CaptureThoughtController knows is that it gets handed a View
            // that it controls (CaptureThoughtForm) and then it has two other injections points,
            // a Bus and a MainQueue.
            // See CaptureThoughtController for more comments on how this design works.

            // The big idea here is that in the future, a controller can be passed an
            // INTERFACE (say, ICaptureThoughtForm) INSTEAD OF a concrete Windows Forms
            // implementation (CaptureThoughtForm) like it currently uses.
            // So we may have a WPF form in the future that implements ICaptureThoughtForm
            // and we could use that View implementation with the SAME CONTROLLER we already have.
            // Very similar to how you could use the MVVM pattern with MvvmCross to
            // reuse common code from the ViewModel down, but implement
            // platform-specific Views on top of that common/shared code.
            #endregion

            #region Wiring up our Views to Controllers... 
            // A "Controller" or "Service" in this client-side ecosystem would usually
            // define at least two parameters:
            // MainQueue
            // and
            // "Bus"
            // MainQueue is the place where it would send events that happen inside of it.
            // Bus is what it subscribes to so that it will be called when specifc events happen.

            // "Wire" is a static method defined on these controllers that our setup
            // can call to let them know which form they control,
            // the bus they can use as a source to subscribe to UI events to react to,
            // and the target queue that they can use tell the rest of the world about events they generate.
            #endregion

            MainFormController.Wire(form, mainQueue, uiBus);
            AddStuffToInboxController.Wire(new AddStuffToInboxForm(form), uiBus, mainQueue);
            AddActionToProjectController.Wire(new AddActionToProjectForm(form),uiBus, mainQueue );
            DefineProjectController.Wire(new DefineProjectForm(form), uiBus, mainQueue);
            InboxController.Wire(form.MainRegion, mainQueue, uiBus, provider);
            NavigationController.Wire(navigation, mainQueue, uiBus, provider);
            ProjectController.Wire(form.MainRegion, mainQueue, uiBus, provider);

            NavigateBackController.Wire(uiBus, mainQueue, form);

            mainQueue.Enqueue(new AppInit());
            mainQueue.Start();
            
            Application.Run(form);
        }
Esempio n. 37
0
        static void Main(string[] args)
        {
            var d = new Dispatcher();
            var midgetHouse = new MidgetHouse(d);
            d.Subscribe<OrderPlaced>(midgetHouse);
            d.Subscribe<DodgyOrderPlaced>(midgetHouse);
            var manager = new Manager();
            var cashier = new Cashier(d);
            var ass = new AssMan(d);

            var cookDispatcher = new SmartDispatcher<CookFood>();
            var cookTtlGate = new TimeToLiveGate<CookFood>(cookDispatcher);
            var cookQueudHandler = new QueuedHandler<CookFood>(cookTtlGate, "cook ttl gate");
            var cookLimiter = new Limiter<CookFood>(cookQueudHandler);
            //var cookScrewsUp = new ScrewsUp<CookFood>(cookLimiter);

            var alarmClock = new AlarmClock(d);

            var messageMonitor = new MessageMonitor(d);
            var fmm = new FilePerOrderMonitor(d);

            d.Subscribe(alarmClock);
            d.Subscribe(cookLimiter);
            d.Subscribe(ass);
            d.Subscribe(cashier);
            d.Subscribe(manager);
            d.Subscribe<OrderPlaced>(messageMonitor);
            d.Subscribe<DodgyOrderPlaced>(messageMonitor);
            d.Subscribe<OrderPlaced>(fmm);
            d.Subscribe<DodgyOrderPlaced>(fmm);

            var cookQueudHandler1 = new QueuedHandler<CookFood>(new Cook(d, 10000), "c1");
            cookDispatcher.AddHandler(cookQueudHandler1);
            var cookQueudHandler2 = new QueuedHandler<CookFood>(new Cook(d, 5000), "c2");
            cookDispatcher.AddHandler(cookQueudHandler2);
            var cookQueudHandler3 = new QueuedHandler<CookFood>(new Cook(d, 100), "c3");
            cookDispatcher.AddHandler(cookQueudHandler3);

            var queueMonitor = new QueueMonitor(new IAmMonitored[] {cookQueudHandler1, cookQueudHandler2, cookQueudHandler3, cookQueudHandler,d.QueudHandler});

            //Cook cook = new Cook(ass);
            var waiter = new Waiter(d);

            cookQueudHandler1.Start();
            cookQueudHandler2.Start();
            cookQueudHandler3.Start();
            cookQueudHandler.Start();
            d.Start();
            alarmClock.Start();
            queueMonitor.Start();

            new Thread(TryPay).Start(cashier);

            Random r = new Random();
            for (int i = 0; i < 500; i++)
            {
                Guid orderNumber;
                if (r.Next()%2 == 0)
                {
                    orderNumber = waiter.PlaceDodgyOrder(new[] {Tuple.Create("Burger", 1)}, 15);

                }
                else
                {
                    orderNumber = waiter.PlaceOrder(new[] { Tuple.Create("Burger", 1) }, 15);
                }

                if(i > 2)Thread.Sleep(2000);

                orders.TryAdd(orderNumber, null);
            }
            //var orderNumber = waiter.PlaceOrder(new[] {Tuple.Create("Burger", 1)}, 15);
            //cashier.PayForOrder(orderNumber);
            Console.ReadLine();
        }
Esempio n. 38
0
 public NodeEntryPoint(QueuedHandler handler, ManualResetEventSlim exitWait)
 {
     _handler = handler;
     _exitWait = exitWait;
 }