Exemplo n.º 1
0
        /// <summary>
        /// Checks if server at the given location is alive
        /// </summary>
        /// <param name="serverUrl">Server URL</param>
        /// <returns>True if the server is alive; false otherwise.</returns>
        public bool TryConnection(string serverUrl, bool tryRemove)
        {
            TSMan.CheckFreeze();

            TSMan.CheckDelay();


            if (serverUrl.Equals(TSMan.URL))
            {
                return(true);
            }

            // Get the reference for the tuple space server
            ITSpaceServer server = (ITSpaceServer)Activator.GetObject(typeof(ITSpaceServer), serverUrl);


            PingDelegate del         = new PingDelegate(server.Ping);
            IAsyncResult asyncResult = del.BeginInvoke(TSMan.URL, null, null);

            asyncResult.AsyncWaitHandle.WaitOne(5000, false);

            if (asyncResult.IsCompleted)
            {
                try
                {
                    del.EndInvoke(asyncResult);
                    return(true);
                }
                catch (Exception)
                {
                    if (tryRemove)
                    {
                        TryRemoveFromView(serverUrl);
                    }

                    return(false);
                }
            }
            if (tryRemove)
            {
                TryRemoveFromView(serverUrl);
            }

            return(false);
        }
Exemplo n.º 2
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);
        }