Example #1
0
 private void SendUserMessage(PID target, object message)
 {
     if (_senderMiddleware != null)
     {
         if (message is MessageEnvelope messageEnvelope)
         {
             //Request based middleware
             _senderMiddleware(this, target, messageEnvelope);
         }
         else
         {
             //tell based middleware
             _senderMiddleware(this, target, new MessageEnvelope(message, null, null));
         }
     }
     else
     {
         //Default path
         target.Tell(message);
     }
 }
Example #2
0
 private Task DefaultSender(ISenderContext context, PID target, MessageEnvelope message)
 {
     target.Tell(message);
     return(Actor.Done);
 }
Example #3
0
        public Task ReceiveAsync(IContext context)
        {
            switch (context.Message)
            {
            case Started _:
                _brokerMasterActor.Tell(context.Self);

                Props props = Actor.FromProducer(() => new PublisherActor());
                foreach (int _ in Enumerable.Range(0, Environment.ProcessorCount))
                {
                    PID publisher = context.SpawnPrefix(props, nameof(PublisherActor));
                    context.Watch(publisher);
                    _publisherActors.Add(publisher);
                }

                _clusterTopologyChangedSubscription = Actor.EventStream.Subscribe <ClusterTopologyEvent>(clusterTopologyEvent =>
                {
                    _brokerMasterActor.Tell(context.Self);
                });

                break;

            case Stopped _:
                _clusterTopologyChangedSubscription?.Unsubscribe();

                foreach (Subscription subscription in _subscriptions)
                {
                    TellAll(new Unsubscribe {
                        SubscriptionId = subscription.SubscriptionId
                    });
                }

                break;

            case GetPublisherPiDs _:
                GetPublisherPiDsResponse response = new GetPublisherPiDsResponse();
                response.PIds.AddRange(_publisherActors);
                context.Respond(response);
                break;

            case Subscribe subscribe:
                Subscription newSubscription = new Subscription
                {
                    SubscriptionId = ByteString.CopyFrom(Guid.NewGuid().ToByteArray()),
                    Subscriber     = subscribe
                };
                _subscriptions.Add(newSubscription);
                TellAll(newSubscription);
                break;

            case Unsubscribe unsubscribe:
                _subscriptions.RemoveWhere(subscription => unsubscribe.SubscriptionId == subscription.SubscriptionId);
                TellAll(unsubscribe);
                break;

            case Terminated terminated:
                _publisherActors.Remove(terminated.Who);
                context.Unwatch(terminated.Who);
                break;
            }

            return(Task.CompletedTask);
        }