private void BroadcastAdd(DIDATuple didaAdd) { string mypath = System.Reflection.Assembly.GetEntryAssembly().Location; string finalpath = mypath.Substring(0, mypath.Length - 10); string newpath = Path.GetFullPath(Path.Combine(finalpath, @"..\..\")); string pathToList = newpath + "ListaServers.txt"; string[] lines = System.IO.File.ReadAllLines(pathToList); foreach (string line in lines) { //port : name string[] args = line.Split(':'); int server_port = Int32.Parse(args[0]); string server_name = args[1]; if (didaAdd.getName() != server_name) { try { string url = "tcp://localhost:" + server_port + "/" + server_name; TcpClientChannel channelnovo = new TcpClientChannel(server_name, null); ChannelServices.RegisterChannel(channelnovo, false); IServerInterface servernovo = (IServerInterface)Activator.GetObject(typeof(IServerInterface), url); servernovo.Add(didaAdd); ChannelServices.UnregisterChannel(channelnovo); } catch { } } } }
private bool BroadcastRemove(DIDATuple didatuple, string[] lines) { int numberOfActiveServers = 1; bool ack = false; int numberOfAcks = 1; do { foreach (string line in lines) { //port : name : priority string[] args = line.Split(':'); int server_port = Int32.Parse(args[0]); string server_name = args[1]; if (didatuple.getName() != server_name) { try { string url = "tcp://localhost:" + server_port + "/" + server_name; TcpClientChannel channelnovo = new TcpClientChannel(server_name, null); ChannelServices.RegisterChannel(channelnovo, false); IServerInterface servernovo = (IServerInterface)Activator.GetObject(typeof(IServerInterface), url); ack = servernovo.XLBroadcastRemove(didatuple); ChannelServices.UnregisterChannel(channelnovo); if (ack) { numberOfAcks++; } else { break; } numberOfActiveServers++; } catch (Exception e) { } } } } while (numberOfAcks != numberOfActiveServers); return(true); }
public void SimulateDelay(IDIDATuple x) { DIDATuple didat = (DIDATuple)x; string name = didat.getName(); string mypath = System.Reflection.Assembly.GetEntryAssembly().Location; string finalpath = mypath.Substring(0, mypath.Length - 10); string newpath = Path.GetFullPath(Path.Combine(finalpath, @"..\..\")); string pathToDelay = newpath + name + "Delay.txt"; string[] lines = System.IO.File.ReadAllLines(pathToDelay); foreach (string line in lines) { //min : max string[] args = line.Split(':'); MinDelay = Int32.Parse(args[0]); MaxDelay = Int32.Parse(args[1]); } Random r = new Random(); int delay = r.Next(MinDelay, MaxDelay); this.Wait(delay); }
public IDIDATuple XLBroadcastRead(IDIDATuple didaTuple) { DIDATuple didaRead = (DIDATuple)didaTuple; return(Read(didaTuple, didaRead.getName(), didaRead.getPort(), true)); }
private DIDATuple BroadcastTake(DIDATuple didatuple) { IDictionary <DIDATuple, int> intersectDictionairy = new Dictionary <DIDATuple, int>(); List <IDIDATuple> didaResult = tupleSpace.Take((DIDATuple)didatuple); int numberOfActiveServers = 1; string mypath = System.Reflection.Assembly.GetEntryAssembly().Location; string finalpath = mypath.Substring(0, mypath.Length - 13); string newpath = Path.GetFullPath(Path.Combine(finalpath, @"..\..\")); string pathToList = newpath + "ListaServers.txt"; List <IDIDATuple> resultOfOtherS = null; string[] lines = System.IO.File.ReadAllLines(pathToList); bool repeat = false; do { repeat = false; foreach (string line in lines) { //port : name : priority string[] args = line.Split(':'); int server_port = Int32.Parse(args[0]); string server_name = args[1]; if (didatuple.getName() != server_name) { try { string url = "tcp://localhost:" + server_port + "/" + server_name; TcpClientChannel channelnovo = new TcpClientChannel(server_name, null); ChannelServices.RegisterChannel(channelnovo, false); IServerInterface servernovo = (IServerInterface)Activator.GetObject(typeof(IServerInterface), url); resultOfOtherS = servernovo.XLBroadcastTake(didatuple); ChannelServices.UnregisterChannel(channelnovo); if (resultOfOtherS != null) { didaResult.AddRange(resultOfOtherS); numberOfActiveServers++; } else { //TODO falta implementar se tiver null repeat = true; break; } } catch (Exception e) { } } } } while (repeat); //ADD to dict foreach (IDIDATuple didaElem in didaResult) { DIDATuple didaTupleElem = (DIDATuple)didaElem; if (intersectDictionairy.ContainsKey(didaTupleElem)) { intersectDictionairy[didaTupleElem]++; } else { intersectDictionairy.Add(didaTupleElem, 1); } } List <DIDATuple> matches = new List <DIDATuple>(); //getIntersect foreach (KeyValuePair <DIDATuple, int> elem in intersectDictionairy) { if (elem.Value == numberOfActiveServers) { matches.Add(elem.Key); } } Random r = new Random(); int a = r.Next(0, matches.Count - 1); if (BroadcastRemove(matches[a], lines)) { if (tupleSpace.Remove(matches[a])) { Console.Write("Client removed tuple : "); this.PrintTuple(matches[a]); } else { Console.Write("Error removing Tuple"); } return(matches[a]); } return(null); }