Posts Tagged ‘java’

First steps in Scala, second steps in Play Framework

27 september 2012

Last year I experimented with the Play Framework to find out how it works and what its strengths and weaknesses are. Although I was quite enthousiastic about it, I did not look at it any further, seeing no immediate need for it in my professional life.

Now, a year later, I decided to dive into Scala. A free online course is being given by the creator himself, Martin Odersky, on Coursera. I am very happy about both the presentation and the level of the course. The lectures and the exercises are of a high level, and the whole package is well thought through.

As it turns out, the two can be integrated seamlessly, as Scala is the native language of the Play Framework, and is one of the languages of choice (Java being the other) for projects. Here are some findings that I have about the both.

As many Java developers, I am quicky falling in love with Scala. Its syntax is very elegant, but most of all being a functional language it is extremely powerful. At the same time you don’t have to throw Object Oriented Programming overboard. Scala is specifically designed to suppport both OO and functional programming. As Odersky says it, the two paradigms don’t interfere and can be used together well. To be honest, I can not think of anything why Java should still be favoured over Scala, other than the smaller pool of programmers with knowledge of the latter.

The idea of the Play Framework is to bypass the entire Java Servlet stack. Instead it has its own stack built up from the ground. Very noticeable are the short cycles in the development. You edit some code, refresh and see the output in the browser window. No need to look at an empty screen and look at logging to find out what has gone wrong. Even compilation errors show up in the browser. There are numerous helper utilities for HTTP traffic handling, remote web service calling, JSON handling etcetera. In general, working with Play is really fun! It brings back the joy in web development that has been far away with many previous frameworks.

In my research I have started an app that currently does nothing more than make users log in through Facebook or Google. On first login, a User object is created and saved. All other domain classes will circle around it, like with most typical apps. There are two advantages to this approach. First, I don’t need to write the authentication myself, and just rely on Google and Facebook to do it better than I possibly could. I also can’t make any passwords leak, as I simply don’t have them. Second advantage is that I will have access to the users friend list and other data at a later time. As it is such a common requirement, I decided to save the app in its current state as a template for new Play Framework/Scala apps where logging in to social networks is required. The code is on Github: https://github.com/jkransen/PlayScalaGoogleFacebookTemplate. Feel free to download/fork it and start your own project from there.

Advertenties

Raspberry Pi

16 juli 2012

Since a few days I’m the proud owner of a Raspberry Pi. It’s a simple, but complete computer that allows -or rather encourages- tweaking. Since I ordered it (months ago), I have been thinking about what to do with it. There are some features that make the Pi special:

  • Cheap, got mine for €35 including shipping.
  • Small, a bit bigger than a credit card.
  • Runs popular Linux distros, notably Debian and Arch Linux, with Fedora expected soon.
  • It has good connectivity options: USB, Ethernet and open pins that allow you to use low-level protocols such as GPIO, I2C and SPI.
  • It uses little power, somewhat like 4W.

Application

Some people got XBMC running on it, or use it for educational software like Scratch. To me that does not utilize the benefits of the Pi, as these can be run (faster) on any other computer. Instead, its low power consumption and small form factor make it particularly portable. So much so, that one was sent almost into space! Add a smallish solar panel and a battery, and you can place a running computer anywhere! Instead of the solar panel, you can hook it to the cigarette lighter socket of your car. You can program it to do all kinds of measurements and take decisions by controlling motors. With its network interface, you can read its measurements from a distance or manually trigger its controls.

Smartdust

The Raspberry Pi brings the concept of smartdust closer. Instead of one, you can have many Pis communicating with each other. The only state they have in between restarts is the SD card they boot from. Copy that to another Pi, and you have two identical machines.

Where to start

The distro I chose was Debian. It is widely used and supported, and comes with a lot of software that you assume to be running on a Linux machine. I got myself a starter kit, that gives me a simple but nicely finished housing, some basic components and a breadboard to stick them in to. I installed the wiringPi library to allow communication with the GPIO port through virtual files on the file system. Then I wrote some Java code that uses these files to actually communicate through GPIO. You can download the project from GitHub. If you have the same starter kit, you can wire up the LEDs and run the algorithms that light them up in different patterns. If you don’t, then you can still use it as a basis to communicate to the GPIO ports from Java.

From an educating view, I hope to take the program to a state where it could be used to introduce people (children) to programming in general and OOP and Java in particular, and provide a framework to try their own algorithms. But for that, the feedback cycle would need to be shorter. BlueJ seems to be a good IDE to compile and run directly on the Pi. For now, I do a mvn install on another PC, scp it to the Pi and launch it over ssh.

If you want a a quick start, just install a copy of my SD card image. It’s the Debian image with wiringPi and OpenJDK 6. Credentials are the same as for the clean Debian distro: pi/raspberry. It’s a 4G image, so you’ll need at least a 4G SD card. From another Linux machine, download the file, bunzip2 it, and write it to your SD card with the correct character for your SD drive:

dd if=./debian4G_20120716.img of=/dev/sd[character] bs=1M