Пример #1
0
        //==============================================================
        //==============================================================


        public static void CheckCommandsInQueue_thread()
        {
            while (true)
            {
                while (MustFreeze == true)
                {
                    ;                             //FREEZE ****************************
                }
                Thread.Sleep(50);                 //Min time to check commands
                if (commLayer.GetQueueSize() > 0) //if there is commands
                {
                    Command cmd     = commLayer.RemoveFromCommandQueue();
                    MyTuple payload = (MyTuple)cmd.GetPayload();
                    Object  tmp;

                    Console.WriteLine("SERVER ======================== " + cmd.GetCommand() + " ----- " + cmd.GetPrevCommand() + "---------------" + cmd.GetUriFromSender());
                    switch (cmd.GetCommand())
                    {
                    case "read":
                        Console.WriteLine("START ========= REGISTER");

                        tmp = ts.Read(payload);
                        MyTuple a = tmp as MyTuple;

                        if (CommandsAlreadyReceived.Contains(cmd) == false)     //Test If command is received by the first time
                        {
                            //First time received
                            for (int i = 0; i < CommandsAlreadyReceived.Count; ++i)
                            {
                                if (CommandsAlreadyReceived[i].GetUriFromSender().Equals(cmd.GetUriFromSender()) == true)     //Remove the last command sent by the client
                                {
                                    CommandsAlreadyReceived.RemoveAt(i);
                                    break;
                                }
                            }
                            CommandsAlreadyReceived.Add(cmd); //add

                            if (a == null)                    //object does not exist in the tuple space so we refuse
                            {
                                GiveBackResult(cmd.GetUriFromSender(), new Command("refuse", null, ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                                CommandsAlreadyReceived.Remove(cmd);
                                //commLayer.InsertInBackLog(cmd);
                                //Console.WriteLine("(ServerService) Comando no Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                            }
                            else
                            {
                                //Ok
                                GiveBackResult(cmd.GetUriFromSender(), new Command("ack", a, ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                            }
                        }
                        //Ignore command
                        break;

                    case "add":

                        Console.WriteLine("============ADD: " + cmd.GetUriFromSender());

                        ////
                        if (CommandsAlreadyReceived.Contains(cmd) == false)     //Test If command is received by the first time
                        {
                            //First time received
                            for (int i = 0; i < CommandsAlreadyReceived.Count; ++i)
                            {
                                if (CommandsAlreadyReceived[i].GetUriFromSender().Equals(cmd.GetUriFromSender()) == true)     //Remove the last command sent by the client
                                {
                                    CommandsAlreadyReceived.RemoveAt(i);
                                    break;
                                }
                            }
                            CommandsAlreadyReceived.Add(cmd); //add

                            ts.Add(payload);                  //Insert in the tuple space

                            Console.WriteLine("Imagem: ");
                            Console.WriteLine(ts.ToString());


                            GiveBackResult(cmd.GetUriFromSender(), new Command("ack", null, ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                        }
                        ///
                        break;

                    case "take":

                        /*
                         * if (CommandsAlreadyReceived.Contains(cmd) == false) //Test If command is received by the first time
                         * {
                         *  //First time received;
                         *  for (int i = 0; i < CommandsAlreadyReceived.Count; ++i)
                         *  {
                         *      if (CommandsAlreadyReceived[i].GetUriFromSender().Equals(cmd.GetUriFromSender()) == true) //Remove the last command sent by the client
                         *      {
                         *         CommandsAlreadyReceived.RemoveAt(i);
                         *         break;
                         *      }
                         *  }
                         *  CommandsAlreadyReceived.Add(cmd); //add
                         */

                        //========================================================
                        //============================= PHASE 1
                        //========================================================
                        bool res = ts.IsTupleIn(payload);

                        if (res == false)         //object does not exist in the tuple space so we must Send REFUSE
                        {
                            GiveBackResult(cmd.GetUriFromSender(), new Command("refuse", null, ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                            CommandsAlreadyReceived.Remove(cmd);        //Remove because we refuse at the moment
                            //Console.WriteLine("(ServerService) Comando no Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                        }
                        else
                        {
                            //Check if is in lock
                            if (LockedTuples.Count == 0)
                            {
                                //I will acquire the lock for sure
                                //Acquire Lock
                                Lock LockTmp = new Lock(cmd.GetUriFromSender());

                                MyTuple RoverTmp;
                                int     Rover = 0;
                                while (true)         //Check all that match
                                {
                                    RoverTmp = ts.RoverOneByOne(Rover);
                                    if (RoverTmp == null)
                                    {
                                        break;
                                    }
                                    else
                                    {
                                        if (RoverTmp.Equals(payload) == true)
                                        {
                                            LockTmp.Insert(RoverTmp);
                                        }
                                    }
                                    ++Rover;
                                }
                                LockedTuples.Add(LockTmp);
                                //Return to the caller the set of all tuples that match
                                GiveBackResult(cmd.GetUriFromSender(), new Command("ack", LockTmp.GetSet(), ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                            }
                            else
                            {
                                for (int i = 0; i < LockedTuples.Count; ++i)
                                {
                                    if (LockedTuples[i].IsIn(payload) == true)
                                    {
                                        //Object already has lock. So I must send Refuse
                                        GiveBackResult(cmd.GetUriFromSender(), new Command("refuse", null, ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                                        break;
                                    }
                                    else
                                    {
                                        //Acquire Lock
                                        Lock LockTmp = new Lock(cmd.GetUriFromSender());

                                        MyTuple RoverTmp;
                                        int     Rover = 0;
                                        while (true)         //Check all that match
                                        {
                                            RoverTmp = ts.RoverOneByOne(Rover);
                                            if (RoverTmp == null)
                                            {
                                                break;
                                            }
                                            else
                                            {
                                                if (RoverTmp.Equals(payload) == true)
                                                {
                                                    LockTmp.Insert(RoverTmp);
                                                }
                                            }
                                            ++Rover;
                                        }
                                        LockedTuples.Add(LockTmp);
                                        //Return to the caller the set of all tuples that match
                                        GiveBackResult(cmd.GetUriFromSender(), new Command("ack", LockTmp.GetSet(), ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                                    }
                                }
                            }
                            //}



                            //Console.WriteLine("Imagem: ");
                            //Console.WriteLine(ts.ToString());
                        }
                        break;

                    case "remove":
                        //========================================================
                        //============================= PHASE 2
                        //========================================================
                        if (CommandsAlreadyReceived.Contains(cmd) == false)     //Test If command is received by the first time
                        {
                            //First time received
                            for (int i = 0; i < CommandsAlreadyReceived.Count; ++i)
                            {
                                if (CommandsAlreadyReceived[i].GetUriFromSender().Equals(cmd.GetUriFromSender()) == true)     //Remove the last command sent by the client
                                {
                                    CommandsAlreadyReceived.RemoveAt(i);
                                    break;
                                }
                            }
                            CommandsAlreadyReceived.Add(cmd);     //add
                            ts.Take((MyTuple)cmd.GetPayload());
                            //release do lock
                            Console.WriteLine("Imagem: ");
                            Console.WriteLine(ts.ToString());

                            LockedTuples.Remove(new Lock(cmd.GetUriFromSender()));
                            GiveBackResult(cmd.GetUriFromSender(), new Command("ack", cmd.GetPayload(), ServerProgram.GetMyAddress(), cmd.GetSequenceNumber(), cmd.GetCommand()));
                        }
                        break;

                    case "free_lock":
                        if (CommandsAlreadyReceived.Contains(cmd) == false)     //Test If command is received by the first time
                        {
                            //First time received
                            for (int i = 0; i < CommandsAlreadyReceived.Count; ++i)
                            {
                                if (CommandsAlreadyReceived[i].GetUriFromSender().Equals(cmd.GetUriFromSender()) == true)     //Remove the last command sent by the client
                                {
                                    CommandsAlreadyReceived.RemoveAt(i);
                                    break;
                                }
                            }
                            CommandsAlreadyReceived.Add(cmd);     //add

                            //release do lock
                            LockedTuples.Remove(new Lock(cmd.GetUriFromSender()));
                            //GiveBackResult(cmd.GetUriFromSender(), new Command("ack", null, ServerProgram.GetMyAddress(), cmd.GetSequenceNumber()));
                        }
                        break;
                    }
                }
            }
        }
Пример #2
0
        public static void CheckCommandsInQueue_thread()
        {
            while (true)
            {
                while (MustFreeze == true)
                {
                    ;                             //FREEZE ****************************
                }
                Thread.Sleep(50);                 //Min time to check commands

                if (commLayer.GetQueueSize() > 0) //if there is commands
                {
                    Command cmd     = commLayer.RemoveFromCommandQueue();
                    MyTuple payload = (MyTuple)cmd.GetPayload();
                    Object  tmp;



                    switch (cmd.GetCommand())
                    {
                    case "read":
                        tmp = ts.Read(payload);
                        MyTuple a = tmp as MyTuple;

                        Console.WriteLine("Imagem: ");
                        Console.WriteLine(ServerService.GetTupleSpaceRepresentation());

                        if (a == null)     //object does not exist in the tuple space so we put in backlog
                        {
                            commLayer.InsertInBackLog(cmd);
                            Console.WriteLine("(ServerService) Comando no Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                        }
                        else
                        {
                            GiveBackResult(cmd.GetUriFromSender(), a);
                        }
                        break;

                    case "add":
                        ts.Add(payload);
                        //Console.WriteLine(" ================= UMA VEZ ================ ");

                        Console.WriteLine("Imagem: ");
                        Console.WriteLine(ServerService.GetTupleSpaceRepresentation());

                        GiveBackResult(cmd.GetUriFromSender(), null);
                        MyTuple a1;
                        //serach in the backlog
                        for (int i = 0; i < commLayer.GetBackLogSize(); ++i)
                        {
                            Command Command_tmp = commLayer.GetBackLogCommand(i);
                            if (Command_tmp.GetCommand().Equals("read"))
                            {
                                tmp = ts.Read((MyTuple)Command_tmp.GetPayload());
                                a1  = tmp as MyTuple;
                                if (a1 != null)
                                {
                                    commLayer.RemoveFromBackLog(i);
                                    i = -1;
                                    Console.WriteLine("(ServerService) Comando Atendido e Removido do Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                                    GiveBackResult(Command_tmp.GetUriFromSender(), a1);
                                }
                            }
                            else
                            {
                                if (Command_tmp.GetCommand().Equals("take"))
                                {
                                    tmp = ts.Take((MyTuple)Command_tmp.GetPayload());
                                    a1  = tmp as MyTuple;
                                    if (a1 != null)     //test if is a MyTuple
                                    {
                                        commLayer.RemoveFromBackLog(i);
                                        i = -1;
                                        GiveBackResult(Command_tmp.GetUriFromSender(), a1);
                                    }
                                }
                            }
                        }
                        break;

                    case "take":
                        tmp = ts.Take(payload);
                        MyTuple a2 = tmp as MyTuple;
                        if (a2 == null)     //object does not exist in the tuple space so we put in backlog
                        {
                            commLayer.InsertInBackLog(cmd);
                            Console.WriteLine("(ServerService) Comando no Backlog: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString());
                        }
                        else
                        {
                            Console.WriteLine("Imagem: ");
                            Console.WriteLine(ServerService.GetTupleSpaceRepresentation());

                            GiveBackResult(cmd.GetUriFromSender(), a2);
                        }
                        break;
                    }
                }
            }
        }