# Tuesday, 27 September 2005

Since our beloved husky-German Shephard Saffy passed away in July we’ve been casually shopping for a new dog.  Vikki and I both decided we wanted something smaller, and mellower.  In short, something that will be content to just hang out, and doesn’t take up too much room. 

Last week we found what we were looking for in the form of Carter.  He’s a Chihuahua/terrier mix that we got from the animal shelter.  About a year and a half old, had his shots, neutered, some puppy school.  All is good.  He’s a bit of an escape artist, but when he’s not escaping, he pretty much just wants to hang out with us, and he’s about the same size as our cat.  Perfect. :-)

Pictures to follow.

Tuesday, 27 September 2005 16:08:42 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  | 

I’ll be starting the class I’m teaching this term at OIT tomorrow night.  Introduction to Web Services.  There’s still time to get in on it.  CST 407.

Tuesday, 27 September 2005 16:04:41 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 
# Tuesday, 23 August 2005

I’m not actually dead.  Here I am posting.  I’ve been doing a lot of design work lately, and have reached a generally introspective period, so not posting much lately. 

That said…

I was trying to come up with a way to make asynchronous requests from a web server to a back end system.  Like really asynchronous.  Fire and forget, no socket open on the network, etc.  Maybe implemented as two one-way web services, whatever.  The glaringly obvious fact is that given the nature of HTTP, the web client has to block, since an HTTP request/response is by its very nature synchronous and blocking.

So, the solution I came up with was to do the blocking in the one place where it matters, which is in the web client.  It depends on sending some kind of correlation ID between client and server, and back.  So sending the message from ASP.NET might look like this:

    public WaitHandle Send(Envelope env)

    {

      WaitHandle h = new AutoResetEvent(false);

      string correlation = Guid.NewGuid().ToString();

      env.CorrelationID = correlation;

      lock(correlationToWaitHandle.SyncRoot)

      {

        correlationToWaitHandle.Add(correlation, h);

      }

 

      ThreadPool.QueueUserWorkItem(new WaitCallback(DoSending), env);

 

      return h;

    }

 

This will do an async send to your back end, keeping track of a WaitHandle by the correlation ID.  That way, you hand the WaitHandle back to the client, who can wait on it as required by HTTP.

In the mean time, the DoSending method has gone and sent the Envelope to your back end.  When it’s done, it sends the response back to the client machine by whatever means (I’m using ASMX web services) and calls the PostResult method with the response…

    public static void PostResult(Envelope env)

    {

      if(correlationToWaitHandle.Contains(env.CorrelationID))

      {

        AutoResetEvent are = null;

        lock(correlationToWaitHandle.SyncRoot)

        {

          are = (AutoResetEvent)correlationToWaitHandle[env.CorrelationID];

          if(are == null)//this shouldn't happen, but would be bad.  Other option is to check Contains again after lock.

            throw new InvalidOperationException("Threading problem!  CorellationID got removed before lock!");

        }

        lock(waitHandleToResult.SyncRoot)

        {

          waitHandleToResult[are] = env;

        }

        are.Set();

      }

      else

      {

        throw new ArgumentException("unknown corellationID","env");

      }

    }

Look up the wait handle associated with the correlation ID, since we need it, and it’s all the client has at this point, then take the response we got back from the server and store it against the wait handle.  Finally, signal the wait handle, so the client will no we’re done.  When that happens, the client will stop waiting, and call the Receive method to get the results.

    public Envelope Receive(WaitHandle wh)

    {

      Envelope result = null;

 

 

      if(waitHandleToResult.Contains(wh))

      {

        lock(waitHandleToResult.SyncRoot)

        {

          if(waitHandleToResult.Contains(wh))

          {

            result = (Envelope)waitHandleToResult[wh];

            wh.Close();

            waitHandleToResult.Remove(wh);

          }

        }

        lock(correlationToWaitHandle.SyncRoot)

        {

          correlationToWaitHandle.Remove(result.CorrelationID);

        }

      }

      else

      {

        throw new InvalidOperationException("No response received.");

      }

     

      return result;

 

    }

