Ejemplo n.º 1
0
        public Tuple Take(Tuple tuple)
        {
            bool ViewOutOfDate = true;

            while (ViewOutOfDate)
            {
                View actualView    = this.GetView();
                int  numberServers = actualView.Servers.Count;
                numServers  = numberServers;
                takeHandles = new AutoResetEvent[numberServers];

                for (int i = 0; i < actualView.Servers.Count; i++)
                {
                    takeHandles[i] = new AutoResetEvent(false);
                }

                Console.WriteLine("Vou tentar take: " + tuple);

                List <string> toRemove = new List <string>();

                _responseTake = new List <List <Tuple> >();

                foreach (string serverPath in actualView.Servers)
                {
                    try
                    {
                        ITupleSpaceXL server = (ITupleSpaceXL)Activator.GetObject(typeof(ITupleSpaceXL), serverPath);

                        RemoteAsyncTakeDelegate RemoteDel      = new RemoteAsyncTakeDelegate(server.take);
                        AsyncCallback           RemoteCallback = new AsyncCallback(CallbackTake);
                        IAsyncResult            RemAr          = RemoteDel.BeginInvoke(_workerId, _requestId, tuple, _view, CallbackTake, null);
                        ViewOutOfDate = false;
                    }
                    catch (System.Net.Sockets.SocketException)
                    {
                        Console.WriteLine("** FRONTEND TAKE: Could not call take on server " + serverPath);
                    }
                }

                //miguel: this only works in perfect case
                //TODO: One machine belonging to the view has just crashed

                WaitHandle.WaitAll(takeHandles, 10000);

                if (_responseTake.Count != _view.Servers.Count)
                {
                    takeCounter = 0;
                    foreach (string s in _view.Servers)
                    {
                        try
                        {
                            ITupleSpaceXL tupleSpace = (ITupleSpaceXL)Activator.GetObject(typeof(ITupleSpaceXL), s);
                            tupleSpace.checkView();
                        }
                        catch (Exception) { Console.WriteLine("Server " + s + " is dead."); }
                    }
                    if (_responseTake.Count > 0)
                    {
                        ViewOutOfDate = false;
                    }
                }
                else
                {
                    if (_responseTake.Contains(null) == true)
                    {
                        Console.WriteLine("** FRONTEND TAKE: View has been changed.");
                        _requestId++;
                    }
                    else
                    {
                        ViewOutOfDate = false;
                    }
                }
            }

            //Performs the intersection of all responses and decide using TupleSelection
            Tuple tup = TupleSelection(Intersection(_responseTake));

            if (tup == null)
            {
                Remove(null);
            }

            //Tuple tup is now selected lets remove
            if (tup != null)
            {
                Remove(tup);
            }
            _requestId++;

            if (tup != null)
            {
                Console.WriteLine("** FRONTEND TAKE: Here is a response: " + tup);
            }
            else
            {
                Console.WriteLine("** FRONTEND TAKE: Here is a NULL response");
            }
            return(tup);
        }