private void BL_InitSeats()
        {
            var queue = new MessageQueue(_queueName){
                Formatter = new XmlMessageFormatter(new[] { typeof(Seat) })
            };

            if (!MessageQueue.Exists(_queueName))
            {
                using (queue = MessageQueue.Create(_queueName))
                {
                    queue.Label = "CinemaSeats";
                }
            }
            
            Task.Factory.StartNew(() => PeekMessages(queue))
                .ContinueWith(x =>
                {
                    var enumerable = x.Result;
                    var seats = enumerable as IList<LabelIdMapping> ?? enumerable.ToList();

                    var dbTickets = GetSqlTickets();

                    if (seats.Count == 0 && dbTickets.Count == 0)
                    {
                        UI_InitSeats();
                        return;
                    }

                    //deserialize messsages
                    var messages = seats.Select(labelIdMapping => queue.PeekById(labelIdMapping.Id).Body as Seat).ToList();

                    for (var i = 0; i < 30; i++)
                    {
                        for (var j = 0; j < 40; j++)
                        {
                            var bookedSeat = messages.FirstOrDefault(n => n.Number == j && n.Row == i)
                                            ?? dbTickets.FirstOrDefault(n => n.Number == j && n.Row == i)
                                            ?? new Seat();

                            //TODO: change to dynamic BookingState
                            bookedSeat.ViewModel = DrawSeat(bookedSeat.BookingState);
                            canvas.Children.Add(bookedSeat.ViewModel);
                            Canvas.SetTop(bookedSeat.ViewModel, i*10);
                            Canvas.SetLeft(bookedSeat.ViewModel, j*10);

                            _room[i, j] = bookedSeat;
                        }
                    }
                }, TaskScheduler.FromCurrentSynchronizationContext());
        }
        public MessageDetailsModel(string messageId, string queuePath)
        {
            var additionalDetails = new List<MessageDetail>();
            using (var messageQueue = new MessageQueue(queuePath))
            {
                messageQueue.MessageReadPropertyFilter.ClearAll();
                messageQueue.MessageReadPropertyFilter.Body = true;
                foreach (IMessageExtension messageExtension in MessageExtensions.GetMessageExtensions())
                {
                    messageExtension.CustomizeMessagePropertyFilter(messageQueue.MessageReadPropertyFilter);
                }

                try
                {
                    using (var message = messageQueue.PeekById(messageId))
                    {
                        this.Body = new StreamReader(message.BodyStream).ReadToEnd();
                        foreach (IMessageExtension messageExtension in MessageExtensions.GetMessageExtensions())
                        {
                            additionalDetails.AddRange(messageExtension.GetMessageDetails(message));
                        }
                    }
                }
                catch (InvalidOperationException ex)
                {
                    this.Body = "No message with the id " + messageId + " exists. Probably it was consumed few moments ago.\n"
                        + "Exception details: " + ex.ToString();
                }
                catch (Exception ex)
                {
                    this.Body = "Could not read details of message with id " + messageId + ".\n"
                        + "Exception details: " + ex.ToString();
                }

                this.AdditionalDetails = additionalDetails;
            }
        }
        private void RefreshSeats()
        {
            while (true)
            {
                

                var seats = GetSqlTickets();
                var seatsCancelledByAdmin = new List<Seat>();

                foreach (var seat in seats)
                {
                    var ticket = _room[seat.Row, seat.Number];
                    if (ticket == null)
                        continue;

                    if (ticket.BookingState == seat.BookingState)
                        continue;

                    ticket.BookingState = seat.BookingState;
                    if (seat.BookingState == BookingState.Cancelled)
                    {
                        seatsCancelledByAdmin.Add(seat);
                    }

                    Dispatcher.Invoke(() =>
                    {
                        ticket.ViewModel.Fill = UpdateState(seat.BookingState);
                    });

                }

                var queue1 = new MessageQueue(_queueName) { Formatter = new XmlMessageFormatter(new[] { typeof(Seat) }) };

                var messages = PeekMessages(queue1);
                //var pendingApprovalMsgs = messages.Where(labelIdMapping => queue1.Peek(TimeSpan.FromMilliseconds(1)) != null)
                //    .Select(x => (x.Body) as Seat).ToList();

                foreach (var message in messages)
                {
                    Seat msg = null;
                    try
                    {
                        msg = queue1.PeekById(message.Id).Body as Seat;
                    }
                    catch (InvalidOperationException ex)
                    {
                        _seats = new List<LabelIdMapping>();
                        continue;
                    }
                    

                    var ticket = _room[msg.Row, msg.Number];
                    if (ticket == null)
                        continue;

                    if (ticket.BookingState == msg.BookingState)
                        continue;

                    ticket.BookingState = msg.BookingState;
                    if (msg.BookingState == BookingState.Cancelled)
                    {
                        seatsCancelledByAdmin.Add(msg);
                    }

                    Dispatcher.Invoke(() =>
                    {
                        ticket.ViewModel.Fill = UpdateState(msg.BookingState);
                    });
                }

                foreach (var cancelledSeat in _seatsCancelledByClient)
                {
                    if (!seats.Contains(cancelledSeat))
                    {
                        var ticket = _room[cancelledSeat.Row, cancelledSeat.Number];
                        ticket.BookingState = BookingState.Free;

                        Dispatcher.Invoke(() =>
                        {
                            ticket.ViewModel.Fill = UpdateState(ticket.BookingState);
                        });
                    }
                }
                _seatsCancelledByClient.Clear();

                Thread.Sleep(1000);

                foreach (var cancelledSeat in seatsCancelledByAdmin)
                {
                    var queue = new MessageQueue(_queueName);
                    if (seats.Contains(cancelledSeat))
                    {
                        var ticket = _room[cancelledSeat.Row, cancelledSeat.Number];
                        ticket.BookingState = BookingState.Free;
                        queue.Send(cancelledSeat);

                        Dispatcher.Invoke(() =>
                        {
                            ticket.ViewModel.Fill = UpdateState(ticket.BookingState);
                        });
                    }
                }

                seatsCancelledByAdmin.Clear();
            }
        }
Пример #4
0
 public Message GetFullMessage(MessageQueue messageQueue, string messageId)
 {
     messageQueue.MessageReadPropertyFilter.SetAll();
     messageQueue.MessageReadPropertyFilter.SourceMachine = true;
     return messageQueue.PeekById(messageId);
 }