Two Guys Arguing

MonoRail with URL Rewriter working on IIS 7

Posted in .net, notes_to_self by benjaminplee on 02.25.10

I have spent the last few days setting up an existing client application in a new data center (IIS 7 +, Windows Server 2008, x64 bit).  Sounded easy, but it turns out that IIS 7 has some differences from IIS 6 and this app and its libraries (Castle project’s MonoRail & ActiveRecord, and URL Rewriter) were written for IIS 6.  Below are a few tips and tricks that helped us finally get the app up and running this afternoon.  Some of these may be obvious to your average .Net developer, but I come from a Java background and IIS configuration is a enigma wrapped in a mystery to me.  In hind site the few articles I found on Google were helpful and contained good information, but weren’t framed in a way that I immediately could get value from.

  • Setup the application pool with “Classic” managed pipeline mode.  From what I found, this has IIS run the app in IIS6 compatible mode.
  • Setup a new website using the above pool (not some other one, this is important)
  • Add handler mappings for MonoRail and the UrlRewriter (web.config XML snip-it can be found on my GitHub Gist 315115)
  • Most of the MonoRail and URL Rewriter install instructions focus on changing “Application Extension Mappings” in IIS 6.  These are found under “Handler Mappings” in IIS 7.  Note: these changes now modify your site’s web.config directly (instead of just being a IIS setting).  The changes are under the <handlers> element.  If you do an automated build (e.g. with NAnt or MSBuild) and push your files out these changes need to be committed into source control otherwise your normal file will override them the next time you deploy.
  • We ended up including both the 32 and 64 bit Framework isapi filter .dll’s as our handlers but it looks like Windows Server 2008 only cares about the 64 bit libraries.  We are still testing this with our builds because currently all of our workstations are 32 bit XP machines.
  • We also removed all of the original mappings since our application didn’t use them, static content is hosted elsewhere, and they were conflicting with the MonoRail mappings

	
Tagged with: , ,

OpenGL and Haskell

Posted in haskell by youngnh on 02.20.10

Haskell doesn’t sacrifice speed for power, or abstraction for control

Haskell is an amazing language for this type of program for a couple of reasons. It’s static typing eliminates an entire class of errors before your program will even compile. It is garbage collected, so tricky memory-management code is non-existent, it has a fantastic foreign function interface allowing it to wrap and call any C code ever written (and vice versa), and it compiles to native libraries and executables, making it not only fast, but a legitimate candidate to run on platforms that don’t yet have or won’t allow virtual machine code interpreters (iPhones, Pres and their ilk).

So where do we start learning how to write OpenGL in Haskell?

The first thing to realize is that there are a million OpenGL tutorials in C and in comparison, far fewer in Haskell. That’s ok, because Haskell’s bindings to OpenGL are low-level enough that you can actually use C examples to guide your Haskell code. For example, I found this OpenGL example demonstrating how to use GLUT to make drawing a cube super-easy. To make the executable, I had to download a ton of libraries, but they were all available via apt in Ubuntu and I could finally write this Makefile to make compiling and linking a one-command affair:

cube: cube.o
	gcc -o cube cube.o -lglut

Running the resultant cube executable, produces a pretty picture:

Red Cube

The rest is getting familiar with the Haskell OpenGL and GLUT libraries. The functions reside in the Graphics.Rendering.OpenGL and Graphics.UI.GLUT modules. You can find their documentation here and here.

I wrote my first version in a manner that I thought most closely resembled the C syntax. The n, faces and v functions in the Haskell version are standins for the arrays the C version uses.

n :: [Normal3 GLfloat]
n = [(Normal3 (-1.0) 0.0 0.0),
     (Normal3 0.0 1.0 0.0),
     (Normal3 1.0 0.0 0.0),
     (Normal3 0.0 (-1.0) 0.0),
     (Normal3 0.0 0.0 1.0),
     (Normal3 0.0 0.0 (-1.0))]

faces :: [[Vertex3 GLfloat]]
faces = [[(v 0), (v 1), (v 2), (v 3)],
         [(v 3), (v 2), (v 6), (v 7)],
         [(v 7), (v 6), (v 5), (v 4)],
         [(v 4), (v 5), (v 1), (v 0)],
         [(v 5), (v 6), (v 2), (v 1)],
         [(v 7), (v 4), (v 0), (v 3)]]

v :: Int -> Vertex3 GLfloat
v x = Vertex3 v0 v1 v2
    where v0
              | x == 0 || x == 1 || x == 2 || x == 3 = -1
              | x == 4 || x == 5 || x == 6 || x == 7 = 1
          v1
              | x == 0 || x == 1 || x == 4 || x == 5 = -1
              | x == 2 || x == 3 || x == 6 || x == 7 = 1
          v2
              | x == 0 || x == 3 || x == 4 || x == 7 = 1
              | x == 1 || x == 2 || x == 5 || x == 6 = -1

And here's the C code:

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};  /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};  /* Infinite light location. */
GLfloat n[6][3] = {  /* Normals for the 6 faces of a cube. */
  {-1.0, 0.0, 0.0}, 
  {0.0, 1.0, 0.0}, 
  {1.0, 0.0, 0.0},
  {0.0, -1.0, 0.0}, 
  {0.0, 0.0, 1.0}, 
  {0.0, 0.0, -1.0} };
