Archive for the 'Development' Category

The Evolution of Generics in C# 4.0

Before I begin, let me point out that I'm primarily writing this post to solidify my own understanding of the new generic structures and put it into my own words, there are already some great posts that already explain this in much greater depth and detail: (Generic Variance in C# 4.0 by Discord & Rhyme, What's the difference between covariance and assignment compatibility? By Eric Lippert)

So that being said, let start off with the problem that generics solve in the first place. (If you have a good understanding, you can jump to the new features in C# 4.0)

C# Prior to 2.0

Lets say I have the following class hierarchy:

    public abstract class Fruit { }
 
    public class Apple : Fruit { }
 
    public class Orange : Fruit { }
 
    public class FruitBasket
    {
        public Fruit[] Fruit { get; set; }
    }
 
    public class Tree
    {
        public Fruit[] PickFruit()
        {
            Fruit[] daFruit = new Fruit[2];
            daFruit[0] = new Apple();
            daFruit[1] = new Orange();
            return daFruit;
        }
    }

We have a hierarchy of objects, a base Fruit class, an Apple class, which is a Fruit, and an Orange class, which is also a Fruit. In addition, we have a FruitBasket which has an enumerable of Fruit, and a Tree which I can "PickFruit()" from.

Now, lets say I want to have an instance of a fruit basket, however, I also want another one that deals with only Oranges (I don't want it to even be possible to put apples in the basket) so that I can make orange juice, apples just ruin a good glass of orange juice. I have two possibilities, I can create another fruit basket and ASSUME that I only put oranges in, or I create another classes that only allows oranges to be inserted in. Because I decide that I want the compiler to absolutely not allow apples in with the oranges, I now have to create a new class:

    public class OrangeBasket
    {
        public Oranges[] Oranges { get; set; }
    }

Or if I now want to have a basket that holds potatoes, I have to build another class:

    public class PotatoBasket
    {
        public Potato[] Potatos { get; set; }
    }

Right. Pattern. We are repeatedly creating virtually identical objects that simply contain other objects or that apply some sort of processing to those specific elements, all because we want the consumer (the person that is using this object) to be able to put an specifically typed object in and get a specifically typed object out, without having to cast it.

C# 2.0 - 3.5

With the release of C# 2.0 Microsoft introduced this concept of "generics" in programming. It allows programmers to create generalized algorithms that take and receive specific objects, store or process them, and return them, without knowing the creator of the object being worked on or knowing specific type. C# 2.0 also included a number of generic collections and interfaces that implemented these features, the most useful in my mind being the generic IEnumerable<T> interface. This now allows us to rewrite our basket class like follows:

    public class Basket<T>
    {
        IEnumerable<T> Contents{ get; set; }
    }

Now, instead of having a PotatoBasket, a FruitBasket, and a OrangeBasket we can replace it like so while still using the same class:

    Basket<Potato>
    Basket<Fruit>
    Basket<Orange>

And our Tree class now becomes:

    public class Tree
    {
        public IEnumerable<Fruit> PickFruit()
        {
            yield return new Apple();
            yield return new Orange();
        }
    }

So now, we have a Basket of Potatos, a Basket of Fruit, and a Basket of Oranges. Yet, we didn't have to duplicate the classes, and any the implementations still preserves type safety.

This is where it gets… Interesting.

Lets say I have an instance of a Tree class, and an instance of my Basket<Fruit> class. Now, it’s simple to do an assignment like this:

    var fruitBasket = new Basket<Fruit>();
    var Tree = new Tree();
    fruitBasket.Contents = tree.PickFruit();

BUT if I have a tree that has a PickApples() method like this:

    public IEnumerable<Apples> PickApples()
    {
        yield return new Apple();
        yield return new Apple();
        yield return new Apple();
    }

And I attempt to do the same assignment as before but with the PickApples() method instead, C# 3.5 will not compile it because the generic parameters are not the same:

    var fruitBasket = new Basket<Fruit>();
    var Tree = new Tree();
    fruitBasket.Contents = tree.PickApples();

That sucks. Why?

The reason has to do with the relationships of types, covariance / contra-variance / invariance (Erick Lipperts post on the difference between covariance and assignment compatibility is a much better source for it's relation with mathematics and type hierarchies). With versions prior to 4.0 the C# compiler does NOT allow generics of one type to be assigned to generics of another type. Thus, an IEnumerable<Apple> cannot be assigned to a variable of IEnumerable<Fruit>. But lets say we were allowed to do this, why would it be a problem? Lets say I have an IList<Fruit> and an IList<Apple>; it seems to make perfect sense that I could so the following assignment:

    var fruitList = new List<Fruit>();
    var appleList = new List<Apple>();
    fruitList = appleList;

Right?

Wrong.

This assignment would actually be plausable for IEnumerable<T> because IEnumerable<T> is immutable, it can not change, there's no way to "Insert" a new element into an IEnumerable. However, with an IList<T> it defines an Add(T item) method. An IList can be altered. In the previous example, if I were to take fruitList and look at the Add method in intellisense, it would show that I can insert any object that is a fruit into the add method of the fruitList, however, fruitlist isn't technically a list of fruits anymore given this senario, its a pointer to a List<Apple>. So in this flawed example, I could now call fruitList.Add(new Orange()); which would now be a runtime error because I can't insert an Orange into a List<Apple>. Bad. (In C# however, there is an edge case where this sort of error can occur with arrays. For instance, if you have a object[] objarray = new string[10]; you can then assign a fruit to one of the slots and cause a runtime exception. For more detail on this, see Erick Lipperts post on Covariance and Contravariance in C# arrays) So we have two things that should be solved. We know that there are certain situations where IEnumerable<X> should be assignable to IEnumerable<T>, specifically when X is a subclass of T, but we also realize that something like IList<X> should not be assignable to a variable of IList<T>.

C# 4.0

Enter variance modifiers for generic types that have now been introduced in C# 4.0.

out and in.

First, these can only be applied to generic type parameters of interfaces and delegates, in can only be applied to generic parameters that are contra-variant or invariant valid, and out can only be applied to generic parameters that are co-variant or invariant valid.

Gulp.

First, invariance, contra-variance, and covariance. In terms of types, if I have T1 and T2, those types are Invariant if they are the same type. For instance, a Fruit and a Fruit are invariant, a Fruit and an Apple is not invariant even though a Fruit variable can hold an Apple object.

Co-variant is where the inheritance chains are kept, if I have T1 and T2, the projection of T1 to T2 is covariant if T2 is lower in the inheritance chain than T1. For instance, a Fruit to Apple is Co-variant because Apple is an instance of Fruit.

Contra-variant is where the inheritance chain is reversed, if I have T1 and T2, the projection of T1 to T2 is contra-variant if T2 is higher up the inheritance chain than T1. For instance, an Apple to Fruit is contra-variant because Apple is an instance of Fruit, the relationship is reversed, flipped.

So, it allows me to do the following in C# 4.0 with the new variant structure:

    var fruitBasket = new Basket<Fruit>();
    var Tree = new Tree();
    fruitBasket.Contents = tree.PickApples();

Because IEnumerable is defined as follows:

    IEnumerable<out T> { /* .. */ }

Remembering that out is co-variant because it can return the item or a subclass of that item, in this case PickApples() is returning an IEnumerable that is lower in the inheritance chain than the variable it's being assigned to (by lower, I mean that its a subclass, or sub-sub..n class of the other object)

To demonstrate a class with contra-variance with an in parameter, lets say we have an interface and classes like so:

    IPieMaker<in T> where T : Fruit
    {
        Pie MakePie(IEnumerable<T> fruits);
    }
 
    ApplePieMaker : IPieMaker<Apple> { /* ... Some implementation ...*/ }
    FruitPieMaker : IPieMaker<Fruit> { /* ... Some implementation ...*/ }

Now, because the input parameter is contra-variant, I can have a variable declaration like so:

    IPieMaker<Apple> applePieMaker = new FruitPieMaker();

Wait. Seem odd? Because T is not specifying what Pie we are making only what is put IN to make the pie, I can put apples into a FruitPieMaker. It’s contra-variant.

Try and wrap you head around that :)

Note: There's a good possibility that I didn't accurately describe the terms covariance and contra-variance in relation to mathematical projection and ordering, corrections and better descriptions are greatly appreciated.

Additional Links:

Native Glass Windows with WPF in Windows 7 / Vista

One of the awesome benefits of working at InterKnowlogy is that we get time to do what we call RECESS: Research and Experimental Coding to Enhance Software Skills. It’s a 4 hour time span where we can work on interesting technologies to enhance, grow, and keep up on the latest technologies and methodologies. It keeps us sharp, interested, and many of our demos have come from these short code jams.

Over the past few weeks I’ve wanted to figure out how to add or extend glass into my application like you see in Word (2010 preview):

Glass

As you can see, the whole title bar area is seamlessly integrated into the look and feel of windows, it feels native, its got that cool semi-transparent blur-the-background effect and everything. So come RECESS I did some research and pieced together what you’d need to get an effect like this.

DesktopWindowManagerAPI.cs
 
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Codelogic.Windows.Native.APIManagedExceptions;
 
namespace Codelogic.Windows.Native
{
    public static class DesktopWindowManagerAPI
    {
        public static void AllGlassWindow(this Window window)
        {
            ExtendFrameIntoClientArea(window, new Thickness(-1), false);
        }
 
        public static void ExtendFrameIntoClientArea(Window window, Thickness thickness)
        {
            ExtendFrameIntoClientArea(window, thickness, false);
        }
 
        public static void ExtendFrameIntoClientArea(Window window, Thickness thickness, bool exceptionOnFail)
        {
            var compEnabled = IsCompositionEnabled();
            if (exceptionOnFail && !compEnabled)
                throw new DWMNotEnabledException();
 
            if (exceptionOnFail && !window.IsInitialized)
                throw new WindowNotLoadedException();
 
            if (!compEnabled) return;
 
            var margins = thickness.ToDWMMargins();
            var windowPointer = new WindowInteropHelper(window).Handle;
 
            //convert the background to nondrawing
            var mainWindowHwnd = HwndSource.FromHwnd(windowPointer);
            if (mainWindowHwnd != null)
                mainWindowHwnd.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
 
            try
            {
                DwmExtendFrameIntoClientArea(windowPointer, ref margins);
            }
            catch (DllNotFoundException)
            {
                window.Background = Brushes.White;
            }
        }
 
        public static bool IsCompositionEnabled()
        {
            try
            {
                return DwmIsCompositionEnabled();
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 
        #region WPF to Native
 
        private static DWMMargins ToDWMMargins(this Thickness t)
        {
            var rtrn = new DWMMargins();
 
            rtrn.Top = (int)t.Top;
            rtrn.Bottom = (int)t.Bottom;
            rtrn.Left = (int)t.Left;
            rtrn.Right = (int)t.Right;
 
            return rtrn;
        }
 
        #endregion
 
        #region Native Interop
 
        [StructLayout(LayoutKind.Sequential)]
        private struct DWMMargins
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }
 
        /// <summary>
        /// Extends an hwind's frame into the client area by the specified margins.
        /// </summary>
        /// <param name="hwnd">Integer pointer to the window to change the glass area on.</param>
        /// <param name="margins">Margins, what to set each side to</param>
        [DllImport("dwmapi.dll", PreserveSig = false)]
        private static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref DWMMargins margins);
 
        /// <summary>
        /// Checks to see if the Desktop window manager is enabled.
        /// </summary>
        [DllImport("dwmapi.dll", PreserveSig = false)]
        private static extern bool DwmIsCompositionEnabled();
 
        #endregion
    }
}
 

Alright, lets begin breaking this down.

DesktopWindowManagerAPI.cs
 
        [StructLayout(LayoutKind.Sequential)]
        private struct DWMMargins
        {
            public int Left;
            public int Right;
            public int Top;
            public int Bottom;
        }
 
        [DllImport("dwmapi.dll", PreserveSig = false)]
        private static extern void DwmExtendFrameIntoClientArea(IntPtr hwnd, ref DWMMargins margins);
 
        [DllImport("dwmapi.dll", PreserveSig = false)]
        private static extern bool DwmIsCompositionEnabled();
 

First, Windows 7 and Vista provide us with the Desktop Window Manager which was first included in Vista and continues on into Windows 7, it manages all the cool graphical windowing and effects you see in those operating systems. It also gives us an API dll to access these all the features, including many that are unavailable in WPF.

The first declaration you see sets up the data type that the DLL uses internally to represent Left, Right, Top, and Bottom glass margins. The others are pointers to unmanaged (non .NET) methods in the DLL. DwmExtendFrameIntoClientArea is the method that allows me to adjust how far in the glass extends in to the client drawable area, and DwmIsCompositionEnabled tells me if Aero Glass is enabled.

DesktopWindowManagerAPI.cs
 
        private static DWMMargins ToDWMMargins(this Thickness t)
        {
            var rtrn = new DWMMargins();
 
            rtrn.Top = (int)t.Top;
            rtrn.Bottom = (int)t.Bottom;
            rtrn.Left = (int)t.Left;
            rtrn.Right = (int)t.Right;
 
            return rtrn;
        }
 

A simple extension method (denoted by the ‘this’ in front of the Thickness t, it allows me to write the declaration as though the method were part of the Thickness class, so if I have a thickness variable thick I could convert it to a DWMMargins type by writing var margins = thick.ToDWMMargins();) it converts a WPF Thickness object to the internal DWMMargins struct.

DesktopWindowManagerAPI.cs
 
        public static void ExtendFrameIntoClientArea(this Window window, Thickness thickness)
        {
            ExtendFrameIntoClientArea(window, thickness, false);
        }
 
        public static void ExtendFrameIntoClientArea(this Window window, Thickness thickness, bool exceptionOnFail)
        {
            var compEnabled = IsCompositionEnabled();
            if (exceptionOnFail && !compEnabled)
                throw new DWMNotEnabledException();
 
            if (exceptionOnFail && !window.IsInitialized)
                throw new WindowNotLoadedException();
 
            if (!compEnabled) return;
 
            var margins = thickness.ToDWMMargins();
            var windowPointer = new WindowInteropHelper(window).Handle;
 
            //convert the background to nondrawing
            var mainWindowHwnd = HwndSource.FromHwnd(windowPointer);
            if (mainWindowHwnd != null)
                mainWindowHwnd.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
 
            try
            {
                DwmExtendFrameIntoClientArea(windowPointer, ref margins);
            }
            catch (DllNotFoundException)
            {
                window.Background = Brushes.White;
            }
        }
 
        public static bool IsCompositionEnabled()
        {
            try
            {
                return DwmIsCompositionEnabled();
            }
            catch (DllNotFoundException)
            {
                return false;
            }
        }
 

Finally I wrapped native methods with .NET versions that take a more useful WPF Window class and WPF Thickness class for the ExtendFrameIntoClientArea method. Internally it checks to make sure the window is initialized and that Desktop Composition is enabled, gets the integer pointer to the window, resets the background, and then calls the native method to extend the glass into the drawable (client) area of the window. Two custom classes not shown are the DWMNotEnabledException class and the WindowNotLoadedException class, which are thrown if something goes wrong.

That’s all well and good, but wouldn’t it be nice if we didn’t have to worry about all these calls to this custom DesktopWindowManagerAPI class and could just set how much we wanted the glass to extend into the client area? Or bind it to something so that the glass area expands or contracts when a value changes?

I thought so:

GlassWindow.cs
 
using System.Windows;
using Codelogic.Windows.Native;
 
namespace Codelogic.Controls.WPF
{
    public class GlassWindow : Window
    {
         #region Glass Thickness Dependency Property
 
        public static readonly DependencyProperty GlassThicknessProperty = DependencyProperty.Register(
            "GlassThickness", typeof(Thickness), typeof(GlassWindow), new PropertyMetadata(new Thickness(0, 0, 0, 0), GlassThicknessChanged));
 
        //when the thickness changes, apply the change to the window.
        private static void GlassThicknessChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((GlassWindow)d).UpdateGlassState();
 
        }
 
        /// <summary>
        /// Local property for Glass thickness.
        /// </summary>
        public Thickness GlassThickness
        {
            get { return (Thickness)GetValue(GlassThicknessProperty); }
            set { SetValue(GlassThicknessProperty, value); }
        }
 
        public static readonly DependencyProperty IsAllGlassProperty = DependencyProperty.Register("IsAllGlass",
                                                                                                   typeof(bool),
                                                                                                   typeof(GlassWindow),
                                                                                                   new PropertyMetadata(
                                                                                                       OnIsAllGlassChanged));
 
        private static void OnIsAllGlassChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ((GlassWindow)d).UpdateGlassState();
        }
 
        public bool IsAllGlass
        {
            get { return (bool)GetValue(IsAllGlassProperty); }
            set { SetValue(IsAllGlassProperty, value); }
        }
 
        private void UpdateGlassState()
        {
            if (!IsInitialized) return;
 
            if (IsAllGlass)
                this.AllGlassWindow();
            else
                this.ExtendFrameIntoClientArea(GlassThickness);
        }
 
        static GlassWindow()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(GlassWindow), new FrameworkPropertyMetadata(typeof(GlassWindow)));
        }
 
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);
            UpdateGlassState();
        }
    }
}
 

