示例#1
0
        public void Remove(Tuple tuple)
        {
            Console.WriteLine("Remove o tuplo: " + tuple);
            View actualView = this.GetView();
            List <ITupleSpaceXL> serversObj = new List <ITupleSpaceXL>();

            ITupleSpaceXL tupleSpace = null;

            //save remoting objects of all members of the view
            foreach (string serverPath in actualView.Servers)
            {
                try
                {
                    tupleSpace = (ITupleSpaceXL)Activator.GetObject(typeof(ITupleSpaceXL), serverPath);
                    tupleSpace.ItemCount(); //just to check availability of the server
                }
                catch (Exception) { tupleSpace = null; }
                if (tupleSpace != null)
                {
                    serversObj.Add(tupleSpace);
                }
            }

            foreach (ITupleSpaceXL server in serversObj)
            {
                try
                {
                    RemoteAsyncSecondPhaseDelegate RemoteDel = new RemoteAsyncSecondPhaseDelegate(server.remove);
                    IAsyncResult RemAr = RemoteDel.BeginInvoke(tuple, _workerId, null, null);
                }
                catch (Exception) { }
            }
            Console.WriteLine("** FRONTEND REMOVE: Just removed " + tuple);
        }
示例#2
0
        public void SimpleParserWithWildCardsTest()
        {
            ParseTree tree = _parser.Parse(INST_2);

            Assert.IsNotNull(tree);

            Tuple t = (Tuple)tree.Eval(null);

            Assert.IsNotNull(t);

            Assert.AreEqual(_tuple1, t);
        }
示例#3
0
        private static Tuple checkTupleSyntax(Parser parser, string input)
        {
            ParseTree tree  = parser.Parse(input);
            Tuple     tuple = null;

            tuple = (Tuple)tree.Eval(null);

            if (tuple == null)
            {
                Console.WriteLine("### ERROR: Invalid tuple syntax representation");
            }
            return(tuple);
        }
示例#4
0
        public void TestInitialize()
        {
            _fields  = new List <object>();
            _fields2 = new List <object>();
            _fields.Add("dog");
            _fields.Add("brown");

            _tuple1 = new Tuple(_fields);

            _scanner = new Scanner();

            _parser = new Parser(_scanner);
        }
示例#5
0
        public void Write(Tuple tuple)
        {
            ITupleSpace tupleSpace = null;

            foreach (string i in this.GetView())
            {
                try
                {
                    tupleSpace = (ITupleSpace)Activator.GetObject(typeof(ITupleSpace), i);
                    tupleSpace.ItemCount();
                    tupleSpace.write(tuple);
                }catch (Exception) { Console.WriteLine("Server with address: " + i + "has crashed"); }
            }
        }
示例#6
0
        /// <summary>
        /// Callback function to process read's server' replies
        /// <param name="IAsyncResult"A AsyncResult Delgate Object.</param>
        /// </summary>
        public static void CallbackRead(IAsyncResult ar)
        {
            RemoteAsyncReadDelegate del = (RemoteAsyncReadDelegate)((AsyncResult)ar).AsyncDelegate;

            lock (ReadLock)
            {
                //Only care about one reply, ignore others
                if (_responseRead == null)
                {
                    _responseRead = del.EndInvoke(ar);
                    readHandles[0].Set();
                }
            }
        }
示例#7
0
        public void ParseStringAndObjectTest()
        {
            ParseTree tree = _parser.Parse(INST_3);

            Assert.IsNotNull(tree);

            Tuple t = (Tuple)tree.Eval(null);

            Assert.IsNotNull(t);

            _fields2.Add("dog");
            _fields2.Add(new DADTestA(1, "Cat"));

            _tuple2 = new Tuple(_fields2);

            Assert.AreEqual(_tuple2.GetNumberOfFields(), t.GetNumberOfFields());

            Assert.AreEqual(_tuple2, t);
        }
示例#8
0
        public Tuple Take(Tuple tuple)
        {
            ITupleSpace tupleSpace = null;

            foreach (string i in this.GetView())
            {
                try
                {
                    tupleSpace = (ITupleSpace)Activator.GetObject(typeof(ITupleSpace), i);
                    tupleSpace.ItemCount();
                    Tuple response = null;
                    response = tupleSpace.take(tuple);
                    if (response != null)
                    {
                        return(response);
                    }
                }catch (Exception) { Console.WriteLine("Server with address: " + i + "has crashed"); }
            }
            return(null);
        }
示例#9
0
        public void ParseEqualsTest()
        {
            ParseTree tree1 = _parser.Parse(INST_4);

            Assert.IsNotNull(tree1);

            Tuple t1 = (Tuple)tree1.Eval(null);

            Assert.IsNotNull(t1);

            ParseTree tree2 = _parser.Parse(INST_5);

            Assert.IsNotNull(tree2);

            Tuple t2 = (Tuple)tree2.Eval(null);

            Assert.IsNotNull(t2);

            Assert.AreNotEqual(t1, t2);

            Assert.AreNotEqual(t2, t1);
        }
示例#10
0
        /// <summary>
        /// Execute an operation based on a parsed instruction.
        /// </summary>
        /// <param name="operation">Operation type (e.g. read, add, write, begin-repeat)</param>
        /// <param name="input">A complete string instruction</param>
        /// <param name="parser">An instance of the parser</param>
        /// <param name="frontEnd">An instance of the frontEnd (could be SMR ou XL).</param>
        /// <param name="prompt">A default string which are printed as prompt label.</param>
        private static void ExecuteOperation(string operation, string input, Parser parser, IFrontEnd frontEnd, string prompt)
        {
            Tuple tuple = null;

            switch (operation)
            {
            case "read":
                tuple = checkTupleSyntax(parser, input);

                Console.WriteLine("Tuple received: " + frontEnd.Read(tuple));
                _counter++;
                break;

            case "add":
                tuple = checkTupleSyntax(parser, input);
                frontEnd.Write(tuple);
                _counter++;
                break;

            case "take":
                tuple = checkTupleSyntax(parser, input);

                Console.WriteLine("Tuple received: " + frontEnd.Take(tuple));
                _counter++;
                break;

            case "begin-repeat":
                try
                {
                    int times = Int32.Parse(input.Split(' ')[1]);
                    if (times <= 0)
                    {
                        Console.WriteLine("### ERROR: Invalid begin-repeat arg: must be a positive integer!");
                    }
                    else
                    {
                        _counter++;
                        List <string> inputs = new List <string>();
                        while (true)
                        {
                            string innerInput = lines[_counter];
                            string innerOperation;

                            //Only when end is provided we execute the all body of begin-repeat
                            if (innerInput == "end-repeat")
                            {
                                for (int i = 0; i < times; i++)
                                {
                                    foreach (string storedInput in inputs)
                                    {
                                        innerOperation = storedInput.Split(' ')[0];
                                        ExecuteOperation(innerOperation, storedInput, parser, frontEnd, prompt);
                                    }
                                }
                                break;
                            }
                            else
                            {
                                inputs.Add(innerInput);
                                _counter++;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("### ERROR: Invalid begin-repeat arg");
                    Console.WriteLine(e.StackTrace);
                }

                break;

            case "wait":
                try
                {
                    int seconds = Int32.Parse(input.Split(' ')[1]);
                    if (seconds <= 0)
                    {
                        Console.WriteLine("### ERROR: Invalid wait arg: must be a positive number!");
                    }
                    else
                    {
                        Console.WriteLine("I'm waiting ...");
                        Thread.Sleep(seconds);
                        Console.WriteLine("Finished waiting!");
                        _counter++;
                    }
                }
                catch (Exception)
                {
                    Console.WriteLine("### ERROR: Invalid wait arg");
                }
                break;

            default:
                Console.WriteLine("### ERROR: Invalid command");
                break;
            }
        }
示例#11
0
        public void Write(Tuple tuple)
        {
            bool ViewOutOfDate = true;

            while (ViewOutOfDate)
            {
                _responseWrite = new List <bool>();
                View actualView    = this.GetView();
                int  numberServers = actualView.Count();
                numServers = numberServers;

                writeHandles = new AutoResetEvent[numberServers];

                Console.WriteLine(actualView);

                for (int i = 0; i < numberServers; i++)
                {
                    writeHandles[i] = new AutoResetEvent(false);
                }

                //List<ITupleSpaceXL> serversObj = new List<ITupleSpaceXL>();

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


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

                        RemoteAsyncWriteDelegate RemoteDel = new RemoteAsyncWriteDelegate(server.write);
                        a++;
                        AsyncCallback RemoteCallback = new AsyncCallback(CallbackWrite);
                        IAsyncResult  RemAr          = RemoteDel.BeginInvoke(_workerId, _requestId, tuple, _view, CallbackWrite, null);
                    }
                    catch (System.Net.Sockets.SocketException)
                    {
                        Console.WriteLine("** FRONTEND WRITE: Could not call write on server");
                    }
                }

                WaitHandle.WaitAll(writeHandles, 2000);

                //When some server joins the view while the operation is taking place

                //Because some machine on my view crashed while the operation was taking place
                if (_responseWrite.Count != _view.Servers.Count)
                {
                    writeCounter = 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 (_responseWrite.Count > 0)
                    {
                        ViewOutOfDate = false;
                    }
                }
                else
                {
                    if (_responseWrite.Contains(false) == true)
                    {
                        Console.WriteLine("** FRONTEND WRITE: View has been changed");
                    }
                    else
                    {
                        ViewOutOfDate = false;
                    }
                }
                _requestId++;
            }
            Console.WriteLine("** FRONTEND WRITE: Just wrote " + tuple + " a = " + a);
        }
示例#12
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);
        }
