Пример #1
0
        void WithPeer(Guid peerId, Uri controlUri, Action <ActorRef> callback, bool createIfMissing)
        {
            bool found = _peers.Has(controlUri);

            if (!found)
            {
                if (!createIfMissing)
                {
                    return;
                }

                ActorRef peer = _peerHandlerFactory.GetActor();
                peer.Send(new InitializePeerHandler(peerId, controlUri));
                _peers.Add(controlUri, peer);
                _peerIds[peerId] = controlUri;
            }
            else
            {
                if (!_peerIds.Has(peerId))
                {
                    _peerIds[peerId] = controlUri;
                }
            }

            callback(_peers[controlUri]);
        }
Пример #2
0
        public override Host Build()
        {
            if (ServiceBuilders.Count > 1)
            {
                throw new HostConfigurationException("A shelf can only have one service configured");
            }

            ServiceBuilder builder = ServiceBuilders.Single();

            _log.DebugFormat("[Shelf:{0}] Building Service: {1}", Description.Name, builder.Name);

            var controllerFactory = new ServiceControllerFactory();
            ActorFactory <IServiceController> factory = controllerFactory.CreateFactory(inbox =>
            {
                var publish = new PublishChannel(_channel, inbox);

                IServiceController service = builder.Build(inbox, publish);

                return(service);
            });

            ActorRef instance = factory.GetActor();

            _channel.Connect(x => x.AddChannel(instance));

            // this creates the state machine instance in the shelf and tells the servicecontroller
            // to create the service
            instance.Send(new CreateService(Description.Name));

            return(new ShelfHost(instance));
        }
Пример #3
0
        public void Start()
        {
            AppDomain.CurrentDomain.UnhandledException += UnhandledException;

            CreateCoordinatorChannel();

            BeforeStartingServices();

            _startupServices.Each((name, builder) =>
            {
                ActorFactory <IServiceController> factory = _controllerFactory.CreateFactory(inbox =>
                {
                    IServiceController controller = builder(inbox, this);
                    _serviceCache.Add(name, controller);

                    return(controller);
                });

                ActorInstance instance = factory.GetActor();
                _actorCache.Add(name, instance);

                instance.Send(new CreateService(name));
            });

            WaitUntilServicesAreRunning(_startupServices.GetAllKeys(), _timeout);

            AfterStartingServices();
        }
Пример #4
0
        public TraceChannelImpl()
        {
            _factory = ActorFactory.Create(inbox => new TraceActor(inbox));
            _inbox   = _factory.GetActor();

            _dispose = _inbox.ExitOnDispose();
        }
Пример #5
0
        public void Should_bind_channels_for_each_method()
        {
            var subject = new Subject();

            ActorFactory <Subject> factory = ActorFactory.Create <Subject>(x =>
            {
                x.ConstructedBy(() => subject);

                x.ConnectPublicMethods();
                x.ConnectPropertyChannels();
            });

            ActorRef actor = factory.GetActor();

            subject.ShouldNotBeNull();

            actor.Send <Message <A> >();
            actor.Send <Request <B> >();
            actor.Send <Response <C> >();
            actor.Send <Message <D> >();

            subject.FutureA.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();
            subject.FutureB.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();
            subject.FutureC.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();
            subject.FutureD.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();
        }
Пример #6
0
		public void Adding_an_actor_to_a_registry()
		{
			_auctionFactory = ActorFactory.Create(inbox => new Auction(inbox));

			_auctionId = CombGuid.Generate();

			ActorInstance auction = _auctionFactory.GetActor();

			ActorRegistry registry = ActorRegistryFactory.New(x =>
				{
					//x.Remote(r => r.ListenTo("rm://234.0.0.7:40001"));
				});

			registry.Register(_auctionId, auction);


			var message = new MessageImpl<Bid>(new BidImpl(27.5m));

			message.DestinationAddress = new Uri("urn:uuid:" + _auctionId.ToString("N"));

			registry.Send<Message<Bid>>(message);


			// need to proxy the channel with headers somehow... 

			// untyped channel => channel mapper -> actor instance

			// DestinationAddress -> set by outbound channel proxy on message<>
			// SourceAddress -> set by outbound channel proxy when available (not likely)
			// ResponseAddress -> set by outbound channel for ResponseChannel on Request to map to channel
			// Id -> system assigned id
			// DestinationAddress = urn:actor:554FC958-4661-4FE9-94F5-21D190417BCC
		}
        public void An_active_auction()
        {
            Id = CombGuid.Generate();

            ActorFactory <Auction> factory = ActorFactory.Create((f, i) => new Auction(f, i, Id));

            Auction = factory.GetActor();
        }
Пример #8
0
        public void A_stream_of_messages_is_received()
        {
            ActorFactory <Agent> agentFactory = ActorFactory.Create(inbox => new Agent(inbox));

            ActorRef agent = agentFactory.GetActor();

            for (int i = 0; i < _count; i++)
            {
                agent.Send(new Add
                {
                    Value = i
                });

                if (i == 100)
                {
                    agent.Send <Suspend>();
                }

                if (i == 500)
                {
                    agent.Send <Resume>();
                }
            }

            _response = new Future <Status>();
            var actor = AnonymousActor.New(inbox =>
            {
                Action loop = null;
                loop        = () =>
                {
                    agent.Request(new Status(), inbox)
                    .Receive <Response <Status> >(response =>
                    {
                        if (response.Body.Count == _count)
                        {
                            _response.Complete(response.Body);
                        }
                        else
                        {
                            loop();
                        }
                    });
                };

                loop();
            });

            _response.WaitUntilCompleted(8.Seconds());
        }
Пример #9
0
        public void Easy_syntax_love()
        {
            ServiceController controller = null;

            ActorFactory <ServiceController> factory =
                StateMachineActorFactory.Create <ServiceWorkflow, ServiceController>(inbox =>
            {
                controller = new ServiceController(inbox);
                return(controller);
            }, x =>
            {
                x.AccessCurrentState(s => s.CurrentState);

                x.Initially()
                .When(e => e.Started)
                .Then(i => i.OnStart())
                .TransitionTo(s => s.Running);

                x.During(s => s.Running)
                .When(e => e.Stopped)
                .Then(i => i.OnStop())
                .Finalize()
                .When(e => e.Paused)
                .Then(i => i.OnPause)
                .TransitionTo(s => s.Idle);

                x.During(s => s.Idle)
                .When(e => e.Started)
                .TransitionTo(s => s.Running);
            });

            ActorRef service = factory.GetActor();

            service.Send(new Pause());
            service.Send(new Start());
            service.Send(new Start());
            service.Send(new Stop());

            try
            {
                controller.Started.WaitUntilCompleted(2.Seconds()).ShouldBeTrue("Never received start");
                controller.Paused.WaitUntilCompleted(2.Seconds()).ShouldBeTrue("Never received pause");
                controller.Stopped.WaitUntilCompleted(2.Seconds()).ShouldBeTrue("Never received stop");
            }
            finally
            {
                service.Exit();
            }
        }
Пример #10
0
        public void An_agent_is_paused()
        {
            ActorFactory <Agent> agentFactory = ActorFactory.Create(inbox => new Agent(inbox));

            ActorRef agent = agentFactory.GetActor();

            _first  = new Update();
            _second = new Update();

            agent.Send(_first);
            agent.Send <Suspend>();
            agent.Send(_second);

            _first.WaitUntilCompleted(2.Seconds());
            _second.WaitUntilCompleted(2.Seconds());
        }
Пример #11
0
        public void Easy_syntax_love()
        {
            ActorFactory <QuoteService> factory =
                StateMachineActorFactory.Create <QuoteServiceWorkflow, QuoteService>(inbox => new QuoteService(inbox), x =>
            {
                x.AccessCurrentState(s => s.CurrentState);

                x.Initially()
                .When(e => e.GetQuoteRequest)
                .Then(i => i.SendQuoteRequest)
                .TransitionTo(s => s.WaitingForResponse);

                x.During(s => s.WaitingForResponse)
                .When(e => e.CancelRequest)
                .Then(i => i.Cancel)
                .Finalize();
            });

            ActorRef service = factory.GetActor();


            var response = new Future <Response <RequestSent> >();

            var actor = AnonymousActor.New(inbox =>
            {
                service.Request(new GetQuote
                {
                    Symbol = "MSFT"
                }, inbox)
                .Receive <Response <RequestSent> >(r =>
                {
                    service.Request(new GetQuote
                    {
                        Symbol = "AAPL"
                    }, inbox)
                    .Receive <Response <RequestSent> >(rr =>
                    {
                        response.Complete(r);
                    });
                });
            });

            response.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();

            service.Exit();
        }
        bool SameMessageTypeTest <TInput, TConsumer>(int messageCount, Future <int> complete, Func <TInput> messageProvider)
        {
            var latch = new CountdownLatch(messageCount, complete.Complete);

            ActorFactory <TestActor <TConsumer> > factory = ActorFactory.Create(inbox => new TestActor <TConsumer>(inbox, latch));
            ActorRef actor = factory.GetActor();

            for (int i = 0; i < messageCount; i++)
            {
                actor.Send(messageProvider());
            }

            bool completed = complete.WaitUntilCompleted(30.Seconds());

            actor.Exit();

            return(completed);
        }
Пример #13
0
        void OnCreateShelfService(CreateShelfService message)
        {
            _log.InfoFormat("[Topshelf] Create Shelf Service: {0}{1}", message.ServiceName,
                            message.BootstrapperType == null
                                                ? ""
                                                : " ({0})".FormatWith(message.BootstrapperType.ToShortTypeName()));

            ActorFactory <IServiceController> factory = _controllerFactory.CreateFactory(inbox =>
            {
                IServiceController controller = new ShelfServiceController(inbox, message.ServiceName, this, message.ShelfType,
                                                                           message.BootstrapperType, message.AssemblyNames);
                _serviceCache.Add(message.ServiceName, controller);

                return(controller);
            });

            ActorInstance instance = factory.GetActor();

            _actorCache.Add(message.ServiceName, instance);

            instance.Send(new CreateService(message.ServiceName));
        }
Пример #14
0
        public void Adding_an_actor_to_a_registry()
        {
            _auctionFactory = ActorFactory.Create(inbox => new Auction(inbox));

            _auctionId = CombGuid.Generate();

            ActorRef auction = _auctionFactory.GetActor();

            ActorRegistry registry = ActorRegistryFactory.New(x =>
            {
                //x.Remote(r => r.ListenTo("rm://234.0.0.7:40001"));
            });

            registry.Register(_auctionId, auction);

            _response = new Future <Response <Bid> >();

            AnonymousActor.New(inbox =>
            {
                registry.Request <Bid>(new BidImpl(27.42m),
                                       header => { header.DestinationAddress = new ActorUrn(_auctionId); }, inbox)
                .Receive <Response <Bid> >(x => _response.Complete);
            });

            _response.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();

            // need to proxy the channel with headers somehow...

            // untyped channel => channel mapper -> actor instance

            // DestinationAddress -> set by outbound channel proxy on message<>
            // SourceAddress -> set by outbound channel proxy when available (not likely)
            // ResponseAddress -> set by outbound channel for ResponseChannel on Request to map to channel
            // Id -> system assigned id
            // DestinationAddress = urn:actor:554FC958-4661-4FE9-94F5-21D190417BCC
        }
Пример #15
0
        public void Adding_an_actor_to_a_registry()
        {
            _auctionFactory = ActorFactory.Create(inbox => new Auction(inbox));

            _auctionId = CombGuid.Generate();

            ActorRef auction = _auctionFactory.GetActor();

            ActorRegistry registry = ActorRegistryFactory.New(x =>
                {
                    //x.Remote(r => r.ListenTo("rm://234.0.0.7:40001"));
                });

            registry.Register(_auctionId, auction);

            _response = new Future<Response<Bid>>();

            AnonymousActor.New(inbox =>
                {
                    registry.Request<Bid>(new BidImpl(27.42m),
                                          header => { header.DestinationAddress = new ActorUrn(_auctionId); }, inbox)
                        .Receive<Response<Bid>>(x => _response.Complete);
                });

            _response.WaitUntilCompleted(5.Seconds()).ShouldBeTrue();

            // need to proxy the channel with headers somehow... 

            // untyped channel => channel mapper -> actor instance

            // DestinationAddress -> set by outbound channel proxy on message<>
            // SourceAddress -> set by outbound channel proxy when available (not likely)
            // ResponseAddress -> set by outbound channel for ResponseChannel on Request to map to channel
            // Id -> system assigned id
            // DestinationAddress = urn:actor:554FC958-4661-4FE9-94F5-21D190417BCC
        }