public MessageSystem( IMessageFactory <M> messageFactory, IMessagePatternFactory <P> patternFactory, ILogger logger ) { MessageFactory = messageFactory; PatternFactory = patternFactory; Logger = logger; }
public async Task <IEnumerable <MatchingActor> > GetMatchingActorsAsync(M message, IMessagePatternFactory <P> messagePatternFactory) { var replicator = DistributedData.Get(ActorSystem).Replicator; GetKeysIdsResult keys = await replicator.Ask <GetKeysIdsResult>(Dsl.GetKeyIds); var matchingActors = new List <MatchingActor>(); foreach (var node in keys.Keys) { var setKey = new LWWRegisterKey <ActorProps[]>(node); var getResponse = await replicator.Ask <IGetResponse>(Dsl.Get(setKey, ReadLocal.Instance)); if (!getResponse.IsSuccessful) { throw new Exception($"cannot get message patterns for node {node}"); } var actorPropsList = getResponse.Get(setKey).Value; foreach (var actorProps in actorPropsList) { var path = actorProps.Path; var matchingPatterns = actorProps.Patterns.Where(pattern => pattern.Match(message)); int matchingPatternsCount = matchingPatterns.Count(); if (matchingPatternsCount > 0) { var matchingPattern = matchingPatterns.First(); if (matchingPatternsCount > 1) { Logger.Warning("For actor {0}, found {1} patterns matching message {2}. Taking first one = {3}", path, matchingPatternsCount, message, matchingPattern); } matchingActors.Add( new MatchingActor { Path = path, IsSecondary = matchingPattern.IsSecondary, MatchingScore = matchingPattern.Conjuncts.Length, MistrustFactor = actorProps.MistrustFactor }); } } } return(matchingActors); }