Exemplo n.º 1
0
        public void pingLoop()
        {
            while (true)
            {
                List <string>  oldView      = view;
                WaitHandle[]   handles      = new WaitHandle[numServers];
                IAsyncResult[] asyncResults = new IAsyncResult[numServers];
                try {
                    int   i         = 0;
                    int[] responses = new int[numServers];
                    foreach (KeyValuePair <string, IServerService> remoteObjectpair in serverRemoteObjects)
                    {
                        ServerService remoteObject = (ServerService)remoteObjectpair.Value;
                        pingDelegate  pingDel      = new pingDelegate(remoteObject.Ping);
                        IAsyncResult  ar           = pingDel.BeginInvoke(null, null);

                        asyncResults[i] = ar;
                        handles[i]      = ar.AsyncWaitHandle;
                        i++;
                    }
                    if (!WaitHandle.WaitAll(handles, 300))
                    {
                        for (int k = 0; k < numServers; k++)
                        {
                            if (handles[k].WaitOne(0) == false)
                            {
                                responses[k] = -1;
                            }
                        }
                    }
                    for (i = 0; i < numServers; i++)
                    {
                        try {
                            if (responses[i] != -1)   //responses with -1 already timed out, we don't want to endinvoke them
                            {
                                IAsyncResult asyncResult = asyncResults[i];
                                pingDelegate pingDel     = (pingDelegate)((AsyncResult)asyncResult).AsyncDelegate;
                                responses[i] = pingDel.EndInvoke(asyncResult);
                            }
                        }
                        catch (SocketException) {
                            responses[i] = -1;
                        }
                        catch (NullReferenceException) {
                            responses[i] = -1;
                        }
                    }
                    lock (view) {
                        view = new List <string>();
                        for (int j = 0; j < numServers; j++)
                        {
                            if (responses[j] != -1)
                            {
                                view.Add(allServers[j]);
                            }
                        }
                    }
                }
                catch (Exception e) {
                    Console.WriteLine(e.StackTrace);
                }

                bool isChanged = false;
                if (oldView.Count != view.Count)
                {
                    isChanged = true;
                }

                foreach (string bla in view)
                {
                    if (!oldView.Contains(bla))
                    {
                        isChanged = true;
                    }
                }
                if (isChanged)
                {
                    modified = true;
                    Console.WriteLine("View Changed. View count: " + view.Count);
                }
            }
        }
Exemplo n.º 2
0
        public void pingLoop()
        {
            while (true)
            {
                if (!_server.frozen)
                {
                    List <string>  oldView      = view;
                    WaitHandle[]   handles      = new WaitHandle[numServers];
                    IAsyncResult[] asyncResults = new IAsyncResult[numServers];
                    try {
                        int   i         = 0;
                        int[] responses = new int[numServers];
                        foreach (KeyValuePair <string, IServerService> remoteObjectpair in serverRemoteObjects)
                        {
                            ServerService remoteObject = (ServerService)remoteObjectpair.Value;
                            pingDelegate  pingDel      = new pingDelegate(remoteObject.Ping);
                            IAsyncResult  ar           = pingDel.BeginInvoke(null, null);

                            asyncResults[i] = ar;
                            handles[i]      = ar.AsyncWaitHandle;
                            i++;
                        }
                        if (!WaitHandle.WaitAll(handles, 300))
                        {
                            for (int k = 0; k < numServers; k++)
                            {
                                if (handles[k].WaitOne(timeouts[k]) == false)
                                {
                                    responses[k] = -2;
                                    if (!suspects.Contains(allServers[k]))   //timeout e ainda não era suspeito
                                    {
                                        Console.WriteLine("Added " + allServers[k] + " to suspects list");
                                        suspects.Add(allServers[k]);
                                    }
                                }
                            }
                        }
                        for (i = 0; i < numServers; i++)
                        {
                            try {
                                if (responses[i] != -2)   //responses with -2 already timed out, we don't want to endinvoke them
                                {
                                    IAsyncResult asyncResult = asyncResults[i];
                                    pingDelegate pingDel     = (pingDelegate)((AsyncResult)asyncResult).AsyncDelegate;
                                    responses[i] = pingDel.EndInvoke(asyncResult);
                                }
                            }
                            catch (SocketException e) {
                                responses[i] = -1;  // -1 means they are dead
                            }
                            catch (NullReferenceException e) {
                                responses[i] = -1;
                            }
                        }
                        lock (view) {
                            view = new List <string>();
                            for (int j = 0; j < numServers; j++)
                            {
                                if (responses[j] != -1)
                                {
                                    view.Add(allServers[j]);
                                }
                                if (responses[j] != -1 && responses[j] != -2 && suspects.Contains(allServers[j]))
                                {
                                    suspects.Remove(allServers[j]);
                                    timeouts[j] += 100;
                                    Console.WriteLine("Increased timeout (" + allServers[j] + " - " + timeouts[j].ToString() + ")");
                                }
                            }
                        }
                    }
                    catch (Exception e) {
                        Console.WriteLine(e.StackTrace);
                    }
                    bool isChanged = false;
                    if (oldView.Count != view.Count)
                    {
                        isChanged = true;
                    }

                    foreach (string str in view)
                    {
                        if (!oldView.Contains(str))
                        {
                            isChanged = true;
                        }
                    }
                    if (isChanged)
                    {
                        Console.WriteLine("View Changed");
                    }
                }
                else
                {
                    Thread.Sleep(100);
                }
            }
        }