Conference videographer

Conference videographer

A couple of years ago, when our family was planning a trip to South Africa, I bought a nice DSLR. We had friends with excellent cameras, huge lenses and wild life photography experience. So instead of trying to compete with them I decided to buy a camera suited for filming. It was the rise of filming-DSLRs, and even television shows were adapting DSLRs for filming. I settled on a Canon 550D also known as Rebel T2i.

Just before I left for South Africa I decided to take the camera to the Devoxx 2011 conference. I didn’t even have a tripod at that time! After shooting some quick footage I decided to edit everything to make a nice movie and share it with the organiser. He liked it so much he placed it on the main website for a while. This is what I made:

Devoxx 2012

The following year Stephan asked me to be the official videographer for Devoxx 2012! So I went back and made another video, it seems like I’m turning from regular visitor/developer to videographer. I took some more time to analyse the conference to include the main sponsors, show every aspect of the conference and to get across the Devoxx atmosphere (including the James Bond theme, we all watched SkyFall during the conference!):

Devoxx4Kids 2012

The best time I had as videographer was during Devoxx4Kids. It is a separate event first hosted in 2012. The kids were having a great time and that is clearly visible in the video. Kids aren’t shy at all and much easier to approach then adults. That is why I did some quick interviews and I love the result, I might do that in the future for other conferences as well:

Devoxx UK 2013

The latest conference I filmed was Devoxx UK. For the first time the Devoxx conference has crossed the big blue sea to go to the United Kingdom. I’ve tried to make the resulting video look a bit more professional although I’m still just a Java developer with a hobby:

The future…

The last three years I’ve filmed at three weddings, four conferences and edited together our vacations to South Africa, Norway and some trips into the Alps (wintersport). As an amateur videographer (with a growing list of references) there still is a lot to learn, but I’m really enjoying the process! Any tips/hints/tricks are absolutely welcome, and I’m open for invitations!


Test Swing applications in Fitnesse (Nonkey)

Test Swing applications in Fitnesse (Nonkey)

Fitnesse

Fitnesse is a very popular test framework, used in a lot of projects. Using a wiki you describe the tests as plain text, which get translated into ‘fixtures’, code that is called that, in turn, can call your application. An example of a fixture we use:

|when the user logs in with username|roy               |and password   |test123   |
|and asks for the account details                                                 |
|ensure                             |displayed fullname|Roy van Rijn              |
|ensure                             |websites contains |http://www.royvanrijn.com/|
|ensure                             |websites contains |http://www.redcode.nl/    |

The corresponding Java code could be something like:

private Credentials credentials;
private AccountDetails accountDetails;

public void whenTheUserLogsInWithUsernameAndPassword(String username, String password) {
    credentials = new Credentials(username, password);
}

public void andAsksFotTheAccountDetails() {
    accountDetails = mySystem.getAccountDetails(credentials);
}

public boolean displayedFullname(String fullname) {
    return fullname.equals(accountDetails.getFullename());
}

public boolean websitesContains(String url) {
    for(String website:accountDetails.getWebsites()) {
        if(url.equals(website)) {
            return true;
        }
    }
    return false;
}

This is a very simplified example on how you could use Fitnesse to test your application. But the idea is very powerful, there are implementations for Java (initially) and now .NET, Groovy etc. Also, most of the time the system you’re testing doesn’t have a Java API but instead you’d like to test the user interface. With most Java applications being web applications there are numerous plugins to connect Fitnesse with for example Selenium to ease web application testing. The fixtures fill webpages and analyze the resulting HTML response.

At my current client (Port of Rotterdam) we’re building a very rich, partially offline, Swing application. This application has a backend and numerous clients running the Swing application. We’ve used Fitnesse to test the backend code from the start, and the last year we’ve put in some effort to also use Fitnesse for the Swing application. We’ve called this “Nonkey”, being an automated version of our Monkey integration testing we’d been doing up to that moment.

UISpec4J

The first ingredient we need to test Swing GUI’s in Fitnesse is UISpec4J. This is an excellent Swing test framework that allows you to manipulate GUI elements from code. The framework is made with unit-tests in mind, but can also be used in integration testing. In our SetUp page in Fitnesse we call the following code:

private Window mainWindow;
private JFrame mainFrame;
    
//Called from the Suite Set Up page:

public void launchNonkey() {
    mainWindow = WindowInterceptor.run(new Trigger() {
        @Override
        public void run() throws Exception {
            new OurApplication().launch();
            // Get the main JFrame from Spring, used directly in some cases instead of UISpec4J

            mainFrame = BeanFactory.getBean("mainFrame", MainFrame.class);
        }
    });
}

The WindowInterceptor captures the Swing application and allows for manipulation in a later stage. For example:

//In some fixture:

public void loginWithUsernameAndPassword(String username, String password) {
    //UISpec4J uses JComponent.setName() for lookup:

    Panel loginPanel = nonKey.mainWindow.getPanel("loginPanel"); 
    final TextBox usernameTextbox = loginPanel.getTextBox("usernameTextbox");
    usernameTextbox.setText(username);
    final TextBox passwordTextbox = loginPanel.getTextBox("passwordTextbox");
    passwordTextbox.setText(password);
    
    loginPanel.getButton("loginButton").click();
}

This fixture causes the Swing application to fill in the username, password and press the login button. It is easy to read and can be launched from Fitnesse with a single line:

|login with username|roy   |and password   |test123   |

Nice! We are done, we can test Swing GUI’s in Fitnesse now! Well no… not really, unfortunately there are a couple more problem we ran into, regaring the Event Dispatch Thread (EDT).

Event Dispatch Thread (EDT)

The EDT, sometimes called Swing Thread, is a special thread. Everything you do in Swing should always happen on this thread. Setting components to visible, pressing buttons etc, all need to be done in the EDT. If you don’t do this correctly the application gets into a lot of race conditions and it can even cause the application to hang.

When we combined Fitnesse with UISpec4J and started making a lot of tests we noticed that sometimes mysteriously a test would fail without changes. It turned out to be a problem with the EDT. UISpec4J doesn’t worry too much about the EDT, and when we click a button or setText (like the examples above) it does this in the current thread. In Fitnesse this thread is the thread that called the fixture, obviously not the EDT.

We brainstormed a lot about how we could fix this, maybe change UISpec4Js code? This turned out to be a hellish task. How about setting SwingUtilities.invokeAndWait() in every fixture? That could work but is also a massive undertaking. We decided to take a look at how Fitnesse calls the fixtures. This is always done from a ‘StatementExecutor’. So we decided to build a custom StatementExecutor which causes the fixtures to all run in the EDT.

To do this we need a custom SlimService. First I tried to extend the normal SlimService, but this wouldn’t work for some odd reason (still unexplained, should try this again). We decided to copy the SlimService code and make our own:

public class SwingSlimService extends SocketService {

    public static SwingSlimService instance = null;
    public static boolean verbose;
    public static int port;

    public static void main(final String[] args) throws Exception {
        if (parseCommandLine(args)) {
            startWithFactory(args, new SwingJavaSlimFactory()); //<-- custom SwingJavaSlimFactory!

        } else {
            parseCommandLineFailed(args);
        }
    }

    protected static void parseCommandLineFailed(final String[] args) {
        System.err.println("Invalid command line arguments:" + Arrays.asList(args));
    }

    protected static void startWithFactory(final String[] args, 
            final SlimFactory slimFactory) throws Exception {
        new SwingSlimService(port, slimFactory.getSlimServer(verbose));
    }

    protected static boolean parseCommandLine(final String[] args) {
        final CommandLine commandLine = new CommandLine("[-v] port");
        if (commandLine.parse(args)) {
            verbose = commandLine.hasOption("v");
            final String portString = commandLine.getArgument("port");
            port = Integer.parseInt(portString);
            return true;
        }
        return false;
    }

    public SwingSlimService(final int port, final SlimServer slimServer) throws Exception {
        super(port, slimServer);
        instance = this;
    }
}

Next we needed a custom JavaSlimFactory:

public class SwingJavaSlimFactory extends JavaSlimFactory {

    @Override
    public StatementExecutorInterface getStatementExecutor() {
        return new SwingStatementExecutor(); //<-- custom SwingStatementExecutor!

    }
}

And last but not least our custom StatementExecutor which forces all calls into the EDT thread:

package com.portofrotterdam.hamis;

import fitnesse.slim.StatementExecutor;
import fitnesse.slim.StatementExecutorInterface;

/**
 * HaMIS custom StatementExecutor, to call and create in the EDT thread
 */
public class SwingStatementExecutor implements StatementExecutorInterface {

    private final StatementExecutor wrapped = new StatementExecutor();

    private abstract class EDTRunnable implements Runnable {

        private Object returnValue;

        @Override
        public abstract void run();

        public void setReturnValue(final Object returnValue) {
            this.returnValue = returnValue;
        }

        public Object getReturnValue() {
            return returnValue;
        }
    }

    @Override
    public Object call(final String instanceName, final String methodName, final Object... args) {

        final EDTRunnable caller = new EDTRunnable() {

            @Override
            public void run() {
                setReturnValue(wrapped.call(instanceName, methodName, args));
            }
        };
        EDTRunner.run(caller);
        return caller.getReturnValue();
    }

    @Override
    public Object callAndAssign(final String variable,
            final String instanceName,
            final String methodName,
            final Object[] args) {

        final EDTRunnable callAndAssigner = new EDTRunnable() {

            @Override
            public void run() {
                setReturnValue(wrapped.callAndAssign(variable, instanceName, methodName, args));
            }
        };
        EDTRunner.run(callAndAssigner);
        return callAndAssigner.getReturnValue();
    }

    @Override
    public Object create(final String instanceName, final String className, final Object[] args) {

        final EDTRunnable creater = new EDTRunnable() {

            @Override
            public void run() {
                setReturnValue(wrapped.create(instanceName, className, args));
            }
        };
        EDTRunner.run(creater);
        return creater.getReturnValue();
    }

    @Override
    public Object addPath(final String path) {
        return wrapped.addPath(path);
    }

    @Override
    public Object getInstance(final String instanceName) {
        return wrapped.getInstance(instanceName);
    }

    @Override
    public void reset() {
        wrapped.reset();
    }

    @Override
    public void setVariable(final String name, final Object value) {
        wrapped.setVariable(name, value);
    }

    @Override
    public boolean stopHasBeenRequested() {
        return wrapped.stopHasBeenRequested();
    }
}

This uses a small static class we’ve used in our code before to safely run a Runnable on the EDT and invokeAndWait (without waiting IN the EDT):

public class EDTRunner {

