public override bool Equals(object obj) { EachServer a = obj as EachServer; if (a == null) { return(false); } return(id == a.ID); }
//========================================================================================= // MAIN //========================================================================================= public ServerProgram(Uri uri, int id, List <EachServer> allServers, EachServer rootServer) { MyAddress = uri; MyID = id; AllServers = allServers; RootServer = rootServer; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_START; NetworkStatusLoop(); }
//=================================================================== // MAIN VIEW UPDATE PROGRAM //=================================================================== private static void View_Update_Program() { switch (STATE_MACHINE_NETWORK) { case STATE_MACHINE_NETWORK_START: if (RootServer != null && MyID != RootServer.id) { ss = (IServerServices)Activator.GetObject(typeof(IServerServices), RootServer.uid.ToString() + "MyRemoteObjectName"); if (ss != null) { serversAlive.Add(RootServer); first_request = true; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_REQUEST_VIEW; } } else { Ping_All_Servers(); CurrentViewID = new View ( nodeid: MyID, sequence: 0, servers_in_View: serversAlive ); Im_Root = true; RootServer = new EachServer(MyAddress, MyID); Console.WriteLine("IM ROOT"); } STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_RESTART; break; case STATE_MACHINE_NETWORK_REQUEST_VIEW: if (first_request == true && Im_Root == false) { ss = (IServerServices)Activator.GetObject(typeof(IServerServices), RootServer.uid.ToString() + "MyRemoteObjectName"); ss.RX_ReplicaCommand(new CommandReplicas("REQUEST_VIEW_AND_IMAGE", null, null, MyAddress, MyID)); first_request = false; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_WAIT; } else { try { Console.WriteLine(MyID + "ooooooooooooooooooooooooooooo" + CurrentViewID.Servers_in_View.Count); foreach (EachServer server_alive in CurrentViewID.Servers_in_View) { //if (server_alive.id != MyID) //{ ss = (IServerServices)Activator.GetObject(typeof(IServerServices), server_alive.uid.ToString() + "MyRemoteObjectName"); ss.RX_ReplicaCommand(new CommandReplicas("REQUEST_VIEW", null, null, MyAddress, MyID)); //} } STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_WAIT; } catch { if (Im_Root == true) { Ping_All_Servers(); foreach (EachServer server_alive in serversAlive) { if (server_alive.id != MyID) { view_sequence++; View tmp = new View(MyID, view_sequence, serversAlive); ss = (IServerServices)Activator.GetObject(typeof(IServerServices), server_alive.uid.ToString() + "MyRemoteObjectName"); ss.RX_ReplicaCommand(new CommandReplicas("UPDATE_VIEW", tmp, null, MyAddress, MyID)); } } } else { } } } //Console.WriteLine("adeus"); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_RESTART2; break; case STATE_MACHINE_NETWORK_CHECK_VIEW: foreach (View proposed_view in Proposed_Views) { if (!GetCurrentViewID().Equals(proposed_view)) { getFromRoot = true; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_CHANGE_VIEW; break; } } STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_REQUEST_VIEW; break; case STATE_MACHINE_NETWORK_CHANGE_VIEW: if (getFromRoot == true && root_view != null) { SetCurrentViewID(root_view); root_view = null; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_RESTART; } else { STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_UPDATE_ROOT; } break; case STATE_MACHINE_NETWORK_UPDATE_VIEW: Console.WriteLine("Entrei no update view"); Ping_All_Servers(); view_sequence++; View new_view = new View(MyID, view_sequence, serversAlive); root_view = new_view; SetCurrentViewID(new_view); Console.WriteLine("UPDATE VIEW______=> " + new_view.ToString()); foreach (EachServer server_alive in serversAlive) { if (server_alive.id != MyID) { ss = (IServerServices)Activator.GetObject(typeof(IServerServices), server_alive.uid.ToString() + "MyRemoteObjectName"); ss.RX_ReplicaCommand(new CommandReplicas("UPDATE_VIEW", new_view, null, MyAddress, MyID)); } } STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_WAIT; break; case STATE_MACHINE_NETWORK_UPDATE_ROOT: if (serversAlive.Count == 0 || ((Server.My_Identification.ID - 1) < serversAlive[0].id)) //check if anyone is ROOT { //IM ROOT RootServer = new EachServer(MyAddress, MyID); Ping_All_Servers(); View new_view2 = new View(MyID, view_sequence++, serversAlive); root_view = new_view2; SetCurrentViewID(new_view2); Console.WriteLine("IM THE NEW ROOT-----VIEW => " + new_view2.ToString()); foreach (EachServer server_alive in serversAlive) { if (server_alive.id != MyID) { ss = (IServerServices)Activator.GetObject(typeof(IServerServices), server_alive.uid.ToString() + "MyRemoteObjectName"); ss.RX_ReplicaCommand(new CommandReplicas("UPDATE_VIEW", new_view2, null, MyAddress, MyID)); } } } //else //{ // //Console.WriteLine("MY_IDENTIFICATION: {0}, {1}", Server.My_Identification.ID, (int)serversAlive[0]); // if () // { // //IM ROOT // RootServer = new EachServer(MyAddress, MyID); // View new_view = new View(MyID, view_sequence++); // root_view = new_view; // SetCurrentViewID(new_view); // Console.WriteLine("IM THE NEW ROOT-----VIEW => " + new_view.ToString()); // foreach (EachServer server_alive in serversAlive) // { // if (server_alive.id != MyID) // ss.RX_ReplicaCommand(new CommandReplicas("UPDATE_VIEW", new_view, null, MyAddress, MyID)); // } // } //} break; case STATE_MACHINE_NETWORK_WAIT: while (wait == true) { Thread.Sleep(50); } wait = true; STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_RESTART; break; case STATE_MACHINE_NETWORK_RESTART: Thread.Sleep(3000); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_REQUEST_VIEW; break; case STATE_MACHINE_NETWORK_RESTART2: Thread.Sleep(3000); break; case STATE_MACHINE_NETWORK_RESTART3: Thread.Sleep(5000); STATE_MACHINE_NETWORK = STATE_MACHINE_NETWORK_UPDATE_ROOT; 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(); }