コード例 #1
0
        internal IEnumerable <AgentInformation> GetAgentInfos <TAgent>(ExecutionScopeOptions scope)
            where TAgent : IAgent
        {
            var agents = Enumerable.Empty <AgentInformation>();

            if (scope.HasFlag(ExecutionScopeOptions.Local))
            {
                agents = agents.Concat(GetLocalAgentInfos <TAgent>());
            }

            if (scope.HasFlag(ExecutionScopeOptions.Remote))
            {
                agents = agents.Concat(GetRemoteAgentInfos <TAgent>());
            }

            return(agents);
        }
コード例 #2
0
        public IEnumerable <Tuple <AgentInformation, TAgent> > GetAgents <TAgent>(ExecutionScopeOptions scope, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            var agents = Enumerable.Empty <Tuple <AgentInformation, TAgent> >();

            if (scope.HasFlag(ExecutionScopeOptions.Local))
            {
                agents = agents.Concat(GetLocalAgentInfos <TAgent>().Select(info => Tuple.Create((AgentInformation)info, (TAgent)info.Agent)));
            }

            if (scope.HasFlag(ExecutionScopeOptions.Remote))
            {
                agents = agents.Concat(GetRemoteAgentInfos <TAgent>().Select(info => Tuple.Create((AgentInformation)info, (TAgent)info.ServiceClientFactory.CreateServiceClient <TAgent>())));
            }

            if (!ignoreAgentState)
            {
                agents = agents.Where(a => a.Item1.LastKnownState == AgentState.Activated);
            }

            return(agents);
        }
コード例 #3
0
        public IObservable <T> ObserveAny <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            return(ObserveFirstUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState)
                   .Select(
                       r =>
            {
                if (!r.IsLeft)
                {
                    throw r.Right;
                }
                else
                {
                    return r.Left;
                }
            })
                   .Retry());
        }
コード例 #4
0
        public async Task <ExecutionResult <TResult> > TryExecuteOnFirst <TAgent, TResult>(Func <TAgent, Task <TResult> > operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }

            ct = ct ?? CancellationToken.None;

            ExecutionResult <TResult> result = null;

            foreach (var agentInfo in GetAgentInfos <TAgent>(scope).Where(info => ignoreAgentState || (info.IsReachable && info.LastKnownState == AgentState.Activated)))
            {
                if (ct.Value.IsCancellationRequested)
                {
                    result = new ExecutionResult <TResult> {
                        AgentId = agentInfo.AgentId, IsCanceled = true
                    };
                    break;
                }
                else
                {
                    result = await TryExecuteOnOne(agentInfo.AgentId, operation, ignoreAgentState).ConfigureAwait(false);

                    if (result.IsSuccessful)
                    {
                        break;
                    }
                }
            }

            if (result == null)
            {
                result = new ExecutionResult <TResult> {
                    Exception = new InvalidOperationException(string.Format("An agent that implements the contract '{0}' was not found.", typeof(TAgent).AssemblyQualifiedName))
                }
            }
            ;

            return(result);
        }
    }
コード例 #5
0
 public Task <ExecutionResult <TResult> > TryExecuteOnFirst <TAgent, TResult>(Func <TAgent, TResult> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(TryExecuteOnFirst <TAgent, TResult>(a => Task.Run(() => operation(a), ct ?? CancellationToken.None), scope, ct, ignoreAgentState));
 }
コード例 #6
0
 public async Task <ExecutionResult> TryExecuteOnFirst <TAgent>(Func <TAgent, Task> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(await TryExecuteOnFirst <TAgent, object>(async a => { await operation(a).ConfigureAwait(false); return (object)null; }, scope, ct, ignoreAgentState).ConfigureAwait(false));
 }
コード例 #7
0
 public IObservable <Tuple <AgentInformation, IObservable <T> > > ObserveAll <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(ObserveAllUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState).Select(t => Tuple.Create(t.Item1, t.Item2.SelectLeft(left => left))));
 }
