Scala and ActionScript

Like many experienced Java programmers, I've played with several other languages and platforms over time. While most development languages are not game changing on their own, they can have an impact on the speed and level of enjoyment of a developer. Development languages are like a carpenter's tools. Most tools won't change they house an experienced carpenter can build, but they may make the experience more enjoyable.

With that caveat in mind, I wanted to explore my (current) preferred toolchain, Scala and ActionScript (Flex). Not only are both languages powerful and enjoyable in their own right, their similar syntax and easy integration provide a powerful combination.

Scala is:
"... a general purpose programming language designed to express common programming patterns in a concise, elegant, and type-safe way. It smoothly integrates features of object-oriented and functional languages, enabling Java and other programmers to be more productive."
Scala brings the benefits of a functional language, the accessibility of an object-oriented language and the power of the Java platform. I find the concise yet familiar syntax a joy to work with.

ActionScript is a dynamic language based on ECMAScript used to develop Flex applications. ActionScript brings the familiarity and dynamic nature of JavaScript, with the addition of more object-oriented features, and a powerful Flex API.

While there are some major differences in these languages, their basic syntax is very similar. To define a 'POJO' (where J is really Scala or ActionScript):

ActionScript:
package com.ericdaugherty.sample
{
public class Person
{

public var firstName:String;
public var lastName:String;
public var middleInitial:String;
}
}
Scala:
package com.ericdaugherty.sample

class Person {

var firstName:String = ""
var lastName:String = ""
var middleInitial:String = ""
}
While they are very similar, there are a few differences:

  • Scala defaults properties and functions to public, so you save on some boilerplate code.

  • In Scala, every variable must be initialized, or the compiler will assume it is abstract.

  • While this example provided explicit types, they can be inferred by the compiler.

In this example, the properties are still all statically typed Strings, but the explicit definition is unnecessary:
package com.ericdaugherty.sample

class Person {

var firstName = ""
var lastName = ""
var middleInitial = ""
}
You can leave off the types in ActionScript as well. However, this results in an untyped variable instead of using type inference to infer the static type. It also results in the following compiler warning (using Flex Builder):
1008: variable 'firstName' has no type declaration.
Finally, Flex Builder does not support code completion without explicit typing, so in practice ActionScript really requires explicit type definition.

In both languages, you can define explicit get/set behavior instead of a generic property. To only define a setter in ActionScript:
private var _firstName:String;

public function set firstName(firstName:String) {
_firstName = firstName.toLowerCase();
}
In Scala:
var _firstName = ""

def firstName_=(firstName:String) {
_firstName = firstName
}

In Scala and ActionScript, the trailing semicolons are optional, reducing the need for superfluous characters.

While these comparisons are very superficial, they do make context switching between Scala and ActionScript a bit easer. However, it is important to understand the differences in implementation. Sometimes similar syntaxes can disguise vast differences.

Note: While this example makes use of 'var' definitions in Scala, in practice a functional program would use the final 'val' definitions instead. One of my most common muscle memory issues is typing val in ActionScript instead of var.

DSL Woes

I've been struggling with my DSL connection for a while now. Over the past week it became intolerable, and I dug in to investigate this weekend.

