Rants, rambles, news and notes from another geek

OpenPGP.NET

A while back I posted about an OpenPGP for .NET implementation called SharpPrivacy. Until recently I hadn’t had much time to go over the code with a serious eye. I spent 4 or 5 hours this weekend going through it and although it is fairly complete, the code isn’t structured very well. Additionally there is a bunch of copied straight├é from the mono source that is redundant with the code in System.Security,Cryptography.

So, all of that said, I decided to start my open OpenPGP implementation in C#. I’m going to use as much CLR provided functionality as possible to minimize code size. I’m also going to be implementing it in a test-driven fashion, so it should have a nice rich set of checked examples (aka unit tests).

I will probably use the SharpPrivacy code base as a reference at the beginning but since that project is GPL (not LGPL) I don’t want to include any of its code in this project. I want this project to be open source, but not as restricted as GPL projects (e.g. commercial use). Anyone want to suggest a licence? I kind of like the Artistic License and the BSD License.

I’m thinking about naming the project OpenPGP.NET, but that domain name is taken. That isn’t really too important, but it would be nice to have a place to park this project if it goes well. Suggestions?

Also, I’d love to have another person to work on this project with me. I’m not going to release the code to the public until I feel it is actually usable, but I would love to have another person or two to code with. If you’re interested, drop me an email.

PDC

A while back I posted that I would not be attending the PDC because it was too expensive (I was essentially self-employed at the time).

Well my new employer has decided to send me! Yippee! I don’t think they signed me up for the PreCon sessions, which is too bad, but the rest should be good.

I will be actively blogging during the conference so keep an eye on this site.

Â

Fun With WSE 2.0

My boss has a demo coming up where he wanted to have a little private wireless network (not Internet connected) and he wanted to be able to send IM-like messages to all of the machines. He wanted the little “piece of toast” popup and to be able to send an arbitrary piece of text and a hyperlink.

Also, I’ve had a couple of WSE2 articles and MSDN TV downloads in my “To Be Reviewed” folder for a while now… why not use WSE2 to build this IM thingy?

I decided to use the WSE 2.0 Messaging API, which is very easy to use. You can literally send a message in less than 10 lines…

private void sendButton_Click(object sender, System.EventArgs e)
{
    Uri destination = new Uri("soap.tcp://" +
        machineNameTextBox.Text + ":8888/WseMessenger");
    SoapSender soapSender = new SoapSender(destination);
    SoapEnvelope envelope = new SoapEnvelope();
    envelope.Context.Action = "Receiver";
    XmlElement body = envelope.CreateBody();
    body.InnerText = messageTextBox.Text;
    soapSender.Send(envelope);
}

Pretty slick eh? Receiving is a little more complicated only in that you need to derive a new class from SoapReceiver:

using Microsoft.Web.Services;
using Microsoft.Web.Services.Messaging;
using System;
public delegate void MessageReceivedEvent( object sender,   
                        SoapEnvelope envelope );
  
public class MyReceiver : SoapReceiver
{
    public event MessageReceivedEvent MessageReceived;
    protected override void Receive(SoapEnvelope envelope)
    {
        if( MessageReceived != null )
            MessageReceived( this, envelope );
    }
}

Then from my Receiver Form I just add MyReceiver to the SoapReceivers collection and subscribe to the event:

private void Form1_Load(object sender, System.EventArgs e)
{
    MyReceiver receiver = new MyReceiver();
    receiver.MessageReceived += 
        new MessageReceivedEvent(receiver_MessageReceived);
    SoapReceivers.Add( "soap.tcp://" + 
        System.Net.Dns.GetHostName() +
        ":8888/WseMessenger", receiver );
}

Easy as pie! So now the question is… when will WSE 2.0 ship?

PS. This was all moot of course. I came into the office this morning and we decided to go with Microsoft Office Live Communication Server instead. Since we were running on a private little demo network anyway, why not? It was still fun to play around with the Messaging API.

PPS. If you are looking for a “piece of toast” control for C#/.NET, check out TaskbarNotifier by John O’Byrne. It is fully skinnable and very easy to use.

Snippet Compiler 1.6 Released

I can’t believe how fast Jeff is busting out releases of Snippet Compiler. This one has a colored syntax editor and much more. This is certainly the coolest little tool I’ve added to my arsenal lately.

HttpFilter for Removing Whitespace

Donny Mack posted an interesting idea. Add an HTTP Filter to strip out tabs and CRs. I wonder how much it will shrink the average page? I just might try it on a few of my sites.

One thing to keep in mind though, his algorithm is doing a naive replacement of whitespace and carriage returns. The problem is if you have Javascript that isn’t semicolon delimited. Since it isn’t required, many developers don’t bother. So check your client-side script before using this.

The Agile Hacker

Bill Burris has started a page on his site called The Agile Hacker. His goal is to bring agile development techniques to solo development. As a guy who has had to wear a solo agile hat many times in the past, it looks very very interesting.

Multiple Return Statements

I was involved in a code review the other day with a colleague and we got into a discussion about having multiple return statements in a method. He felt that it was almost always bad, and I felt that it depended on the code in question. My goal is to produce code that is easy to understand and has a minimal amount of duplication.

I think I know where this rule came from, and in some languages it makes sense. In C, C++, and COM, for example, you often have a method structure that looks something like this:

function DoSomething()
{  
    // Allocate some stuff  
  
    // Do something  
    // Release the stuff  
    // Return a value
}

In this case, you want to make sure that you always release the memory that is allocated at the top. So by having developers always drop through to the return statement at the end, you help them to not forget. This actually makes the code easier to understand. It also produces code with less duplication.

But in languages with garbage collection (like C# and Java), you don’t often need to do this. Notice I didn’t say never. Even in GC systems, we still sometimes have to clean up after ourselves (e.g. IDisposable and COM proxies) but for the most part you don’t have to.

So remember, a rule like this was originally created for a reason. But that doesn’t mean you should follow it like a religious fundamentalist. Use your brain and decide when it is appropriate and when it isn’t.

PS. If you run across code that was written with massively nested if statements to enforce this rule, take a look at the Replace Nested Conditional With Guard Clauses refactoring.

Full Featured Argument Parser

Via The Daily Grind 168:

Full-featured Automatic Argument Parser - Another command-line parsing class for .NET. This one makes good use of attributes to set things up in a natural way.

Very nice. That is a slick way of dealing with that problem. I’ve seen a bunch of different approaches to handling command-line arguments, but that attribute stuff wins the elegant award.

XML Schema

I finally got around to watching the Don Box SOA videos (here and here) that were recently posted to MSDN TV. Having just gone through the XSD Tutorial that I mentioned earler, I was very interested to hear him talk about RELAX NG.

From the RELAX NG home page:

RELAX NG is a schema language for XML. The key features of RELAX NG are that it:

  • is simple
  • is easy to learn
  • has both an XML syntax and a compact non-XML syntax
  • does not change the information set of an XML document
  • supports XML namespaces
  • treats attributes uniformly with elements so far as possible
  • has unrestricted support for unordered content
  • has unrestricted support for mixed content
  • has a solid theoretical basis
  • can partner with a separate datatyping language (such W3C XML Schema Datatypes)

Hmmmm… interesting. I have to agree with Don that XSD is a beast of an abstraction that is probably much more that we really need, but whatcha gonna do?

How about write your XML Schemas in RELAX NG and convert them to XSD using Trang?

I’ll have to give it a try…