public void RX_Command(Command cmd) // Get Commands from clients { Thread.Sleep(DelayMessagesTime); //Delay Insertion of messages commLayer.InsertCommand(cmd); if (Root == true) //Only the root server send updates { ServerProgram.UpdateAll(cmd); } }
//============================================================== //============================================================== 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 bool IsRoot() { return(ServerProgram.AmIRoot()); }
public static void CheckCommandsInQueue_thread_Replica() { while (true) { Thread.Sleep(50); //Min time to check commands if (CommLayer_forReplica.GetQueueSize() > 0) //if there is commands { CommandReplicas cmd = CommLayer_forReplica.RemoveFromCommandQueue(); //MyTuple payload = (MyTuple)cmd.GetPayload(); //Object tmp; switch (cmd.GetCommand()) { case "REQUEST_VIEW_AND_IMAGE": //RECEBI UM PEDIDO Console.WriteLine("-----REQUEST_VIEW_AND_IMAGE-----"); //Console.WriteLine("CURRENT VIEW1 ==> " + ServerProgram.GetCurrentViewID().ToString()); //Console.WriteLine("CURRENT IMAGE ==> " + ts.ToString()); GiveBackResultToReplica(cmd.GetURI(), new CommandReplicas("RECEIVE_VIEW_AND_IMAGE", ServerProgram.GetCurrentViewID(), ts, ServerProgram.GetMyAddress(), ServerProgram.GetMyId())); //ENVIO O ACK Console.WriteLine("olaaa"); ServerProgram.SetStateMachine(ServerProgram.STATE_MACHINE_NETWORK_UPDATE_VIEW); break; case "RECEIVE_VIEW_AND_IMAGE": //RECEBI UM AKC Console.WriteLine("-----RECEIVE_VIEW_AND_IMAGE----"); ServerProgram.SetCurrentViewID(cmd.GetProposedView()); //View update ts = cmd.GetTSS(); //tuple space update Console.WriteLine("VIEW RECEBIDA1 ==> " + cmd.GetProposedView().ToString()); Console.WriteLine("IMAGEM RECEBIDA1 ==> " + ts.ToString()); ServerProgram.wait = false; ServerProgram.SetStateMachine(ServerProgram.STATE_MACHINE_NETWORK_RESTART); break; case "REQUEST_VIEW": Console.WriteLine("-----REQUEST_VIEW-----"); //RECEBI UM PEDIDO //Console.WriteLine("CURRENT VIEW2 ==> " + ServerProgram.GetCurrentViewID().ToString()); GiveBackResultToReplica(cmd.GetURI(), new CommandReplicas("RECEIVE_VIEW", ServerProgram.GetCurrentViewID(), null, ServerProgram.GetMyAddress(), ServerProgram.GetMyId())); //ENVIO O ACK ServerProgram.SetStateMachine(ServerProgram.STATE_MACHINE_NETWORK_WAIT); break; case "RECEIVE_VIEW": //RECEBI UM AKC Console.WriteLine("-----RECEIVE_VIEW-----"); if (cmd.GetURI().AbsolutePath == ServerProgram.RootServer.UID.AbsolutePath) { ServerProgram.root_view = cmd.GetProposedView(); //Console.WriteLine("Recebi a view do root ---> " + ServerProgram.root_view.ToString()); } ServerProgram.Proposed_Views.Add(cmd.GetProposedView()); //Console.WriteLine("VIEW RECEBIDA2 ==> " + cmd.GetProposedView().ToString()); ServerProgram.wait = false; ServerProgram.SetStateMachine(ServerProgram.STATE_MACHINE_NETWORK_CHECK_VIEW); break; case "UPDATE_VIEW": //RECEBI UM PEDIDO DO ROOT PARA FAZER UPDATE DA VIEW Console.WriteLine("-----UPDATE_VIEW-----"); ServerProgram.SetCurrentViewID(cmd.GetProposedView()); //View update //Console.WriteLine("NOVA VIEW ==> " + ServerProgram.GetCurrentViewID().ToString()); GiveBackResultToReplica(cmd.GetURI(), new CommandReplicas("ACK_UPDATE_VIEW", ServerProgram.GetCurrentViewID(), null, ServerProgram.GetMyAddress(), ServerProgram.GetMyId())); //ENVIO O ACK ServerProgram.SetStateMachine(ServerProgram.STATE_MACHINE_NETWORK_WAIT); break; case "ACK_UPDATE_VIEW": Console.WriteLine("-----ACK_UPDATE_VIEW-----"); //RECEBI ACK DE CONFIRMAÇÃO DA ALTERAÇÃO DA VIEW ServerProgram.wait = false; ServerProgram.SetStateMachine(ServerProgram.STATE_MACHINE_NETWORK_RESTART); break; } } } }