public SenderPool(ThreadWithState thrS, Thread thr, Uri dest, Command com) { ts = thrS; t = thr; c = com; destination = dest; }
//Send commands to thread public static void SendToView(Command c) { List <string> currentView = GetView(); for (int i = 0; i < currentView.Count; i++) { ThreadWithState tws = new ThreadWithState(c, i); Thread td = new Thread(new ThreadStart(tws.TX_Command_thread)); ThreadsInAction.Add(new SenderPool(tws, td, new Uri(currentView[i]), c)); td.Start(); } }
/// <summary> /// Execute the command. Sends to Server /// </summary> /// <param name="command"></param> /// <param name="t"></param> public static void Execute(string command, MyTuple t) { Console.WriteLine("EXECUTAR"); Command c = new Command(command, t, MyAddress, ++SequenceNumber); Stopwatch sw = new Stopwatch(); while (true) { switch (STATE_EXECUTE) { case STATE_COMMAND: switch (command) { case "read": //Console.WriteLine("========================================"); for (int i = 0; i < AllServers.Count; i++) { ThreadWithState tws = new ThreadWithState(c, i); Thread td = new Thread(new ThreadStart(tws.TX_Command_thread)); ThreadsInAction.Add(new SenderPool(tws, td, new Uri(AllServers[i]), c)); td.Start(); } STATE_EXECUTE = STATE_WAIT_FOR_REPLY_READ; break; case "add": Console.WriteLine("========================================"); for (int i = 0; i < AllServers.Count; i++) { ThreadWithState tws = new ThreadWithState(c, i); Thread td = new Thread(new ThreadStart(tws.TX_Command_thread)); ThreadsInAction.Add(new SenderPool(tws, td, new Uri(AllServers[i]), c)); td.Start(); } STATE_EXECUTE = STATE_WAIT_FOR_REPLY_ADD; break; case "take": //FASE 1 Console.WriteLine("========================================"); for (int i = 0; i < AllServers.Count; i++) { ThreadWithState tws = new ThreadWithState(c, i); Thread td = new Thread(new ThreadStart(tws.TX_Command_thread)); ThreadsInAction.Add(new SenderPool(tws, td, new Uri(AllServers[i]), c)); td.Start(); } STATE_EXECUTE = STATE_WAIT_FOR_REPLY_TAKE; break; case "remove": //FASE 2 Console.WriteLine("========================================"); for (int i = 0; i < AllServers.Count; i++) { ThreadWithState tws = new ThreadWithState(c, i); Thread td = new Thread(new ThreadStart(tws.TX_Command_thread)); ThreadsInAction.Add(new SenderPool(tws, td, new Uri(AllServers[i]), c)); td.Start(); } STATE_EXECUTE = STATE_WAIT_FOR_REPLY_TAKE; break; } break; case STATE_WAIT_FOR_REPLY_READ: Pending_SignalEvent.WaitOne(); Pending_SignalEvent.Reset(); STATE_EXECUTE = STATE_COMMAND; return; case STATE_WAIT_FOR_REPLY_ADD: Pending_SignalEvent.WaitOne(); Pending_SignalEvent.Reset(); STATE_EXECUTE = STATE_COMMAND; return; case STATE_WAIT_FOR_REPLY_TAKE: Console.WriteLine("============INICIO======================STATE_WAIT_FOR_REPLY_TAKE"); Pending_SignalEvent.WaitOne(); Pending_SignalEvent.Reset(); Console.WriteLine("============ENTREI======================STATE_WAIT_FOR_REPLY_TAKE: " + TerminateTakenActivity.ToString()); STATE_EXECUTE = STATE_COMMAND; if (TerminateTakenActivity == true && command == "take") { //Console.WriteLine("============ACABEI======================STATE_WAIT_FOR_REPLY_TAKE"); TerminateTakenActivity = false; command = "remove"; c = new Command(command, t, MyAddress, ++SequenceNumber); //Take_SignalEvent.Set(); //return; } else { if (TerminateTakenActivity == true && command == "remove") { TerminateTakenActivity = false; return; } } break; } /*try * { * Command c = new Command(command, t, MyAddress); * ss.RX_Command(c); * while (BlockUntilAnswer == false) ; * BlockUntilAnswer = false; * break; * } * catch (System.Net.Sockets.SocketException e) * { * Console.WriteLine("AQUI"); * Stopwatch sw = new Stopwatch(); * sw.Start(); * while (SearchForRootServer() == false) //Search for ROOT * { * if (sw.ElapsedMilliseconds > Timeout) * { * Console.WriteLine("No Server available."); * Console.WriteLine("Retrying in {0} [s]", Timeout); * Thread.Sleep(Timeout); * //break; * } * } * }*/ } }