Exemplo n.º 1
0
        public TSpaceMsg SMRProcessRequest(TSpaceMsg msg)
        {
            TSMan.CheckFreeze();

            TSMan.CheckDelay();

            lock (AggredOperations)
            {
                if (msg.Code != "proposeSeq")
                {
                    AggredOperations[msg.OperationID] = msg.SequenceNumber;
                }
                else
                {
                    //Console.WriteLine("Not Agreed: " + msg.OperationID);
                }
            }

            TSpaceMsg response = new TSpaceMsg
            {
                ProcessID   = TSMan.URL,
                OperationID = msg.OperationID,
                RequestID   = msg.RequestID,
                MsgView     = TSMan.GetTotalView()
            };

            // Verifying View! Wrong view sends updated view
            if (!TSMan.ValidView(msg))
            {
                Console.WriteLine("Wrong View ( s = " + TSMan.ServerView.ID + "; c = " + msg.MsgView.ID + " )");
                return(TSMan.CreateBadViewReply(msg));
            }

            lock (TSpaceAdvManager.ProcessedRequests)
            {
                // Check if request as already been processed
                if (TSpaceAdvManager.ProcessedRequests.Contains(msg.RequestID))
                {
                    // Check if it was processed in a previous viwew
                    if (TSpaceAdvManager.ProcessedRequests.GetByKey(msg.RequestID).Request.MsgView.ID < TSMan.ServerView.ID)
                    {
                        //Console.WriteLine(TSMan.ServerView.ID);
                        TSpaceAdvManager.ProcessedRequests.UpdateView(msg.RequestID, TSMan.ServerView);
                        TSpaceMsg resp = TSpaceAdvManager.ProcessedRequests.GetByKey(msg.RequestID).Response;
                        if (resp == null)
                        {
                            Console.WriteLine("NULL RESPONSE SAVED");
                            return(null);
                        }

                        return(resp);
                    }
                    else
                    {
                        response.Code = "Repeated";
                        return(response);
                    }
                }
                Console.WriteLine("Starting processing of request " + msg.RequestID);

                // Add sequence number of request to processed requests

                TSpaceAdvManager.ProcessedRequests.Add(msg);
            }

            string command = msg.Code;

            Message update = null;

            // Sequence number proposal request
            if (command.Equals("proposeSeq"))
            {
                lock (AggredOperations)
                {
                    //Already was executed corresponsing operation
                    if (AggredOperations.ContainsKey(msg.OperationID))
                    {
                        response.Code           = "proposedSeq";
                        response.SequenceNumber = AggredOperations[msg.OperationID];
                        return(response);
                    }


                    lock (MessageQueue)
                    {
                        // Increment sequence number
                        Interlocked.Increment(ref SequenceNumber);
                    }
                    response.SequenceNumber = SequenceNumber;
                    response.Code           = "proposedSeq";
                    AddMessageToQueue(msg);
                }

                Console.WriteLine("Proposing (id = " + msg.OperationID + "; seq = " + response.SequenceNumber + ")");

                return(response);
            }

            // Message with agreed sequence number
            else
            {
                //Update message queue with agreed seq number
                update = UpdateMessage(msg.OperationID, msg.SequenceNumber);
                if (update == null)
                {
                    Console.WriteLine("Err: operation message not in queue");
                    //response.Code = "Err";
                }
            }


            //Wait for the message head of queue
            Monitor.Enter(MessageQueue);
            while (MessageQueue.Count == 0 || !MessageQueue[0].MessageID.Equals(msg.OperationID))
            {
                TSMan.FinishedProcessing();
                Monitor.Wait(MessageQueue, 500);
                TSMan.Processing();
            }
            Monitor.Exit(MessageQueue);


            Console.WriteLine("Execute operation " + msg.OperationID + ": code = " + command);

            // Execute the operation
            if (TSMan.Verbose)
            {
                Console.WriteLine(msg);
            }

            switch (command)
            {
            case "add":
                lock (TakeLock)
                {
                    if (msg.Tuple == null)
                    {
                        response.Code = "ERR";
                        break;
                    }

                    TSMan.TSpace.Add(msg.Tuple);
                    response.Code = "ACK";
                    break;
                }

            case "read":
                if (msg.Tuple == null)
                {
                    response.Code = "ERR";
                    break;
                }
                response.Tuple = TSMan.TSpace.Read(msg.Tuple);
                response.Code  = "OK";
                if (response.Tuple == null)
                {
                    Console.WriteLine("Match not Found");
                }
                break;

            case "take1":
                lock (TakeLock)
                {
                    if (msg.Tuple == null)
                    {
                        response.Code = "ERR";
                        break;
                    }

                    // Get matching tuple
                    response.Tuple = TSMan.TSpace.Read(msg.Tuple);
                    response.Code  = "OK";
                    if (response.Tuple != null)
                    {
                        // Delete it
                        TSMan.TSpace.Take2(response.Tuple);
                    }
                }
                response.Code = "OK";
                break;

            case "take2":
                Console.WriteLine("Current Tuple Space not in XL mode");
                response.Code = "ERR";
                break;


            // Operation exclusive of the XL Tuple Space
            case "releaseLocks":

                lock (TakeLock)
                    response.Code = "ERR";
                Console.WriteLine("Current Tuple Space not in XL mode");
                break;

            default:
                Console.WriteLine("Invalid command.");
                break;
            }


            RemoveFromQueue(msg.OperationID);

            return(response);
        }
