示例#1
0
        //
        // starts the sorter
        // sorts all the luggages and puts them in a buffer
        //
        public void StartSorting()
        {
            Luggage luggage = null;
            bool    wait;

            while (true)
            {
                //counts so if the all the frontdesk buffer is empty then wait to get a pulse
                byte count = 0;
                //runs through every frontdesk buffer
                for (int i = 0; i < FrontDeskBuffer.FrontDesks.Length; i++)
                {
                    wait = false;
                    //looks if it has a luggage
                    if (luggage == null)
                    {
                        //locks a buffer
                        lock (FrontDeskBuffer.FrontDesks[i])
                        {
                            //looks if the buffer is full
                            if (FrontDeskBuffer.FrontDesks[i].Reserved != 0)
                            {
                                //if not take a luggage from a buffer

                                //infrom the user
                                //by locking the writer obj
                                lock (Program.writerLock)
                                {
                                    Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                                    Console.WriteLine("{0,-5}{1,-11} takes a luggage from frontdeskbuffer {2}", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(Sorter)", i);
                                    Console.SetCursorPosition(0, 0);
                                }
                                //take the luggage
                                luggage = FrontDeskBuffer.FrontDesks[i].GetLuggage();
                                //pulse to the frontdesk that it is not full any more
                                lock (FrontDeskBuffer.FrontDesks[i].full)
                                {
                                    Monitor.Pulse(FrontDeskBuffer.FrontDesks[i].full);
                                }
                            }
                            else
                            {
                                //if the buffer is empty count one up
                                count++;
                            }
                        }
                    }
                    //looks if it have a luggage
                    if (luggage != null)
                    {
                        //lock the terminal buffer the luggage is going
                        lock (TerminalBuffers.Terminals[luggage.Tarminal])
                        {
                            //looks if the buffer is full
                            if (!TerminalBuffers.Terminals[luggage.Tarminal].IsFull)
                            {
                                //if not inform the user
                                //by locking the writer obj
                                lock (Program.writerLock)
                                {
                                    Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                                    Console.WriteLine("{0,-5}{1,-11} Adds the luggage to {2}", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(Sorter)", luggage.Tarminal);
                                    Console.SetCursorPosition(0, 0);
                                }
                                //adds the time the luggage got sorted
                                luggage.Sorted = DateTime.Now;
                                //insert the luggage to the terminal buffer
                                TerminalBuffers.Terminals[luggage.Tarminal].AddLuggage(luggage);
                                //informs the terminal that it is not empty any more
                                lock (TerminalBuffers.Terminals[luggage.Tarminal].empty)
                                {
                                    Monitor.Pulse(TerminalBuffers.Terminals[luggage.Tarminal].empty);
                                }
                                //removes the luggage
                                luggage = null;
                            }
                            else
                            {
                                //if the buffer is full set wait to true
                                wait = true;
                            }
                        }
                        //if it has to wait before it can insert the luggage
                        if (wait)
                        {
                            //wait to the terminal has pulse that the buffer is not full anymore
                            lock (TerminalBuffers.Terminals[luggage.Tarminal].full)
                            {
                                Monitor.Wait(TerminalBuffers.Terminals[luggage.Tarminal].full);
                            }
                        }
                    }
                }
                //if all three buffer is empty
                if (count == FrontDeskBuffer.FrontDesks.Length)
                {
                    //wait to get a pulse from one of the frontdesk
                    lock (FrontDeskBuffer.FrontDesks)
                    {
                        //inform the user
                        //by locking the writer obj
                        lock (Program.writerLock)
                        {
                            Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                            Console.WriteLine("{0,-5}{1,-11} I will wait \t\t\t\t\t\t", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(Sorter)");
                            Console.SetCursorPosition(0, 0);
                        }
                        Monitor.Wait(FrontDeskBuffer.FrontDesks);
                    }
                }
            }
        }
        //
        //starts the frontdesk it can be closed and open, and still run in the method
        //
        public void StartFrontDesk()
        {
            //used to wait if the buffer is full
            bool wait;
            //the luggage
            Luggage luggage = new Luggage();

            while (true)
            {
                //looks if the desk has to close
                if (!closeDesk)
                {
                    //if not "make" a luggage
                    //sets the wait to false as default
                    wait = false;

                    //takes a person and makes the kuggage to for them
                    if (luggage == null)
                    {
                        //locks the person buffer
                        lock (ReservationBuffer.reservationBuffer)
                        {
                            AirplanenTime airplanenTime;
                            Person        person    = ReservationBuffer.reservationBuffer.GetPerson();
                            int           Terminal  = 0;
                            bool          planeFund = false;
                            //look if the plan has a terminal yet
                            for (int i = 0; i < AirplanenController.GetAirplanenTimes.Count; i++)
                            {
                                airplanenTime = AirplanenController.GetAirplanenTimes[i];
                                if (airplanenTime.Destination == person.Destination && airplanenTime.Landing < DateTime.Now && DateTime.Now < airplanenTime.LeftOff.AddSeconds(-30))
                                {
                                    planeFund = true;
                                    Terminal  = airplanenTime.TermainalNumber;
                                }
                            }
                            if (planeFund)
                            {
                                luggage         = new Luggage(id++, Terminal);
                                luggage.CheckIn = DateTime.Now;
                                lock (ReservationBuffer.reservationBuffer.full)
                                {
                                    Monitor.Pulse(ReservationBuffer.reservationBuffer.full);
                                }
                            }
                            else
                            {
                                ReservationBuffer.reservationBuffer.AddPerson(person);
                            }

                            lock (Program.writerLock)
                            {
                                Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                                Console.WriteLine("{0,-5}{1,-5} assisting {2} that will go to {3} \t\t", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(FrontDesk)", person.Name, person.Destination);
                                Console.SetCursorPosition(0, 0);
                            }
                        }
                    }
                    //lock the buffer
                    lock (FrontDeskBuffer.FrontDesks[buffer])
                    {
                        //look if it is full
                        if (!FrontDeskBuffer.FrontDesks[buffer].IsFull && luggage != null)
                        {
                            //if there is room
                            //inform the user
                            //add the luggage
                            FrontDeskBuffer.FrontDesks[buffer].AddLuggage(luggage);
                            //set the luggage to null
                            luggage = null;
                            //pulse to sorter that something is in the buffer
                            lock (FrontDeskBuffer.FrontDesks)
                            {
                                Monitor.Pulse(FrontDeskBuffer.FrontDesks);
                            }
                        }
                        else if (luggage != null)
                        {
                            //if it is full
                            //inform the user

                            lock (Program.writerLock)
                            {
                                Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                                Console.WriteLine("{0,-5}{1,-5} Buffer is full", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(FrontDesk)");
                                Console.SetCursorPosition(0, 0);
                            }
                            //set wait to true
                            wait = true;
                        }
                        //else
                        //{
                        //    //if no terminals are open
                        //    //inform the user
                        //    lock (Program.writerLock)
                        //    {
                        //        Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                        //        Console.WriteLine("{0,-5}{1,-5} No Terminals is open \t\t\t\t\t", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(FrontDesk)");
                        //        Console.SetCursorPosition(0, 0);
                        //    }
                        //}
                    }
                    if (wait)
                    {
                        //if it has to wait lock the full obj in the buffer
                        lock (FrontDeskBuffer.FrontDesks[buffer].full)
                        {
                            //inform the user

                            lock (Program.writerLock)
                            {
                                Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                                Console.WriteLine("{0,-5}{1,-5} I will wait \t\t\t\t\t", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(FrontDesk)");
                                Console.SetCursorPosition(0, 0);
                            }
                            //wait to it gets a pulse
                            Monitor.Wait(FrontDeskBuffer.FrontDesks[buffer].full);
                        }
                    }
                }
                else
                {
                    //if it has to close lock the open obj
                    lock (open)
                    {
                        //infrom the user

                        lock (Program.writerLock)
                        {
                            Console.SetCursorPosition(0, Thread.CurrentThread.ManagedThreadId);
                            Console.WriteLine("{0,-5}{1,-5} Close frontdesk  \t\t\t\t", "[" + Thread.CurrentThread.ManagedThreadId + "]", "(FrontDesk)");
                            Console.SetCursorPosition(0, 0);
                        }
                        //waits to it gets a pulse
                        Monitor.Wait(open);
                    }
                }
                Thread.Sleep(250);
            }
        }