GLint faces[6][4] = {  /* Vertex indices for the 6 faces of a cube. */
  {0, 1, 2, 3}, 
  {3, 2, 6, 7}, 
  {7, 6, 5, 4},
  {4, 5, 1, 0}, 
  {5, 6, 2, 1}, 
  {7, 4, 0, 3} };
GLfloat v[8][3];  /* Will be filled in with X,Y,Z vertexes. */
  /* Setup cube vertex data. */
  v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
  v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
  v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
  v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
  v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
  v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;

Also, the drawBox function was interesting to write, because Haskell has no for loop, so I had to rethink what was going on and translate the idea of executing a block of code over a list of data into it's functional equivalent.

drawBox :: IO ()
drawBox = let nfaces = zip n faces
          in do mapM (\(n, [v0, v1, v2, v3]) -> do
                        renderPrimitive Quads $ do
                          normal n
                          vertex v0
                          vertex v1
                          vertex v2
                          vertex v3) nfaces
                return ()

The Haskell OpenGL bindings have no glBegin/glEnd functions, but rather, renderPrimitive, which takes a PrimitiveMode and a function/block of vertex-related actions.

Beyond that, the only other thing that tripped me up was that I couldn't figure out how to enable depth testing. I passed the option to display mode to use a depth buffer:

initialDisplayMode $= [DoubleBuffered, RGBMode, WithDepthBuffer]

and in C, there's a single call to enable it:

glEnable(GL_DEPTH_TEST);

which I couldn't find anywhere in the Haskell API. I figured it would look like the call to enable lighting:

lighting $= Enabled

so I ended up choosing depthMask $= Enabled, but my first clue that things weren't working was what my program displayed:

Red Cube Without Face

More of a box than a cube, really.

The key, I found, was that the C version makes a call to enable depth buffering, but omits a call to set the actual depth test the depth buffer uses, relying on default behavior. The Haskell API only provides the second, depth test-setting operation.

depthFunc $= Just Lequal

in place of my misguided depthMask, that code did the trick.

You can find my finished Haskell source in this gist right above the original C program I translated. If you're interested in running it, runhaskell Cube.hs should do the trick.

This is by no means the best looking Haskell code. It could be more idiomatic, but I wrote it this way to show how similar it looked to the C code it came from. It ended up still being 20 lines shorter and allows you to focus on more important ideas than properly updating index counters or setting integer bit-flags properly.

Tagged with: ,

Notes to self: Find all files NOT containing text

Posted in Uncategorized by benjaminplee on 02.09.10

Handy script to find all files in a given subfolder that do NOT contain a certain text line. Sorted for easy reading:

find ./PATH/TO/FOLDER -type f -size +1c ! -exec grep -q ‘SEARCH TEXT’ {} \; -print | sort

The revenge of the 5 questions

Posted in questions by benjaminplee on 02.04.10

Ever since CodeMash my brain has been a buzz of activity, but I haven’t had time to really dive into anything too substantial.  I have a number of posts/ideas in the works but nothing to show for it.  But, if large corporate cultures have tought us nothing, it is to write status reports about work are often more important than the work itself.

In lieu of a “new year” post that I never ended up writing, I am going to revisit one of this blog’s first posts: 5 questions. To revisit the questions:

  1. What are you currently hacking on?
  2. What are you currently getting better at?
  3. What do you do when your computer is asleep?
  4. Describe that ‘big fish’ project that’s been stewing in your brain.
  5. What are you gonna post about this coming week?

1.) Clojure, JSON, and misc little coding challenges.  Instead of writing a list of goals for the year I am taking a bit more “agile” approach and just spent 2 minutes writing down anything and everything I could think of that I “wanted to know more about”.  JSON and functional programming bubbled up to the top of a quick random sorting, so the fact that my current side project w/ Mr. Zac Duncan involves both is very convenient.

2.) TDD.  Nate’s recent TDD Rock Paper Scissors challenge and very interesting talks with the guys at CodeMash left me questioning why I work the way I do, how I might do things better, and how I can effectively communicate that to another developer.  I haven’t gotten very far with it yet, but it is definitely something I am putting effort into now.  I am also currently reading Andy Hunt’s Pragmatic Thinking and Learning: Refactor Your Wetware which looks very interesting.

3.) Relaxing with the wife, and trying to get back in shape (same as last time).  Hopefully joining up with the new year’s company weight loss competition will give me that extra bit of motivation.

4.) No big fishes at the moment, just lots of little ones.  And damn are they fast.  Along with Zac’s and my side project I have mainly been pushing myself to write more code.  Any code.  And get feedback on it.  I would love to take up one of my past personally projects and see it through to fruition but for now I am content if I can get some constructive feedback on whatever I am doing.

5.) JSON and my first impressions of trying to write a real app in a functional programming language.  Along with the “want to know more about ___” items I wrote down I want to get into the habit of posting on each topic on aprox 1 week intervals, putting down whatever I have learned as best I can.

Hopefully this post will convince Jeff to post another response ;-)  He is always up to something interesting.

Tagged with:
Follow

Get every new post delivered to your Inbox.