I had been experiencing intermittent outages, which I wrote off as bad service (Ameritch and I have had issues in the past*). Over the past week I started experiencing significant packet loss (>30% on pings to the ISP's DNS server), so I knew it was getting worse.

Doing some basic troubleshooting, I thought I heard 'noise' on the line, so I replaced the DSL filter I setup. No luck.

I gave up and called SBC (Ameritech). They immediately acknowledged that something wasn't right, and referred me to their troubleshooting center. The next morning I got an automated call that I needed to call them to discuss. After some debugging on the phone, they decided that it was probably my DSL Modem. Since it was > 5 years old they thought it was probably starting to flake. I went out and bought a new one.

The new modems actually have a nice web interface so you can see what is going on. Unfortunately, it told me that my downstream connection was ~ 500 Kbps, while my upstream was 640 Kbps. Yes, this is WRONG. The downstream should have been 6 Mbps, or ~6,000Kbps. I called them back, and they agreed it was still wrong, and dispatched a technician, with the warning that there may be a charge if the problem is in my internal wiring.

I decided to verify that the issue was external, so I took my laptop, DSL Modem, and some wires outside and plugged them directly into the telephone box on the outside of my house. To my surprise it connected at full speed, indicating that the issue really was internal.

I tried some simple debugging (unplugging devices, etc.), but eventually just ran a fresh line from the inside junction box to my DSL modem. Success!

I'm a little annoyed I couldn't find the problem, but there must be some type of interference on the wiring inside the house. However, since it is all effectively hard wired together, it is nearly impossible (well, given a reasonable amount of effort) to track down. So, I'll have to settle for good enough.


* 'Back in the day' I had a frustrating experience with Ameritch (or whoever they were back then). There was noise on our line, causing my modem all sorts of problems. What really annoyed me was when they told me they only supported speeds up to 9600 bps, and if I was using a faster modem I was simply out of luck. It took a while but I convinced them that the issue was bad enough I could hear with my own ears. They ended up dispatching a truck and found that there was 'water in the line' on their end.

iTunes Export 2.1 Released

This release of iTunes Export provides some additional features requested after the 2.0 release. These features include:

The ability to override the file path separator used. This is useful when you will be using the playlist on a different operating system.

iTunes 9 File Structure. iTunes changed the file structure starting with version 9, with a separate 'Music' folder under the iTunes Media folder. This version now includes the Music folder as part of the musicFolder, excluding it from the path when using file copy with the iTunes Structure.

The GUI version now remembers your last settings and will use those as the new defaults. There are buttons to restore the initial defaults if you wish to revert to the normal defaults.

The new version can be downloaded from the project homepage: http://www.ericdaugherty.com/dev/itunesexport/

If you find any issues or have questions please email me (eric@ericdaugherty.com). Please include which application and version (GUI or Console, 2.1 etc.) of iTunes Export and the operating system you are using.

Embedding PHP in HTML files and GoDaddy Statistics

I host my site at GoDaddy, for better or worse. The site is comprised entirely of 'static' HTML pages at GoDaddy. The Blog is managed using Blogger, but it publishes HTML files via FTP to GoDaddy, and the rest of the content is HTML files I manage directly.

I wanted to avoid the redundant header/footer/etc. structures in my pages since I was editing them manually. So, I setup my site to parse the HTML files as PHP files, and then used PHP includes:
<?php include 'head.php' ?>
to build the pages on the fly. This allows me to maintain each page using simple HTML and avoid duplication of boilerplate HTML. This works because I added the following line to the .htaccess file:
AddHandler x-httpd-php .html .htm
This solutions works well, but unfortunately destroys GoDaddy's web statistics (/stats/) output. Apache attempts to parse these html files as well, but because of how they setup permissions (you have none), it fails and results in an HTTP 500 error.

This would be a simple fix if I could edit the .htaccess file in the stats directory. I could simply override the .html handler back to the default, and avoid the PHP processing.

Instead, it looks like my options are:
  • FTP the files to my local machine and view them there.
  • Create a script that copies them to a different directory (with the correct permissions and .htaccess override) and view them there.
  • Change my files to .htm or another extension that doesn't conflict with the /stats files (.html).
  • Get Creative.
Changing the file extensions isn't really an option for me as the entire point of mapping the .html files as PHP is to maintain all the existing incoming links without doing tons of rewrites. I view that stats infrequently enough that the first option is workable, but it is annoying. So I got creative...

My hack was simply to create a PHP file that reads the files out of the /stats directory. Here is a crude version:
<?php
$file=fopen("stats/INDEX.html","r");
while(!feof($file))
{
echo fgets($file);
}
fclose($file);
?>
You could expand this to be dynamically driven from a request parameter. Again, this is a HACK, but for the specific purpose it works. It should of course be secured if you want to maintain the security defined in the original /stats directory.

Flex: Monospace Fonts and Sizing Text Components

For a recent project I wanted to simulate an 80x24 terminal window in a Flex application. To accomplish this I needed a fixed width (monospace) font and the ability to size the text control for the current font. Here is what I did:

First, I needed a monospace (fixed width) font. You can embed your own fonts in a Flash SWF, but that was overkill for this effort. You could specify a specific font, such as 'Courier New' that is monospace. However, there can be issues if the client's device does not have the specific font face. Device Fonts often serve as a fallback font if the specific font requested does not exist on the user's device. In this case, I didn't want to select a specific font, I just wanted the default monospace font. So I used the _typewriter font (the other types are _sans and _serif).
<mx:text id="myText" fontfamily="_typewriter" />
Once I had a monospace font, I needed to determine the proper size of the control for an 80x24 terminal. I started by simply setting the text control to an 80 character wide (and then 24 character high) string and noting the size, but I needed something that would work easily at runtime. I created this quick helper method that would resize my control:
private function setSize(control:mx.controls.Label, charWidth:int, charHeight:int, defaultText:String = "") : void {

var testString:String = "0";
var width:int = control.width + (control.measureText(testString).width * charWidth);
var height:int = control.height + (control.measureText(testString).height * charHeight);

control.text = defaultText;
control.width = width;
control.height = height;
}
I set this method to be called when the container's creationComplete event fired. This method assumes that the control is set to an empty string initially, and accepts an optional defaultText parameter if you wish to initialize it.

The measureText method returns the size of the text, but not the surrounding control borders, so I added the default control size to the text size to achieve the proper control size.

iTunes Export 2.0 - Now Supports OS X!

This release of iTunes Export marks a milestone and major transition of the development focus. The prior releases, 1.0-1.6 were all build using the Microsoft .Net Framework, which limited deployment to Windows. With this release, iTunes Export now fully support Windows and Mac OS X.

The Graphical User Interface version has been ported to Adobe Flex and is deployed as an AIR desktop application. The Command Line version is an update of the Scala version I developed earlier this year.

iTunes Export exports your iTunes playlists as M3U, WPL, ZPL, and MPL files, allowing you to setup playlists in iTunes and use them with other software or devices.

The primary focus of this release was to enable Mac OS X support. However, in addition to this, the copy files (songs) logic has been revamped. There are now three options:
- FLAT - Copies all the songs into the output directory
- ITUNES - Retains the iTunes structure (Artist/Album)
- PLAYLIST - The 'original' copy logic that copies the songs into a directory per playlist.

The new version can be downloaded from the project homepage: http://www.ericdaugherty.com/dev/itunesexport/

As this is a major new release, please keep an eye out for any bugs or issues that may appear. If you find any issues or have questions please email me (eric@ericdaugherty.com). Please include which application and version (GUI or Console, 2.0 etc.) of iTunes Export and which Operating System version.