Musings from an east coast software developer, writer and reader.

From the Blog

The last time that I touched C# was about four years ago. I had just finished my first semester at NJIT as a sophomore transfer, and I took a summer course with Daniel Boston where we developed a payment gateway for the Vector newspaper. Since neither of us had ever used any of the .NET languages, and one of our requirements was that this had to run on a Windows server, we figured it would be a very good learning experience. We taught ourselves the ASP.NET and C# syntax in only six weeks, working tirelessly during the nights over Skype (we both had summer jobs), to complete our project on time and present it to the class. Needless to say, we succeeded, but got burned a few times by how much a pain in the ass ASP.NET was back then.

A couple of weeks ago I had the opportunity to get familiar with one of our developer tools at my day job, which is written with C# and WPF, and I have to say that I am quite impressed. Microsoft has identified a key point in the application development pipeline, and as far as I can tell, made a valiant attempt at solving it: the user interface should not be tied directly to the application (business) logic, and using WPF with XAML you give designers the ability to modify the layout of the application through the use of themes. This approach is very similar to how the web development process, through the use of HTML and CSS, allows for web designers to not affect the application logic that sits in the “backend.”

WPF 4 UnleashedThe #1 WPF Book

This has been a huge help learning WPF and XAML.

I haven’t yet spent enough time with WPF 4.0, but from what I can tell this is the way to be developing all future Windows-based applications. After a little searching it unfortunately looks like that the team behind Mono will not be supporting WPF in the near future. It would be nice to be able to use XAML and C# to quickly develop cross-platform applications which would use the native API for each platform. That means Aqua for Windows (Vista/7), Cocoa for Mac OS X and either Gnome or KDE for Linux/Solaris/Unix. Not to mention each time that I get a chance to use Visual Studio I feel like any other software development IDE pales in comparison, but I have felt that way for awhile now.

If you’re interested in getting your hands into WPF 4.0, XAML, and the whole caboodle, head on over to Amazon.com and pick up Adam Nathan’s WPF 4: Unleashed which is the first programming book that I have seen that has high quality, full color, code samples. You won’t be disappointed!

Jul
30
Posted by JB at 12:00 pm

A few days ago to the sweet, sweet melodies of Billy Joel, I woke up in the wee hours of the morning to code up a very crude C web server. Of course, I tried to pass it off as a C++ implementation, but in reality I was pulling the wool over your eyes – the only C++ was used to build error messages, and the actual message to send down to the client. Not to mention it was a very crude, basic, implementation of socket handling in C. Nothing fancy.

Yesterday I was talking with some kind folks at the re:build 2011 conference in Indianapolis, and it got me to thinking that there aren’t enough examples of C++ applications talking to the web. You know, actual C++, using the boost libraries and what not. So this morning, after grabbing my amazing breakfast burrito at Henry’s in downtown Indianapolis I scoured the Internet and re-wrote my example application in Boost.Asio.

The code is much simpler, straight forward, and just damned better to look at. As always it is up and available at my Github account for consumption. If you’re interested in seeing the actual difference in implementations than go no further than right here.

I have a few more ideas on web services written in C++, but that’s for another day. Let me know if you have any questions. As always, enjoy the code and report any bugs.

Jul
21
Posted by JB at 6:54 am

So, this morning I couldn’t sleep, and around 4:00AM I started messing around with nginx, fastcgi and C++. For awhile now I have been considering the feasibility of an extendable and reusable framework for serving up web content from C++ applications. Of course, all you web programers want to write code in Javascript and Ruby, but sometimes there are applications that can be core to a company that do not need to (or should not) be rewritten merely to service a different type of client. I wrote a little test program that I threw up on my Github.com account.

That’s my reasoning for developing this simple proof of concept application. I didn’t want to actually download the fastcgi libraries that exist for C, mainly because it was 4AM and I am a lazy fuck, so I decided to write up something simple with accept(), recv() and send(). The actual configuration for nginx is quite simple, you can just use their example for proxying to an Apache web server.

For those of you that are just as lazy as I am, click on this big bad beautiful link, and enjoy the show. Later, at some point, I will write up my thoughts on how an actual framework to solve this problem may work (and look), but right now I need to take a shower and go to work.

The past couple of weeks I have been doing some research because I am planning on working on a FastCGI Model-View-Controller framework for developing web application services in C++. I am a C++ engineer by day and have been working with the language for nigh on a decade. The brief research I was able to do has shown me that there are no DataMapper or Active Record open source libraries available.

What Exactly Is The N+1 Query Problem?

When using an abstract framework to build a data model for use with an SQL-esque language you generally need to build a set of classes to model the schema in said language. For Type Aloud, lets say that I want to display all of the stories in a set of categories after you get them back based upon two slugs taken from the site’s URL.

SELECT id FROM categories WHERE slug='sci-fi' OR slug='fantasy'
SELECT id, name FROM stories WHERE category_id = 0 OR category_id = 1

How many queries will be run? At most there will be two queries, but what about if none of the categories match the two slugs provided? That first dangling select query will be executed each and every time even if there are no matches to search against. How can we fix this?