We look up the stored result by the wait handle, clean up our hashtables, and return the result to the client.

For the sake of simplicity, we can go ahead and wrap this in a synchronous method for the client to call.

    public void SendAndWait(ref Envelope env)

    {

 

      WaitHandle h = Send(env);

      if(h.WaitOne(TimeSpan.FromSeconds(90.0),true))

      {

        env = Receive(h);

      }

      else

      {

        throw new RequestTimeoutException("Timed out waiting for server");

      }

 

    }

That way all the wait handle stuff is hidden, and the client just sees their response come back in the [ref] Envelope in a synchronous fashion. 

By decoupling the client from the server this way, we not only get better control over how many threads are running in our server side implementation (although there are other ways to do that) but we also aren’t dependent on the ability to hold open a network socket from client to server during the time it takes to process the request.  Normally that’s not an issue, but it can be a problem in certain circumstances. 

Tuesday, 23 August 2005 14:39:05 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, 11 August 2005
My sister Erica delivered my first nephew yesterday afternoon.  Connor James Mullen, 7 lb. 15 oz.  Woo hoo!  Everyone is doing beautifully.
Thursday, 11 August 2005 11:02:43 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 
# Tuesday, 19 July 2005
There is a grass roots initiative, begun in the UK, called the ICE (In Case of Emergency) initiative.  Both the goal and the implementation are very simple.  Take a moment and add some additional entries to your cell phone / PDA addressbook that start with ICE.  Examples are “ICE – wife”, or “ICE – Mom”.  That way, first responders who are coming to your aid can quickly determine from your phone (assuming it’s working of course, but it couldn’t hurt) who to call on your behalf.  It takes only a moment, and could save a lot of time and hassle in case of emergency.
Tuesday, 19 July 2005 10:51:47 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, 18 July 2005

For around 2 years now, I’ve been involved with my local CERT (Community Emergency Response Team) program.  I took the initial training a little over 2 years ago, and since they I’ve been to several refresher classes and exercises, and joined the Steering Committee as the newsletter editor.  For those not familiar with CERT, it’s a nation-wide program that teaches people how to prepare for disasters, and how they can help themselves, their families, and their neighborhoods in times of disaster or emergency.  Our local program in Hillsboro is open to anyone who lives or works in Western Washington County, and involves 24 hours of training (3 hours, one night a week for 8 weeks).  The training includes disaster medical training, light search & rescue, team organization, and other important preparedness skills.

This past weekend I got a chance to go to the Western Territorial Citizens Corps Conference in beautiful Bozeman, Montana.  It was a great conference, and I learned a lot about the overall organization that is the Citizens Corps, which was really worthwhile.  I also got a chance to see a bit of Bozeman, which I really enjoyed.  What a gorgeous town!  We also got a chance to hit the Museum of the Rockies, which has a truly amazing collection, including some of the best dinosaur exhibits I’ve seen.

Citizens Corps is the (relatively) new umbrella organization under which the national CERT program is housed, along with four other “partner” programs. 

All of these organizations provide opportunities for the average citizen to volunteer some of their time to better prepare themselves and their communities for disasters and emergencies, as well helping out and saving a lot of money for our frontline first responders.  For examples, volunteers for the Fire Corps can do things like help cook at fire stations, or do paperwork which frees up firefighters for the work they are best qualified to do. 

Check out the websites and find out how you can help.  It’s every American’s responsibility to be prepared.  And if anyone has questions about the CERT program, I’d be happy to answer them.

Monday, 18 July 2005 15:29:47 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 
# Monday, 11 July 2005

