public static void ProcessReceivedReplys_thread() { while (true) { Thread.Sleep(50); //Min time to check commands if (commLayer.GetQueueSize() > 0) //if there is commands { Command mt = commLayer.RemoveFromCommandQueue(); //MyTuple payload = (MyTuple)mt.GetPayload(); Object tmp; switch (mt.GetCommand()) { case "read": for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { if (ClientProgram.ThreadsInAction[i].Equals(new SenderPool(null, null, mt.GetUriFromSender(), mt)) == true) { ClientProgram.ThreadsInAction[i].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[i].GetThread().Join(); ClientProgram.ThreadsInAction.RemoveAt(i); ClientProgram.Pending_SignalEvent.Set(); Console.WriteLine("(ClientServices) RECEBI: " + mt.GetPayload().ToString()); flag = true; break; } } if (flag == true) { for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { ClientProgram.ThreadsInAction[i].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[i].GetThread().Join(); flag = false;; } ClientProgram.ThreadsInAction.Clear(); } break; case "add": for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { if (ClientProgram.ThreadsInAction[i].Equals(new SenderPool(null, null, mt.GetUriFromSender(), mt)) == true) //Se está OK { //ClientProgram.ThreadsInAction[i].GetThreadState().Kill_hread(); //ClientProgram.ThreadsInAction[i].GetThread().Join(); //ClientProgram.ThreadsInAction.RemoveAt(i); if (++NumOfAddReceived == ClientProgram.AllServers.Count) { NumOfAddReceived = 0; for (int j = 0; j < ClientProgram.ThreadsInAction.Count; ++j) { ClientProgram.ThreadsInAction[j].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[j].GetThread().Join(); flag = false;; } ClientProgram.ThreadsInAction.Clear(); //todos recebidos } if (AlreadyEvolvedFromAdd == false) { flag = true; } break; } } if (flag == true) { if (NumOfAddReceived >= (ClientProgram.AllServers.Count / 2 + 1)) { AlreadyEvolvedFromAdd = true; ClientProgram.Pending_SignalEvent.Set(); //Signal UpperLayer that it can evolve Console.WriteLine("(ClientServices) ADD Success in the majority of replicas: "); flag = false; //Majority of ADD Acknowledge received } } break; case "refuse": ++NumOfRefusedReceived; if ((NumOfRefusedReceived + NumOfAcceptedReceived) == ClientProgram.AllServers.Count) { //At least One refused. Start al over again NumOfRefusedReceived = 0; NumOfAcceptedReceived = 0; ClientProgram.Pending_SignalEvent.Set(); } break; case "accept": ++NumOfAcceptedReceived; AcceptedReply.Add(mt); if (NumOfAcceptedReceived == ClientProgram.AllServers.Count) { //All accpeted HashSet <MyTuple> Intersection = (HashSet <MyTuple>)AcceptedReply[0].GetPayload(); for (int i = 1; i < AcceptedReply.Count; ++i) { HashSet <MyTuple> mySet2 = (HashSet <MyTuple>)AcceptedReply[i].GetPayload(); Intersection.Intersect(mySet2); } //Test if null if (Intersection.Count == 0) { //Restart FAse 1 ClientProgram.Pending_SignalEvent.Set(); } else { foreach (MyTuple i in Intersection) { //Console.WriteLine("(ClientServices) TAKE Success: " + i.ToString()); ClientProgram.FinnishTake(); ClientProgram.Pending_SignalEvent.Set(); //Console.WriteLine("Antes do Remove"); //Console.WriteLine("Depois do Remove"); break; } } } break; case "ack": //FASE 2 ++NumOfAckReceived; if (NumOfAckReceived == ClientProgram.AllServers.Count) { //All received. Finnish NumOfAckReceived = 0; Console.WriteLine("(ClientServices) TAKE Success in all"); ClientProgram.FinnishTake(); } ClientProgram.Pending_SignalEvent.Set(); break; } } } }
//============================================================== //============================================================== 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; } } } }
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; } } } }
public static void ProcessReceivedReplys_thread() { while (true) { Thread.Sleep(50); //Min time to check commands if (commLayer.GetQueueSize() > 0) //if there is commands { Command mt = commLayer.RemoveFromCommandQueue(); //MyTuple payload = (MyTuple)mt.GetPayload(); Object tmp; Console.WriteLine("=================" + mt.GetCommand()); switch (mt.GetCommand()) { case "ack": if (CommandInUse == "read" && mt.GetPrevCommand() == "read") { for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { if (ClientProgram.ThreadsInAction[i].Equals(new SenderPool(null, null, mt.GetUriFromSender(), mt)) == true) { ClientProgram.ThreadsInAction[i].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[i].GetThread().Join(); ClientProgram.ThreadsInAction.RemoveAt(i); ClientProgram.Read_SignalEvent.Set(); //Read Ok. At least One received Console.WriteLine("Value: " + mt.GetPayload().ToString()); flag = true; break; } } if (flag == true) { for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { ClientProgram.ThreadsInAction[i].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[i].GetThread().Join(); flag = false;; } ClientProgram.ThreadsInAction.Clear(); } } else { if (CommandInUse == "add" && mt.GetPrevCommand() == "add") //=================================================== { for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { if (ClientProgram.ThreadsInAction[i].Equals(new SenderPool(null, null, mt.GetUriFromSender(), mt)) == true) //Se está OK { if (ServersInTheView.Contains(mt.GetUriFromSender()) == false) { ServersInTheView.Add(mt.GetUriFromSender()); if (ServersInTheView.Count >= (currentView.Count / 2 + 1)) { //Can progress for (int j = 0; j < ClientProgram.ThreadsInAction.Count; ++j) { for (int k = 0; k < ServersInTheView.Count; ++k) { if (ClientProgram.ThreadsInAction[j].GetUri().Equals(ServersInTheView[k])) { ClientProgram.ThreadsInAction[j].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[j].GetThread().Join(); ClientProgram.ThreadsInAction.RemoveAt(j); j = -1; break; } } } ClientProgram.Add_SignalEvent.Set(); //Signal UpperLayer that it can evolve Console.WriteLine("(ClientServices) ADD Success in the majority of replicas: "); } } break; } } } else { if (CommandInUse == "take" && mt.GetPrevCommand() == "take") //=================================================== { for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { if (ClientProgram.ThreadsInAction[i].Equals(new SenderPool(null, null, mt.GetUriFromSender(), mt)) == true) //Se está OK { if (ServersInTheView.Contains(mt.GetUriFromSender()) == false) { ServersInTheView.Add(mt.GetUriFromSender()); AcceptedReply.Add(mt); //Accepted stored if (ServersInTheView.Count == currentView.Count) { //Can progress //Check intersection of the tuple space HashSet <MyTuple> Intersection = (HashSet <MyTuple>)AcceptedReply[0].GetPayload(); for (int j = 1; j < AcceptedReply.Count; ++j) { HashSet <MyTuple> mySet2 = (HashSet <MyTuple>)AcceptedReply[i].GetPayload(); Intersection.Intersect(mySet2); } //Test if null if (Intersection.Count == 0) { //Restart FAse 1 ServersInTheView.Clear(); AcceptedReply.Clear(); break; //ClientProgram.Take_SignalEvent.Set(); } else { foreach (MyTuple t in Intersection) { //Console.WriteLine("(ClientServices) TAKE Success: " + i.ToString()); //ClientProgram.FinnishTake(); //ClientProgram.Take_SignalEvent.Set(); //Vou ter que mandar REMOVE //Limpar tudo for (int j = 0; j < ClientProgram.ThreadsInAction.Count; ++j) { for (int k = 0; k < ServersInTheView.Count; ++k) { if (ClientProgram.ThreadsInAction[j].GetUri().Equals(ServersInTheView[k])) { ClientProgram.ThreadsInAction[j].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[j].GetThread().Join(); ClientProgram.ThreadsInAction.RemoveAt(j); j = -1; break; } } } //===================================================== Preparar REMOVE ClientProgram.SendToView(new Command("remove", t, ClientProgram.MyAddress, ClientProgram.SequenceNumber, null)); SetCurrentCommandAndView("remove", ClientProgram.GetView()); //Console.WriteLine("Depois do Remove"); break; } } //ClientProgram.SendToView(new Command("remove", t, MyAddress, ++SequenceNumber, null)); //ClientProgram.Take_SignalEvent.Set(); //Signal UpperLayer that it can evolve Console.WriteLine("(ClientServices) TAKE Success in all the replicas: "); } } break; } } } else { if (CommandInUse == "remove" && mt.GetPrevCommand() == "remove") //=================================================== { for (int i = 0; i < ClientProgram.ThreadsInAction.Count; ++i) { if (ClientProgram.ThreadsInAction[i].Equals(new SenderPool(null, null, mt.GetUriFromSender(), mt)) == true) //Se está OK { if (ServersInTheView.Contains(mt.GetUriFromSender()) == false) { ServersInTheView.Add(mt.GetUriFromSender()); if (ServersInTheView.Count == currentView.Count) { //Can progress //Limpar tudo for (int j = 0; j < ClientProgram.ThreadsInAction.Count; ++j) { for (int k = 0; k < ServersInTheView.Count; ++k) { if (ClientProgram.ThreadsInAction[j].GetUri().Equals(ServersInTheView[k])) { ClientProgram.ThreadsInAction[j].GetThreadState().Kill_hread(); ClientProgram.ThreadsInAction[j].GetThread().Join(); ClientProgram.ThreadsInAction.RemoveAt(j); j = -1; break; } } } ClientProgram.Take_SignalEvent.Set(); } } } } } } } } break; case "refuse": if (CommandInUse == "refuse" && mt.GetPrevCommand() == "read") { } else { if (CommandInUse == "take" && mt.GetPrevCommand() == "refuse") { Thread.Sleep(250); } else { if (CommandInUse == "remove" && mt.GetPrevCommand() == "refuse") { } } } break; } } } }
public void TakeCommand(Command cmd) { //Get Commands from ROOT Thread.Sleep(DelayMessagesTime); //Delay Insertion of messages commLayer.InsertCommand(cmd); Console.WriteLine("(ServerService) Comando no Queue: " + cmd.GetCommand() + " " + cmd.GetPayload().ToString()); }