示例#13
0
        public Tuple Read(Tuple tuple)
        {
            readHandles    = new AutoResetEvent[1];
            readHandles[0] = new AutoResetEvent(false);

            View actualView = this.GetView();
            List <ITupleSpaceXL> serversObj = new List <ITupleSpaceXL>();
            List <string>        toRemove   = new List <string>();

            ITupleSpaceXL tupleSpace = null;

            _responseRead = null;

            //save remoting objects of all members of the view
            foreach (string serverPath in actualView.Servers)
            {
                try
                {
                    tupleSpace = (ITupleSpaceXL)Activator.GetObject(typeof(ITupleSpaceXL), serverPath);
                    tupleSpace.ItemCount(); //just to check availability of the server
                }
                catch (System.Net.Sockets.SocketException) {
                    tupleSpace = null;
                    toRemove.Add(serverPath);
                }
                if (tupleSpace != null)
                {
                    serversObj.Add(tupleSpace);
                }
            }

            if (serversObj.Count > 0)
            {
                foreach (string crashed in toRemove)
                {
                    //only one can crash so serverObj is obviously online
                    _view = serversObj[0].Remove(crashed);
                }
            }
            else
            {
                Console.WriteLine("All servers are dead! Exiting...");
                Environment.Exit(1);
            }


            foreach (ITupleSpaceXL server in serversObj)
            {
                try
                {
                    RemoteAsyncReadDelegate RemoteDel      = new RemoteAsyncReadDelegate(server.read);
                    AsyncCallback           RemoteCallback = new AsyncCallback(CallbackRead);
                    IAsyncResult            RemAr          = RemoteDel.BeginInvoke(tuple, CallbackRead, null);
                }
                catch (System.Net.Sockets.SocketException) { Console.WriteLine("** FRONTEND READ: Cannot invoke read on server!"); }
            }

            while (_responseRead == null)
            {
                Console.WriteLine("** FRONTEND READ: Not yet receive any reply let me wait...");

                WaitHandle.WaitAny(readHandles);
            }

            _requestId++;
            Console.WriteLine("** FRONTEND READ: Here is a response: " + _responseRead);
            return(_responseRead);
        }