public void InitializeMeetings(Dictionary <string, Proposal> proposals, Dictionary <string, LocationMeetings> meetings) { foreach (KeyValuePair <String, Proposal> entry in proposals) { AbstractMeeting p = (AbstractMeeting)entry.Value; Console.WriteLine("Vou ter esta proposal sobre " + p.Topic); lock (this.Meetings) { if (p.N_invitees == 0 || (p.N_invitees != 0 && (p.Invitees.Contains(this.UserName) || this.UserName == p.Coordinator))) { this.Meetings[p.Topic] = p; Console.WriteLine("Tenho esta proposal sobre " + p.Topic); } } } foreach (KeyValuePair <string, LocationMeetings> entry in meetings) { foreach (Meeting m1 in entry.Value.Meetings) { AbstractMeeting m = (AbstractMeeting)m1; lock (this.Meetings) { this.Meetings[m.Topic] = m; } } } }
public void UpdateMeetingsFromClient() { Console.WriteLine("Entrei a meio da execucao"); foreach (KeyValuePair <String, AbstractMeeting> entry in this.AllMeetings) { /*AbstractMeeting test; * this.Meetings.TryGetValue(entry.Key, out test); * if (test == null) * this.AllMeetings[entry.Key] = entry.Value;*/ AbstractMeeting am1 = entry.Value; lock (this.Meetings) { AbstractMeeting test; this.Meetings.TryGetValue(entry.Key, out test); if (test == null && am1.N_invitees == 0) { this.Meetings[am1.Topic] = am1; Console.WriteLine("Esta meeting e aberta. Passei a conhece-la"); } else if (test == null && am1.N_invitees != 0 && am1.Invitees.Contains(this.UserName)) { this.Meetings[am1.Topic] = am1; Console.WriteLine("Esta meeting e fechada e sou convidado."); } } } }
public void UpdateMeetings(Dictionary <string, Proposal> proposals, Dictionary <string, LocationMeetings> meetings) { foreach (KeyValuePair <String, Proposal> entry in proposals) { AbstractMeeting test; this.Meetings.TryGetValue(entry.Key, out test); if (test == null) { this.AllMeetings[entry.Key] = entry.Value; } //Proposal que veio do servidor Proposal p1 = entry.Value; //Respetivo proposal no cliente AbstractMeeting p2; lock (this.Meetings) { this.Meetings.TryGetValue(p1.Topic, out p2); if (p2 != null && p1.Version > p2.Version) // || p2 == null shouldnt be necessary in the condition TODO why? { this.Meetings[p1.Topic] = p1; } } } foreach (KeyValuePair <string, LocationMeetings> entry in meetings) { foreach (Meeting m1 in entry.Value.Meetings) { AbstractMeeting test; this.Meetings.TryGetValue(m1.Topic, out test); if (test == null) { this.AllMeetings[entry.Key] = m1; } //Respetivo meeting no cliente AbstractMeeting m2; lock (this.Meetings) { this.Meetings.TryGetValue(m1.Topic, out m2); if ((m2 != null && m1.Version > m2.Version) || m2 == null) // || m2 == null shouldnt be necessary in the condition TODO why? { this.Meetings[m1.Topic] = m1; } } } } foreach (KeyValuePair <String, AbstractMeeting> entry in Meetings) { AbstractMeeting m = entry.Value; Console.WriteLine(m.PrintInfo()); } }
public void Gossip(Proposal p, int actualRound, int totalRounds, int numberOfMessages) { Dictionary <String, String> clientsToSend = new Dictionary <String, String>(); String actualServerUrl = this.Server_url; Console.WriteLine("Comecei o Gossip"); if (actualRound > totalRounds) { Console.WriteLine("Acabou o gossip"); return; } while (true) { try { ServerInterface s = this.Server; clientsToSend = s.GetListOfRandomClients((totalRounds - actualRound + 1) * numberOfMessages, this.UserName); break; } catch (SocketException e) { lock (this.Server) { lock (this.Server_url) { if (actualServerUrl == this.Server_url) { Console.WriteLine(e.Message); this.Server_url = GetNextAvailableServer(); this.Server = (ServerInterface)Activator.GetObject(typeof(ServerInterface), this.Server_url); } } } } } AbstractMeeting am = (AbstractMeeting)p; AbstractMeeting test = null; this.AllMeetings.TryGetValue(am.Topic, out test); if (test == null) { this.AllMeetings[am.Topic] = am; } lock (this.Meetings) { AbstractMeeting p2; this.Meetings.TryGetValue(am.Topic, out p2); if (p2 == null && am.N_invitees == 0) { Console.WriteLine("Este proposal é aberta, sou o/a " + this.UserName + " e passei a conhecer a meeting com o Topic " + am.Topic); this.Meetings.Add(am.Topic, am); } else if (p2 == null && am.N_invitees != 0 && (am.Invitees.Contains(this.UserName) || this.UserName == am.Coordinator)) { Console.WriteLine("Este proposal é fechada e sou convidado/a, sou o/a " + this.UserName + " e passei a conhecer a meeting com o Topic " + am.Topic); this.Meetings.Add(am.Topic, am); } } Console.WriteLine("Actual Round: " + actualRound); Console.WriteLine("Total rounds: " + totalRounds); Console.WriteLine("Vou mandar " + numberOfMessages + " mensagens"); Thread[] pool; List <String> clientsToSendURLList = clientsToSend.Values.ToList(); List <String> clientsToSendNameList = clientsToSend.Keys.ToList(); if (clientsToSend.Count > numberOfMessages) { pool = new Thread[numberOfMessages]; for (int i = 0; i < numberOfMessages; i++) { String name = clientsToSendNameList[i]; String url = clientsToSendURLList[i]; pool[i] = new Thread(() => DoSpreadMessage(p, actualRound, totalRounds, numberOfMessages, url)); pool[i].Start(); } } else { pool = new Thread[clientsToSend.Count]; for (int i = 0; i < clientsToSend.Count; i++) { String name = clientsToSendNameList[i]; String url = clientsToSendURLList[i]; pool[i] = new Thread(() => DoSpreadMessage(p, actualRound, totalRounds, numberOfMessages, url)); pool[i].Start(); } } }