コード例 #8
0
        private IObservable <Tuple <AgentInformation, IObservable <Either <T, Exception> > > > ObserveAllUnsafe <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            var currentAgents = GetAgentInfos <TAgent>(scope);

            if (!ignoreAgentState)
            {
                currentAgents = currentAgents.Where(a => a.IsReachable && a.LastKnownState == AgentState.Activated);
            }

            var newAgentsSource = this.AgentDataSource
                                  .Where(a =>
                                         (scope.HasFlag(a.IsLocal ? ExecutionScopeOptions.Local : ExecutionScopeOptions.Remote)) &&
                                         a.Contracts.Contains(typeof(TAgent).AssemblyQualifiedName));

            if (!ignoreAgentState)
            {
                newAgentsSource = newAgentsSource.Where(a => a.IsReachable && a.LastKnownState == AgentState.Activated);
            }

            return(Observable
                   .Merge(currentAgents.ToObservable(), newAgentsSource)
                   .Distinct(a => a.AgentId)
                   .Select(a => Tuple.Create(a, ObserveOneUnsafe <T>(a.AgentId, propertyName, ignoreAgentState))));
        }
コード例 #9
0
        public IEnumerable <Task <ExecutionResult <TResult> > > TryExecuteOnAll <TAgent, TResult>(Func <TAgent, Task <TResult> > operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (operation == null)
            {
                throw new ArgumentNullException("operation");
            }

            return(this.GetAgentInfos <TAgent>(scope)
                   .Where(info => ignoreAgentState || (info.IsReachable && info.LastKnownState == AgentState.Activated))
                   .Select(info => TryExecuteOnOne(info.AgentId, operation, ignoreAgentState)));
        }
コード例 #10
0
 public IEnumerable <Task <ExecutionResult> > TryExecuteOnAll <TAgent>(Func <TAgent, Task> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(TryExecuteOnAll <TAgent, object>(async a => { await operation(a).ConfigureAwait(false); return (object)null; }, scope, ignoreAgentState)
            .Select(async t => { return (ExecutionResult)await t.ConfigureAwait(false); }));
 }
コード例 #11
0
 public IEnumerable <Task <ExecutionResult> > TryExecuteOnAll <TAgent>(Action <TAgent> operation, ExecutionScopeOptions scope = ExecutionScopeOptions.All, CancellationToken?ct = null, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(TryExecuteOnAll <TAgent>(a => Task.Run(() => operation(a), ct ?? CancellationToken.None), scope, ignoreAgentState));
 }
コード例 #12
0
        public IObservable <Tuple <AgentInformation, T> > ObserveAnyWithAgentInfo <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            return(ObserveFirstUnsafeWithAgentInfo <TAgent, T>(propertyName, scope, ignoreAgentState)
                   .Select(
                       t =>
            {
                if (!t.Item2.IsLeft)
                {
                    throw t.Item2.Right;
                }
                else
                {
                    return Tuple.Create(t.Item1, t.Item2.Left);
                }
            })
                   .Retry());
        }
コード例 #13
0
 public IObservable <T> ObserveFirst <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(ObserveFirstUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState).SelectLeft(left => left));
 }
コード例 #14
0
        private IObservable <Tuple <AgentInformation, Either <T, Exception> > > ObserveFirstUnsafeWithAgentInfo <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            return(ObserveAllUnsafe <TAgent, T>(propertyName, scope, ignoreAgentState)
                   .Take(1)
                   .SelectMany(t => t.Item2.Select(data => Tuple.Create(t.Item1, data))));
        }
コード例 #15
0
 public IObservable <Tuple <AgentInformation, T> > ObserveFirstWithAgentInfo <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
     where TAgent : IAgent
 {
     return(ObserveFirstUnsafeWithAgentInfo <TAgent, T>(propertyName, scope, ignoreAgentState)
            .Where(t => t.Item2.IsLeft)
            .Select(t => Tuple.Create(t.Item1, t.Item2.Left)));
 }
コード例 #16
0
        private IObservable <Either <T, Exception> > ObserveFirstUnsafe <TAgent, T>(string propertyName, ExecutionScopeOptions scope = ExecutionScopeOptions.All, bool ignoreAgentState = false)
            where TAgent : IAgent
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw new ArgumentNullException("propertyName");
            }

            return(ObserveFirstUnsafeWithAgentInfo <TAgent, T>(propertyName, scope, ignoreAgentState)
                   .Select(t => t.Item2));
        }