示例#1
0
        /// <summary>
        /// Поиск клиента, прочитающего книгу быстрее всех
        /// </summary>
        /// <param name="book">Книга</param>
        /// <param name="timeRead">Дата-время прочтения книги</param>
        /// <returns>Электронный адресс клиента</returns>
        public string SearchFastReader(BookSDB book, out DateTime timeRead)
        {
            string   clientAddress = null;
            DateTime timeReadEnd   = new DateTime();                    //промежуточное время окончания чтения книги
            DateTime timeReadNow   = new DateTime();                    //время начала чтения книги

            timeRead = new DateTime();                                  //время окончания чтения книги
            bool first = true;

            var selectedClients = from client in ClientDictionary
                                  from lela in client.Value.GetClientServerDB().LevelLanguages
                                  where lela.Language == book.Language
                                  select client.Value.GetClientServerDB();

            foreach (var clientServer in selectedClients)
            {
                DateTime nowTime = DateTime.UtcNow.ToLocalTime();
                timeReadNow = clientServer.TimeRead > nowTime ?
                              clientServer.TimeRead : nowTime;

                timeReadEnd = timeReadNow.AddSeconds(ServerDB.TimeReadingBook(clientServer, book, timeReadNow));
                if (first || timeReadEnd < timeRead)
                {
                    if (first)
                    {
                        first = false;
                    }
                    clientAddress = clientServer.Address;
                    timeRead      = timeReadEnd;
                }
            }
            return(clientAddress);
        }
示例#2
0
        /// <summary>
        /// Получение и чтение книг клиентом без обработки общих исключений
        /// </summary>
        private void TryGettingAndReadingBook()
        {
            int MilisecondOfSecond = 1000;

            ReadingActiveBook();
            while (true)
            {
                if (ClientServer.QueueBook.Count == 0)
                {
                    WaitHandler.Reset();
                    WaitHandler.WaitOne();
                }

                MutexJointOperation.WaitOne();
                string   bookJSON              = ClientServer.QueueBook.Dequeue();
                BookSDB  book                  = JsonSerializer.Deserialize <BookSDB>(bookJSON);
                double   timeSleep             = ServerDB.TimeReadingBook(ClientServer, book, DateTime.UtcNow.ToLocalTime());
                DateTime timeComleteReadActive = DateTime.UtcNow.ToLocalTime().AddSeconds(timeSleep);
                SendBookClient(bookJSON, timeComleteReadActive);
                MutexJointOperation.ReleaseMutex();

                Thread.Sleep((int)timeSleep * MilisecondOfSecond);
                MySqlConnection.ImplementationBook(ClientServer.Address, DateTime.UtcNow.ToLocalTime());
            }
        }