Exemplo n.º 1
0
        public async Task WriteStream(Stream stream, CitylineRequest request, IContext context, CancellationToken cancellationToken = default)
        {
            var queue = new Queue <ICitylineProducer>(_providers);

            while (!cancellationToken.IsCancellationRequested)
            {
                if (queue.Count > 0)
                {
                    var          provider = queue.Dequeue();
                    var          name     = provider.Name;
                    TicketHolder ticket   = null;

                    if (request.Tickets == null)
                    {
                        request.Tickets = new Dictionary <string, string>();
                    }

                    if (request.Tickets.ContainsKey(name))
                    {
                        ticket = new TicketHolder(request.Tickets[name]);
                    }

                    ticket = ticket ?? new TicketHolder();

                    #pragma warning disable 4014
                    Task.Run(async() =>
                    {
                        try {
                            await RunProducer(provider, stream, ticket, context, cancellationToken);

                            if (request.Tickets.ContainsKey(name))
                            {
                                request.Tickets[name] = ticket.AsString();
                            }
                            else
                            {
                                request.Tickets.Add(name, ticket.AsString());
                            }
                        } finally {
                            queue.Enqueue(provider);
                        }
                    }).ContinueWith(t =>
                    {
                        if (t.Exception != null)
                        {
                            throw t.Exception;
                        }
                    }, cancellationToken, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Current)
                    .ConfigureAwait(false)
                    ;
                    #pragma warning restore 4014

                    await Task.Delay(200);
                }

                await Task.Delay(200);
            }
        }
Exemplo n.º 2
0
 protected BaseCitylineWriter(SemaphoreSlim semaphore, ICitylineProducer provider, Stream stream, TicketHolder ticket, CancellationToken cancellationToken = default)
 {
     this.semaphore         = semaphore;
     this.provider          = provider;
     this.stream            = stream;
     this.stream            = stream;
     this.ticket            = ticket;
     this.cancellationToken = cancellationToken;
 }
Exemplo n.º 3
0
 private void addTicketHoldedToEvent(TicketHolder newTicketHolder, String thisEvent)
 {
     foreach (ShowEvent e in eventList)
     {
         if (e.EventName.Equals(thisEvent))
         {
             e.TicketHolders.Add(newTicketHolder);
             e.takeSeat(newTicketHolder.Seat);
         }
     }
 }
Exemplo n.º 4
0
    private void makeHolder()
    {
        String ticketHolder = txtName.Text;
        int    age          = Convert.ToInt16(txtAge.Text);
        String seat         = DropDownSeats.SelectedValue;

        DropDownSeats.Items.Remove(seat);
        lblTicketsAvailableNum.Text = Convert.ToString((Convert.ToInt16(lblTicketsAvailableNum.Text) - 1));
        String       thisEvent       = DropDownEventsList.SelectedItem.Text;
        TicketHolder newTicketHolder = new TicketHolder(ticketHolder, age, seat, thisEvent);

        ticketHolders.Add(newTicketHolder);
        addTicketHoldedToEvent(newTicketHolder, thisEvent);
        txtName.Text = "";
        txtAge.Text  = "";
        printEvents();
    }
Exemplo n.º 5
0
        private async Task RunProducer(ICitylineProducer provider, Stream stream, TicketHolder ticket, IContext context, CancellationToken cancellationToken = default)
        {
            ICitylineWriter writer;

            if (this.UseJson)
            {
                writer = new CitylineJsonWriter(semaphore, provider, stream, ticket, cancellationToken);
            }
            else
            {
                writer = new CitylineLineWriter(semaphore, provider, stream, ticket, cancellationToken);
            }

            // Func<object, Task> emitter = new Func<object, Task>(async obj =>
            // {
            //     var value = JsonConvert.SerializeObject(obj, settings);
            //     try
            //     {
            //         await semaphore.WaitAsync(cancellationToken);
            //         using (var writer = new StreamWriter(stream, new UTF8Encoding(false), value.Length + 1024, true))
            //         {
            //             if (UseJson)
            //                 await WriteJson(writer, value, provider, ticket, CancellationToken.None); // CancellationToken.None, we don't want partial messages
            //             else
            //                 await WriteText(writer, value, provider, ticket, CancellationToken.None);
            //         }
            //     }
            //     finally
            //     {
            //         semaphore.Release();
            //     }
            // });

            // context is shared, so this wont work

            var response = await provider.GetFrame(ticket, context, writer, cancellationToken);

            if (response == null)
            {
                return;
            }

            await writer.Write(response);
        }
