Example #1
0
        public void requestVote()
        {
            if (timerThreadBlock)
            {
                return;
            }
            if (_server.fd.changed())
            {
                _view       = _server.fd.getView();
                _numServers = _view.Count();
                foreach (string url in _view)
                {
                    if (!votemap.ContainsKey(url))
                    {
                        votemap.Add(url, false);
                    }
                }
                foreach (KeyValuePair <string, bool> entry in votemap)
                {
                    if (!_view.Contains(entry.Key))
                    {
                        votemap.Remove(entry.Key);
                    }
                }
            }

            int howmany = 0;

            foreach (KeyValuePair <string, bool> entry in votemap)
            {
                if (!entry.Value)
                {
                    howmany++;
                }
            }

            WaitHandle[]   handles      = new WaitHandle[howmany];
            IAsyncResult[] asyncResults = new IAsyncResult[howmany];
            string[]       requestId    = new string[howmany];
            try {
                int i = 0;
                foreach (KeyValuePair <string, bool> entry in votemap)
                {
                    if (entry.Value)
                    {
                        continue;
                    }
                    ServerService remoteObject = (ServerService)_serverRemoteObjects[entry.Key];
                    voteDelegate  voteDel      = new voteDelegate(remoteObject.vote);
                    IAsyncResult  ar           = voteDel.BeginInvoke(_term, _url, null, null);
                    asyncResults[i] = ar;
                    handles[i]      = ar.AsyncWaitHandle;
                    requestId[i]    = entry.Key;
                    i++;
                }
                if (!WaitHandle.WaitAll(handles, 100))
                {
                    requestVote();
                }
                else
                {
                    for (i = 0; i < howmany; i++)
                    {
                        IAsyncResult asyncResult = asyncResults[i];
                        voteDelegate voteDel     = (voteDelegate)((AsyncResult)asyncResult).AsyncDelegate;
                        bool         response    = voteDel.EndInvoke(asyncResult);
                        votemap.Remove(requestId[i]);
                        votemap.Add(requestId[i], true);
                        if (response)
                        {
                            votes++;
                        }
                    }
                    if (votes > (_numServers / 2))
                    {
                        stopClock();
                        timerThreadBlock = true;
                        _server.updateState("leader", _term, _url);
                        _server = null;
                        Console.WriteLine("Elected in term " + _term);
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Finished elections without success.");
                        pulseVote.Stop();
                        pulseVote.Dispose();
                        SetVoteTimer();
                    }
                }
            }
            catch (SocketException) {
                requestVote();
            }
        }
Example #2
0
        public void requestVote()
        {
            Console.WriteLine("request_vote --t " + Thread.CurrentThread.ManagedThreadId);
            if (timerThreadBlock)
            {
                return;
            }
            _term++;
            Console.WriteLine("Started election in term " + _term);
            int votes = 1;

            if (_server.fd.changed())
            {
                _view       = _server.fd.getView();
                _numServers = _view.Count();
                foreach (string url in _view)
                {
                    Console.WriteLine(url);
                }
            }
            Console.WriteLine("after view change");
            WaitHandle[]   handles      = new WaitHandle[_numServers - 1];
            IAsyncResult[] asyncResults = new IAsyncResult[_numServers - 1];
            try {
                int i = 0;
                foreach (string url in _view)
                {
                    if (url == _url)
                    {
                        continue;
                    }
                    ServerService remoteObject = (ServerService)_serverRemoteObjects[url];
                    voteDelegate  voteDel      = new voteDelegate(remoteObject.vote);
                    IAsyncResult  ar           = voteDel.BeginInvoke(_term, _url, null, null);
                    asyncResults[i] = ar;
                    handles[i]      = ar.AsyncWaitHandle;
                    i++;
                }
                if (!WaitHandle.WaitAll(handles, 4000))  //TODO
                {
                    Console.WriteLine("candidate timeout waiting for votes");
                    requestVote();
                }
                else
                {
                    for (i = 0; i < _numServers - 1; i++)
                    {
                        IAsyncResult asyncResult = asyncResults[i];
                        voteDelegate voteDel     = (voteDelegate)((AsyncResult)asyncResult).AsyncDelegate;
                        bool         response    = voteDel.EndInvoke(asyncResult);
                        if (response)
                        {
                            votes++;
                        }
                    }
                    if (votes > (_numServers / 2))
                    {
                        electionTimeout.Stop();
                        timerThreadBlock = true;
                        _server.updateState("leader", _term, _url);
                        _server = null;
                        electionTimeout.Dispose();
                        Console.WriteLine("elected in term" + _term);
                        return;
                    }
                    else
                    {
                        Console.WriteLine("Finished elections without sucess");
                    }
                }
                wait = rnd.Next(1000, 1200);
                electionTimeout.Interval = wait;
                electionTimeout.Enabled  = true;
            }
            catch (SocketException) {
                //TODO
                throw new NotImplementedException();
            }
        }