private void PingAllServers() { if (Server.AllServers.Count <= 1) { return; } for (int i = 0; i < Server.AllServers.Count; i++) { if (Server.AllServers[i].ID == Server.My_Identification.ID) //Avoid ping to himself { continue; } try { ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[i].UID.AbsoluteUri + "MyRemoteObjectName"); obj.Ping(); Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri); } catch (Exception e) { Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); //Console.WriteLine(e); } } }
/// <summary> /// Send Commands to all the others Servers in an asyncronous way /// </summary> /// <param name="cmd"></param> public static void UpdateAll(Command cmd) { for (int i = 0; i < Server.AllServers.Count; i++) { if (Server.AllServers[i].ID == Server.My_Identification.ID) //Avoid sending to himself { continue; } try { ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[i].UID.AbsoluteUri + "MyRemoteObjectName"); RemoteAsyncDelegate RemoteDel = new RemoteAsyncDelegate(obj.TakeCommand); AsyncCallback RemoteCallback = new AsyncCallback(OurRemoteAsyncCallBack); IAsyncResult RemAr = RemoteDel.BeginInvoke(cmd, RemoteCallback, null); Console.WriteLine("-------UpdateAll-------: {0}", Server.AllServers[i].UID.AbsoluteUri); } catch (Exception e) { //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); Console.WriteLine("-------UpdateAll------- DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); //Console.WriteLine(e); } } }
//=================================================================== // Main program //=================================================================== private static void NetworkStatusStateMachine() { //Console.WriteLine("STATE: " + STATE_MACHINE_NETWORK); int id_tmp; int idx = 0; bool flag = false; switch (STATE_MACHINE_NETWORK) { case STATE_MACHINE_NETWORK_START: //Start searching for the Root Console.WriteLine("================ STATE_MACHINE_NETWORK_START ============="); Thread.Sleep(60000); /*serversAlive.Clear(); * * for (int i = 0; i < Server.AllServers.Count; i++) * { * Thread.Sleep(50); * if (Server.AllServers[i].ID == MyID) //Avoid ping himself * { * continue; * } * try * { * ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[i].UID.AbsoluteUri + "MyRemoteObjectName"); * Console.WriteLine("CHECK: {0}", Server.AllServers[i].UID.AbsoluteUri); * * if (obj.IsRoot() == true) * { * RootIs = Server.AllServers[i].ID; //ID of the current root node * * * * obj.RX_ReplicaCommand(new CommandReplicas("REGISTER", null, null, MyAddress, MyID)); * STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_WAIT_FOR_ANSWER_INIT; * Pending_SignalEvent.Set(); * //object[] imageFromRoot = obj.getImage(); * //ServerService.SetTupleSpace(new TupleSpace((List<MyTuple>)imageFromRoot[0])); //Novo tuplespace criado * //ServerService.SetCommunicationLayer((Queue)imageFromRoot[1], (List<Command>)imageFromRoot[2]); * * * //ServerService.SetTupleSpace( obj.getImage() ); //get the image of the root * //Console.WriteLine("Imagem: "); * //Console.WriteLine(ServerService.GetTupleSpaceRepresentation()); * * * * STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_CHECK_ROOT; * * flag = true; * * * * * //Console.WriteLine("ROOT is {0}", Server.AllServers[i].UID.AbsoluteUri); * * * break; * } * else * { * serversAlive.Add(i); //Server ID * //Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri); * * } * } * catch (Exception e) * { * //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); * Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); * //Console.WriteLine(e); * } * } * //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: TEST_FLAG"); * if (flag == false) * { * * //Console.WriteLine("FLAG==FALSE: {0}", serversAlive.Count); * if (serversAlive.Count == 0) //check if anyone is ROOT * { * //ROOT * RootIs = MyID; //ServerService.setRoot(true); * STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT; * } * else * { * //Console.WriteLine("MY_IDENTIFICATION: {0}, {1}", Server.My_Identification.ID, (int)serversAlive[0]); * if ((Server.My_Identification.ID - 1) < (int)serversAlive[0]) * { * //ROOT * //ServerService.setRoot(true); * RootIs = MyID; * STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT; * } * * } * }*/ break; case STATE_MACHINE_NETWORK_UPDATE_ROOT: serversAlive.Clear(); for (int i = 0; i < CurrentViewID.GetSizeOfView(); i++) { Thread.Sleep(50); id_tmp = CurrentViewID.GetElementOfView(i); if (id_tmp == MyID) //Avoid ping himself { continue; } try { idx = Server.AllServers.IndexOf(new EachServer(null, id_tmp)); ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[idx].UID.AbsoluteUri + "MyRemoteObjectName"); //Console.WriteLine("CHECK: {0}", Server.AllServers[idx].UID.AbsoluteUri); if (obj.IsRoot() == true) { RootIs = id_tmp; //Server.AllServers[i].ID; //ID of the current root node STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_CHECK_ROOT; flag = true; break; } else { serversAlive.Add(id_tmp); //Server ID //Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri); } } catch (Exception e) { //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); // Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); //Console.WriteLine(e); } } //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: TEST_FLAG"); if (flag == false) { //Console.WriteLine("FLAG==FALSE: {0}", serversAlive.Count); if (CurrentViewID.GetSizeOfView() == 1) //check if anyone is ROOT { //ROOT RootIs = MyID; //ServerService.setRoot(true); //STATE_MACHINE_NETWORK_prev = STATE_MACHINE_NETWORK; //prev state STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT; } else { //Console.WriteLine("MY_IDENTIFICATION: {0}, {1}", Server.My_Identification.ID, (int)serversAlive[0]); serversAlive.Sort(); if (MyID < (int)serversAlive[0]) { //ROOT //ServerService.setRoot(true); RootIs = MyID; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT; } } } break; case STATE_MACHINE_NETWORK_WAIT_FOR_ANSWER_INIT: CommandAvailable_SignalEvent.WaitOne(); CommandAvailable_SignalEvent.Reset(); Console.WriteLine("============= STATE_MACHINE_NETWORK_WAIT_FOR_ANSWER_INIT =========="); break; case STATE_MACHINE_NETWORK_CHECK_ROOT: Console.WriteLine("============= STATE_MACHINE_NETWORK_CHECK_ROOT =========="); Console.WriteLine("========================View: " + CurrentViewID); STATE_MACHINE_NETWORK_prev = STATE_MACHINE_NETWORK; //prev state Thread.Sleep(TTL); //id_tmp = CurrentViewID.GetElementOfView(i); //int idx; try { idx = Server.AllServers.IndexOf(new EachServer(null, RootIs)); IServerServices obj = (IServerServices)Activator.GetObject(typeof(IServerServices), Server.AllServers[idx].UID.AbsoluteUri + "MyRemoteObjectName"); Console.WriteLine("CHECK: {0}", Server.AllServers[idx].UID.AbsoluteUri); obj.RX_ReplicaCommand(new CommandReplicas("CHECK_ROOT", null, null, MyAddress, MyID)); } catch (Exception e) { //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); //Console.WriteLine("DEAD: {0}", Server.AllServers[idx].UID.AbsoluteUri); //Timeout_tmp = new Timeout(MyID, TTL + new Random().Next(2000)); CurrentViewID.RemoveNode(RootIs); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE; //Console.WriteLine(e); } break; /*case STATE_MACHINE_NETWORK_CHECK_RETRY: * * Console.WriteLine("CHECK_RETRY + Num: " + NumOfRetry); * * if (NumOfRetry++ < NumOfRetryConst) * { * STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_CHECK_ROOT; * } * else * { * //Assume root deaad * NumOfRetry = 0; * STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE; * * } * Console.WriteLine("SAIR =============== CHECK_RETRY + Num: " + NumOfRetry); * break; */ case STATE_MACHINE_NETWORK_IM_ROOT: Thread.Sleep(TTL); Console.WriteLine("I´M ROOT"); Console.WriteLine("========================View: " + CurrentViewID); STATE_MACHINE_NETWORK_prev = STATE_MACHINE_NETWORK_IM_ROOT; //prev state for (int i = 0; i < CurrentViewID.GetSizeOfView(); i++) { id_tmp = CurrentViewID.GetElementOfView(i); if (id_tmp == MyID) //Avoid ping himself { continue; } try { idx = Server.AllServers.IndexOf(new EachServer(null, id_tmp)); IServerServices obj = (IServerServices)Activator.GetObject(typeof(IServerServices), Server.AllServers[idx].UID.AbsoluteUri + "MyRemoteObjectName"); Console.WriteLine("CHECK: {0}", Server.AllServers[id_tmp - 1].UID.AbsoluteUri); obj.RX_ReplicaCommand(new CommandReplicas("CHECK", null, null, MyAddress, MyID)); } catch (Exception e) { //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); //++Failed; //One is failed. Must restart ViewChange CurrentViewID.RemoveNode(id_tmp); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE; Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); //Console.WriteLine(e); } } break; case STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE: Console.WriteLine("=============== STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE ============"); Console.WriteLine("=============== STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE ============: " + STATE_MACHINE_NETWORK_prev); for (int i = 0; i < CurrentViewID.GetSizeOfView(); i++) { id_tmp = CurrentViewID.GetElementOfView(i); if (id_tmp == MyID) //Avoid ping himself { continue; } try { idx = Server.AllServers.IndexOf(new EachServer(null, id_tmp)); IServerServices obj = (IServerServices)Activator.GetObject(typeof(IServerServices), Server.AllServers[idx].UID.AbsoluteUri + "MyRemoteObjectName"); Console.WriteLine("CHECK: {0}", Server.AllServers[id_tmp - 1].UID.AbsoluteUri); obj.RX_ReplicaCommand(new CommandReplicas("VIEW_CHANGE", CurrentViewID, null, MyAddress, MyID)); } catch (Exception e) { //flag = true; //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); //++Failed; //One is failed. Must restart ViewChange //CurrentViewID.RemoveNode(id_tmp); //STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_INFORM_VIEW_CHANGE; Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); break; //Console.WriteLine(e); } } /*if (flag == true) * { * CurrentViewID.RemoveNode(idx); * RootIs = MyID; * flag = false; * }*/ if (STATE_MACHINE_NETWORK_prev == STATE_MACHINE_NETWORK_CHECK_ROOT) { STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_UPDATE_ROOT; } else { STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_prev; } //CurrentViewID.RemoveNode(idx); //RootIs = MyID; break; default: break; } }
static void Main(string[] args) { int id; Uri uri; int min_delay; int max_delay; if (args.Length != 4) { Console.WriteLine("Insuficient arguments: SERVER_ID URL MIN_DELAY MAX_DELAY"); args = Console.ReadLine().Split(' '); } id = Int32.Parse(args[0].Substring(1)); //catch number from the 1 position try { uri = new Uri(args[1]); } catch (UriFormatException e) { Console.WriteLine("Invalid URL: {0}", args[1]); Console.ReadLine(); return; } min_delay = Int32.Parse(args[2]); max_delay = Int32.Parse(args[3]); Random rnd = new Random(); delay_messages = rnd.Next(min_delay, max_delay + 1); //open file with database of all Servers in the system AllServers = new List <EachServer>(); My_Identification = new EachServer(uri, id); string[] lines = File.ReadAllLines(path); foreach (string line in lines) { string[] words = line.Split(' '); int tmp_id = Int32.Parse(words[0]); Uri uri_tmp; try { uri_tmp = new Uri(words[1]); AllServers.Add(new EachServer(uri_tmp, tmp_id)); if (tmp_id == 1) { RootServer = new EachServer(uri_tmp, tmp_id); Console.WriteLine("ENTRANCE POINT/ROOT SERVER => " + RootServer.uid.ToString()); } } catch (UriFormatException e) { Console.WriteLine("Invalid URL: {0}", words[1]); //Console.ReadLine(); return; } } channel = new TcpChannel(uri.Port); ChannelServices.RegisterChannel(channel, false); new Thread(() => Server_thread()).Start(); new Thread(() => Client_thread(uri, id)).Start(); new Thread(() => ServerService.CheckCommandsInQueue_thread()).Start(); new Thread(() => ServerService.CheckCommandsInQueue_thread_Replica()).Start(); System.Console.WriteLine("I'm server: " + args[1]); //teste tuple space object[] tupleOBJ = new object[1]; tupleOBJ[0] = new StringEmulator("a"); ServerService.ts.Add(new MyTuple(tupleOBJ)); System.Console.ReadLine(); }
private void NetworkStatusStateMachine() { bool flag = false; switch (STATE_MACHINE_NETWORK) { case STATE_MACHINE_NETWORK_START: //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: BEGIN"); serversAlive.Clear(); for (int i = 0; i < Server.AllServers.Count; i++) { if (Server.AllServers[i].ID == Server.My_Identification.ID) //Avoid ping himself { continue; } try { ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[i].UID.AbsoluteUri + "MyRemoteObjectName"); Console.WriteLine("CHECK: {0}", Server.AllServers[i].UID.AbsoluteUri); if (obj.isRoot() == true) { Root_id = i; //ID of the current root node STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_KEEP_ALIVE; flag = true; Console.WriteLine("ROOT is {0}", Server.AllServers[i].UID.AbsoluteUri); object[] imageFromRoot = obj.getImage(); ServerService.SetTupleSpace(new TupleSpace((List <MyTuple>)imageFromRoot[0])); //Novo tuplespace criado ServerService.SetCommunicationLayer((Queue)imageFromRoot[1], (List <Command>)imageFromRoot[2]); //ServerService.SetTupleSpace( obj.getImage() ); //get the image of the root Console.WriteLine("Imagem: "); Console.WriteLine(ServerService.GetTupleSpaceRepresentation()); break; } else { serversAlive.Add(i); //Server ID Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri); } } catch (Exception e) { //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: EXCEPTION"); Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); //Console.WriteLine(e); } } //Console.WriteLine("IN STATE_MACHINE_NETWORK_START: TEST_FLAG"); if (flag == false) { //Console.WriteLine("FLAG==FALSE: {0}", serversAlive.Count); if (serversAlive.Count == 0) //check if anyone is ROOT { //ROOT ServerService.setRoot(true); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT; } else { //Console.WriteLine("MY_IDENTIFICATION: {0}, {1}", Server.My_Identification.ID, (int)serversAlive[0]); if ((Server.My_Identification.ID - 1) < (int)serversAlive[0]) { //ROOT ServerService.setRoot(true); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_IM_ROOT; } } } break; case STATE_MACHINE_NETWORK_KEEP_ALIVE: //Console.WriteLine("IN STATE_MACHINE_NETWORK_KEEP_ALIVE"); //ping root try { ServerService obj = (ServerService)Activator.GetObject(typeof(ServerService), Server.AllServers[Root_id].UID.AbsoluteUri + "MyRemoteObjectName"); obj.Ping(); //Console.WriteLine("ALIVE: {0}", Server.AllServers[i].UID.AbsoluteUri); Thread.Sleep(rand.Next(1000)); //keepalive } catch (Exception e) { STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_START; //Console.WriteLine("IN STATE_MACHINE_NETWORK_KEEP_ALIVE: EXCEPTION"); //Console.WriteLine("GOING TO STATE_MACHINE_NETWORK_START"); //Console.WriteLine("DEAD: {0}", Server.AllServers[i].UID.AbsoluteUri); //Console.WriteLine(e); } break; case STATE_MACHINE_NETWORK_IM_ROOT: Console.WriteLine("I'M THE ROOT SERVER"); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_END; break; case STATE_MACHINE_NETWORK_END: 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; } } } }