    public static void run(final Runnable r) {
        if (SwingUtilities.isEventDispatchThread()) {
            r.run();
        } else {
            try {
                EventQueue.invokeAndWait(r);
            } catch (final InterruptedException e1) {
                throw new RuntimeException(e1);
            } catch (final InvocationTargetException e1) {
                throw new RuntimeException(e1);
            }
        }
    }
}

The only thing left to do is to instruct Fitnesse to use our SlimService instead of the default implementation. This is done using the following parameter in our main page:

!define TEST_SYSTEM {slim}
!define TEST_RUNNER {com.portofrotterdam.hamis.SwingSlimService}

Make sure that the class can be found (for example, package it in a JAR and place it on Fitnesses classpath). And you’re done! Using a ThreadCheckingRepaintManager (workings described here) we checked to see if the tests are now repainting/manipulating Swing from the correct thread, and it works like a charm.

Good luck testing with Fitnesse in the near future, if you have any problems/improvements/ideas please let me know (in the comments below).


Facebook status: Deceased

Facebook status: Deceased

In the next couple of years dead will become an important aspect of the social networks. A time will come that most profiles on Facebook will belong to people who’ll never be able to post another update. Currently due to inactivity most dead profiles will be deleted or become inactive, but I think dead will take a much more important role on the internet.

My parents and even grandparents have a Facebook account now, but in twenty or thirty years, some people I’m connected to won’t be alive anymore. It would be great to be able to browse the lives of people who are no longer around us. I’d love to read about my deceased grandmother for example, just to see how she lived her life. So instead of deleting inactive accounts, death should become more accepted and a part of our social web as well.


Devoxx 2012, the video

Devoxx 2012, the video

Yesterday when I returned home from another amazing Devoxx conference I quickly created another impression video:


Devoxx4Kids

Devoxx4Kids

A couple of weeks ago I went to Devoxx4Kids. Devoxx is a very well known Java conference (the best in the world actually!). This time they organized an event especially for kids ranging from 10 to 14 years old. The goal is twofold, the main goal is to just give them a good time, the second goal is to introduce them to programming and technology in a fun way.

To show the world this is a great idea, Stephan invited me to film the event and capture the happy faces of the kids enjoying themselfs. And now after Devoxx 2012, I’m allowed to show it to everybody:

(more information in Dutch and French: http://www.devoxx.com/display/4KIDS/Home)


Stacktraces should be inverted

Stacktraces should be inverted

Stacktraces in Java are wrong. This is a conclusion that I’ve drawn after years of working with novice programmers.

What is the first thing novice programmers do when they encounter a stacktrace? They start reading it, and this is wrong! The first thing you should do is scroll down. That is because the most important piece of the stacktrace is most likely at the bottom.

Caused by…

Stacktraces in Java are usually wrapped numerous times, because all the frameworks and the application server wants to add their own little bit of information around the original exception. The problem is that Java uses the “Caused by” way of printing the stacktrace. With the “Caused by” you’ll get a lot of information about what happened (afterwards) first, until you get to the actual reason.

For example:

com.portofrotterdam.hamis.service.offline.WebServiceException: Error during webservice call.
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doWebserviceCall(BaseSynchronizingWebservice.java:111)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doCallWithCallback(BaseSynchronizingWebservice.java:97)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doActualCall(BaseSynchronizingWebservice.java:77)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.access$000(BaseSynchronizingWebservice.java:44)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice$1.run(BaseSynchronizingWebservice.java:63)
	at java.lang.Thread.run(Unknown Source)
Caused by: com.portofrotterdam.hamis.service.offline.WebServiceException: Problem while getting data from webservice and translation:
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:60)
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:42)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:26)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:8)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doWebserviceCall(BaseSynchronizingWebservice.java:107)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doCallWithCallback(BaseSynchronizingWebservice.java:97)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doActualCall(BaseSynchronizingWebservice.java:77)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.access$000(BaseSynchronizingWebservice.java:44)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice$1.run(BaseSynchronizingWebservice.java:63)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
	at com.sun.deploy.security.CPCallbackHandler.isAuthenticated(Unknown Source)
	at com.sun.deploy.security.CPCallbackHandler.access$1300(Unknown Source)
	at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(Unknown Source)
	at com.sun.deploy.security.DeployURLClassPath$JarLoader.checkResource(Unknown Source)
	at com.sun.deploy.security.DeployURLClassPath$JarLoader.getResource(Unknown Source)
	at com.sun.deploy.security.DeployURLClassPath.getResource(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:351)
	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:345)
	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:337)
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:48)
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:42)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:26)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:8)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doWebserviceCall(BaseSynchronizingWebservice.java:107)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doCallWithCallback(BaseSynchronizingWebservice.java:97)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doActualCall(BaseSynchronizingWebservice.java:77)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.access$000(BaseSynchronizingWebservice.java:44)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice$1.run(BaseSynchronizingWebservice.java:63)
	at java.lang.Thread.run(Unknown Source)

The first two traces aren’t at all important, it tells us something went wrong with a webservice call. But the bottom trace gives us the real reason, a NullPointerException!

Caused!

I’d rather see stacktraces being inverted, so instead of saying:

**Webservice failed which is caused by -> Webservice failed during translation which is caused by -> A NullPointerException!**

It should say:

**A NullPointerException which caused -> Webservice failed during translation which caused -> Webservice failed…**

So using the example above it would become:

