Beispiel #1
0
        public Task ReceiveAsync(IContext context)
        {
            if (context.Message is Started)
            {
                _config.OnStarted(context, _routerState);
                _wg.Set();
                return(Task.CompletedTask);
            }

            if (context.Message is RouterAddRoutee addRoutee)
            {
                var r = _routerState.GetRoutees();
                if (r.Contains(addRoutee.Pid))
                {
                    return(Task.CompletedTask);
                }

                context.Watch(addRoutee.Pid);
                r.Add(addRoutee.Pid);
                _routerState.SetRoutees(r.ToArray());
                return(Task.CompletedTask);
            }

            if (context.Message is RouterRemoveRoutee removeRoutee)
            {
                var r = _routerState.GetRoutees();
                if (!r.Contains(removeRoutee.Pid))
                {
                    return(Task.CompletedTask);
                }

                context.Unwatch(removeRoutee.Pid);
                r.Remove(removeRoutee.Pid);
                _routerState.SetRoutees(r.ToArray());
                return(Task.CompletedTask);
            }

            if (context.Message is RouterBroadcastMessage broadcastMessage)
            {
                var sender = context.Sender;

                foreach (var routee in _routerState.GetRoutees())
                {
                    context.Request(routee, broadcastMessage.Message, sender);
                }

                return(Task.CompletedTask);
            }

            if (context.Message is RouterGetRoutees)
            {
                var r = _routerState.GetRoutees().ToList();
                context.Respond(new Routees(r));
            }

            return(Task.CompletedTask);
        }
Beispiel #2
0
 public Task ReceiveAsync(IContext context)
 {
     if (context.Message is Started)
     {
         _config.OnStarted(context, _routerState);
         _wg.Set();
         return(Actor.Done);
     }
     if (context.Message is RouterAddRoutee addRoutee)
     {
         var r = _routerState.GetRoutees();
         if (r.Contains(addRoutee.PID))
         {
             return(Actor.Done);
         }
         context.Watch(addRoutee.PID);
         r.Add(addRoutee.PID);
         _routerState.SetRoutees(r);
         return(Actor.Done);
     }
     if (context.Message is RouterRemoveRoutee removeRoutee)
     {
         var r = _routerState.GetRoutees();
         if (!r.Contains(removeRoutee.PID))
         {
             return(Actor.Done);
         }
         context.Unwatch(removeRoutee.PID);
         r.Remove(removeRoutee.PID);
         _routerState.SetRoutees(r);
         return(Actor.Done);
     }
     if (context.Message is RouterBroadcastMessage broadcastMessage)
     {
         foreach (var routee in _routerState.GetRoutees())
         {
             routee.Request(broadcastMessage.Message, context.Sender);
         }
         return(Actor.Done);
     }
     if (context.Message is RouterGetRoutees)
     {
         var r = _routerState.GetRoutees().ToList();
         context.Sender.Tell(new Routees(r));
     }
     return(Actor.Done);
 }