Esempio n. 1
0
        protected MultiThreadOscilloscope(
            ConstructorTuple <ICanvas <T> > canvas_constructor,
            ConstructorTuple <IPointDrawer> point_drawer_constructor,
            IGraphProducer graph_producer,
            IControlPanel control_panel,
            ConcurrentQueue <T> buffer = null)
        {
            ICanvas <T>  canvas       = canvas_constructor.NewInstance();
            IPointDrawer point_drawer = point_drawer_constructor.NewInstance();

            if (canvas.GraphSize != point_drawer.GraphSize)
            {
                throw new OscillocopeBuildException("canvas and point_drawer have different GraphSize", new DifferentGraphSizeException());
            }
            if (graph_producer.RequireConcurrentDrawer && !point_drawer.IsConcurrent)
            {
                throw new OscillocopeBuildException("graph_producer require multi-thread-safe PointDrawer but point_drawer is not");
            }
            this.canvas_constructor       = canvas_constructor;
            this.point_drawer_constructor = point_drawer_constructor;
            this.graph_producer           = graph_producer;
            this.control_panel            = control_panel;
            if (buffer == null)
            {
                this.buffer = new ConcurrentQueue <T>();
            }
            else
            {
                this.buffer = buffer;
            }
            this.free_canvas.Enqueue(canvas);
            this.free_point_drawer.Enqueue(point_drawer);
        }