Exemplo n.º 6
0
        public void Can_serialize_ticket()
        {
            ////Arrange
            var sample = new SampleClass {
                Name = "bob"
            };
            var ticketHolder = new TicketHolder();

            ticketHolder.UpdateTicket(sample);
            var ticketSource = ticketHolder.AsString();

            ////Act
            var result = new TicketHolder(ticketSource);

            ////Assert
            var actualSample = result.GetTicket <SampleClass>();

            Assert.AreEqual("bob", actualSample.Name);
        }
Exemplo n.º 7
0
        private async Task RunProducer(ICitylineProducer provider, Stream stream, TicketHolder ticket, IContext context, CancellationToken cancellationToken = default)
        {
            var response = await provider.GetFrame(ticket, context, cancellationToken);

            if (response == null)
            {
                return;
            }

            lock (padLock)
            {
                using (var writer = new StreamWriter(stream, new UTF8Encoding(false), 1024, true)) {
                    writer.WriteLine($"id: {ticket.AsString()}");
                    writer.WriteLine($"event: {provider.Name}");
                    writer.WriteLine($"data: {JsonConvert.SerializeObject(response, settings)}");
                    writer.WriteLine();
                }
            }
        }
Exemplo n.º 8
0
        protected override async Task Write(TextWriter writer, string dataValue, ICitylineProducer provider, TicketHolder ticket, CancellationToken cancellationToken = default(CancellationToken))
        {
            using (var jsonWriter = new JsonTextWriter(writer)
            {
                Formatting = Formatting.None
            })
            {
                await jsonWriter.WriteStartObjectAsync(cancellationToken);

                await jsonWriter.WritePropertyNameAsync("id", cancellationToken);

                await jsonWriter.WriteValueAsync(ticket.AsString(), cancellationToken);

                await jsonWriter.WritePropertyNameAsync("event", cancellationToken);

                await jsonWriter.WriteValueAsync(provider.Name, cancellationToken);

                await jsonWriter.WritePropertyNameAsync("data", cancellationToken);

                await jsonWriter.WriteValueAsync(dataValue, cancellationToken);

                await jsonWriter.WriteEndObjectAsync(cancellationToken);

                await writer.WriteAsync('\n');
            }
        }
Exemplo n.º 9
0
 public CitylineJsonWriter(SemaphoreSlim semaphore, ICitylineProducer provider, Stream stream, TicketHolder ticket, CancellationToken cancellationToken = default) : base(semaphore, provider, stream, ticket, cancellationToken)
 {
 }
Exemplo n.º 10
0
 protected abstract Task Write(TextWriter writer, string dataValue, ICitylineProducer provider, TicketHolder ticket, CancellationToken cancellationToken);
Exemplo n.º 11
0
        protected override async Task Write(TextWriter writer, string dataValue, ICitylineProducer provider, TicketHolder ticket, CancellationToken cancellationToken = default(CancellationToken))
        {
            await writer.WriteLineAsync($"id: {ticket.AsString()}");

            await writer.WriteLineAsync($"event: {provider.Name}");

            await writer.WriteLineAsync($"data: {dataValue}");

            await writer.WriteLineAsync("");
        }
