Using Javascript to order the client around

May 17, 2009 at 10:00 pm (Java, Javascript, Ruby, Week 8) (, , , , , , )

Micah and I spent most of Friday working on a lightweight messaging system that’s going to allow us to push messages from server to client, even though the client is basically a web browser. Since HTTP is basically stateless, we’re doing it with AJAX requests (Javascript, with some help from JQuery). And in order to keep it really simple and flexible on the client side, we’re developing something like a message queue on the server, which builds up Javascript for the client to execute whenever it’s ready (this way the server keeps track of time-sensitive issues, and the client doesn’t really have to hammer the server to stay updated). It’s a pretty cool idea that Micah had; he likens it to us putting food on a plate (at the server), and when the client’s hungry, he comes along and takes the food that’s there and eats it (executes it in the browser). We’re using the command pattern to fill up the plate of Javascript.

The lunch & learn was a bit more technical this week (after last week’s great Star Trek excursion), but still great fun. Doug presented a prepared code kata: TDD factorials in C++. I had already seen a little of CppUTest from pairing with Doug earlier in the month, but he went a little more in depth explaining the syntax, which we had just kind of touched on before. He’s teaching a TDD class this week, which I’m sure will be awesome for anyone lucky enough to attend.

Eric Meyer presented the development of a story using acceptance test driven development (ATDD) with Cucumber. He and I had worked with it a good bit while developing our Rails app (which by the way has been live for awhile now): a job board for software craftsmen, so I knew the syntax and structure already. Eric used a great technique to get the story done quickly and correctly while doing a good bit of live coding as well: git tags. He had about 8 steps to the development of the story that he had practiced, and each one was tagged, so that he was free to code in a normal way and know that we could fast-forward to the next step at any point. I’d like to learn more about version control (both Subversion and Git), but it’ll just have to go in the bucket of stuff to learn with everything else!

In less technical news, I finally got a Cubs hat. Look out Chicago, now you might have to hear me say “Y’all” before you know I’m not from around here.

Permalink Leave a Comment

More fun with JRuby (but for real this time)

May 14, 2009 at 10:19 pm (Java, Ruby, Week 8) (, , , , , )

Well, I knew I would see some benefits from my seemingly unproductive work yesterday, but I didn’t imagine that I’d be able to have a patch together today. It turned out that with the knowledge I gained yesterday, along with some smarter strategies, I was able to fix the issue with spaces in directories housing jarred gems that was haunting me. It wasn’t easy, but I tracked my way through the JRuby source and submitted a tested patch to the JRuby issue tracker. We’ll see; maybe it’ll get accepted, or maybe there’s something I haven’t thought of that’ll hold it back. Either way, I gained a lot of confidence, and I’m excited about taking up Charles Nutter’s call to fix RubySpecs in JRuby. Very timely!

I was also in on some meetings with the rest of the development team Micah and Doug are working with. They’re based remotely (Doug just flew back into town after working onsite with them), so we did some teleconferencing using Skype for video chat and Adobe Connect for screen sharing. It’s pretty amazing technology, and while I can’t discount the great value you see from working side-by-side all the time, I’m impressed that it’s so easy.

Micah and I did some CSS and Javascript (JQuery) work towards the end of the day. He’s not a fan of the CSS float attribute, so I had to convince him that it was worthwhile for the layout we were working with. Of course, there are generally multiple options, this case included, but I think we ended up with a good starting point for a screen we were laying out. JQuery’s AJAX capability is pretty awesome. We used JQuery.getScript(), which fetches a script from a URL and then loads and executes it (with an optional extra function parameter to run afterwards). It basically does the work that we would’ve had RJS do in a Rails application, but it seems much cleaner to me. We still have some thinking to do about how the performance is going to be in our particular case (1 web request per second will almost certainly be too heavyweight), but it’s a great start. The other big question mark for our current task is to what extent we’ll be able to test it. Luckily, resident TDD Javascript expert Jim Suchy is going to help us out with that. Thanks Jim 😉

Permalink Leave a Comment

JRuby struggles yield rewards

May 13, 2009 at 9:21 pm (Java, Ruby, Week 8) (, , , )

I was a bit cranky when I left work today, because I wasn’t able to track down the problem I was working on. I spent hours looking for the solution(s) to some outstanding JRuby issues with jarred gems, (one related to spaces in directories and one related to nested jars (like Hpricot and Limelight both have). And I got nowhere. Nothing to show for my efforts. I went down a ton of paths, tried logging, and added stack traces, but to no avail. The solution exists in the code, but the codebase is large, and I just wasn’t able to wrap my brain around the problem. And that sucks.

But in every situation, there’s an opportunity for learning. Eventually, on the way home, I relaxed and looked for the lesson in my failure today. I didn’t fix the issues, true. There’s no metric that I know of that says something positive came out of my work today. But I introduced myself to most of the filesystem-related components of the codebase, and I saw the mappings JRuby provides from Ruby methods to Java methods. (Annotations to the rescue for those Ruby methods like []= whose names would be illegal in Java!) I really internalized some keyboard shortcuts in IntelliJ. I learned a new trick where you press Command and hover over a method call to see information about it and the object it’s defined on.

Most of all, I learned what not to do when presented with a big codebase and a specific-sounding problem. I just jumped in, looking at the first place I saw referenced in the issue comments, and logging things, working at a micro level basically all the time. It wasn’t I got home that I realized I needed to step back and look at what was really happening on a larger scale – the context. I should have realized my problem during the day when I tried to explain the problems I was having and couldn’t really do so – next time I’m having these kinds of frustrations I’m going to pull the old rubber-duck trick (explaining the problem to a rubber duck or other inanimate object). If I can’t, something needs to change in my process.

Permalink Leave a Comment

Crazy Java/Windows problem and some Scheming

May 12, 2009 at 8:34 pm (Java, Limelight, Ruby, Scheme, Week 8) (, , , , , )

As soon as I came in this morning, Micah asked for my help with a bug he’d been working on tracking down in some Java JNA code in the Limelight source. The gist of the code is that it was calling some native code in Windows DLLs, and about half the time, one of the API hooks didn’t get properly installed. We looked and looked, eventually doing some print statements looking for threading issues and other possibilities. Take a look at the code and see if you can spot the problem:

new Thread()
{
  public void run()
  {
    W32API.HINSTANCE appInstance = Kernel32.INSTANCE.GetModuleHandle(null);
    final User32.HHOOK keystrokeHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, new KeyboardHandler(), appInstance, 0);
    hookThreadId = Kernel32.INSTANCE.GetCurrentThreadId();
    MsgLoop();
    User32.INSTANCE.UnhookWindowsHookEx(keystrokeHook);
  }
}