Esempio n. 2
0
        protected void Draw(double delta_time)
        {
            ICanvas <T> canvas;

            if (free_canvas.TryDequeue(out canvas))
            {
                if (!canvas.IsReady)
                {
                    free_canvas.Enqueue(canvas_constructor.NewInstance());
                }
            }
            else
            {
                canvas = canvas_constructor.NewInstance();
            }
            IPointDrawer point_drawer;

            if (!free_point_drawer.TryDequeue(out point_drawer))
            {
                point_drawer = point_drawer_constructor.NewInstance();
            }
            while (!canvas.IsReady)
            {
                Thread.Yield();
            }
            graph_producer.Produce(
                delta_time: delta_time,
                canvas: canvas,
                point_drawer: point_drawer,
                information: control_panel.GetStateShot());
            T new_graph = canvas.Output();

            buffer.Enqueue(new_graph);
            free_canvas.Enqueue(canvas);
            free_point_drawer.Enqueue(point_drawer);
        }
        public void PngOutputTest()
        {
            int   TEST_TIMES = 1;
            int   RUNNING_TIME_MILLISCEOND = 10_000;
            Color background_color         = Color.FromArgb(0xff, 0x00, 0x00, 0x00);
            Color ruler_color        = Color.FromArgb(0xff, 0x10, 0x00, 0xff);
            Color graph_color        = Color.FromArgb(0xff, 0x10, 0xff, 0x10);
            CathodeRayTubPanel panel = new CathodeRayTubPanel();

            panel.XWave.Wave  = new SawToothWave(Waves.UNIT_NUMBER_PRO_SECOND / 100, 0.4);
            panel.YWave.Wave  = new SinWave(Waves.UNIT_NUMBER_PRO_SECOND / 220, 0.4);
            panel.PointLength = 1;
            panel.PointWidth  = 1;
            IBackgroundDrawer background_drawer = new CrossBackgroundDrawer(360, 360, background_color, ruler_color, 1);
            ConstructorTuple <ICanvas <Bitmap> > canvas_constructor
                = new ConstructorTuple <ICanvas <Bitmap> >(typeof(BitmapCanvas), 360, 360, background_drawer);
            int drived_total_parallel_count = 0;
            int drived_ghost_parallel_count = 0;
            int drived_serial_count         = 0;
            int drived_ghost_serial_count   = 0;
            int simple_count = 0;

            for (int t = 0; t < TEST_TIMES; t++)
            {
                ConstructorTuple <IPointDrawer> multi_point_drawer_constructor
                    = new ConstructorTuple <IPointDrawer>(typeof(ConcurrentOvalPointDrawer), 360, 360);
                IGraphProducer total_parallel_graph_producer = new TotalParallelProducer(4321, graph_color);
                DrivedOscilloscope <Bitmap> drived_total_parallel_oscilloscope
                    = new DrivedOscilloscope <Bitmap>(
                          canvas_constructor: canvas_constructor,
                          point_drawer_constructor: multi_point_drawer_constructor,
                          graph_producer: total_parallel_graph_producer,
                          control_panel: panel);
                ConcurrentQueue <Bitmap> buffer = drived_total_parallel_oscilloscope.Buffer;
                save_count = drived_total_parallel_count;
                Timer timer = new Timer(o => DequeueAndSave(buffer, base_path + "Drived_TotalParallel\\"), null, 20, 10);
                drived_total_parallel_oscilloscope.Start(10_000);
                Thread.Sleep(RUNNING_TIME_MILLISCEOND);
                drived_total_parallel_oscilloscope.End();
                Thread.Sleep(2_000);
                timer.Dispose();
                drived_total_parallel_count = save_count;

#if GHOST_PRODUCER_NEED
                IGraphProducer ghost_parallel_graph_producer = new GhostParallelProducer(4321, 50, graph_color);
                DrivedOscilloscope <Bitmap> drived_ghost_parallel_oscilloscope
                    = new DrivedOscilloscope <Bitmap>(
                          canvas_constructor: canvas_constructor,
                          point_drawer_constructor: multi_point_drawer_constructor,
                          graph_producer: ghost_parallel_graph_producer,
                          control_panel: panel);
                buffer     = drived_ghost_parallel_oscilloscope.Buffer;
                save_count = drived_ghost_parallel_count;
                timer      = new Timer(o => DequeueAndSave(buffer, base_path + "Drived_GhostParallel\\"), null, 20, 10);
                drived_ghost_parallel_oscilloscope.Start(10_000);
                Thread.Sleep(RUNNING_TIME_MILLISCEOND);
                drived_ghost_parallel_oscilloscope.End();
                Thread.Sleep(2_000);
                timer.Dispose();
                drived_ghost_parallel_count = save_count;
#endif

                ConstructorTuple <IPointDrawer> single_point_drawer_constructor
                    = new ConstructorTuple <IPointDrawer>(typeof(OvalPointDrawer), 360, 360);
                IGraphProducer serial_graph_producer = new SerialProducer(4321, graph_color);
                DrivedOscilloscope <Bitmap> drived_serial_oscilloscope
                    = new DrivedOscilloscope <Bitmap>(
                          canvas_constructor: canvas_constructor,
                          point_drawer_constructor: single_point_drawer_constructor,
                          graph_producer: serial_graph_producer,
                          control_panel: panel);
                buffer     = drived_serial_oscilloscope.Buffer;
                save_count = drived_serial_count;
                timer      = new Timer(o => DequeueAndSave(buffer, base_path + "Drived_Serial\\"), null, 20, 10);
                drived_serial_oscilloscope.Start(10_000);
                Thread.Sleep(RUNNING_TIME_MILLISCEOND);
                drived_serial_oscilloscope.End();
                Thread.Sleep(2_000);
                timer.Dispose();
                drived_serial_count = save_count;

#if GHOST_PRODUCER_NEED
                IGraphProducer ghost_serial_graph_producer = new GhostSerialProducer(4321, 50, graph_color);
                DrivedOscilloscope <Bitmap> drived_ghost_serial_oscilloscope
                    = new DrivedOscilloscope <Bitmap>(
                          canvas_constructor: canvas_constructor,
                          point_drawer_constructor: single_point_drawer_constructor,
                          graph_producer: ghost_serial_graph_producer,
                          control_panel: panel);
                buffer     = drived_ghost_serial_oscilloscope.Buffer;
                save_count = drived_ghost_serial_count;
                timer      = new Timer(o => DequeueAndSave(buffer, base_path + "Drived_GhostSerial\\"), null, 20, 10);
                drived_ghost_serial_oscilloscope.Start(10_000);
                Thread.Sleep(RUNNING_TIME_MILLISCEOND);
                drived_ghost_serial_oscilloscope.End();
                Thread.Sleep(2_000);
                timer.Dispose();
                drived_ghost_serial_count = save_count;
#endif

                IGraphProducer   simple_graph_producer = new SimpleProducer(4321, graph_color);
                ICanvas <Bitmap> canvas             = canvas_constructor.NewInstance();
                IPointDrawer     oval_single_drawer = new OvalPointDrawer(360, 360);
                TimeCountedOscilloscope <Bitmap> simple_oscilloscope
                    = new TimeCountedOscilloscope <Bitmap>(
                          canvas: canvas,
                          point_drawer: oval_single_drawer,
                          graph_producer: simple_graph_producer,
                          control_panel: panel);
                DateTime start_time = DateTime.Now;
                buffer     = new ConcurrentQueue <Bitmap>();
                save_count = simple_count;
                timer      = new Timer(o => DequeueAndSave(buffer, base_path + "Simple\\"), null, 20, 10);
                while (true)
                {
                    DateTime now_time   = DateTime.Now;
                    TimeSpan delta_time = now_time - start_time;
                    if (delta_time.TotalMilliseconds >= RUNNING_TIME_MILLISCEOND)
                    {
                        break;
                    }
                    Bitmap output = simple_oscilloscope.Draw();
                    buffer.Enqueue(output);
                }
                Thread.Sleep(2_000);
                timer.Dispose();
                simple_count = save_count;
            }
            Console.WriteLine("drived_total_parallel_count:\t" + drived_total_parallel_count.ToString());
            Console.WriteLine("drived_ghost_parallel_count:\t" + drived_ghost_parallel_count.ToString());
            Console.WriteLine("drived_serial_count:\t" + drived_serial_count.ToString());
            Console.WriteLine("drived_ghost_serial_count:\t" + drived_ghost_serial_count.ToString());
            Console.WriteLine("simple_count:\t" + simple_count.ToString());
        }