Mike Lindegarde... Online

Mostly things I'm likely to forget.

It's been a while...

You were probably beginning to think that I was one of those flash in the pan bloggers.  Here one day, gone the next.  Nope.

It's been a while, but I'm back.  Some things beyond my control happened.  They sucked, but they're in the past now.  Time to keep moving forward.

My next post will focus on GitLab: an awesome alternative to GitHub if you have the resources to host it locally (and these days, just about everyone should have those resources available in some form or fashion).  After that I'm hoping to move onto some F# posts.  While I've been enamored with the language for some time, I haven't had the chance to really put it to use until now.

I'm also planning to continue my work with Antlr.  According to Google Analytics that's what brings most of you to my blog anyway.

Beyond that we'll just have to wait and see what catches my interest.  I'm doing my best to talk my current client into using a document database for caching.  Not only is it something I want to learn more about, it's something I honestly believe is to valuable to pass up.  Why hammer your relational DB when you can just grab the data from a cache or a secondary data store that has the data in the format you need without having to do four hundred joins?

Thanks for stopping by.  I'll have that GitLab post up soon (unless things start to go south again).

Putting Ubuntu 14.04 to Work via VirtualBox

I'm pretty sure I've done this before

Skip the story

Hosting Ubuntu in a VM makes it incredibly easy to setup a Linux box that you can use to host a Git server, WordPress blog, your Rails apps, Node.js, Apache, mail server, MySQL RDBMS, TeamCity, etc...  Really, there's no reason not to setup an Ubuntu VM.

This isn't the first time I've attempted blogging, nor is this the first time I've put together a post about using VirtualBox to host an Ubuntu VM.  Each time I write this post I discover that the process has gotten easier and eaiser... mostly.

You no longer need to use "VBoxManage setextradata" to setup port forwarding, Ubuntu has gotten easier and easier to use, and most major software package have some sort of setup / installation package you can download and easily install.

However, I continue to write this post every few years because I continue to have a problems trying to install the VirtualBox Guest Additions...


Getting a basic installation of Ubuntu setup is incredibly straight forward.  You pretty much need just two things:

  1. VirtualBox
  2. The latest stable distribution of Ubuntu for desktops (if you're reading this you should probably avoid Ubuntu Server)

Helpful hint:  if you have an old install of VirtualBox laying around from the last time you messed around with virtual machines, delete it and install the newest release.  This will ensure you have the newest version of the Guest Additions.

Install the Guest Additions

I'm going to assume you can figure out how to install VirtualBox and get a basic instance of Ubuntu up and running.  For the most part the options are pretty straight forward.  Give up as much memory as you can and allocate a virtual disk drive that matches your intended use for the VM.

Make sure that you go into the VM's settings (in VirtualBox) and enable 3D acceleration.  With that taken care of, open a terminal and execute the following:

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install build-essential module-assistant
$ sudo m-a prepare
$ sudo apt-get install xserver-xorg xserver-xorg-core

With that taken care of, use the VirtualBox menu to mount the Guest Additions CD in the VM.  Ubuntu should automatically prompt you to begin installation.  If it does not, you can either eject / unmount the CD or run the following command:

sudo sh /media/cdrom/VBoxLinuxAdditions.run

Once the install completes you should reboot the virtual machine.  Hopefully when Ubuntu finishes rebooting you'll be all set.

Hello World

As nifty as it is to have Ubuntu running in a VM on your Windows box, it doesn't do you a lot of good unless you can get to it from the outside world.  Regardless of the port you want to open up, you basically have two ways to accomplish your goal:

  1. Configure your VM to use a Bridged Adapter
  2. Setup port forwarding

Bridged Adapter

With bridged networking your VM essentially appears to be on your network.  There is no need to setup port forwarding.  You can simply configure network router to forward traffic to the IP address of the VM.  Unless you have a compelling reason not to, I'd recommend using this configuration if you want to open something running on your VM to the outside world.

Port Forwarding

With port forwarding your host OS has the opportunity to buffer / forward packets as it sees fit.  I'll let you explore why you would use port forwarding over a bridged adapter on your own.   Here's how you do it:

  1. Open the settings for your VM in Virtual Box
  2. Go to the Network tab
  3. Ensure Attached to is set to NAT
  4. Click on Port Forwarding
  5. In the dialog that appears enter whatever Host and Guest port are appropriate for your use case.  You should leave the two IP columns blank.

Wrap up

With that you should be set to access your VM from anywhere in the world.  Please keep in mind that this probably isn't the most secure thing to do.  I wouldn't really recommend leaving any port open to the world unless you really know what you're doing. 

Random useful links:

Using WCF to Monitor Your Windows Services


Skip the story

Having come to age as a professional developer in an era where putting business logic in your database was considered sacrilege, I never used the database for anything more than storing data.  Using SQL Server was (is) a last resort.

A few months back I was working on a project at a company that has its roots firmly planted in a database oriented approach to development.  I get that it's impossible to rewrite a massive legacy system every time contemporary programming practices change.  However, I was surprised how quickly (and frequently) developers turned to the database or logging as a solution.

One such example, we needed a way to monitor and manage a Windows service.  Certainly logging provided a low level means of monitoring; however, it didn't provide an effective way to manage the Windows service.  One suggestion was to use a table in a database as a control mechanism.  That could work, but what about a more direct approach?

Setting up the solution

In this example we'll Create two console applications.  One will use TopShelf to start a Windows service (this post doesn't cover TopShelf).  The other will be a normal console application that'll communicate with the Windows service via WCF.  Generally I prefer to put a WPF application in the system tray; however, I'm keeping it simple for this example.

