Archive for March, 2010

Solution to Ayende’s Challenge

March 30, 2010 Leave a comment

I have always wanted to solve this puzzle Ayende posted on his blog, but haven’t got around it until today. It was a really hard puzzle, but with a little hint from Marc Gravell in the comments I was able to find a solution strategy based on .NET Remoting. Because Ayende’s original challenge was quite a bit harder (and actually impossible to solve, I wish I knew before) I tried messing with the typeof/new operators first. Sadly this isn’t possible and modifying any IL wasn’t allowed. On the other hand I learned a lot about the CLRs type system.

.NET Remoting is really interesting stuff, but to be honest I wouldn’t want to deal with it’s huge complexity in my every day job. I am not really sure which blank it fills in, besides interacting with ugly COM servers. If I compare all this stuff to Objective-C, I do now know about the benefits of  late-bound, message-passing style function calls. Solving this challenge in Objective-C would have been substantially easier (a simple NSProxy plus Method Swizzling, just what OCMock does).

Anyway, you can find my solution on github.

Categories: .NET, CLR

Setting up a Mercurial Repository Server using hgwebdir.cgi on IIS6

March 28, 2010 1 comment

My exams are over now and I will hopefully have enough time to cross some points off my personal to-do list. One thing I have always wanted to do is setting my home server up to let it serve my Mercurial Repositories via http. Given the distributed nature of Mercurial I don’t find myself in the situation to access my repos remotely but it can be quite handy from times to times. In the past I have accessed them via VPN (using OpenVPN), having them sit on a Windows Network Share. Needless to say performance was suboptimal but actually quite acceptable if my internet connection was fast and stable. However, accessing it via 3G is a completely different story.

When Google Code evaluated their plans to support at least one major DVCS, they did a detailed analysis with a special focus on supported protocols and performance. They finally decided to support Mercurial because it has a very efficient HTTP protocol implementation that was a good fit with Google’s Infrastructure. Besides supporting file shares and HTTP, Mercurial does also support SSH, which is not an option for me since my server runs a Microsoft Stack. There’s a good list of supported protocols and sharing methods on the Mercurial Wiki. Since I want to share multiple repositories, I decided to go with the hgwebdir solution. Basically it is just a smallish Python CGI application that can also serve a nice repository browser etc.

Set up is fairly easy, I found Jeremy Skinners tutorial very helpful, even though it’s using IIS 7 (I’m running IIS 6).  What caused me some headaches, was implementing a nice URL rewriting. Because all access is done via hgwebdir.cgi, it’s an ugly part of the URL like so:


The Idea with using a URL rewriting is to let the user access repositories via a clean URL like http://myserver/hg/LDT-trunk/,which is then rewritten to the former. Hgwebdir.cgi supports this method because one can change the way it generates links on the web interface by providing a new base-Url  (/hg instead of /hg/hgwebdir.cgi). This and URL Rewriting for Apache using a .htaccess file is described on the Mercurial Wiki.

To be honest, it’s a fairly optional step but being the perfectionist I am I couldn’t resist to try it with IIS. Jeremy Skinner proposes Microsoft’s official URL Rewrite, but that’s only compatible with IIS 7.  The best tool I could find for IIS 6 is the excellently documented Ionic’s ISAPI Rewrite Filter, which features a very similar and powerful regular expression based configuration as Apache .htaccess. Set up was very fast but getting the configuration right was not very easy and took a little debugging. The IIRF tooling (TestDriver.exe) was really helpful here. Here’s my IIRF.ini for your reference:

# Send requests for files that exist to those files.
RewriteCond %{REQUEST_FILENAME} !-f
# Send requests for directories that exist to those directories.
RewriteCond %{REQUEST_FILENAME} !-d

# prevents processing of already correct urls
RewriteRule ^/hg/hgwebdir.cgi/(.*)$ - [L,I]
# redirect every request to the hgwebdir.cgi script
RewriteRule ^/hg/(.*)$ /hg/hgwebdir.cgi/$1
# redirect root directory access to the website root
RewriteRule ^/hg /hg/hgwebdir.cgi/

Everything was working fine, except the URL rewriting didn’t work out for URL’s that contained encoded Umlauts (like Ü becomes %DC). On top of that, file contents weren’t displayed using the correct encoding (which is UTF-8). The latter was easy to fix by simply uncommenting the first two lines in hgwebdir.cgi that will set the HGENCODING environment variable, which tells Mercurial what encoding to use, to UTF-8:

import os
os.environ["HGENCODING"] = "UTF-8"

Mercurial internally makes no assumptions about the file contents or file types it tracks, so in this case encoding is simply a matter of presentation. All other internal data (changelogs, tags etc.) are stored as UTF-8 according to the wiki. There are still some minor anomalies in the web interface, but I will investigate them later.

The issue with incorrect URL rewritten URL’s was much harder to track down. I used Fiddler to analyse the GET/RESPONSE stream but couldn’t find anything that was obviously wrong. The IIRF log contained the following lines:

Sun Mar 28 16:31:57 -  3260 - DoRewrites: Url (decoded): '/hg/LDT-trunk/file/c2e75c0ef33c/src/Übc.xx'
Sun Mar 28 16:31:57 -  3260 - DoRewrites: Rewrite Url to: '/hg/hgwebdir.cgi/LDT-trunk/file/c2e75c0ef33c/src/Übc.xx'

