Example #1
0
        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();
                }
            }
        }