Alright, this class creates a WPF Window subclass, adds a dependency property for GlassThickness and a change handler that internally calls the DesktopWindowManagerAPI.ExtendFrameIntoClientArea if the window is loaded or attaches an event handler if the window is not loaded. Now all you have to do is change your window class over to a GlassWindow class, set the thickness and you rock and roll!

GlassDemoWindow.cs
 
<WPF:GlassWindow x:Class="Codelogic.Controls.WPF.Demo.GlassDemoWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WPF="clr-namespace:Codelogic.Controls.WPF;assembly=Codelogic.Controls.WPF"
    Title="Glass Window Demo"
    Height="300"
    Width="300"
    GlassThickness="10000">
    <Grid>
        <InkCanvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="Transparent" />
    </Grid>
</WPF:GlassWindow>
 

Glass Ink

Something I have not yet done that I would like to do is figure out how to add functional buttons into the title-bar of an application. If you remember the Microsoft Word snippet from above there’s save / undo / redo buttons in the title bar. But that will have to be a later post.

Hope someone enjoyed my Glassy exploration,

Updates:

  • 2 Dec 2009
    • Changed ExtendFrameIntoClientArea to check to see if window.IsInitialized instead of window.IsLoaded
    • Updated GlassWindow to override OnSourceInitialized instead of adding an event handler for Loaded
    • Added in a method that turns the entire client area into glass by setting the margins to -1.
    • Changed Glass Window to have a Boolean Dependency property to turn it all glass.