Create a blank Visual Studio 2015 solution named ServiceMonitorDemo.

The Windows service project

Add a new C# Console Application to your project named ServiceMonitorDemo.Service.  The first thing you'll need to do is to add TopShelf to the project:

Install-Package TopShelf

With that taken care of, you'll need to write two service contracts.  For this tutorial we're going to use a duplex channel.  You'll need one interface for each direction through the channel.

 First write the contract that other programs will use to communicate with the service:

using System.ServiceModel;

namespace ServiceMonitorDemo.Service.Contracts
    [ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IDemoServiceCallbackChannel))]
    public interface IDemoServiceChannel
        [OperationContract(IsOneWay = true)]
        void Connect();

        [OperationContract(IsOneWay = false)]
        bool DisplayMessage(string message);

You'll notice this service contract is decorated with the ServiceContract attribute.  This is how you tell .NET what interface to use for the callback contract.  The callback contract is used by the service to communicate with connected clients.  You'll define the callback interface shortly.

Notice that the contract consists of two methods:

  • Connect - This method is used to add the calling client to our list of connected clients.
  • DisplayMessage - This is used as an example of bidirectional communication and to show how clients can control the service through WCF

The callback contract is pretty straight forward:

using System.ServiceModel;
using ServiceMonitorDemo.Model;

namespace ServiceMonitorDemo.Service.Contracts
    public interface IDemoServiceCallbackChannel
        [OperationContract(IsOneWay = true)]
        void UpdateStatus(StatusUpdate status);

        [OperationContract(IsOneWay = true)]
        void ServiceShutdown();

Again, our simple service contract has just two methods

  • UpdateStatus - Used by the service to push the service's status out to all connected clients.
  • ServiceShutdown - WCF does not cleanly handle shutting down things.  We need to make sure that the code takes care of opening and closing connection correctly.

With that out of the way we need to take care of writing the actual service.  For this example the service won't do anything exciting, it'll simply post a status object to all connected clients.  The code for this is pretty long, so I'm only going to post important sections here.  You can find the completed example solution on GitHub.

In order to accept connections to the service you'll need to initialize the named pipe:

_host = new ServiceHost(this);

NetNamedPipeBinding binding = new NetNamedPipeBinding();
binding.ReceiveTimeout = TimeSpan.MaxValue;

    new Uri(Uri));


This code simply creates a new host using the current object for the ServiceHost.  A named pipe binding is is added to the host.  Clients connect to this endpoint via the Connect method on the channel service contract defined above.  Our service implements the Connect method as follows:

public void Connect()

The only other important detail here is that DemoService class is implemented as a singleton and decorated with the following attribute:

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]

In this case I have a single service running and I want to share data with all connected clients.  With this in mind, using InstanceContextMode.Single makes sense.  You can also use per session and per call context modes.  You can find a good overview of the differences on Code Project.

The rest of the cod for this project is pretty much boilerplate setup and tare down.

The Console Application

