Exemplo n.º 1
0
        public TSpaceState GetTSpaceState(string Url)
        {

            TSpaceState xl = new TSpaceState();

            TSpaceManager.RWL.AcquireWriterLock(Timeout.Infinite);


            xl.LockedTuplesKeys = TSLockHandler.GetKeys();
            xl.LockedTuplesValues = TSLockHandler.GetValues();


            this.UpdateView(Url);

            TSMan.AddToView(Url);

            xl.ServerView = TSMan.GetTotalView();

            xl.ProcessedRequests = TSpaceManager.ProcessedRequests;
            xl.TupleSpace = TSMan.GetTuples();


            TSpaceManager.RWL.ReleaseWriterLock();

            return xl;
        }
Exemplo n.º 2
0
 public void SetTSpaceState(TSpaceState smr)
 {
     lock (TSpaceAdvManager.ProcessedRequests)
     {
         TSpaceAdvManager.ProcessedRequests = smr.ProcessedRequests;
         TSMan.setView(smr.ServerView);
         TSMan.SetTuples(smr.TupleSpace);
         TSLockHandler.SetContent(smr.LockedTuplesKeys, smr.LockedTuplesValues);
         Console.WriteLine("Starting with view: " + smr.ServerView.ID);
     }
 }
Exemplo n.º 3
0
        private TSpaceState CopyState(string Url)
        {
            TSpaceState xl = new TSpaceState();


            xl.LockedTuplesKeys   = TSLockHandler.GetKeys();
            xl.LockedTuplesValues = TSLockHandler.GetValues();

            TSMan.AddToView(Url);

            xl.ServerView = TSMan.GetTotalView();

            xl.ProcessedRequests = TSpaceAdvManager.ProcessedRequests; //its static, cant be accessed with instance
            xl.TupleSpace        = TSMan.GetTuples();

            return(xl);
        }
Exemplo n.º 4
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 (TSpaceManager.ProcessedRequests)
            {
                // Check if request as already been processed
                if (TSpaceManager.ProcessedRequests.Contains(msg.RequestID))
                {
                    LogEntry Temp = TSpaceManager.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");
                        
                        TSpaceMsg resp = TSpaceManager.ProcessedRequests.GetByKey(msg.RequestID).Response;
                        
                        if (resp == null)
                        {
                            Console.WriteLine("NULL RESPONSE SAVED");
                            return null;
                        }

                        resp.MsgView = TSMan.ServerView;

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


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

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

                // Add sequence number of request to processed requests

                TSpaceManager.ProcessedRequests.Add(msg);

            }

            string command = msg.Code;


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

                case "read":
                    response.Tuple = TSMan.TSpace.Read(msg.Tuple);
                    response.Code = "OK";
                    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
                        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;
            }
            return response;
        }