- Paul Rohde

My Neumont Educational History Part II

After the great response I got from my previous post on my Neumont Educational History (http://www.paulrohde.com/my-neumont-educational-history/) Several e-mail, comments, and questions later, I’ve decided it’s time for a batch of Q & A to answer additional questions. I recently received an e-mail from a student that had found my website through the Neumont website, I feel it sums up many of the questions I have been asked since my previous post:

Mr. Rohde,

I am currently a high school senior at [omitted]. I am currently in the process of searching for colleges, and Neumont has presented itself to me as being a very unique school for computer science. Programming has been my passion since a very young age, and I hope to go to a college that has a reputable computer science program. While on the Neumont website, I happened to stumble across your blog. To be completely honest with you, your website blew me away. Your fusion of stylistic art and coding are breathtaking- I hope to one day be able to create the same sort of digital art that you have.

That said, I had a few questions regarding Neumont that can only be answered by an alumni. Neumont strikes me as being almost a technical school due to the nature of its program, and although the 2 year degree is definitely a bonus, I do wonder if they cut out the more “traditional” topics thus harming what would otherwise be a well-rounded education. Do you believe that they sacrificed subjects, or were classes such as literature unnecessary because they did not feed into a science degree? I also read on your blog that you debated between Neumont and Winona; were you considering any other schools at that point? How did you hear about Neumont?

Regarding Neumont’s atmosphere, I read about the classes that you took, but you did not particularly go into detail about the campus life. What did you like about Neumont? Were there any glaring drawbacks? How was the food?
Lastly, the administration and cost. I have read from various sources that the administration was difficult to come in contact with; especially the financial aid officers. I’m not sure as to their accuracy, so I figured a former student would be a prime source of information. Secondly, were you offered much of a financial aid package? Ultimately, my college decision depends largely on a financial aid package, and information surrounding how Neumont goes about choosing how to distribute their money is rather vague. Do you believe that Neumont was affordable? Do you still have any debts? Do they have an unduly high interest rate?

Since Neumont is a fairly new school with a small population, articles regarding Neumont are few and far between. As such, I believe that a former student’s opinion on the matter would be greatly influential in my decision. Any input that you could give me would be greatly appreciated.

Thank you for your time,

- Steven

First, many thanks for the awesome comments about the site, its one of those ongoing projects that is continually evolving and changing, so its appreciated when someone does enjoy it.

Neumont is not round.

Ok. Sentimental comments aside, let me do the best I can to answer your questions. Since I am a Neumont Alumni, do keep in mind that the school has been evolving over the past several years and that there have been many alterations to the main program since I started (Cohort 15, July 2007). So take what I say with a grain of salt. Get in touch with one of the Neumont Reps if you haven’t already since they *should* be able to answer more direct questions about the current state of the school and the primary CS degree program.

From my experience, Neumont is a very technical school, it has one degree (There was previously a business masters degree program at the school, but that has been put on hold and may or may not re-emerge), and it is very geared towards graduating high quality and hopefully well rounded CS students. Because the program is so tightly geared toward CS there are some areas and classes that I personally felt were lacking. For instance, I really wish that the mathematics program went further, by the time I started at Neumont I had already completed Calculus II at WSU (Winona State University) and was beyond the current mathematics courses offered at Neumont. In addition, they do lack some of the general classes that form the basis for other fields of study: Chemistry, higher level Mathmatics, Physics, History, Literature, Art(To be fair, they did have an introduction photography class, and the last two quarters when I was leaving they were adding in a digital media concentration with courses that are more artistically bent). Part of my own draw away from Neumont was the fact that I wouldn’t have the opportunity to grow in some of these other areas, I love photography, art, the more classical sciences, and there’s a part of me that really does miss not having more time to explore those other subjects for my own personal interest.

The question about well rounded-ness really boils down to this: Why are you going to school? If your goal is to get a degree, get a full time job in business software development, I honestly cannot recommend a better school. However, if your not sure you want to do business software, if your not sure you want to even DO software, or if your not passionate enough to pursue and devote yourself to it for the two and a half years it’ll take to get through the program, Neumont is probably not the place for you.

Hearing about Neumont

I first heard about Neumont sometime after taking my ACT a year early and getting piles of junk mail from colleges talking about the ‘college decision’ I would be making over the next year.

Right.

After two years of highschool at a private school I returned to being officially ‘home schooled’ after my sophomore year, enrolled in WSU via the PSEO Program (Post Secondary Education Option, basically, if your a good enough student and would like to take classes at the local college the state will pay for the tuition and books), and then spent the following year and a half taking a full load of college classes (plus two side classes at an online school the year I started). Sometime within that college span I began receiving mail from Neumont, glanced and discarded most of it into the large bin of college spam. After receiving several more mailings I did some research, ended up being contacted by a Neumont rep, and was invited to attend their ‘Geek Weekend’ where they flew out potential new students that were interested in the school along with a parent or guardian to visit, meet some of the students, etc… That trip was the turning point in my decision making process. During my time at WSU I was actively taking some CS classes, most of them in Java, most little console algorithm type problems and one cool little robotics project near the end in one of my classes. After a year and a half of WSU, and having had several years of my own personal experience doing web, C++, Visual Basic, Dark Basic, building several of my own small games, and working as a student programmer at WSU doing C#, ASP.NET, and Sharepoint development, I was completely outmatched by students in their second quarter at Neumont (6 months into the program) that I met that weekend. That was cool.

Yes, it was a hard decision, I had a good job for where I was, I could have continued at WSU, graduated with a lot of current job experience (Read: ~4 years if I had graduated from WSU), but it wouldn’t have challenged me as much or pushed me as far as I am today.

The Campus

First of all, Neumont is not a normal school. They are located in an office building that’s been converted into the University as it is today. In fact, there are other companies that have offices in the same building, at least when I was there. Since I’ve been there they’ve made some additions and changes to the building, there’s now a new computer lab complete with iMacs, the common area has a pool table and a large screen TV that students will bring in and hook up their 360, Wii, or PS3 to and play games. The school does have a reasonable catering service during lunch on school days, but nothing like a normal cafeteria. When I was leaving they were experimented with having another place come in on Fridays, so I’m not entirely sure as to the state of the school provided food. From what I’ve experienced however, most students will eventually move away from the catered food and get something nearby (which is what I eventually did), there is a Zupas across the the street and a coffee shop within walking distance, and there’s quite a few additional places a short drive away.

The campus “Housing” is two sets of apartments, one set is at The Falls at Hunters Pointe, the other is Sterling Village in South Jordan, both are about a 20 minute walk from the school (although most people tend take the shuttle or carpool). I was only in student housing for a year, when I first moved in, one of my roommates was also just moving in and the other was close to graduating. During second quarter after the senior in our house moved out we got a new freshman, who ended up moving out without telling any of us before the end of the quarter… Anyhow, after that first ~10 months a friend of mine and two other guy’s rented a 4 bedroom 3 story house for about what we were paying in student housing, so, I ditched Neumont housing and moved. As far as rules and regulations go for student housing (Yes, they are regulated) they aren’t too bad, you do need to keep the apartment clean and they do have inspections. You also can’t bring weapons into the apartment (no paintball guns, swords, non-kitchen-knives, etc…).

As far as ‘life on campus’ goes you have lots and lots of geeked out guys (And about a 50:1 guy to girl ratio) that live life on Mountain Dew, Guitar Hero, WoW, and DotA. They are the stereotype, and you will find them on almost every non Neumont campus you visit, but you’ll find a much higher concentration than you would elsewhere. However, there are many people there that really do take what they’re doing seriously, and are already well rounded individuals. I was never that ‘involved’ in the geek culture. It’s cool, I like parts of it, I’ve rocked my share of Guitar Hero and Rock Band, and played my hours of Counter Strike. Still, I spent a lot of my time outside of Neumont towards the end of my time there. Most of who I considered my ‘good’ friends were up at the University of Utah and were not CompSci majors. I was involved in the photographic community, I did several photowalks, met lots of photographers and models, made some good friends there. I was involved in a church (Gateway Community Church), and had several friends through that. The community is out there, but you have to go to it, it’s not going to drop into your lap as much as it would at a “normal” University.

If you make good friends, keep in touch, work hard, help and teach others (not just giving answers), and really strive to be a personable, well rounded individual, you will be fine. The homework can be hard, and there are ALWAYS group projects that you’re in and will be working on. It’ll force you to deal with and work with other people and figure out how to make things work. From personal experience, it’s exactly what you’ll be doing in the real world.

Administration and Cost

I’m probably not the best person to answer a lot of these questions, but I’ll do what I can. Administration from my experience hasn’t been that difficult to get in touch with, I was always able to go to people and figure out what I needed when I needed it. However, I do know that in the past, people have had issues, specifically with tracking graduation credits. There was (or is) a student written program that allows you to track your progress towards graduation that I used, and it worked well. However, I would also go in and talk directly with the student advisers on a regular basis and verify that I was doing things correctly. There have been several occasions where people have been 1-2 credits short because they thought they were taking enough credits but didn’t take the time to go and check with advisers each quarter…

As far as finances go, I was able to get my loans through the Department of Education and Sallie Mae with my parents as co-signers on those loans. With the economy having tanked, and quite possibly still in the tank, I’m not sure how difficult it will be to get financing, it’ll really depend on your situation. I was lucky enough to not have to worry about it much or have deal with the financial aid department at school. I do know that if I had not received loans or financing, I would have had a VERY difficult (if not impossible) time going to Neumont. Again, this is where I would get a Rep, ask very specific, very direct questions. Make sure they give you an answer, that’s what their job is.

Was Neumont affordable? I made it through. I will be able to pay it off with my current job over the next ~3 years, and my interest rate seems rather standard as far as student loans go.

Was it a lot? Yes.

Was it worth it? For the field I’m in, Yes. For the contacts I made, the experience I gained, Yes.

Wrap Up

Neumont is a great school, for business software development. For what the school is and at the time I went through the program, I don’t know of any other degree program that could trump it. It does have a very narrow focus, it’s hard, it’ll make you learn, and you’ll be better for it. Like any school, you only get out of it what you put into it and there will always be people that don’t care, don’t work, fail out, and complain to all ends of the earth about how it’s the university’s fault or how this or that caused them to _____. Fill in the blank.

Work hard, do your best and you’ll be fine wherever you’re at.

If anyone has any questions, feel free to leave a comment, call me, send me an e-mail, I’ll do my best to get back to you when I can.

- Paul Rohde

Note: The opinions presented here are my own and do not in any way represent the opinion or position of my Employer or that of Neumont University.

What I do at Interknowlogy

I get asked this question a lot:

“So, what exactly do you do at your job?

The most common answer is usually me fumbling through what a surface is, multi-touch, and ending with something about consulting, cool projects, and NDA agreements. They say a picture is worth a thousand words. So, since a video is between 30-60 frames per second, that’s gotta be about a book right there, so I figured I’d give you a taste of what I do at my main full time job.

http://www.interknowlogy.com

A bit of background on the demo; You may remember this post about building a really simple P2P network, and is the foundation of what we were eventually building out here. The framework itself is built out into three distinct layers, each independent of each other and interchangeable. For instance, the demo here is showing a P2P network layer, which is very nice for small demos because there is very little configuration required. However, because this layer is independent of the others it would be trivial to write an enterprise level sever based implementation that could be used to conduct sessions anywhere in the world.

The second layer is the framework that we’ve built up to handle the logic of managing a remote session, adding / removing remotable objects, synchronization, passing through commands, and routing commands from the network up to the individual remotable objects. The final layer is the remoting piece individual controls. It’s a layer of logic that is attached onto existing controls. The cool factor of the way this is built is it allows for a huge degree of flexibility at a control level if you need it, but also the power of being able to drop in a few lines of code and have objects magically start controlling and presenting.

Hope you enjoyed it, and let me know what you think!

- Paul Rohde

Building a REALLY simple WCF P2P application

Often times when I’m looking at playing with a new technology that it becomes extremely difficult to find a simple stripped down easy-to-use chunk of code that also walks through, in-depth, the concepts and reasoning behind the code. This particular code chunk began as I started exploring the Microsoft WCF’s P2P networking services and found a rather distinct lack of code that explained the hows and whys of building a Peer to Peer network.

P2P Basics

Ok. Low level building blocks.

First, everybody (who’s interested in P2P) should already understand the client server paradigm, if not, there are many wonderfully detailed articles to explain the details. In short we have N clients all connected to 1 server. Client A sends a message to the server, the server decides what to do, and then may or may not send a response to Client A and / or Client B, Client C… etc… In a peer to peer network on the other hand, everyone is the client, everyone is a server. Because of this, the client / server naming scheme goes away by popular vote and everyone is called a node or a peer. When a node is connected to other nodes that whole group is called a mesh (or graph, or cloud).

Now, in a pure P2P network there is no central authority to help or govern how nodes find each other to form a mesh or how meshes are initially created.  Every node is connected to some number of other nodes, which are then connected to more nodes and so on. When one node wishes to communicate with another node (or nodes) the message is first passed on to the nodes that the first node knows about. These nodes in turn pass along the message on to other nodes that they know about and so on until finally everybody has seen the message.

One of the best, and probably most used, examples of a peer to peer network is the chat room. Until someone creates it, the chat room does not exist, but once it’s created people can be invited, join, and send messages that appear on everybody else’s screen. Even if the original person leaves the chat room, the room still exists as long as there are participants logged in. Once the last person leaves the chat room no longer exists.

Ping – The P2P Application

Note: I really dislike configuration files in demos or tutorials unless it’s a large application or showing how a particular aspect of a configuration file works. The fact that I can tell a static factory to build me something with a string name that corresponds to another file that somehow gets found, loaded, and happens to reference compiled type that then has a hidden class generated that implements that interface and is passed, bugs me when I’m trying to learn something.

If your going to follow along with this, your going to need .NET 3.5 installed and be running XP SP3, Vista, Win7 or Server 08. Visual Studio doesn’t hurt either.

To set up your project crack open Visual Studio and spin up a new Console Application, call it SimpleP2PExample. Once you have that open go over to the project, right click and Add Service Reference to System.ServiceModel, this allows you to use .NET’s WCF stuff in your app. You can choose to split up each class or interface into its own file or not: Up to you.

IPing
    //Contract for our network. It says we can 'ping'
    [ServiceContract(CallbackContract = typeof(IPing))]
    public interface IPing
    {
        [OperationContract(IsOneWay = true)]
        void Ping(string sender, string message);
    }

Alright, first of all, attributes. If you don’t know what they are, then here’s the low down mouthful one line explanation:

Attributes are essentially binary metadata associated with a class, method, property or whatever that provides additional information about whatever it’s “Decorating”.

The first attribute is the service contract. Wait a second. Contracts.

In our node-talking-to-other-nodes scenario, somehow they have to know how to talk to each other, if I asked what the size of the door was and you handed me a window, I have NO idea what that means or what that represents. A contract defines exactly what I’m telling you, what I’m expecting back, how, and when.

In this case we’re defining a contract that has one operation, a method called Ping. We know that when node A talks to node B that if node A says “Hey, Ping(“MyName”, “Hello.”) to Node B that node B will know what to do with that and how to pass it along to other nodes. It’s also specifies that I don’t expect Node B to give me anything back.

Now, the implementation.

PingImplementation
    //implementation of our ping class
    public class PingImplementation : IPing
    {
        public void Ping(string sender, string message)
        {
            Console.WriteLine("{0} says: {1}", sender, message);
        }
    }

Fairly simple, whenever we receive a ping from another node, this method will be executed.

The Peer Class

Alright, now the fun, magic, and games begin. We’re going to create a class called peer, which will contain all our service start / stop code and also hold our implementation of PingImplementation.

Peer
    public class Peer
    {
        public string Id { get; private set; }
 
        public IPing Channel;
        public IPing Host;
 
        public Peer(string id)
        {
            Id = id;
        }
    }

In order to identify an individual node in the network, so that we know who’s who and don’t get everything mixed up, it’s customary to have a unique Id that’s assigned to the peer. Now, we have two IPing variables, the best way to describe them would be incoming and outgoing. An instance of the PingImplementation class will go in Host since it will be receiving any incoming communication from other nodes; The Channel is used to communicate out to other nodes and is built up via a factory.

Peer
    public void StartService()
    {
        var binding = new NetPeerTcpBinding();
        binding.Security.Mode = SecurityMode.None;
 
        var endpoint = new ServiceEndpoint(
            ContractDescription.GetContract(typeof(IPing)),
            binding,
            new EndpointAddress("net.p2p://SimpleP2P"));
 
        Host = new PingImplementation();
 
        _factory = new DuplexChannelFactory(
            new InstanceContext(Host),
            endpoint);
 
        var channel = _factory.CreateChannel();
 
        ((ICommunicationObject)channel).Open();
 
        // wait until after the channel is open to allow access.
        Channel = channel;
    }
    private DuplexChannelFactory&lt;IPing&gt; _factory;

This is what we will use to start the peer.  This is the part where I’ve built up what could have been done in the configuration file with code instead.

Lets take it from the top.  First off, we have our binding; this defines what communication protocol we are going to be using, and as this is a PeerToPeer app… we use NetPeerTcpBinding().  You’ll also notice that in the next line I set the security mode to none; this is done for simplicities sake. There’s three types of security modes, including None, Message, Transport and TransportWithMessageCredential. Its a bit beyond the scope of this post, but Message security ensures that the message was not tampered with as it was passed from peer to peer, Transport security ensures that the connection between nodes is secure, and TransportWithMessageCredential does both.

Now, our application needs an endpoint, essentially, an service endpoint is a set of information that is exposed outside of the application (in this case on the network as well) so that others can access it. The endpoint defines the address it can be reached at, the contract, and the method that should be used to communicate. In this case, we build up our endpoint by using ContractDescription.GetContract to generate a contract class off of our IPing interface, our network type binding, and an endpoint address where this endpoint can be reached.

Finally, we create a new instance of our PingImplementation class as the Host, and we create our channel factory. A DuplexChannelFactory allows for two way communication, the first parameter is the object that you want to receive incoming calls, and the endpoint is where those calls are coming from. The factory then creates the channel and a whole bunch of magical things happen.

If you’ll remember, our channel is of type IPing, the Duplex Factory performs some magic and generates a concrete implementation of your interface (it also implements ICommunicationObject, which is why you’ll sometimes see people create another interface called something like “IPingChannel : IPing, ICommunicationObject”, it does make it so that you don’t have to cast it, but for the purpose of this post it’s not necessary). Imagine that it takes your interface, implements all the methods and properties with all the cool DuplexChannel code needed to talk back and fourth, creates an instance, and returns it to you.

Finally, I call open on my channel to let the world see that my brand new channel and endpoint are ready for business.

Peer
    public void StopService()
    {
        ((ICommunicationObject)Channel).Close();
        if (_factory != null)
        _factory.Close();
    }

Now, it’s all well and good, until your done. Then you need to close you channel and factory, this should be pretty self explanatory at this point.  Remember our channel is an ICommunicationObject in addition to being a IPing object, so we cast and close, then check to see if our factory is null, and if not, close that as well.

Threading The Peer Class

Something I chose to do was make the peer threaded. This allows me to drop it into an application, in a thread, and receive and push stuff into it at my leisure. To do this I add in a:

Peer
    private readonly AutoResetEvent _stopFlag = new AutoResetEvent(false);

This will allow me to block a method of the thread until I decide fire it (When I stop the peer).

Peer
    public void Run()
    {
        Console.WriteLine("[ Starting Service ]");
        StartService();
 
        Console.WriteLine("[ Service Started ]");
        _stopFlag.WaitOne();
 
        Console.WriteLine("[ Stopping Service ]");
        StopService();
 
        Console.WriteLine("[ Service Stopped ]");
    }
 
    public void Stop()
    {
        _stopFlag.Set();
    }

The run method embodies the lifecycle of this peer. When the peer thread starts into the run method it will start the service, wait until the stop flag is fired (when the Stop() method is called), and then stop and dispose the service.

Putting It All Together
Program
    class Program
    {
        static void Main(string[] args)
        {
            if (Process.GetProcessesByName(Process.GetCurrentProcess().ProcessName).Count() &lt;= 1)
            {
                for (int i = 0; i &lt; 4; i++)
                {
                    Process.Start("SimpleP2PExample.exe");
                }
            }
 
            new Program().Run();
        }
 
        public void Run()
        {
            Console.WriteLine("Starting the simple P2P demo.");
 
            var peer = new Peer("Peer(" + Guid.NewGuid() + ")");
            var peerThread = new Thread(peer.Run) {IsBackground = true};
            peerThread.Start();
 
            //wait for the server to start up.
            Thread.Sleep(1000);
 
            while (true)
            {
                Console.Write("Enter Something: ");
                string tmp = Console.ReadLine();
 
                if (tmp == "") break;
 
                peer.Channel.Ping(peer.Id, tmp);
            }
 
            peer.Stop();
            peerThread.Join();
        }
    }

From top to bottom, the Main method of the application checks to see if it’s the first one of this application to have started up, if it its, then it starts four additional processes (Note that if you called your project something other than SimpleP2PExample, you will need to replace the the string in Process.Start to be the name as the executable file your project generates).

The run method is also fairly simple, it creates a new instance of the Peer class, assigns it a GUID, creates a thread for the peer’s run method, starts up the thread and pauses to wait for the peer’s thread to start.  We then enter a loop until the user presses enter without inputting any text. Any text that is put in is transmitted over the peer channel using the peer’s id and the message. Once we’ve finished, we stop the session and wait for the peerThread to exit and join back up with the main thread. We then exit.

Wrap Up

I hope this helps someone out there get a better understanding of the basic concepts of a simple Peer to Peer application. Feel free to leave feedback!

Paul Rohde