//============================================================== //============================================================== 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; } } } }