SELECT * FROM stories 
INNER JOIN categories ON stories.category_id = categories.id 
AND (categories.slug = 'sci-fi' OR categories.slug = 'fantasy')

This type of explicit join will allow you to execute a single SQL query and, in the long run, will save you thousands of CPU cycles with your web applications. This type of data mapper already exists in Ruby on Rails using the Data Mapper gem which is freely available. But as of right now there is no such framework available (unless I am mistaken) in C++ which gives you the power and flexibility to build your data models and eat your cake too. My first plan in developing an MVC framework is to work on architecting a proper data mapper library for C++.

Anyone else game?

Lazy loading is a software design pattern that delays the initialization of resources until the first time they are to be used inside of an application. Thus, for certain applications that rarely use memory intensive resources, you only need to initialize when there is a guarantee it is to be used. This has obvious performance and efficiency benefits. As a C++ programmer by trade I find it rather irritating that there is not a generic class that automatically handles lazy loading for me. So like any good lazy fuck I decided to spend a few hours and write one of my own. I currently have two use cases for this little tidbit.

When you have available a default construct its use is quite simple and straight forward. This object allocates the class with the default constructor when it is first called directly through any of the accessor operators, or directly with the get method. In this particular example when loaded it correctly returns the unique identifier.

class Unique
{
public:
    Unique() 
        : m_uuid(boost::uuids::random_generator()())
    {
    }
 
    const boost::uuids::uuid& uuid()
    {
        return m_uuid;
    }
private:
    boost::uuids::uuid m_uuid;
};
 
int main(int argc, char* argv[])
{
    thunk::lazy_loader<Unique> loader;
 
    const boost::uuids::uuid& u = loader->uuid();
 
    std::cout<<"uuid: "<<boost::lexical_cast<std::string>(u)<<std::endl;    
 
    return 0;
}

The second example uses a boost function structure as a factory method, and it is executed instead of the default constructor. The factory method takes a reference to a boost shared pointer and correctly calls the constructor with any obligatory parameters. The boost shared pointer is owned by the lazy loader implementation and is used to maintain a reference counted state.

 
class Unique
{
public:
    Unique() 
        : m_uuid(boost::uuids::random_generator()())
    {
    }
 
    Unique(const boost::uuids::uuid& u)
        : m_uuid(u)
    {
    }
 
    const boost::uuids::uuid& uuid()
    {
        return m_uuid;
    }
private:
    boost::uuids::uuid m_uuid;
};
 
static void factory(boost::shared_ptr<Unique>& impl)
{
    impl = boost::shared_ptr<Unique>(
        new Unique(boost::uuids::nil_uuid()) );
}
 
int main(int argc, char* argv[])
{
    thunk::lazy_loader<Unique> loader(&factory);
 
    const boost::uuids::uuid& u = loader->uuid();
 
    std::cout<<"uuid: "<<boost::lexical_cast<std::string>(u)<<std::endl;    
 
    return 0;
}

I am still working on a better method for initializing with a n-argument constructor. I have made my current progress available on my Github.com repository available for use with the standard three-clause BSD license. If you have any questions or bugfixes please feel free to submit them through Github.

On my way home from work I started to put some mind power towards figuring out a solution to the issue of adequate class loading in C++. A quick Google search for “Classloading C++” brings up a few pages that are of quite an interest. You see for awhile now I have been trying to grasp why I have not found any model view controller frameworks written in C++ running on top of FastCGI, and one of the reasons that I have been stuck on is the fact that people do not want to modify, compile, start, execute in order to get through one series of tests for Internet projects. Of course, there are many ways to divide your project up appropriately to limit the compile time of certain small pieces, but in the end, there is still that dependency on the fact that we’re still shit slow compared to Ruby development.

One of the features I love about Ruby on Rails is the fact that there are long periods of time that I really never need to restart Mongrel while developing. I have my Emacs buffer pointed to a couple of files, save out my changes and click the refresh button on Chrome. That’s how easy it is. Could we achieve that development speed with a C++ framework? My my rain soaked walk home I went through it all in my head, and I firmly believe that speedy Web development with a proper C++ framework is achievable. But something core to this, at least as I see it, would be a structured dynamic class loading architecture which would allow on-the-fly loading and unloading of linked objects. In “development” mode (which could merely be controlled by an environment variable) we could even execute g++ each and every time we have a web request as long as the dependencies for these plugins were small enough to minimize compile/link time. A class loader could be pointed to a directory similar to class path environment variables, setup to look for a specific string through a regular expression, and dynamically load object files based upon the timestamp of the file. The core framework would need to be a shared library – so your classes to handle the variety of software patterns you decide to use (Observer, Singleton, MVC, etc) would be something that you’d need to take down the whole server if updated. But once a framework is to a certain point you rarely make changes to the core.

After I worked through these thoughts on the PATH ride into Harrison I got to thinking: because we have access to literally all the low level libraries for Ruby, Python, PHP, what is stopping us from building a framework that will allow simultaneous development in any language that we can plug in easily? Of course, I do not see why anyone would want to do this inside of a real development environment when you could just as quickly (and easily) stick to a single language, but the thought still piqued my interest.

Just something on my mind.