java.lang.NullPointerException
	at com.sun.deploy.security.CPCallbackHandler.isAuthenticated(Unknown Source)
	at com.sun.deploy.security.CPCallbackHandler.access$1300(Unknown Source)
	at com.sun.deploy.security.CPCallbackHandler$ChildElement.checkResource(Unknown Source)
	at com.sun.deploy.security.DeployURLClassPath$JarLoader.checkResource(Unknown Source)
	at com.sun.deploy.security.DeployURLClassPath$JarLoader.getResource(Unknown Source)
	at com.sun.deploy.security.DeployURLClassPath.getResource(Unknown Source)
	at java.net.URLClassLoader$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(Unknown Source)
	at com.sun.jnlp.JNLPClassLoader.findClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at java.lang.ClassLoader.loadClass(Unknown Source)
	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:351)
	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:345)
	at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:337)
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:48)
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:42)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:26)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:8)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doWebserviceCall(BaseSynchronizingWebservice.java:107)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doCallWithCallback(BaseSynchronizingWebservice.java:97)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doActualCall(BaseSynchronizingWebservice.java:77)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.access$000(BaseSynchronizingWebservice.java:44)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice$1.run(BaseSynchronizingWebservice.java:63)
	at java.lang.Thread.run(Unknown Source)
Caused: com.portofrotterdam.hamis.service.offline.WebServiceException: Problem while getting data from webservice and translation:
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:60)
	at com.portofrotterdam.hamis.service.webservice.WebServiceProxy.callWebService(WebServiceProxy.java:42)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:26)
	at com.portofrotterdam.hamis.service.webservice.GetAllTemplateInspectionCheckListsSynchronizingWebservice.callWebservice(GetAllTemplateInspectionCheckListsSynchronizingWebservice.java:8)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doWebserviceCall(BaseSynchronizingWebservice.java:107)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doCallWithCallback(BaseSynchronizingWebservice.java:97)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doActualCall(BaseSynchronizingWebservice.java:77)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.access$000(BaseSynchronizingWebservice.java:44)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice$1.run(BaseSynchronizingWebservice.java:63)
	at java.lang.Thread.run(Unknown Source)
Caused: com.portofrotterdam.hamis.service.offline.WebServiceException: Error during webservice call.
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doWebserviceCall(BaseSynchronizingWebservice.java:111)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doCallWithCallback(BaseSynchronizingWebservice.java:97)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.doActualCall(BaseSynchronizingWebservice.java:77)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice.access$000(BaseSynchronizingWebservice.java:44)
	at com.portofrotterdam.hamis.service.webservice.BaseSynchronizingWebservice$1.run(BaseSynchronizingWebservice.java:63)
	at java.lang.Thread.run(Unknown Source)

This is much easier to understand (IMHO). The first exception you see is now also the first thing that happened. Much clearer for the novice programmer.

Some more tips when you encounter a stacktrace:

  • Look for the first line that starts with YOUR package, this is where your control over the code starts, where you might be able to fix it
  • If there are a lot of other packages mentioned before YOUR package, Google it! You are probably not alone

Hacking with the Mara-Naboisho Lion Project

Hacking with the Mara-Naboisho Lion Project

Yesterday a friend of mine pointed me to the following thread:
https://github.com/giving/giving.github.com/issues/11

lion-with-collar

giving.github.com

Giving.github is a project that tries to bring people together. There are a lot of programmers looking for fun projects in the world, and so are scientists/charities and other people with problems!

Be sure to check it out, maybe you can help somebody as well.

Mara-Naboisho Lion Project

One of the problems posted to giving.github has something to do with Lion collars. The Mara-Naboisho Lion Project has a couple of collars and some files, but no idea what the data inside the files mean. Their initial description was:

The Mara-Naboisho Lion Project has asked for our help decoding and plotting the positions of lions fitted with GPS collars.

They receive SMSes from the collars:

File: Collar07854_100806210058.SMS

074952494449554d0000000000000000000000000000000000000000
000000040f3330303033343031323239373839300000000000000000
0000000000000000f10a0806100028008c13ef348a0039d0fe000de8
71004cc92b5ca92d6213ef26640039d108000de86b004cc92d5ca92d
5d13ef18620039d101000de865004cc92c5ca92d5813ef0a930039d0
fc000de864004cc9311c682d5413eefc170039d045000de7d4004cc9
5b7c692c5013eeee280039d0ff000de85f004cc92a7c692d6fffffff
ffffffffffffffffffffffffffffffffffff
    
File: Collar07854_100807060011.SMS

074952494449554d0000000000000000000000000000000000000000
000000040f3330303033343031323239373839300000000000000000
0000000000000000f10a0807020038008c13efb2eb0039d0ed000de8
53004cc92e3cea2d8813efa5060039d0fb000de860004cc9291c6a2d
8413ef96fd0039d0fc000de85e004cc92d5c6a2d7f13ef88e00039d0
f6000de85a004cc92b5c6a2d7b13ef7ad80039d0fa000de85a004cc9
327c6a2d77ffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffff

These are presumably SMS PDUs, although simple online decoders don’t seem to make much sense of them. There’s the ASCII text “IRIDIUM” in them, presumably being the SMSC or sender or something string, as IRIDIUM is a satellite telephone system and presumably the bearer when the collars send up their positions.

What format is it, and how do we decode it?

Not SMS PDU

