Beispiel #1
0
        /// <summary>
        /// Creates memory-based tape storage factory, using the provided concurrent dictionary.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <returns></returns>
        public static MemoryTapeStorageFactory CreateTape(this MemoryAccount account, string container = "tapes")
        {
            var factory = new MemoryTapeStorageFactory(account.Tapes, container);

            factory.InitializeForWriting();
            return(factory);
        }
Beispiel #2
0
        /// <summary>
        /// Creates the simplified nuclear storage wrapper around Atomic storage.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="strategy">The atomic storage strategy.</param>
        /// <returns></returns>
        public static NuclearStorage CreateNuclear(this MemoryAccount dictionary, IAtomicStorageStrategy strategy)
        {
            var factory = new MemoryAtomicStorageFactory(dictionary.Data, strategy);

            factory.Initialize();
            return(new NuclearStorage(factory));
        }
        public void Run_in_test()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(CreateCustomer), typeof(CustomerCreated));

            var builder = new CqrsEngineBuilder(streamer);
            var account = new MemoryAccount();

            var nuclear = account.CreateNuclear();
            var inbox = account.CreateInbox("input");
            var sender = account.CreateSimpleSender(streamer, "input");

            var handler = new MessageHandler();
            handler.WireToLambda<CreateCustomer>(customer => Consume(customer, nuclear, sender));
            handler.WireToLambda<CustomerCreated>(m => Console.WriteLine("Created!"));
            builder.Handle(inbox, handler.HandleEnvelope);

            using (var engine = builder.Build())
            {
                sender.SendOne(new CreateCustomer
                {
                    CustomerId = 1,
                    CustomerName = "Rinat Abdullin"
                });
                engine.RunForever();
            }
        }
Beispiel #4
0
        public void Run_in_test()
        {
            var streamer = EnvelopeStreamer.CreateDefault(typeof(CreateCustomer), typeof(CustomerCreated));

            var builder = new CqrsEngineBuilder(streamer);
            var account = new MemoryAccount();

            var nuclear = account.CreateNuclear();
            var inbox   = account.CreateInbox("input");
            var sender  = account.CreateSimpleSender(streamer, "input");

            var handler = new MessageHandler();

            handler.WireToLambda <CreateCustomer>(customer => Consume(customer, nuclear, sender));
            handler.WireToLambda <CustomerCreated>(m => Console.WriteLine("Created!"));
            builder.Handle(inbox, handler.HandleEnvelope);

            using (var engine = builder.Build())
            {
                sender.SendOne(new CreateCustomer
                {
                    CustomerId   = 1,
                    CustomerName = "Rinat Abdullin"
                });
                engine.RunForever();
            }
        }
Beispiel #5
0
        public static MemoryPartitionInbox CreateInbox(this MemoryAccount account, params string[] queueNames)
        {
            var queues = queueNames
                         .Select(n => account.Queues.GetOrAdd(n, s => new BlockingCollection <byte[]>()))
                         .ToArray();

            return(new MemoryPartitionInbox(queues, queueNames));
        }
        public void Direct()
        {
            var config = new MemoryAccount();
            var raw = new RawEngineBuilder();
            var doWriter = config.CreateQueueWriter("do");

            // forwarder do => do
            raw.Dispatch(config.CreateInbox("do"), doWriter.PutMessage);
            TestConfiguration(doWriter, raw);
        }
Beispiel #7
0
        public void Direct()
        {
            var config   = new MemoryAccount();
            var raw      = new RawEngineBuilder();
            var doWriter = config.CreateQueueWriter("do");

            // forwarder do => do
            raw.Dispatch(config.CreateInbox("do"), doWriter.PutMessage);
            TestConfiguration(doWriter, raw);
        }
Beispiel #8
0
        public void Test()
        {
            var types = Assembly.GetExecutingAssembly().GetExportedTypes()
                .Where(t => typeof(IPS_SampleMessage).IsAssignableFrom(t));
            var streamer = EnvelopeStreamer.CreateDefault(types);
            var builder = new CqrsEngineBuilder(streamer);
            // only message contracts within this class

            // configure in memory:
            //                            -> sub 1
            //  inbox -> [PubSubRouter] <
            //                            -> sub 2
            //
            var store = new MemoryAccount();
            var nuclear = store.CreateNuclear();

            var router = new PubSubRouter(nuclear, store.CreateWriteQueueFactory(), streamer);
            router.Init();

            builder.Dispatch(store.CreateInbox("sub1"), b => Console.WriteLine("sub1 hit"));
            builder.Dispatch(store.CreateInbox("sub2"), b => Console.WriteLine("sub2 hit"));
            builder.Handle(store.CreateInbox("inbox"), router.DispatchMessage);

            var sender = store.CreateSimpleSender(streamer, "inbox");

            using (var engine = builder.Build())
            using (var cts = new CancellationTokenSource())
            {
                var task = engine.Start(cts.Token);

                // no handler should get these.
                sender.SendOne(new SomethingHappened());
                sender.SendOne(new OtherHappened());

                // subscribe sub1 to all messages and sub2 to specific message
                sender.SendControl(eb =>
                    {
                        eb.AddString("router-subscribe:sub1", ".*");
                        eb.AddString("router-subscribe:sub2", "SomethingHappened");
                    });
                sender.SendOne(new SomethingHappened());
                sender.SendOne(new OtherHappened());

                // unsubscribe all
                sender.SendControl(eb =>
                    {
                        eb.AddString("router-unsubscribe:sub1", ".*");
                        eb.AddString("router-unsubscribe:sub2", "SomethingHappened");
                    });
                sender.SendOne(new SomethingHappened());
                sender.SendOne(new OtherHappened());

                task.Wait(5000);
            }
        }
Beispiel #9
0
        /// <summary>
        /// Creates the simplified nuclear storage wrapper around Atomic storage.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="configStrategy">The config strategy.</param>
        /// <returns></returns>
        public static NuclearStorage CreateNuclear(this MemoryAccount dictionary,
                                                   Action <DefaultAtomicStorageStrategyBuilder> configStrategy)
        {
            var strategyBuilder = new DefaultAtomicStorageStrategyBuilder();

            configStrategy(strategyBuilder);

            var strategy = strategyBuilder.Build();

            return(CreateNuclear(dictionary, strategy));
        }
Beispiel #10
0
        public void Test()
        {
            // this test will start a simple web server on port 8082 (with a CQRS engine)
            // You might need to reserve that port or run test as admin. Check out unit test
            // output for the exact command line on port reservation (or MSDN docs)
            //
            // netsh http add urlacl url=http://+:8082/ user=RINAT-R5\Rinat.Abdullin
            // after starting the test, navigate you browser to localhost:8082/index.htm
            // and try dragging the image around

            // in-memory structure to capture mouse movement
            // statistics
            var stats = new MouseStats();

            // we accept a message just of this type, using a serializer
            var messages = new[] { typeof(MouseMoved), typeof(MouseClick) };
            var serializer = new MyJsonSerializer(messages);
            var streamer = new EnvelopeStreamer(serializer);
            var store = new MemoryAccount();
            var atomic = store.CreateNuclear().Factory;

            // let's configure our custom Http server to
            // 1. serve resources
            // 2. serve MouseStats View
            // 3. accept commands
            var environment = new HttpEnvironment { Port = 8082 };
            var builder = new CqrsEngineBuilder(streamer);

            builder.AddProcess(new Listener(environment,
                 new EmbeddedResourceHttpRequestHandler(typeof(MouseMoved).Assembly, "Snippets.HttpEndpoint"),
                 new MouseStatsRequestHandler(stats),
                 new HeatMapRequestHandler(atomic.GetEntityReader<unit, HeatMapView>()),
                 new MouseEventsRequestHandler(store.CreateQueueWriter("inbox"), serializer, streamer)));

            builder.Handle(store.CreateInbox("inbox"), envelope =>
                {
                    if (envelope.Items.Any(i => i.Content is MouseMoved))
                    {
                        MouseStatHandler(envelope, stats);
                    }
                    else if (envelope.Items.Any(i => i.Content is MouseClick))
                    {
                        MouseClickHandler(envelope, atomic.GetEntityWriter<unit, PointsView>());
                    }
                });

            builder.AddProcess(new HeatMapGenerateTask(atomic.GetEntityReader<unit, PointsView>(), atomic.GetEntityWriter<unit, HeatMapView>()));

            Process.Start("http://localhost:8082/index.htm");
            // this is a test, so let's block everything
            builder.Build().RunForever();
        }
        public void PartitionWithRouter()
        {
            var config = new MemoryAccount();
            var raw = new RawEngineBuilder();

            var inWriter = config.CreateQueueWriter("in");
            var doWriter = config.CreateQueueWriter("do");

            // forwarder in => do
            raw.Dispatch(config.CreateInbox("in"), doWriter.PutMessage);
            // forwarder do => in
            raw.Dispatch(config.CreateInbox("do"), inWriter.PutMessage);

            TestConfiguration(inWriter, raw);
        }
Beispiel #12
0
        public void PartitionWithRouter()
        {
            var config = new MemoryAccount();
            var raw    = new RawEngineBuilder();

            var inWriter = config.CreateQueueWriter("in");
            var doWriter = config.CreateQueueWriter("do");

            // forwarder in => do
            raw.Dispatch(config.CreateInbox("in"), doWriter.PutMessage);
            // forwarder do => in
            raw.Dispatch(config.CreateInbox("do"), inWriter.PutMessage);


            TestConfiguration(inWriter, raw);
        }
        public void RouterChain()
        {
            var config = new MemoryAccount();
            var raw = new RawEngineBuilder();
            var doWriter = config.CreateQueueWriter("do");

            var route1 = config.CreateQueueWriter("route1");
            var route2 = config.CreateQueueWriter("route2");

            // in => (route1 OR route2)
            raw.Dispatch(config.CreateInbox("in"), bytes => LoadBalance(bytes, route1, route2));
            // forwarder (route1,route2) => do
            raw.Dispatch(config.CreateInbox("route1", "route2"), doWriter.PutMessage);

            raw.Dispatch(config.CreateInbox("do"), bytes => LoadBalance(bytes, route1, route2));
            TestConfiguration(config.CreateQueueWriter("in"), raw);
        }
Beispiel #14
0
        public void RouterChain()
        {
            var config   = new MemoryAccount();
            var raw      = new RawEngineBuilder();
            var doWriter = config.CreateQueueWriter("do");

            var route1 = config.CreateQueueWriter("route1");
            var route2 = config.CreateQueueWriter("route2");

            // in => (route1 OR route2)
            raw.Dispatch(config.CreateInbox("in"), bytes => LoadBalance(bytes, route1, route2));
            // forwarder (route1,route2) => do
            raw.Dispatch(config.CreateInbox("route1", "route2"), doWriter.PutMessage);

            raw.Dispatch(config.CreateInbox("do"), bytes => LoadBalance(bytes, route1, route2));
            TestConfiguration(config.CreateQueueWriter("in"), raw);
        }
        public void Memory_lambda()
        {
            var config = new MemoryAccount();
            var writer = config.CreateQueueWriter("test");
            var inbox = config.CreateInbox("test");

            var builder = new RawEngineBuilder();
            builder.Dispatch(inbox, bytes => { });

            var setup = new Setup
                {
                    Send = i => writer.PutMessage(new[] {i}),
                    Engine = builder.Build()
                };

            TestConfiguration(setup, 1000000);
        }
Beispiel #16
0
        public void Memory_lambda()
        {
            var config = new MemoryAccount();
            var writer = config.CreateQueueWriter("test");
            var inbox  = config.CreateInbox("test");

            var builder = new RawEngineBuilder();

            builder.Dispatch(inbox, bytes => { });

            var setup = new Setup
            {
                Send   = i => writer.PutMessage(new[] { i }),
                Engine = builder.Build()
            };

            TestConfiguration(setup, 1000000);
        }
Beispiel #17
0
        public static SimpleMessageSender CreateSimpleSender(this MemoryAccount account, IEnvelopeStreamer streamer, string queueName, Func <string> idGenerator = null)
        {
            var queueWriter = new[] { CreateQueueWriter(account, queueName) };

            return(new SimpleMessageSender(streamer, queueWriter, idGenerator));
        }
Beispiel #18
0
 /// <summary>
 /// Creates the simplified nuclear storage wrapper around Atomic storage, using the default
 /// storage configuration and atomic strategy.
 /// </summary>
 /// <param name="dictionary">The dictionary.</param>
 /// <returns>
 /// new instance of the nuclear storage
 /// </returns>
 public static NuclearStorage CreateNuclear(this MemoryAccount dictionary)
 {
     return(CreateNuclear(dictionary, b => { }));
 }
Beispiel #19
0
 public static MemoryQueueWriterFactory CreateWriteQueueFactory(this MemoryAccount account)
 {
     return(new MemoryQueueWriterFactory(account));
 }
Beispiel #20
0
        public static IQueueWriter CreateQueueWriter(this MemoryAccount account, string queueName)
        {
            var collection = account.Queues.GetOrAdd(queueName, s => new BlockingCollection <byte[]>());

            return(new MemoryQueueWriter(collection, queueName));
        }