Exemplo n.º 2
0
        public TSpaceMsg XLProcessRequest(TSpaceMsg msg)
        {
            TSMan.CheckFreeze();
            TSMan.CheckDelay();

            TSpaceMsg response = new TSpaceMsg
            {
                ProcessID = TSMan.URL,
                RequestID = msg.RequestID,
                MsgView   = TSMan.GetTotalView()
            };

            // Verifying View! Wrong view sends updated view
            if (!TSMan.ValidView(msg))
            {
                Console.WriteLine("client:" + msg.MsgView.ToString() + "server:" + TSMan.GetTotalView().ToString());
                //Console.WriteLine("Wrong View ( s = " + TSMan.ServerView + "; c = " + msg.MsgView + " )");
                return(TSMan.CreateBadViewReply(msg));
            }

            if (TSMan.Verbose)
            {
                Console.WriteLine(msg);
            }


            lock (TSpaceAdvManager.ProcessedRequests)
            {
                // Check if request as already been processed
                if (TSpaceAdvManager.ProcessedRequests.Contains(msg.RequestID))
                {
                    LogEntry Temp = TSpaceAdvManager.ProcessedRequests.GetByKey(msg.RequestID);
                    // Check if it was processed in a previous viwew
                    if (Temp.Request.MsgView.ID < TSMan.ServerView.ID ||
                        (Temp.Response != null && Temp.Response.ProcessID != TSMan.URL))
                    {
                        Console.WriteLine("Processed in previous view");
                        Console.WriteLine(TSpaceAdvManager.ProcessedRequests.GetByKey(msg.RequestID).Request.MsgView.ID);
                        //Console.WriteLine(TSMan.ServerView.ID);

                        TSpaceMsg resp = TSpaceAdvManager.ProcessedRequests.GetByKey(msg.RequestID).Response;

                        if (resp == null)
                        {
                            Console.WriteLine("NULL RESPONSE SAVED");
                            return(null);
                        }

                        resp.MsgView = TSMan.ServerView;

                        TSpaceAdvManager.ProcessedRequests.UpdateView(msg.RequestID, TSMan.ServerView);
                        TSpaceAdvManager.ProcessedRequests.UpdateResponse(msg.RequestID, resp);


                        Console.WriteLine(resp);
                        return(resp);
                    }
                    else
                    {
                        //Console.WriteLine("repeated");
                        response.Code = "Repeated";

                        // Console.WriteLine("Repeated message response was:" + TSpaceAdvManager.ProcessedRequests.GetByKey(msg.RequestID).Response
                        //   + "\r\n IN RESPONSE TO " + msg);

                        return(response);
                    }
                }
                //Console.WriteLine("Starting processing of request " + msg.RequestID);

                // Add sequence number of request to processed requests

                TSpaceAdvManager.ProcessedRequests.Add(msg);
            }

            string command = msg.Code;

            Console.WriteLine("Processing Request " + command + " (seq = " + msg.RequestID + ")");



            switch (command)
            {
            case "add":
                TSMan.TSpace.Add(msg.Tuple);
                if (Freezer.Contains(msg.Tuple))
                {
                    TSMan.TSpace.Take2(msg.Tuple);
                }
                response.Code = "ACK";
                break;

            case "read":
                response.Tuple = TSMan.TSpace.Read(msg.Tuple);

                response.Code = "OK";
                if (response.Tuple == null)
                {
                    Console.WriteLine("Match not Found");
                }
                else
                {
                    Console.WriteLine("Match found");
                }
                break;

            case "take1":


                lock (TSLockHandler.Lock)
                {
                    // find suitable matches for tuple
                    List <ITuple> matches = TSMan.TSpace.Take1(msg.Tuple);
                    // Locks all unlocked and matchable tuples for UserID
                    response.Tuples = TSLockHandler.LockTuples(msg.ProcessID, matches);
                }

                response.Code = "OK";
                break;

            case "take2":
                lock (TSLockHandler.Lock)
                {
                    // Deletes tuple
                    if (!(TSMan.TSpace.Take2(msg.Tuple)))
                    {
                        ;
                    }
                    Freezer.Add(msg.Tuple);
                    //TSMan.TSpace.Take2(msg.Tuple);
                    // Unlocks all tuples previously locked under UserID
                    TSLockHandler.UnlockTuples(msg.ProcessID);
                }
                response.Code = "ACK";
                break;

            // Operation exclusive of the XL Tuple Space
            case "releaseLocks":
                try
                {
                    TSLockHandler.UnlockTuples(msg.ProcessID);
                    response.Code = "ACK";
                }
                catch (InvalidCastException)
                {
                    Console.WriteLine("Current Tuple Space not in XL mode");
                    response.Code = "ERR";
                }

                break;

            default:
                Console.WriteLine("Invalid command.");
                break;
            }

            Console.WriteLine("Return answer to " + command + " (seq = " + msg.RequestID + ")");
            return(response);
        }