public uint AddHouseroom(Houseroom houseroom)
        {
            houseroom.Id = NextHouseroomId;
            Houserooms.Add(NextHouseroomId, houseroom);

            return(NextHouseroomId++);
        }
        public bool OpenFile()
        {
            Houserooms.Clear();

            XmlDocument document = new XmlDocument();

            try
            {
                document.Load(fileName);

                XmlNodeList UsersNodes = document.SelectNodes("/DataBase/Users/User");

                XmlNodeList HouseroomsNodes = document.SelectNodes("/DataBase/Houserooms/Houseroom");

                foreach (XmlNode HouseroomNode in HouseroomsNodes)
                {
                    //считываем атрибуты
                    uint    id          = uint.Parse(HouseroomNode.SelectSingleNode("ID").InnerText);
                    string  city        = HouseroomNode.SelectSingleNode("CITY").InnerText;
                    string  street      = HouseroomNode.SelectSingleNode("STREET").InnerText;
                    decimal price       = decimal.Parse(HouseroomNode.SelectSingleNode("PRICE").InnerText);
                    string  description = HouseroomNode.SelectSingleNode("DESCRIPTION").InnerText;
                    //uint numberOfRooms = uint.Parse(HouseroomNode.SelectSingleNode("NUMBER_OF_ROOMS").InnerText);

                    Houseroom item = new Houseroom(id, city, street, price, description);
                    Houserooms.Add(item.Id, item);

                    if (item.Id > NextHouseroomId)
                    {
                        NextHouseroomId = item.Id;
                    }
                }
                NextHouseroomId++;
            }
            catch
            {
                //не открылся файл
                return(false);
            }

            return(true);
        }
Ejemplo n.º 3
0
        private void ClientHandle(object client)
        {
            byte[] bytes = new byte[sizeOfPacket];
            int    bytesRead;

            int cUserId = -1;

            Socket     Client         = (Socket)client;
            IPEndPoint ClientEndPoint = (IPEndPoint)Client.RemoteEndPoint;

            Console.WriteLine("Открыли соединение с {0} на порту {1}",
                              ClientEndPoint.Address, ClientEndPoint.Port);

            while (true)
            {
                bytesRead = 0;
                try
                {
                    bytesRead = Client.Receive(bytes);
                }
                catch
                {
                    break;
                }

                if (bytesRead == 0)
                {
                    //Клиент был отсоединен от сервера
                    Console.WriteLine("Закрыли соединение с {0}", ClientEndPoint.Address);
                    break;
                }

                if (bytesRead == 2)
                {
                    short Command = BitConverter.ToInt16(bytes, 0);

                    DBManager.Inst().SaveFile();

                    switch (Command)
                    {
                    case (int)Commands.ADD_USER:
                    {
                        Console.WriteLine("\tКом. 1 - Добавление нового пользователя");

                        string Login    = recvString(Client);
                        string Password = recvString(Client);

                        bool isUsed = DBManager.Inst().FindUser(Login);

                        if (isUsed)
                        {
                            sendDigit16((short)Answers.USER_NAME_ALREADY_USED, Client);
                        }
                        else
                        {
                            sendDigit16((short)Answers.USER_NAME_OK, Client);

                            User user = new User(0, Login, Password);
                            DBManager.Inst().AddUser(user);
                        }

                        break;
                    }

                    case (int)Commands.LOGIN_USER:
                    {
                        Console.WriteLine("\tКом. 2 - Авторизация пользователя");

                        string Login    = recvString(Client);
                        string Password = recvString(Client);

                        bool isExist = DBManager.Inst().FindUser(Login);

                        if (!isExist)
                        {
                            sendDigit16((short)Answers.USER_NOT_EXIST, Client);
                        }
                        else
                        {
                            cUserId = -1;

                            int id = DBManager.Inst().FindUser(Login, Password);
                            if (id >= 0)
                            {
                                sendDigit16((short)Answers.USER_LOGIN_OK, Client);
                                cUserId = id;
                            }
                            else
                            {
                                sendDigit16((short)Answers.USER_LOGIN_BREAK, Client);
                            }
                        }

                        break;
                    }

                    case (int)Commands.ADD_HOUSEROOM:
                    {
                        Console.WriteLine("\tКом. 3 - Добавление жилплощади");

                        string  City        = recvString(Client);
                        string  Street      = recvString(Client);
                        decimal Price       = Convert.ToDecimal(recvString(Client));
                        string  Description = recvString(Client);

                        sendDigit16((short)Answers.COMMAND_OK, Client);

                        Houseroom houseroom = new Houseroom(0, City, Street, Price, Description);
                        DBManager.Inst().AddHouseroom(houseroom);

                        break;
                    }

                    case (int)Commands.LIST_HOUSEROOM:
                    {
                        Console.WriteLine("\tКом. 4 - Вывести список жилья");

                        sendDigit16((short)Answers.COMMAND_OK, Client);

                        //Console.WriteLine("отправил ОК");

                        string City = recvString(Client);
                        //Console.WriteLine(City);
                        string Street = recvString(Client);
                        //Console.WriteLine(Street);
                        decimal Price = Convert.ToDecimal(recvString(Client));
                        //Console.WriteLine("Принял цену");

                        int[] Rows = DBManager.Inst().FindHouseroom(City, Street, Price);

                        if (Rows.Count() == 0)
                        {
                            sendDigit16((short)Answers.USER_NOT_LOGIN, Client);
                        }
                        else
                        {
                            sendDigit16((short)Answers.COMMAND_OK, Client);
                            sendDigit32(Rows.Length, Client);
                            //Console.WriteLine("Отправил rows");

                            for (int i = 0; i < Rows.Length; i++)
                            {
                                sendDigit32(Rows[i], Client);
                            }
                        }

                        break;
                    }

                    case (int)Commands.SELECT_HOUSEROOM:
                    {
                        Console.WriteLine("\tКом. 5 - Получить данные для жилья");

                        sendDigit16((short)Answers.COMMAND_OK, Client);

                        int houseId = recvDigit16(Client);

                        sendString(DBManager.Inst().Houserooms[(uint)houseId].Description, Client);
                        sendString(Convert.ToString(DBManager.Inst().Houserooms[(uint)houseId].Price), Client);

                        break;
                    }
                    }
                }
            }
        }