This is where I started analyzing, and I quickly ruled out that the hex-code is pure PDU (which can easily be translated). It seems to be some dialect/own protocol.

The first major breakthrough was realizing that there is one big header that both files have in common, and the real message starts with 0A. And the two messages after that have a repeating pattern:

Common header?

074952494449554d000000000000000
0000000000000000000000000000000
040f333030303334303132323937383
9300000000000000000000000000000
0000f10a

Sub header?
SMS 1: 0806100028008c13
SMS 2: 0807020038008c13


Message #1:
ef348a0039d0fe000de871004cc92b5ca92d6213
ef26640039d108000de86b004cc92d5ca92d5d13
ef18620039d101000de865004cc92c5ca92d5813
ef0a930039d0fc000de864004cc9311c682d5413
eefc170039d045000de7d4004cc95b7c692c5013
eeee280039d0ff000de85f004cc92a7c692d6fff
ffffffffffffffffffffffffffffffffffffffff

Message #2:
efb2eb0039d0ed000de853004cc92e3cea2d8813
efa5060039d0fb000de860004cc9291c6a2d8413
ef96fd0039d0fc000de85e004cc92d5c6a2d7f13
ef88e00039d0f6000de85a004cc92b5c6a2d7b13
ef7ad80039d0fa000de85a004cc9327c6a2d77ff
ffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffff

This sure adds some structure to the files. I also guessed the first file has actually 6 entries and the second file has only 5 entries.

First field deciphered

The second breakthrough came through Twitter. I posted this problem in a tweet and after a couple of hours I got a reply that there was progress on the stackoverflow question.

This was DStibbe’s reply:

-——————————————————————————–
Perhaps the ‘13’ is not part of the headers but the message instead?
The messages would then be:

Message #1
13ef 348a 0039 d0fe 000d e871 004c c92b 5ca9 2d62
13ef 2664 0039 d108 000d e86b 004c c92d 5ca9 2d5d
13ef 1862 0039 d101 000d e865 004c c92c 5ca9 2d58
13ef 0a93 0039 d0fc 000d e864 004c c931 1c68 2d54
13ee fc17 0039 d045 000d e7d4 004c c95b 7c69 2c50
13ee ee28 0039 d0ff 000d e85f 004c c92a 7c69 2d6f
ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff
ff

Message #2
13ef b2eb 0039 d0ed 000d e853 004c c92e 3cea 2d88
13ef a506 0039 d0fb 000d e860 004c c929 1c6a 2d84
13ef 96fd 0039 d0fc 000d e85e 004c c92d 5c6a 2d7f
13ef 88e0 0039 d0f6 000d e85a 004c c92b 5c6a 2d7b
13ef 7ad8 0039 d0fa 000d e85a 004c c932 7c6a 2d77
ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff
ffff ffff ffff ffff ffff ffff ffff ffff ffff ffff
ff

It does seem like a log , with the message for Aug 06 (guessing) has 6 entries and the second from Aug 07 having 7.
What would a collar record? I’m guessing longitude,latitude and timestamp?

Chances are high that the first eight digits represent the timestamp. They are incremental from bottom to top.

Eg. 13ef348a = 334443658.
334443658 seconds = 10 years, 8 months, 6 days, 23:00:58

This matches the date in the title of the first sms: Collar07854_100806 210058.SMS
It even matches the timestamp minus 2 hrs in the title!
-——————————————————————————–

More information

At this point the progress stopped a bit, we couldn’t get the other bytes translated. There seemed to be certain integer values: 0039 d0ed for example. This probably isn’t floating point (which we thought it would be, latitude/longitude/altitude) because floating point numbers have the exponent in the first 11 bits. When a number starts with 00’s it doesn’t have the exponent part filled.

So we asked the project for some more information, for example about the recorded data (where/when) and the manufacturer of the collars.
They gave us this KML file, and this company name: Vectronic-Aerospace.

Lat/lon, I’ve got you!

The biggest part we wanted to decipher was the lan/lon location (that is what the researchers want to have). I came to this breakthrough after reading a PDF file from Vectronic-Aerospace’s website. It has some screenshots from their original software (why aren’t they using that free software?!). In these screenshots I noticed integer coordinates very close to the integers I’ve managed to get from the hex-data! It was called ECEF data, this is X,Y,Z data! Until now my guess was that the SMS files had lat/lon coded in binary, but it has XYZ information.

After a quick google I managed to find some code to translate from ECEF coordinates to lat/lon and it gave me the following location:
Lat: 52.43067054630929
Lon: 13.525755535406619
Height: 88.99500464554876

When plotted in Google Maps it gave me a location in Germany, at first I thought it was another dead end, but then I looked here. It turns out it was the EXACT location of Vectronic Aerospace, the creators of the collar! Problem solved.

What’s next?

There are still some mysteries, for example… what are the last bytes? Looking at the screenshots in the PDF I suspect it would be main battery voltage, backup voltage and temperature. The project also probably wants to integrate the collar files with the website. And there are of course a lot more giving.github.com problems to solve!


Visitors, you make me proud

Visitors, you make me proud

ripIE
With the sudden surge of visitors from Reddit (Hello!) I decided to take look at Google Analytics. And two things caught my eye:

First of all, Google Analytics ‘Realtime’ is awesome, you can see minute by minute how many people are on your website and what they are looking at, where they come from (internetwise and geographically).