That looked somewhat unsuspicious to me, until I remembered that special characters in URL’s need to be specially encoded. By default IIRF attempts to decode the URLs when a request is received and does only process the decoded URLs (hence the small hint in the log file). This behavior can easily be configured via the IIRF configuration using UrlDecoding Off.

Categories: Source Control, Tools

New Project: DirectoryVersioningService

March 15, 2010 Leave a comment

My brother asked me the other day whether there’s any software that can keep track of a directories’ content and automatically create a backup on each change. He works at an equipment supplier for events (sound, light and rigging etc.) and they have  a software for managing their inventory and rental business. This software generates a variety of reports using the List&Label report engine, which is driven by report templates that are stored on a network share. The templates that shipped with the software didn’t look nice, nor were they sophisticated enough to capture all the required information, so they find themselves messing with the report templates very often. And from time to time they break something. Figuring out what exactly broke is a time-consuming process and is especially annoying when you sit right next to a customer and simply want to check-in some equipment and give him an invoice.

“This is a perfect use case for Mercurial”, came immediately to my mind. The idea was to have a Windows Service monitor the directory on the network share using File System events and perform a commit on each change. I did a little googling just to check if there’s anyone who’d done this before but didn’t find anything useful. Four solid hours later, I had the first version of my DirectoryVersioningService ready, including a simple GUI to install/uninstall and rename the service, so you can install multiple copies of it in order to monitor different directories. A side effect of this is that I do now know how Windows Services work. Especially the installing and uninstalling process takes a little time to grasp, but it’s easy once you’ve got it.

Each time a change is made to the directory, a timer starts and is set to execute a commit in 5 seconds. This is because operations like moving or renaming a file cause several file system events to be triggered and committing intermediate states is not what we want. If an event is triggered while the timer is already running, the timer is restarted, effectively establishing a 5 seconds “threshold” before a commit. The versioning service needs to track added, removed and renamed files automatically too, so the following mercurial commands are issued for each commit:

hg addremove -s 50

hg commit -m “Automatic commit.”

So far, it works pretty well. You can find the code and an executable download at my bitbucket repository. It’s bare bones at the moment and I haven’t had time to write usage instructions but will get around it after my exams.

There are a couple of alternatives though. One solution is to use a Versioning File System. Sadly enough, there is none supported on the Windows platform. Another possibility is using some commercial software like FileHamster. Both solutions don’t feel right to me. From what I know about VFS, the tool support is very immature and it would require setting up a samba server. Commercial software costs money, may have bugs I can’t fix and is yet another tool people have to learn. From looking at it, I get the impression it’s more like version control done badly. Nothing that any mature VCS couldn’t do better.

Categories: Open Source, Projects

wxMaxima CAS is Amazing

March 4, 2010 Leave a comment

Yesterday I forgot my beloved Casio FX991-ES calculator at my friends’. At the moment I am very busy preparing for my math exams at March 19th, so this unfortunate situation had to be resolved somehow.

The Mac OS X Snow-Leopard on my surf-station (it’s a MacMini if you wonder) has a cheap scientific calculator. It’s neither capable of calculating functions, nor can you enter complete expressions but have to work with intermediate results as only a single expression can be entered at a time. It follows the value->operator->result approach, I am not all too deep into calculator techniques, so I don’t know how that’s called exactly.

In contrast the Casio-FX991-ES features a sophisticated 96*96 dot matrix display which you can use to enter complex expressions using graphical representations. You can do pretty much everything I need for school with this calculator, it has a great matrix mode and the statistics features are also very nice. Additionally it has shortcuts for the 40 most important natural constants, so I don’t have to type e=1.6*10E-19 all the time. Only downside is that it’s a little slow calculating approximations for integrals or iterating the newton algorithm, but that’s okay as I don’t need to do that very often.

I googled a little and found an Open-Source CAS called Maxmima. It’s cross platform but unfortunately it’s only a command line application, so I needed to find a good GUI for it. Since I do extensively work on MacOS, Windows and Linux at the moment and do often switch back and forth, a uniform cross platform GUI is a big bonus for me (as it was with Diffmerge, a merge/diff tool in one of my recent posts). The best GUI I could find meeting these requirements was wxMaxmima, a LaTex based front end built on wxWidgets.

Unfortunately the input formatting is not as nice as it’s on my FX991-ES, however the results returned by Maxima are (graphically) formatted using the jsMath TeX font  (which I do strongly recommend to install, it won’t get installed automatically. See the instructions.)

What I do really like about wxMaxmima is the ability to create “interactive” documents, as they demo with their great tutorials. As a wxMaxima document is basically a LaTeX document you can apply all the crazy formatting, can include interactive GNUPlot diagrams and a lot more.

It has a very clean syntax and supports all the important algorithms you need and it’s easily programmable if it doesn’t. Particular cool is the ability to simplify expressions. Another thing I which I think is really great is its great menu structure and an auto-completion for all the available commands, making it feel a little like a good IDE.

Categories: Open Source, Tools
%d bloggers like this: