Saturday, November 29, 2008

Ninject MessageBroker Update

It has been a long time (10 months) since I worked with the Ninject MessageBroker and a couple things have changed since my last post. In the old version you had to connect the MessageBroker as a kernel component by hand. Since then the extension environment has been flushed out a lot more. Now you can register the MessageBrokerModule when constructing your kernel object.



using System;
using System.Diagnostics;
using System.Net;
using System.Text.RegularExpressions;
using Ninject.Core;
using Ninject.Extensions.MessageBroker;

namespace NinjectMessageBroker
{
internal class Program
{
private static void Main()
{
// Intialize our injection kernel adding message broker functionality.
using (var kernel = new StandardKernel(new[] { new MessageBrokerModule() }))
{
// Get the event publisher. It reads the current time and fires an event
var pub = kernel.Get<TimeReader>();
Debug.Assert(pub != null);

// Get the subscriber, it waits to get the current time and writes it to stdout
var sub = kernel.Get<TimeWriter>();
Debug.Assert(sub != null);

// Verify that they were wired together
Debug.Assert(pub.HasListeners);
Debug.Assert(sub.LastMessage == null);

// Get the current time. It should automatically let the TimeWriter know
// without either of them ever knowing of one another.
pub.GetCurrentTime();

// Wait to exit.
Console.ReadLine();
}
}
}

internal class TimeWriter
{
public string LastMessage { get; set; }

[
Subscribe("message://Time/MessageReceived")]
public void OnMessageReceived(object sender, EventArgs<string> args)
{
LastMessage = args.EventData;
Console.WriteLine(LastMessage);
}
}

internal class TimeReader
{
public bool HasListeners
{
get { return (MessageReceived != null); }
}

[
Publish("message://Time/MessageReceived")]
public event EventHandler<EventArgs<string>> MessageReceived;

/// <summary>
///
Gets the current time and updates all subscribers.
/// </summary>
public virtual void GetCurrentTime()
{
string text = GetWebPage();
var regex = new Regex(@"\d\d:\d\d:\d\d");
MatchCollection matches = regex.Matches(text);
string time = ((matches.Count == 2) ? matches[1] : matches[0]).Value;
SendMessage(time);
}

/// <summary>
///
Gets the contents of a web page as a string.
/// </summary>
/// <returns></returns>
private static string GetWebPage()
{
const string url = "http://www.time.gov/timezone.cgi?Eastern/d/-5";
var webClient = new WebClient();
return webClient.DownloadString(url);
}

/// <summary>
///
Sends the message to all subscribers in a threadsafe manner.
/// </summary>
/// <param name=
"message">The message.</param>
public void SendMessage(string message)
{
EventHandler<EventArgs<string>> messageReceived = MessageReceived;

if (messageReceived != null)
{
messageReceived(this, new EventArgs<string>(message));
}
}
}

public class EventArgs<TData> : EventArgs
{
public new static readonly EventArgs<TData> Empty;

static EventArgs()
{
Empty = new EventArgs<TData>();
}

private EventArgs()
{
}

public EventArgs(TData eventData)
{
EventData = eventData;
}

public TData EventData { get; private set; }
}
}

Friday, November 28, 2008

C++ if statement oddity

I found that you can do work such as assigning a variable and executing a method outside of the main expression of an if statement. For example:



    if(hRes = GetApplicationState(), NT_SUCCESS(hRes))
{
// Do something
}



Yes, that is a comma inside of the if statement. To the left of the comma is executed first and then the if statement uses the right side as its evaluation expression.

Open Source Projects

I had two open source projects: Ensurance, and IMAPI.Net.

  • Ensurance did not really fit into any particular group. It was a tool for me while Spec# was still a research project at MS. I wanted to be able to use pre/post conditions, parameter constraints, and tie them into logging and debugging. I don’t think that Spec# will do the last bit, but I am really looking forward to its release.
  • IMAPI.Net is a C# cd authoring library that wraps the IMAPI system in windows to allow programmers of any application to write to CD. It supports data and audio discs. It was expensive to test the library and the COM issues were a huge problem for a while.
    • PInvoke.net was a good help for the most part, but the marshalling took a long time to get right. I started the project back in .net 1.1 while working on my BS - and it worked. With each .NET release the project would have been easier to write with the COM interop support increasing. I used to be the lead of the XPBurn component project on GotDotNet before it shut down; in truth though, I had stopped supporting it a couple months before that. No one would read directions, read the forum, follow any kind of guidelines and would essentially expect me to figure out why they are using the code wrong. It became too much hassle in the end for what I got out of the project. It was a great learning experience though.

Both projects are essentially dead and I will be closing down the Ensurance  project on CodePlex. I will leave IMAPI.Net up on google code in case anyone ever finds use for the code. If I get the time I will try to add my PInvoke code to the wiki so others don’t have to work as hard as I did.

Friday, June 13, 2008

Yet Another Video Update (YAVU)

Here is the latest PLAYXPERT video demo. While playing the game the user:

  • Starts playing a song.
  • Searches the web for a video.
    • Web browser
    • AJAX
    • FLASH
  • Opens the Friends widget and initiates an IM session
  • Continues playing the game the whole time.

STAY IN GAME!

Friday, May 2, 2008

PLAYXPERT in Action

Although this blog is in no way associated with my employer, I feel I need to post this. It is a video of PLAYXPERT running in game. For many years I had wished there was a product like this, and now I get to be part of the team that brings it to life :)

Wednesday, January 16, 2008

Ninject Message Broker

I updated my local copy of Ninject today and decided to try out the message broker. I opened the test fixture to see how it is used and wrote up this little test.  Overall I am very happy with how this simple demo worked out. I am going to try to set aside some time to try something more in depth. This simple example has one object getting the current time from the Internet and then publishes the new time it received. The second object listens for the time to be updated and then writes it to the console. What I really dig is that the two objects never know of one another.

 

internal class Program
{
private static IKernel _kernel;

private static void Main(string[] args)
{
// Intialize our injection kernel
_kernel = new StandardKernel();

// Add message broker functionality.
_kernel.Connect<IMessageBroker>(new StandardMessageBroker());

// Get the event publisher. It reads the current time and fires an event
TimeReader pub = _kernel.Get<TimeReader>();
Debug.Assert(pub != null);

// Get the subscriber, it waits to get the current time and writes it to stdout
TimeWriter sub = _kernel.Get<TimeWriter>();
Debug.Assert(sub != null);

// Verify that they were wired together
Debug.Assert(pub.HasListeners);
Debug.Assert(sub.LastMessage == null);

// Get the current time. It should automatically let the TimeWriter know
// without either of them ever knowing of one another.
pub.GetCurrentTime();

// Wait to exit.
Console.ReadLine();
}
}

internal class TimeWriter
{
private string _lastMessage;

public string LastMessage
{
get { return _lastMessage; }
set { _lastMessage = value; }
}

[Subscribe("message://Time/MessageReceived")]
public void OnMessageReceived(object sender, EventArgs<string> args)
{
_lastMessage = args.EventData;
Console.WriteLine(_lastMessage);
}
}

internal class TimeReader
{
public bool HasListeners
{
get { return (MessageReceived != null); }
}

[Publish("message://Time/MessageReceived")]
public event EventHandler<EventArgs<string>> MessageReceived;


/// <summary>
/// Gets the current time and updates all subscribers.
/// </summary>
public virtual void GetCurrentTime()
{
string text = GetWebPage();
Regex regex = new Regex(@"<b>\d\d:\d\d:\d\d<br>");
Match match = regex.Match(text);
string time = match.Value.TrimStart("<b>".ToCharArray()).TrimEnd("<br>".ToCharArray());
SendMessage(time);
}

/// <summary>
/// Gets the contents of a web page as a string.
/// </summary>
/// <returns></returns>
private static string GetWebPage()
{
string url = "http://www.time.gov/timezone.cgi?Eastern/d/-5";
HttpWebRequest webRequest = WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = "GET";
using (WebResponse webResponse = webRequest.GetResponse())
{
using (StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8))
{
return sr.ReadToEnd();
}
}
}

/// <summary>
/// Sends the message to all subscribers in a threadsafe manner.
/// </summary>
/// <param name="message">The message.</param>
public void SendMessage(string message)
{
EventHandler<EventArgs<string>> messageReceived = MessageReceived;

if (messageReceived != null)
{
messageReceived(this, new EventArgs<string>(message));
}
}
}

Tuesday, December 11, 2007

Visual Studio 2008 InstallFest

I will be attending the Visual Studio 2008 InstallFest in Spokane Valley on Wednesday the 12th with a few other deve lopers from PLAYXPERT. It will be nice to meet some other .NET coders in the Spokane area. Hopefully we can pick something up soon (better SIGS). I was really looking forward to attending the Agile Philly and Philly ALT.NET group meetings before the move to Spokane.

Settling In

Well, I have been in Spokane for a week now. I still feel like I am just settling in. My car finally shipped (thanks to MN for helping) and should arrive this week. The contents of my apt may not be here until Christmas, so it will be a little rough until then. The weather the last two weekends has been as bad as the worst I saw in Philadelphia (and it is only December). I wish I could find reliable information on what are the best winter tires.

Monday, November 12, 2007

SC07: It Begins

All around you will find Intel trying to make waves

Celebrating the 45nm Launch - 45 steps to Technology Leadership

Ok. So they made 45nm, what's next? With the physical limit a few nm away (from what I understand, 30nm is the limit), what are they going to shock and awe us with next?

Everyone is trying to get the final touches finished on their booths while eagerly awaiting the food coming in just 5 minutes. After an hour of eating, we will have a half hour break before the VIPs get their privileged two hour private run on the show floor. From what I hear, D-Wave is claiming to have the first quantum computer presented here, but we will have to see about that.

Saturday, November 10, 2007

News

It has been too long since my last post, for which there are a number of reasons.

  1. Super Computing 2007
  2. Vacation Preparation
  3. New Job
  4. Moving

I head to Reno, Nevada early tomorrow morning for SC07. I have been trying to get my research ready for presentation.  Hopefully this year will be as fun and interesting as last.

The day after I get back from Reno, my girlfriend and I are flying to Aruba for ten days.

There is a lot of preparation when I will essentially be gone for fifteen days. This should be a nice break compared to the bedlam to come.

I resigned.  When I get back from Aruba, I will be working my last week as a Software Developer for NAVTEQ. I will be moving 2529 miles from Philadelphia to Spokane, WA to take the role of Software Quality Assurance Director for PlayXPert.

I will be doing software architecture, software development, developer management, project management, system administration, continuous integration, QA management, and other tasks.

Along with the new job comes moving. I work for one week, during which I need to pack every morning and night ( as I will be gone for the two weeks prior ) which leaves no room for anything. I start Monday, December 3rd.

This come as a stumbling block for my Ensurance project on CodePlex. It is stable, fully featured, and usable, but I will not be able to work on it for a month which really pains me. The only pieces missing are the distribution tasks such as strong naming, finishing internationalization, and such. I was really looking forward to implementing the full set of fluent parameter attributes, but there is a minimal implementation right now.

I will try to post from SC07 with anything that I find.