Exemplo n.º 1
0
        static void Main(string[] args)
        {
            #region Initialise Data
            using (DataRepository Repo = new DataRepository())
            {
                Repo.Departments.Add(new Department()
                {
                    Name = "Maths"
                });
                Repo.Departments.Add(new Department()
                {
                    Name = "Science"
                });
                Repo.Departments.Add(new Department()
                {
                    Name = "Computing/IT"
                });
                Repo.Departments.Add(new Department()
                {
                    Name = "History"
                });

                Repo.SaveChanges();

                List <string> ComputerNames = GenerateComputerNames();
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "D6", SpecialSeats = 5, StandardSeats = 10, SpecialSeatType = "Computer", Department = Repo.Departments.Single(d => d.Name == "Computing/IT"), ComputerNames = ComputerNames
                });
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "D12", SpecialSeats = 20, StandardSeats = 5, SpecialSeatType = "Workbench", Department = Repo.Departments.Single(d => d.Name == "Computing/IT"), ComputerNames = ComputerNames
                });
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "Library", SpecialSeats = 20, StandardSeats = 30, SpecialSeatType = "Computer", Department = Repo.Departments.Single(d => d.Name == "Science")
                });
                Repo.Rooms.Add(new Room()
                {
                    RoomName = "Sports Hall", SpecialSeats = 0, StandardSeats = 100, SpecialSeatType = "", Department = Repo.Departments.Single(d => d.Name == "Maths")
                });

                //Repo.Periods.Add(new TimeSlot() { Name = "Period 1", Start = new TimeSpan(8, 50, 0), End = new TimeSpan(9, 50, 0) });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 1", Start = new TimeSpan(8, 50, 0), End = new TimeSpan(9, 50, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 2", Start = new TimeSpan(9, 50, 0), End = new TimeSpan(10, 50, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 3", Start = new TimeSpan(11, 10, 0), End = new TimeSpan(12, 10, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 4", Start = new TimeSpan(12, 10, 0), End = new TimeSpan(13, 10, 0)
                });
                Repo.Periods.Add(new TimeSlot()
                {
                    Name = "Period 5", Start = new TimeSpan(14, 0, 0), End = new TimeSpan(15, 0, 0)
                });

                string LogonName = DataRepository.Home ? "Keith" : "09135"; // For testing on home versus school computers
                Repo.Students.Add(new Student()
                {
                    FirstName = "Keith", LastName = "Collister", Form = "WT", Year = 13, LogonName = LogonName, Access = AccessMode.Admin
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Dan", LastName = "Wrenn", Form = "MB", Year = 13, LogonName = "09154", Access = AccessMode.Teacher
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Euan", LastName = "Rossie", Form = "WT", Year = 13, LogonName = "09185"
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Max", LastName = "Norman", Form = "WT", Year = 13, LogonName = "Max"
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Peter", LastName = "Champion", Form = "WT", Year = 13, LogonName = "Peter"
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Mia", LogonName = "Mia", LastName = "West", Form = "MB", Year = 13
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Matthew", LogonName = "Matthew", LastName = "Pilkington", Form = "WT", Year = 13
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Kaleb", LogonName = "Kaleb", LastName = "Poole", Form = "BR", Year = 11
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Sam", LogonName = "Sam", LastName = "Kitto", Form = "BR", Year = 11
                });
                Repo.Students.Add(new Student()
                {
                    FirstName = "Isobel", LogonName = "Isobel", LastName = "Stephens", Form = "MI", Year = 11
                });

                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "Maths", Colour = Colors.Red
                });
                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "Physics", Colour = Colors.Orange
                });
                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "Computing", Colour = Colors.Blue
                });
                Repo.Subjects.Add(new Subject()
                {
                    SubjectName = "History", Colour = Colors.Green
                });

                Repo.SaveChanges();

                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mrs", LogonName = "mb", FirstName = "Mary", LastName = "Bogdiukiewicz", Department = Repo.Departments.ToList().Where(d => d.Name.Contains("Computing")).Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mr", LogonName = "cn", FirstName = "Patrick", LastName = "Count", Department = Repo.Departments.ToList().Where(d => d.Name.Contains("Computing")).Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mr", LogonName = "jk", FirstName = "James", LastName = "Kenny", Department = Repo.Departments.Where(d => d.Name == "Science").Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mrs", LogonName = "rb", FirstName = "Rosemary", LastName = "Britton", Department = Repo.Departments.Where(d => d.Name == "Maths").Single(), Email = "*****@*****.**"
                });
                Repo.Teachers.Add(new Teacher()
                {
                    Title = "Mrs", LogonName = "ed", FirstName = "Emma", LastName = "Denny", Department = Repo.Departments.ToList().Where(d => d.Name.Contains("History")).Single(), Email = "*****@*****.**"
                });

                Repo.SaveChanges();

                Repo.Classes.Add(new Class()
                {
                    ClassName = "Computing Yr13", Students = Repo.Students.Where(s => s.FirstName == "Keith" || s.FirstName == "Max" || s.FirstName == "Dan" || s.FirstName == "Peter").ToList(), Owner = Repo.Teachers.Where(t => t.LogonName == "mb").Single()
                });
                Repo.Classes.Add(new Class()
                {
                    ClassName = "Maths Yr13", Students = Repo.Students.Where(s => s.Form == "WT").ToList(), Owner = Repo.Teachers.Where(t => t.LogonName == "rb").Single()
                });
                Repo.Classes.Add(new Class()
                {
                    ClassName = "History Yr11", Students = Repo.Students.Where(s => s.Year == 11).ToList(), Owner = Repo.Teachers.Where(t => t.LogonName == "rb").Single()
                });

                Repo.SaveChanges();

                Repo.Bookings.Add(new Booking()
                {
                    Rooms       = Repo.Rooms.ToList().Where(r => r.RoomName[0] == 'D').ToList(),
                    Students    = Repo.Students.ToList().Where(s => s.FullForm == "13WT").ToList(),
                    BookingType = BookingType.Single,
                    Date        = DateTime.Now.Date,
                    Subject     = Repo.Subjects.Where(s => s.SubjectName == "Maths").Single(),
                    Teacher     = Repo.Teachers.Where(t => t.LastName == "Britton").Single(),
                    TimeSlot    = Repo.Periods.Where(p => p.Name == "Period 1").Single(),
                });
                Repo.Bookings.Add(new Booking()
                {
                    Rooms       = Repo.Rooms.Where(r => r.RoomName == "Library").ToList(),
                    Students    = Repo.Students.ToList().Where(s => s.FullForm == "13MB").ToList(),
                    BookingType = BookingType.Weekly,
                    Date        = DateTime.Now.Date,
                    Subject     = Repo.Subjects.Where(s => s.SubjectName == "Computing").Single(),
                    Teacher     = Repo.Teachers.Where(t => t.LastName == "Bogdiukiewicz").Single(),
                    TimeSlot    = Repo.Periods.Where(p => p.Name == "Period 2").Single(),
                });

                Repo.SaveChanges();
            }
            #endregion

            Print("Initialised data", ConsoleColor.Gray);

            // Load the settings from the Settings.txt file
            Settings.Load();
            Print("Loaded settings", ConsoleColor.Gray);


            // Flag to represent whether the server is in the middle of shutting down
            bool Closing = false;
            // Initialise the Listener with the port defined in the settings file
            Listener = new Listener(Settings.Port);
            try
            {
                // Hook up the event handlers - these define the actual action taken by the server
                Listener.ClientConnect         += ClientConnected;
                Listener.ClientDisconnect      += ClientDisconnect;
                Listener.ClientMessageReceived += ClientMessageReceived;

                // Start and don't buffer messages - use events instead
                Listener.Start(false);
                Print("Listener started...", ConsoleColor.Green);

                // Wait for a keypress (to signal exit)
                while (true)
                {
                    ConsoleKey Key = Console.ReadKey(true).Key;
                    if (Key == ConsoleKey.Escape)
                    {
                        break;
                    }
                    else if (Key == ConsoleKey.K)
                    {
                        Listener.Send(new TestMessage("kill"));
                    }
                }

                // Shut down the server, unhook the handlers
                Closing = true;
                Listener.Stop();
                Print("Listener stopped...", ConsoleColor.Red);
                Listener.ClientConnect         -= ClientConnected;
                Listener.ClientDisconnect      -= ClientDisconnect;
                Listener.ClientMessageReceived -= ClientMessageReceived;
            }
            catch (Exception e)
            {
                // If an exception is fired while the server's shutting down,
                // it's usually a send error and is safe to ignore
                if (!Closing)
                {
                    Print("Error: " + e.ToString(), ConsoleColor.Red);
                    Console.ReadKey(true);
                }
            }
            try
            {
                Listener.Dispose();
            }
            catch { }
        }