Click here to Skip to main content
15,896,269 members
Articles / Web Development / ASP.NET

Legion: Build your own virtual super computer with Silverlight

Rate me:
Please Sign up or sign in to vote.
4.87/5 (139 votes)
27 Oct 2008LGPL321 min read 423.3K   1.1K   335  
Legion is a grid computing framework that uses the Silverlight CLR to execute user definable tasks. It provides grid-wide thread-safe operations for web clients. Client performance metrics, such as bandwidth and processor speed, may be used to tailor jobs. Also includes a WPF Manager application.
//  This code file for the WPF Realtime Line Graph control was developed
//  by Andre de Cavaignac and Daniel Simon at Lab49.
//
//  The code in this file can be freely used and redistributed in applications
//  providing that this file header is maintained in files relating to the
//  line graph control.
//
//  2007, Andre de Cavaignac and Daniel Simon
//
//  Lab49 Blog:
//      http://blog.lab49.com
//  Andre de Cavaignac's Blog:
//      http://decav.com
//  Andre de Cavaignac's Blog Article on this Control:
//      http://decav.com/blogs/andre/archive/2007/08/25/live-updating-line-graph-in-wpf.aspx
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Decav.Windows.Controls.LineGraph
{
    /// <summary>
    /// The main window of the application that displays a series of tickers.
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();

            // Use the random ticker adapter to generate securities and ticks.

            // NOTE: Change these properties to see how the tickers will react
            // under various scenarios.
            TimeSpan virtualTimePerSecond = TimeSpan.FromDays(1);
            _adapter = new RandomTickerAdapter(
                virtualTimePerSecond,
                TimeSpan.FromSeconds(0.5),
                true,
                false);

            _adapter.SecurityAdded += new EventHandler<SecurityAddedEventArgs>(Adapter_SecurityAdded);
            _adapter.TickerAdded += new EventHandler<TickerAddedEventArgs>(Adapter_TickerAdded);
            
            // Begin our simulation.
            _adapter.Start();

            // Bind the slider at the bottom to all the graph durations.  The max value of the slider will be the
            // virtual time that occurs every second times 120.
            _adapter.SetBinding(TickerAdapter.GraphDurationProperty,
                new Binding("Value") { Source = TimeSlider, Converter = new DoubleToTimeSpanConverter(), ConverterParameter = TimeSpan.FromSeconds(virtualTimePerSecond.TotalSeconds * 120) });
        }

        private TickerAdapter _adapter;
        private int _currentTickerCount;

        public int CurrentTickerCount
        {
            get
            {
                return _currentTickerCount;
            }
            set
            {
                _currentTickerCount = value;
            }
        }

        public int TotalRows
        {
            get
            {
                return TickerPanel.RowDefinitions.Count - 1;
            }
        }

        public int TotalColumns
        {
            get
            {
                return TickerPanel.ColumnDefinitions.Count;
            }
        }

        public int TotalTickerCount
        {
            get
            {
                // -1 because of our slider at the last row.
                return (TotalRows * TotalColumns);
            }
        }

        /// <summary>
        /// Occurs when a security is added to the adapter (From the datasouce).  Here we will add
        /// a ticker for our new security.
        /// </summary>
        /// <remarks>
        /// We don't add a ticker per security because some datasources have thousands of securities.
        /// </remarks>
        void Adapter_SecurityAdded(object sender, SecurityAddedEventArgs e)
        {
            if (CurrentTickerCount >= TotalTickerCount)
                return;

            _adapter.CreateTicker(e.Security);
        }

        /// <summary>
        /// Occurs when a ticker is added to our adapter so that we can add it to the form and display it.
        /// </summary>
        void Adapter_TickerAdded(object sender, TickerAddedEventArgs e)
        {
            TickerPanel.Children.Add(e.Ticker);

            int curRow = CurrentTickerCount / TotalColumns;
            int curCln = CurrentTickerCount - (curRow * TotalColumns);

            e.Ticker.SetValue(Grid.RowProperty, curRow);
            e.Ticker.SetValue(Grid.ColumnProperty, curCln);

            // Stretch to fill the cell.
            e.Ticker.Height = Double.NaN;
            e.Ticker.Width = Double.NaN;
            e.Ticker.HorizontalAlignment = HorizontalAlignment.Stretch;
            e.Ticker.VerticalAlignment = VerticalAlignment.Stretch;
            e.Ticker.Margin = new Thickness(0,0,6,6);

            CurrentTickerCount++;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Engineer
Switzerland Switzerland
Daniel is a former senior engineer in Technology and Research at the Office of the CTO at Microsoft, working on next generation systems.

Previously Daniel was a nine-time Microsoft MVP and co-founder of Outcoder, a Swiss software and consulting company.

Daniel is the author of Windows Phone 8 Unleashed and Windows Phone 7.5 Unleashed, both published by SAMS.

Daniel is the developer behind several acclaimed mobile apps including Surfy Browser for Android and Windows Phone. Daniel is the creator of a number of popular open-source projects, most notably Codon.

Would you like Daniel to bring value to your organisation? Please contact

Blog | Twitter


Xamarin Experts
Windows 10 Experts

Comments and Discussions