Fortunately this entire application is less than 100 lines long.  Again, I'll refer you to the GitHub repository to see the full implementation.  Below you'll find the most important section of the code:

        private void Connect()
                    DuplexChannelFactory<IDemoServiceChannel> channelFactory = new DuplexChannelFactory<IDemoServiceChannel>(
                        new InstanceContext(this),
                        new NetNamedPipeBinding(),
                        new EndpointAddress(Uri));

                    _channel = channelFactory.CreateChannel();

                    _isConnected = true;
                    Console.WriteLine("Channel connected.");
                    Console.WriteLine("Failed to connect to channel.");


I wouldn't recommend taking the above code and dumping into your production code.  It's designed to demonstrate how to establish a connection to the Windows service via a WCF named pipe.

As long as the console application is connected to the service, the service will continue to trigger the UpdateStatus method.  In a real world implementation UpdateStatus would most likely toggle some sort of visual status indicator on a WPF application (e.g. a red / green light).  In tutorial land displaying a message in the console works just fine.

Wrap Up

If you've cloned, forked, or downloaded a zip file of the repository you should be able to run the Windows service as a service by navigating to your binary folder for the project (Debug or Release depending on your active build configuration) in your preferred terminal / command prompt and running:

../ServiceMonitorDemo.Service install
../ServiceMonitorDemo.Service start

You can then run a few instances of the ServiceMonitorDemo.Monitor and see what happens.

GitHub repository Link

Developer Street Cred

Important Information in the Next Section...

Skip the story, take me to the instructions.

I did my share of text editor coding and command line / terminal compiling back in the day.  However, between 2004 and 2013 I was pretty much doing all of my programming in Visual Studio.  In 2013 I started using my Mac Mini and eventually my MacBook Pro to do some work.  That lead to using Eclipse and Android Studio for some Android development.  Than I started to play with Rails, Node.js, etc... 

Long story short, I realized the Windows command prompt doesn't compare to using the terminal on OS X (and of course Linux / Unix based distributions).


Does not equal this:

But this is pretty close:

Getting There

I'm no expert when it comes to the different options available to Windows users (or terminals in general).  However, I can tell you what I did to at least improve my situation when working on a Windows box.

Step 1: Install Babun

Head over to the Babun homepage and following the instructions there.  Pretty straight forward.

Step 2: Find You Theme

I ended up going with the powerlevel9k theme for ZSH.  I used that as a starting point and made a few modifications from there.  You can either follow the instruction on the linked page or do the following:

  1. Download the powerlevel9k.zsh-theme file
  2. Move the theme file to C:\Users\[username]\.babun\cygwin\home\[username]\.oh-my-zsh\custom.
  3. Navigate to C:\Users\[username]\.babun\cygwin\home\[username].
  4. Open .zshrc in your favorite text editor.
  5. Make the following change:
# Set name of the theme to load.
# Look in ~/.oh-my-zsh/themes/
# Optionally, if you set this to "random", it'll load a random theme each
# time that oh-my-zsh is loaded.

I made a slight change to the default theme.  To do this, I created a copy of the file I downloaded it and renamed it to powerlevel9k-modified.zsh-theme.  I then changed one line in the file:

  local current_path='%C'

To see the change you'll need to once again edit your .zshrc file to reflect the "new" theme.

Step 3: Locate a Powerline font

You can find the Powerline fonts on GitHub.  I'm currently using DejaVu Sans Mono for Powerline.  Simply download and install the ttf file (double click on it once you've downloaded it).  You'll then need to edit your ~/.minttyrc file to use the font:

Font=DejaVu Sans Mono for Powerline

Step 4: Set You Color Scheme

I'm a fan of the Solarized color scheme.  You can find both the light and dark versions on GitHub.  I opted not to edit my config file.  Instead I created a ~/.solar folder and put the sol.dark file there.  You'll then need to add the following line to the end of your ~/.babunrc file:

source ~/.solar/sol.dark

Step 5: Enable Syntax Highlighting

Reading the most current documentation suggest this step may not be necessary; however, when I did my setup I had to do this:

cd ~/.oh-my-zsh/custom/plugins
git clone git://github.com/zsh-users/zsh-syntax-highlighting.git

Then you'll need to edit your ~/.zshrc file as follows:

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(svn zsh-syntax-highlighting)

That should ensure you have proper syntax highlighting in your terminal.

Random Useful Links:

Getting Started with Visual Studio 2015, Antlr 4, C#, and a SQL Grammar

The Part You Can Skip...

Back when I had a lot of free time (high school / college) I enjoyed studying the art of compiler design.  My first first lexer and parser (LL(1)) were written from scratch.  By college I had discovered lex / yacc and flex / bison.  Unfortunately, I haven't made the time to dabble in this area in the decade or so that has lapsed since leaving school.

Fortunately I recently had a real world problem I was able to over complicate enough to warrant a domain specific language.  Of course I could have solved the problem with a switch statement, but switch statements are evil (I can't back that up with facts).  Instead I took the opportunity to play with Antlr.  Given the current state of the open source community and the abundance of Stack Overflow answers in search results I expected this to be much more straight forward than it was.

Although I'm perfectly content to develop in Java, my use case for the DSL is based in .NET.  Antlr 4 has a C# target, so I didn't expect this to be a problem.  It turns out that the documentation is either sparse or a bit out dated.  Below you'll find what I had to do to get things up and running using Antlr 4 and Visual Studio 2015...

Setting up Visual Studio 2015

You can find the outdated documentation for the C# Antlr 4 target on GitHub.  The documentation is pretty complete and still useful.  The biggest problem you'll run into is that the Antlr language support extension it references has not been updated for Visual Studio 2015.

Step 1: Create a new Class Library project

Generating the C# lexer and parser happens through the magic of NuGet packages.  All you need is a class library project, a valid grammar file (or two), and the appropriate NuGet packages.

Step 2: Add NuGet packages to your class library

Install-Package Antlr4
Install-Package Antlr4.Runtime

As of version 4.3.0, the NuGet package will not work with .NET 4.5.1 or newer.  I'm currently targeting 4.6.  It took me a moment or two to realize this problem.  As long as you're isolating your lexer and parser in it's own project (as you should be) this shouldn't be a problem.  Just edit your project to target the older version of the .NET framework.

Step 3: Add your grammar file(s)

If the Antlr extension for Visual Studio work with 2015 this would be trivial: just follow the documentation.  But... it doesn't (at least not at the time of writing this).

You must make sure the grammar file has the correct encoding.  If you don't, things won't work.  The GitHub documentation walks you through using the advanced save options in Visual Studio to set the proper encoding.  Although I found the advanced save options in Visual Studio 2015, I did not see Unicode (UTF-8 without signature) - Codepage 65001 listed as an option.  I ended up using NotePad++ and setting the encoding to UTF-8-BOM.

I was not able to edit the grammar file properties as outlined in the official documentation.  Instead I resorted to editing the csproj file.  You can do this two ways:

  1. Unload the project in Visual Studio, then open the project file in Visual Studio for editing.  Once your done, reload the project
  2. Use an external editor (like NotePad++, Atom, Sublime, VIM, whatever floats your boat) to edit the csproj file.  When you switch back to Visual Studio you'll be prompted to reload the project.

I had to add the following section to my csproj file

    <Compile Include="Properties\AssemblyInfo.cs" />
    <Antlr4 Include="SomeParser.g4">
    <None Include="packages.config" />

Make sure you remove the grammar file from any other section it may appear in.  If you have more than one g4 file (your lexer and parser are split) you will need to add an Antlr element for each file.

Finding a SQL Grammar

At this point you should be able to build your class library and generate an assembly that has your lexer, parser, base visitor, and base listener classes in it.  The next hurdle is finding an existing Antlr 4 SQL grammar.

After some digging, I found a SQL grammar file on GitHub (where else?).  Whether or not you can legally use the grammar I've linked you to is something you need to decide.  According to a Google Group discussion I found, Salesforce.com may have released their SQL grammar.  I have not looked for that.  Let me know if you find it.  You can also check the GitHub Antlr 4 grammar repository for grammar files.  As of writing there wasn't a SQL grammar file there.

Random Other Links

Here are few other random links that helped me in some way:

Helpful Beginner Hints

Make sure you order your predicates with the most specific at the top.  If you don't you'll end up in a situation where your intended predicate can't be reached because a more generic one is reached first (i.e. defining a string literal that can have spaces before you define an identifier token that can't have spaces).

Take a look at ANTLRWorks 2.  It's incredibly useful when writing your grammar files.

Let me know if you know something I don't.  I'd love to update this article with more useful and / or up to date information.