Well, if you knew right away that the new KeyboardHandler() might sometimes get garbage collected before the Windows code gets around to calling it, then you’re lying. Just kidding! I am very impressed if so. Once we looked back on the problem, we could see how the problem might have happened, but it was really bizarre. The idea is that because we don’t hold onto a reference to the new KeyboardHandler() that we create, the garbage collector could think we’re all done with it, even though the Windows code really needs to hold onto it. We didn’t look in depth into how JNA translates the Java code into native Windows code, but Micah understands it, and I think he said it passes a function pointer to the Windows code in this case, so when Windows tries to callback to our Java code residing in that pointer, that code is no longer there. Wacky.

We also made an installer using install4j, which seemed really easy. I’m not sure how much setup Micah had already done, but we were able to drop in some custom icons and a splash screen really easily for our application, and it looks good on both Mac and Windows. I did my first work in Photoshop CS 3, and it was weird not so have the Save Optimized As.. option that I’m used to from Photoshop/ImageReady CS.

I spent some time this afternoon practicing recursion by writing methods to identify palindromes. I did it in Scheme, Ruby, and Java (in that order), all TDD, all in different editors. Of course, Ruby was the easiest and clearest by the end (negative array indexing helped out!), but Scheme wasn’t as hard as I’d expected. Things may be getting easier!

By the way, if anyone else is going through SICP and feeling like you need some more interesting assignments than reading the online book and watching the lectures, check out the sample programming assignments on the website. Even the first one (Exercise 11) might blow your mind if you’re like me and not so used to functional programming.

Here’s a taste:

(define foo6
  (lambda (x)
    (x (lambda (y) (y y))))

Give a Curried application of foo6 that evaluates to 3. Keep in mind this means that at some point you have to evaluate an expression which is a function applied to itself! Yikes. I’ll post my solution later, if anybody’s interested, but I don’t want to give it away too easily. I probably spent a good hour thinking this last one over, and learned to think in a different way in the process.

I need to find a new way of embedding code in this blog. Since it’s hosted on WordPress, I can’t include Javascript embeds of a Gist or Pastie, and I also don’t really have control over the styling. Any suggestions?

Permalink 1 Comment

Cleaning XHTML & jarring gems

May 11, 2009 at 9:30 pm (Java, Limelight, Ruby, Week 8) (, , , , )

Back to my beloved XHTML 1.0 Strict and CSS today. I took some code that came from a client as a quick MS Word-generated web page, grabbed the important things, and deleted. And deleted. And deleted. Always a great feeling to get rid of redundancy. Code like this:

<tr>
<td bgcolor="#FFFFFF">&nbsp;&nbsp;&nbsp;</td>
<td bgcolor="#FFFFFF">Some Text</td>
<td bgcolor="#FFFFFF">&nbsp;&nbsp;&nbsp;</td>
</tr>
<tr>
<td bgcolor="#FFFFFF">&nbsp;&nbsp;&nbsp;</td>
<td bgcolor="#FFFFFF">Some More Text</td>
<td bgcolor="#FFFFFF">&nbsp;&nbsp;&nbsp;</td>
</tr>

makes me want to laugh and cry at the same time. Maybe it should start its own Broadway musical. At any rate, it’s a bad laughing and a bad crying, so it had to be fixed.

I got to check out Sinatra a little bit in the process, which was pretty cool. It took me a little bit to figure how to get Sinatra serving up images and stylesheets, but it’s very clear once you know!

set :public, File.dirname(__FILE__) + "/views"
set :views, File.dirname(__FILE__) + "/views"

Public and Views here have similarly-named analogues in Rails. They’re basically what you’d expect.

Micah, Doug, and I worked some more on jarring up gems, and despite some current limitations, we now have a great start on an idea for distribution. If you’re looking for answers on how to do it, check out Nick Sieger’s blog, the best source we’ve found on the topic. Be sure you know what commands and options you’re using, and how they relate to gem directory structure. I had a lot of trouble understanding how gem directories are laid out, and how that related to the jar directory structure, but now I have a good basic idea.

On an unrelated note, if you walk outside real quick to pet the cute new puppy next door, it’s great that you remembered to take your jacket, but it would be best to bring at least one of the following, since you locked the door on the way out: (1) keys, (2) phone, or (3) wallet with ID and spare key. This will avoid embarrassing encounters with neighbors you hadn’t met (although this is one good way to meet people). Not that it happened to me… just sayin’.

Permalink Leave a Comment