Cheb i Sabbah is an Algerian-born San Franciscan who makes some truly incredible music.  I’m a big fan of “Asian Underground”, which is a relatively recent genre that mixes modern electronica/dance music with traditional South Asian/Middle Eastern music.  I just finished listening to Cheb i Sabbah’s latest album, “La Kahena”, and it ROCKS!  It’s all centered around the traditional music of the Berbers of North Africa.  Fantastic.  It’s amazing how such ancient music blends seamlessly with modern electronica. 

Other great Asian Underground acts include State of Bengal, Billy Sagoo, Panjabi MC, Asian Dub Foundation, and Talvin Singh.  All worth a listen.

Monday, 11 July 2005 14:33:59 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  | 
# Friday, 08 July 2005
I’ll be teaching Introduction to Web Services (CST 407) at OIT (Portland) this Fall.  Tell your friends!  We’ll be covering the basics of Web Services, including theory, history, best practices, and a firm grounding in underlying technologies like XML and SOAP.  Should be a good time.  If you are interested you should be prepared to write code in C#.
Friday, 08 July 2005 16:21:38 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 

Over the holiday weekend we lost our 11-year-old Husky/German Shephard, Saffy (short for Saffron).  She went quickly, and didn't suffer, which is all we could have hoped for.  We can only hope that wherever she is now that the sun is warm, and the squirrels are slow.  She'll be missed.

Saffy

Friday, 08 July 2005 08:07:38 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [0]  | 
# Thursday, 30 June 2005

I finally solved the namespace issue I was having, although I’ll probably burn for all eternity for the solution.  In short, because of the behavior of XmlTextWriter, the only solution that could be implemented in a reasonable amount of time was to post-process the XML and strip out the extra namespace declarations. 

So I started down the path of using XmlTextReader to spin through and collect up all the namespaces that I needed, then add those to the root node.  After that I could use a regular expression to strip out all the unneeded ones.  Turns out I had overlooked the fact that the input isn’t guaranteed to be well-formed XML.  :-(

The “XML” is actually a template that our system uses to do some tag replacement.  So the output of that process is well-formed, but the input can contain the “@” character inside element names.  A no-no according to the XML spec. 

So here it is, the all-regular-expression solution.  I wouldn’t suggest you try this at home, but it does actually work, and seems to be quite fast (sub 1/4 second for a 1.5Mb input, and the typical input is more like 10K). 

Note: this is made a little simpler because I know (since I just wrote out the “XML”) that all the namespace prefixes we care about start with ns, e.g. ns0, ns1, etc.

                    #region begin hairy namespace rectifying code here

                    //this is necessary because the XmlTextWriter puts in more namespace

                    //declarations than we want, which causes file bloat.

 

                    Regex strip = new Regex(@"xmlns\:ns\d=""[^""]*""");

                    ArrayList names = new ArrayList();

                    MatchCollection matches = strip.Matches(result);

                    foreach(Match match in matches)

                    {

                        string val = match.Value;

                        if(!val.StartsWith("xmlns:ns0"))

                            if(!names.Contains(match.Value))

                                names.Add(match.Value);

                    }

 

                    string fixedNamespaces = null;

                    StringBuilder sb = new StringBuilder();

                    foreach(string name in names)

                    {

                        sb.AppendFormat(" {0}",name);

                    }

 

                    fixedNamespaces = result;

 

                    int pos = fixedNamespaces.IndexOf(">",0);//should be the end of the xml declaration

                    pos = fixedNamespaces.IndexOf(">",pos+1);//should be end of root node.

 

                    fixedNamespaces = fixedNamespaces.Insert(pos,sb.ToString());

 

                    pos = fixedNamespaces.IndexOf(">",0);//should be the end of the xml declaration

                    pos = fixedNamespaces.IndexOf(">",pos+1);//should be end of root node.

                    result = strip.Replace(fixedNamespaces, "", -1, pos);

 

                    #endregion

XML
Thursday, 30 June 2005 12:53:56 (Pacific Daylight Time, UTC-07:00)  #    Disclaimer  |  Comments [1]  |