Second, my website gets the right type of visitors, I’m so proud at you guys and girls…

1.	Chrome			53.83%
2.	Firefox			20.70%
3.	Safari			8.83%
4.	Mozilla			7.48%
5.	Android	Browser		4.85%
6.	Internet Explorer	1.98%
...

Java vs Javascript: Speed of Math

Java vs Javascript: Speed of Math

Last week I’ve created a ray marcher 3d engine which renders the Mandelbulb. And I’ve translated it into pure Javascript a couple of days later. After the translation I decided I should optimize the code a little for speed, so I made some speed improvements in the Javascript code. The main optimization was using an array for the vector3d instead of a class/function.

Rendering the Mandelbulb on a 400x400 canvas now took just 1850ms in Javascript (Chrome, V8). Which is very fast! Even faster than my Java implementation (running on Java 1.6.0.33 -server, which was faster than Java 7). But the Java code didn’t have some of the speed optimizations. So I re-translated the Javascript code back to Java. It produced the following numbers (lower is better performance):

Comparing Javascript and Java

What has happened here? The output is the same, why is Java so much slower than Javascript? I would have suspected the opposite…

I fired up the profiler to see what was causing the Java code to be so slow, and it turned out the method it spend most time in was Math.pow(). Other slow methods were Math.acos(), cos(), sin() etc. It turns out that the Math library isn’t very fast, but there is an alternative, FastMath. Apache Commons has implemented a faster Math library for commons-math. Lets see what changing Math.* to FastMath.* does to the performance:

compare2

This is already much better. But still the method causing most delay is FastMath.pow(). Why is Javascript so much faster? The method is made so you can calculate the power of two doubles, not only integer values. But I’m only doing Integer powers (7 and 8 to be precise). So I decided to implement my own method:

private double fasterPow(double d, int exp) {
	double r = d;
	for(int i = 1; i<exp; i++) {
		r *= d;
	}
	return r;
}

Warning: This isn’t the same as Math.pow/FastMath.pow!

compare3

The speed with this new method is much better and seems comparable with Javascript. Maybe this is an optimization the V8 engine does by default? Who knows.

The slowest method in the program now is FastMath.acos. From highschool I know that acos(x) can also be calculated as atan(sqrt(1-x*x)/x). So I created a own version of acos. When benchmarked, the different methods: Math.acos(), FastMath.acos() and FastMath.atan(FastMath.sqrt(1-x*x)/x), the result is again surprising:

compare4

The custom acos() function is a bit faster than FastMath.acos() and a lot faster than Math.acos(). Using this function in the Mandelbulb renderer gives us the following metric:

compare5

So it turns out that with a bit of tweaking we can get the Java version faster than Javascript, but I would have never imagined Java would be slower in the first place. The Chrome V8 guys really did an amazing job improving the speed of their Javascript VM. Mozilla isn’t far behind, they are getting +/- 2200 ms in the benchmark. Which is also faster than Java.Math and FastMath! It seems that V8’s math implementation has some optimizations that Java could really use. The tricks used above don’t make any difference with the Javascript version.

Edit 1: Is Javascript faster than Java?

Well surprisingly in this case it is. With the code a 100% the same, using arrays as vector and Math.* the code actually runs faster in my browser!

Edit 2: People have been asking me: What could have been done to make it faster in Java? And, why is it slow?

Well the answer is twofold:

1) The Math libraries are made for ‘double’ in Java. Having a power() method work with doubles is much harder than working with just integer numbers. The only way to optimize this would be to overload the methods with int-variants. This would allow much greater speeds and optimizations. I think Java should add Math.pow(float, int), Math.pow(int, int) etc.

2) All the Math libraries have to work in all situations, with negative numbers, small numbers, large numbers, zero, null etc. They tend to have a lot of checks to cope with all those scenario’s. But most of the time you’ll know more about the numbers you put in… For example, my fastPower method will only work with positive integers larger than zero. Maybe you know that the power will always have even numbers…? This all means that the implementation can be improved. The problem is, this can’t be easily achieved in a generic (math) library.


Introducing: mandelbulb.js

Introducing: mandelbulb.js

This afternoon Will (the friend I mentioned in this previous post) showed up again on Google Talk.
He set me a new challenge: “How about translating your 3d engine/mandelbulb code into pure Javascript?”

First Mandelbulb render, full of programming bugs

This seemed impossible, but I quickly realized the code I had was very easy to convert. It is my first piece of Javascript larger than 10 lines I think, but it is working!

If you have a bit of time:

mandelbulb.js

Any Javascript developers who would like to take the code and improve on it (trust me, there is more then enough room for improvement) go ahead! I’m releasing it under creative commons, do with it what you like, just be sure to mention me!

The code can be found here: https://github.com/royvanrijn/mandelbulb.js

I’ve already included one improvement by Will himself, scanlines to smooth up the rendering (good for inpatient people!).


Mandelbulb rendering

Mandelbulb rendering

Last friday a friend of mine was talking about ray marching, the Mandelbulb and programming his own 3D fractal engine. He also kind of challenged me to do the same… So I picked up the challenge and set to work on my own 3D (CPU only) ray marching fractal engine (in Java). It was a very steep learning curve for a programmer with limited math knowledge, but I’m pretty pleased with the first results!

My first 3d engine (tm)