Exemplo n.º 12
0
        public void CreateStudentTicket(string Name, string Number, int TicketNumber)
        {
            TicketHolder.TranslatesAutoresizingMaskIntoConstraints = false;

            UIView Ticket = new UIView();

            Ticket.BackgroundColor = UIColor.FromRGBA(21, 21, 30, 255);
            Ticket.HeightAnchor.ConstraintEqualTo(160).Active = true;
            Ticket.WidthAnchor.ConstraintEqualTo(UIScreen.MainScreen.Bounds.Width).Active = true;

            UILabel StudentName = new UILabel();

            //    StudentName.LineBreakMode = UILineBreakMode.Clip;
            StudentName.Text      = Name;
            StudentName.TextColor = UIColor.White;
            StudentName.Font.WithSize(40);



            Ticket.AddSubview(StudentName);

            UILabel StudentNumber = new UILabel();

            StudentNumber.Text      = Number;
            StudentNumber.TextColor = UIColor.White;
            StudentNumber.Font.WithSize(40);

            Ticket.AddSubview(StudentNumber);

            TicketHolder.AddArrangedSubview(Ticket);

            UIImageView ImageView = new UIImageView();

            ImageView.BackgroundColor = UIColor.Black;
            ImageView.ContentMode     = UIViewContentMode.ScaleAspectFit;
            ImageView.Image           = UIImage.FromBundle("GoingPro_Icon.png");


            ImageView.Frame     = new CoreGraphics.CGRect(15, 15, 50, 50);
            StudentName.Frame   = new RectangleF(75, 18, 500, 20);
            StudentNumber.Frame = new RectangleF(75, 36, 90, 20);
            Ticket.AddSubview(ImageView);

            UIDropDown DropDown = new UIDropDown(Ticket, View, new List <string>()
            {
                "Showing Responsibility",
                "Showing Respect",
                "Demonstrating Initiative",
                "Being Safe",
                "Demonstrating Professionalism"
            }, new CoreGraphics.CGRect(25, 85, TicketHolder.Bounds.Width - 65, 30), Scroll, TicketNumber, this);


            UIDropDown DropDown2 = new UIDropDown(Ticket, View, new List <string>()
            {
                "KTC",
                "MySchool",
                "KCTC",
                "KIH",
            }, new CoreGraphics.CGRect(25, 120, 100, 30), Scroll, TicketNumber, this);


            UIDropDown DropDown3 = new UIDropDown(Ticket, View, new List <string>()
            {
                "Classrooms",
                "Parking Lot",
                "Vending Machines",
                "Commons",
            }, new CoreGraphics.CGRect(182, 120, 100, 30), Scroll, TicketNumber, this);

            DropDown2.OptionSelected += (e) =>
            {
                Console.WriteLine("Here " + e);
                DropDown3.Options = BLL[e].ToList();
                DropDown3.GenerateNewOptions();
                return(null);
            };
            LayoutSpinner.Add(TicketNumber, new List <UIDropDown>()
            {
                DropDown, DropDown2, DropDown3
            });
            NumberOfTickets += 1;
        }
Exemplo n.º 13
0
        public async Task DoWriteStream(Stream stream, CitylineRequest request, IContext context, CancellationToken cancellationToken = default)
        {
            ConcurrentDictionary <Task, object> tasks = new ConcurrentDictionary <Task, object>();

            var queue = new ConcurrentQueue <ICitylineProducer>(_providers);

            while (!cancellationToken.IsCancellationRequested)
            {
                if (queue.Count > 0)
                {
                    if (!queue.TryDequeue(out ICitylineProducer provider))
                    {
                        continue;
                    }

                    var          name   = provider.Name;
                    TicketHolder ticket = null;

                    if (request.Tickets == null)
                    {
                        request.Tickets = new Dictionary <string, string>();
                    }

                    if (request.Tickets.ContainsKey(name))
                    {
                        ticket = new TicketHolder(request.Tickets[name]);
                    }

                    ticket = ticket ?? new TicketHolder();

#pragma warning disable 4014
                    tasks.TryAdd(Task.Run(async() =>
                    {
                        try
                        {
                            var ownSource = new CancellationTokenSource();
                            cancellationToken.Register(ownSource.Cancel);

                            await RunProducer(provider, stream, ticket, context, ownSource.Token);

                            if (request.Tickets.ContainsKey(name))
                            {
                                request.Tickets[name] = ticket.AsString();
                            }
                            else
                            {
                                request.Tickets.Add(name, ticket.AsString());
                            }
                        }
                        catch (Exception ex) {
                            _logger.WriteLine($"Producer {provider.Name} failed: {ex}");
                        }
                        finally
                        {
                            queue.Enqueue(provider);
                        }
                    }).ContinueWith(task =>
                    {
                        _logger.WriteLine($"Task failed for {provider.Name} failed: {task.Exception}");

                        if (task.Exception != null)
                        {
                            throw task.Exception;
                        }
                    }, cancellationToken, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Current)
                                 .ContinueWith(task => tasks.TryRemove(task, out object value)), null);
#pragma warning restore 4014
                }

                await Task.Delay(200, cancellationToken);
            }

            await Task.WhenAll(tasks.Keys);
        }