Ejemplo n.º 1
0
 public void ServerSend(int id, MessageFromServer msg)
 {
     lock (obj)
     {
         clientMessages[id].Enqueue(msg);
         if (clientMessages[id].Count == 1)
         {
             emptyClient[id].Pulse();
         }
     }
 }
Ejemplo n.º 2
0
 public MessageFromServer ClientReceive(int id)
 {
     lock (obj)
     {
         if (clientMessages[id].Count == 0)
         {
             emptyClient[id].Wait(obj);
         }
         MessageFromServer message = clientMessages[id].Dequeue();
         return(message);
     }
 }
Ejemplo n.º 3
0
        public void DoWork()
        {
            Thread.Sleep(500);
            int counter     = 0;
            int roundNumber = 1;

            int clientsLeft = 0;

            while (list.Count > 0)
            {
                Console.WriteLine("Runda {0}", roundNumber++);

                while (true)
                {
                    clientsLeft = 0;
                    counter     = 0;

                    Thread.Sleep(250);
                    foreach (var j in list)
                    {
                        if (j.status == JankielStatus.winner)
                        {
                            foreach (var jj in j.neighbours)
                            {
                                jj.status = JankielStatus.loser;
                            }
                        }
                    }

                    foreach (var j in list)
                    {
                        if (j.status == JankielStatus.unknown)
                        {
                            clientsLeft++;
                        }
                    }

                    while (counter++ < clientsLeft)
                    {
                        MessageFromClient msg = mail.ServerReceive();

                        foreach (var jj in msg.sender.neighbours)
                        {
                            if (jj.status == JankielStatus.unknown)
                            {
                                MessageFromServer message = new MessageFromServer(ServerMessageType.ID, msg.sender.id);
                                mail.ServerSend(jj.id, message);
                            }
                        }
                    }

                    Thread.Sleep(250);

                    if (clientsLeft > 0)
                    {
                        foreach (var j in list)
                        {
                            MessageFromServer message = new MessageFromServer(ServerMessageType.MOVE_ON);
                            mail.ServerSend(j.id, message);
                        }
                    }
                    else
                    {
                        foreach (var j in list)
                        {
                            if (j.status != JankielStatus.winner)
                            {
                                continue;
                            }
                            MessageFromServer message = new MessageFromServer(ServerMessageType.RESOLVED);
                            mail.ServerSend(j.id, message);
                        }
                        break;
                    }
                    Thread.Sleep(250);
                } // koniec rundy

                list.ForEach(item =>
                {
                    if (item.status == JankielStatus.loser)
                    {
                        item.status = JankielStatus.unknown;
                    }
                });

                foreach (var j in list)
                {
                    if (j.status == JankielStatus.winner)
                    {
                        Console.WriteLine("Jankiel {0} gra", j.id);
                    }
                }

                list.RemoveAll(p => { return(p.status == JankielStatus.winner); });
            }
        }