On sunday I had the first things ready, lighting (Blinn-Phong), soft shadows, but still I had no perspective build on (all rays travelled in the same direction) and I had no way to change the view point/camera position:

Simple ray marching example, wrong perspective, two balls and a cube

Next step was to render something other than spheres and cubes, and get the camera position under control, that breakthrough came monday evening:

First Mandelbulb render, full of programming bugs

My first mandelbulb! The perspective is still flat and it misses detail, we’ve also rendered through the camera/near field.
So I made some more improvements and managed to render a nice wallpaper for myself:

First Mandelbulb render, full of programming bugs

Yesterday I’ve been playing around with optimizing the code a little bit (less memory usage, already twice as fast as it was, but still slow). And I’ve added some glow and the ability to add ‘distance fog’. Also I’ve recorded my first movie, just to show some moving fractals:

(yes, it has a glitch with the pink mandelbulb, can’t be bothered to fix…)


Rotoscoping in Prince of Persia

Rotoscoping in Prince of Persia

This afternoon I was reading about rotoscoping. Rotoscoping is a technique where an artist takes a filmed movie and animates a drawn (cartoon) character frame by frame. The end result is highly realistic. The technique was first used in 1915’s with an ink cartoon called Out of the Inkwell and has been used in many cartoons since.

But what about video games?

It turns out that Prince of Persia was the first computer game to use rotoscoping. That game brings back a lot of good memories to me, and probably everybody my age.

After a quick search I found this little gem of a video:

The person in the video is David Mechner. He is the younger brother of Jordan Mechner, the creator of Prince of Persia.
Jordan used the clips shown above to rotoscope the movements of the main character in the game.

I had never realised why I loved Prince of Persia so much in my youth. But now I think it is probably because of rotoscopy! The animations are so realistic and life like, it has brought a whole new level of realism into video games.

Here is another video of Jordan, showing even more rotoscoping in Prince of Persia:

And the icing on the cake, a video about rotoscoping in Mortal Kombat 1:


Getting started: Arduino Mega

Getting started: Arduino Mega

A couple of days ago I’ve received my Arduino Mega in the mail. Together with a breadboard, some plug-wires and Arduino-starters kit (with some resistors, capacitors, and a couple of sensors).

arduino_mega

Arduino?

The Arduino is a handy little board with micro controller. It allows you to connect your computer using USB to the micro controller and easily program and upload the program to the board. I’m a very experienced programmer with almost no experience in electronics.

I know what a resistor is and what it does, but I have no idea how/when to use which resistor. But I’d love to learn more, maybe even build some toy robots in the end. A final goal would be to have a working humanoid with 20+ servo’s, but that goal might be a bit out of reach…

The examples

The first thing I’ve done after unboxing the Arduino was plugging it in my laptop and browsing to the arduino.cc website. There you can download the Arduino IDE.

