Пример #1
0
        private static async Task <Response[]> ExecServicesAsync(ITAPServices svc, UserId uid, Request[] requests)
        {
            Task <Response>[] responseTasks = new Task <Response> [requests.Length];
            Session           session;

            try
            {
                session = await svc.LoginAsync(uid);
            }
            catch
            {
                //throw illegal user exception
                throw;
            }

            for (int i = 0; i < requests.Length; i++)
            {
                try
                {
                    responseTasks[i] = svc.ExecServiceAsync(session, requests[i]);
                }
                catch
                {
                    //on server too busy exception
                    responseTasks[i] = null;
                }
            }

            await Task.WhenAll(responseTasks);

            await svc.LogoutAsync(session);

            return(responseTasks.Select(task => task.Result).ToArray());
        }
Пример #2
0
 public async Task <R> ExecOnNearServerAsync_4 <S, R>(ITAPServices <S, R> svc, Uri[] servers, S service)
 {
     Task <Uri>[] tasks = new Task <Uri> [servers.Length];
     for (int i = 0; i < servers.Length; ++i)
     {
         tasks[i] = svc.PingServerAsync(servers[i]);
     }
     Task.Factory.ContinueWhenAll(tasks, (antecedents) => {
         try { Task.WaitAll(antecedents); } catch { /*** log any thrown exceptions ***/ }
     });
     return(await svc.ExecServiceAsync(await Task.WhenAny(tasks).Result, service));
 }
Пример #3
0
 /// <summary>
 /// Versão original (promove o bloqueio de threads),
 /// acabando por ser equivalente a um aversão síncrona
 /// </summary>
 /// <param name="svc"></param>
 /// <param name="uid"></param>
 /// <param name="requests"></param>
 /// <returns></returns>
 public static Task <Response[]> ExecServicesOldAsync(ITAPServices svc, UserID uid, Request[] requests)
 {
     return(Task.Run(() => {
         Session session = svc.LoginAsync(uid).Result;
         try {
             Response[] responses = new Response[requests.Length];
             for (int i = 0; i < requests.Length; i++)
             {
                 responses[i] = svc.ExecServiceAsync(session, requests[i]).Result;
             }
             return responses;
         }
         finally { try { svc.LogoutAsync(session).Wait(); } catch { } }
     }));
 }
Пример #4
0
        /// <summary>
        /// Versão que optimiza o paralelismo potencial
        /// </summary>
        /// <param name="svc"></param>
        /// <param name="uid"></param>
        /// <param name="requests"></param>
        /// <returns></returns>
        public static async Task <Response[]> ExecServicesAsync(ITAPServices svc,
                                                                UserID uid, Request[] requests)
        {
            Session session = await svc.LoginAsync(uid);

            try {
                Task <Response>[] tasks = new Task <Response> [requests.Length];

                for (int i = 0; i < requests.Length; i++)
                {
                    tasks[i] = svc.ExecServiceAsync(session, requests[i]);
                }

                return(await Task.WhenAll(tasks));
            }
            finally { try { await svc.LogoutAsync(session); } catch { } }
        }
Пример #5
0
        public Task <R> ExecOnNearServerAsync <S, R>(ITAPServices <S, R> svc, Uri[] servers, S service)
        {
            Task <Uri>[] pingTasks = new Task <Uri> [servers.Length];
            for (int i = 0; i < servers.Length; ++i)
            {
                pingTasks[i] = svc.PingServerAsync(servers[i]);
            }
            return(Task.Factory.ContinueWhenAny(pingTasks, (ant) =>
            {
                pingTasks = pingTasks.Where(t => t != ant).ToArray();
                Task.Factory.ContinueWhenAll(pingTasks, (ant2) =>
                {
                    try
                    {
                        Task.WaitAll(ant2);
                    }
                    catch { }
                });

                return svc.ExecServiceAsync(ant.Result, service);
            }).Unwrap());
        }
Пример #6
0
        public async Task <R> ExecOnNearServerAsync_5 <S, R>(ITAPServices <S, R> svc, Uri[] servers, S service)
        {
            Task <Uri>[] pingTasks = new Task <Uri> [servers.Length];
            for (int i = 0; i < servers.Length; ++i)
            {
                pingTasks[i] = svc.PingServerAsync(servers[i]);
            }
            do
            {
                Task <Uri> pingTask = await Task.WhenAny(pingTasks);

                pingTasks = pingTasks.Where(t => t != pingTask).ToArray();
                try
                {
                    Uri uri = pingTask.Result;
                    Task.Factory.ContinueWhenAll(pingTasks, (ant) =>
                    {
                        try
                        {
                            Task.WaitAll(ant);
                        }
                        catch
                        {
                        }
                    });
                    return(await svc.ExecServiceAsync(uri, service));
                }
                catch (AggregateException ae)
                {
                    if (pingTasks.Length == 0)
                    {
                        throw ae.InnerException;
                    }
                }
            } while (true);
        }