In the ‘Learning’ section (http://arduino.cc/en/Tutorial/HomePage) you’ll find everything you need to get started. There are diagrams on how to wire everything into the breadboard, and there are also code examples.

Some stuff I made using the ‘Learning’ page:

  • Blinking LED (the hardware Hello World, yay!)
  • Slowly fading blinking LED
  • Knight rider, KITT LED bar
  • Tiny keyboard with 5 buttons
  • Light reactive theremin

And some projects that aren’t on the example page, but could be made using the things I’ve learned:

  • Extended keyboard with 5 buttons and LEDs!
  • Light reactive LED bar (showing the amount of light in LEDs)
  • Knock-sensor, LED shows when it ‘feels/hears’ a knock

All the above was made in a single evening! And there is much much more that you could make with the Arduino. I haven’t even started using servos and/or DC motors. The only problem left is lack of imagination!

For a programmer the code is very easy to understand, and the Arduino Programming Language used is closely related to the C/Java family.

/*
  keyboard
 
 Plays a pitch that changes based on a changing analog input
 
 circuit:
 * 3 force-sensing resistors from +5V to analog in 0 through 5
 * 3 10K resistors from analog in 0 through 5 to ground
 * 8-ohm speaker on digital pin 8
 
 created 21 Jan 2010
 modified 9 Apr 2012
 by Tom Igoe 

This example code is in the public domain.
 
 http://arduino.cc/en/Tutorial/Tone3
 
 */

#include "pitches.h"

const int threshold = 10;    // minimum reading of the sensors that generates a note


// notes to play, corresponding to the 3 sensors:

int notes[] = {
  NOTE_A4, NOTE_B4,NOTE_C3 };

void setup() {

}

void loop() {
  for (int thisSensor = 0; thisSensor < 3; thisSensor++) {
    // get a sensor reading:

    int sensorReading = analogRead(thisSensor);

    // if the sensor is pressed hard enough:

    if (sensorReading > threshold) {
      // play the note corresponding to this sensor:

      tone(8, notes[thisSensor], 20);
    } 
  }
}

The language has borrowed a lot of syntax from C, but there are some tricky issues (in the eyes of a programmer). For example:

int sensorValue = analogRead(A0); 
String stringThree = "Sensor value: " + sensorValue;
Serial.println(stringThree);

This little piece of code above gives unpredictable results because ‘stringThree’ never got an initial value before you started concatenating different data types. Instead you’ll have to do:

int sensorValue = analogRead(A0); 
String stringOne = "Sensor value: ";
String stringThree = stringOne + sensorValue;
Serial.println(stringThree);

Weird…! But I really suggest trying out the Arduino, give it a chance and you’ll probably like it and learn a lot about electronics and microcontrollers.


We are now cached

We are now cached

Yesterday we (at JPoint) invited Stefan Tilkov over for a discussion about REST and RESTful services. He knows a lot about the subject and could educate us and help with any questions.

One of the things he mentioned was: If you don’t use caching, you are an idiot.

Where do websites cache?

There are multiple tiers where caching of websites is done, and is useful.

Browser cache

The best cache you can have is the cache inside the browser. If a website knows it has the latest version, it can just read it from disk. There is absolutely no reason to go online.

Proxy cache

The second type of cache would be the proxy cache. As you would have guessed this is a proxy and it does caching. It sits between the user/browser and the internet gateway. This cache sees all the requests and stores pages that can be cached. If another user requests a webpage that hasn’t changed it can provide the page instantly.

Reversed proxy cache

You could also have a cache between the internet and the content providing server. If the server processes the request it might need to access databases and maybe other slow resources to build up the webpage. The resulting page can than be cached on the providing side in a “reverse” proxy cache. All subsequent requests can just be provided from the cache, as long as the page is still fresh.

Making pages cacheable

If you maintain a website, or you create web applications, you should be aware of caching. After Stefan’s rant, I’m completely convinced about that. If you don’t do anything all the requests will always go into the server and over the internet. There are HTML ways to control caching (META-Tags etc) but this just doesn’t work, and shouldn’t be used (!). So what could we do?

Expires header

When sending a page back to the user you are able to set some HTTP headers. And “expires” is one of them.
An example:

Expires: Fri, 11 May 2012 18:19:42 GMT

This indicates that the current page is valid until the timestamp. Then it ‘Expires’. Easy!

The only problem is generating the timestamp, it can be a bit tricky. Also you’ll have to be sure you’ve set the time correct on your system. Also, the next time you update the page, you have to also update the timestamp!

Cache-Control headers

With HTTP 1.1 there is a new class of headers called “Cache-Control”. These headers are more powerful than the Expires header.
To enable caching using Cache Control headers you can set:

Cache-Control: max-age=3600, must-revalidate

The “max-age” is time in ms that the current page is valid. And by adding “must-revalidate” we tell the cache it should obey our max-age. If you don’t want an object to be cached you can use:

Cache-Control: no-cache

Refreshing cached data

The two methods described above will tell the cache if the content is cacheable. But what happens when the max-age or Expires timestamp expires? There are smarter ways to update the cache instead of getting the latest content from the server.

Last-Modified

Websites should always set the response header called “Last-Modified”. This is a timestamp of the moment a webpage last changed.

Last-Modified: Fri, 11 May 2012 18:19:42 GMT

When a cache has expired (max-age or Expires) and has to get a new version from the server it can set the request header “If-Modified-Since” and include the timestamp.

If-Modified-Since: Fri, 11 May 2012 18:19:42 GMT

If the content on the server hasn’t been changed it’ll reply “304 Not Modified”. The cache can now keep the cached version.

ETag

With HTTP 1.1 there is also an improved method of doing the “Last-Modified”. Instead of using a timestamp (which is error prone), they’ve introduced the “ETag”. This is a tag that is completely customisable. Most of the time it will just be a hash of the content. The server sets the ETag as response header:

ETag: "686897696a7c876b7e"

When a cache can no longer use the cached version (due to max-age or Expires) is will ask the server:

If-None-Match: "686897696a7c876b7e"

The term “If-None-Match” isn’t very clear, but is means “if-etag-changed-since” and works the same way as “If-Modified-Since”. When the ETag is the same the server will reply “304 Not Modified”, it won’t send the content back.

When you are working on a web application you could just add an ETag which is the MD5 of the returning content. If the content is the same, you don’t have to send the content over the line. The only drawback to this method is that you still need to generate the entire reply to calculate the MD5 hash to see if the content has changed…! But sometimes you’ll know in advance if the content has been changed.

Improving royvanrijn.com

I’m using WordPress and I’ve found the excellent plugin “WP Total Cache”.

It will involve a bit of tweaking, because only you can decide which stuff should be cached. But I think it worked out great, press F5 right now and you’ll probably be reading this from the browser cache.


Ludum Dare #23: Itty-bitty botty

Ludum Dare #23: Itty-bitty botty

bodyIdle1The last two days I’ve been competing in a competition called Ludum Dare.

This is a short, 48 hour, contest. In this time you have to build an entire game based on a theme given at the start of the 48 hours. It is a good exercise is planning, scaling, hacking, imagining and just having fun! I really enjoyed it, and recommend you join LD24 four months from now.

The concept:

For this game I decided to stick with Java. To make it playable for as many people I decided to make an Applet. It can easily become a standalone app, or maybe an Android app…!
I loved the old point-and-click games, from Dirty Larry to The Day of the Tentacle, from Monkey Island to Gobli(iiii)ns. So that was settled.

The big pro:

Not a lot of physics or game code.

The big con:

I’d have to brush off my paint skills because point-and-click adventures are filled with graphics and animation!
One big factor in games is music, and for this contest I took some midi control code I made some years ago. This was turned into a procedurally generated music generator. Every time you play you’ll hear something new.

The result:

With visitors coming to see our little baby girl on Sunday I decided to end early.

Screenshots:
screenshot1
screenshot2

Here is my result, have fun playing the game: Itty-bitty botty!