Drainbamage.nl blog of Christiaan Ottow

17Jul/091

AS3 object serialization pitfalls

I'm working on an AIR project now, and I wanted to save some user data locally. There are a few ways to do so, including SQLite, LSO, and plain file writing in the local datastore.
I wanted to save an ArrayCollection containing connection profiles the user specified, and SQLite seemed like a bit of an overkill for this. Coming from a Java background, I just wanted to serialize and save my ArrayCollection so I wouldn't have to reconstruct it from SQL every time.

Fortunately, this is possible with ActionScript 3, using the FileStream class's readObject() and writeObject() methods. Here's the code I used to read and write the profiles:

private function loadProfiles():void
{
	var prefsFile:File = File.applicationStorageDirectory.resolvePath(fileName);
	var fs:FileStream = new FileStream();
	if( !prefsFile.exists )
	{
		profiles = new ArrayCollection();
	} else {
		try {
			fs.open( prefsFile, FileMode.READ );
			profiles = fs.readObject() as ArrayCollection;
			fs.close();
		} catch( e:Error ) {
			Alert.show( "Error while loading profiles: "+e.message, "Load error");
		}
	}
}

public function saveProfiles():void
{
	var prefsFile:File = File.applicationStorageDirectory.resolvePath(fileName);
	var fs:FileStream = new FileStream();
	try {
		fs.open( prefsFile, FileMode.WRITE );
		fs.writeObject(profiles);
		fs.close();
	} catch( e:Error ) {
		Alert.show("Failed to save profiles: "+e.message, "Save error");
	}
}

There are a few pitfalls however when loading the profiles. First, the player must be able to tell the class of the objects it is loading. For some reason, it cannot do so unless you specify it explicitly:

package nl.aboutcoding.servicebrowser.model
{
	import mx.collections.ArrayCollection;

	[RemoteClass(alias="nl.aboutcoding.servicebrowser.model.Profile")]
	public class Profile
	{

Secondly, the class MUST have a constructor, otherwise the objects are typed as Object and type casting will fail with a message concernin "Type coercion failed". I often skip the constructor on ValueObjects, and it took some time to figure out.

Post to Twitter Tweet This Post

6Jun/092

Automatic uploading of SWF using FDT and Ant

FDT and Flex Builder 3 both feature the Ant builder (Flex Builder only via an addon). Ant is a project by Apache (http://ant.apache.org/), which does for the Java and AS3 worlds kind of the same thing as make does for the C world. It takes a build file as input, and calls the correct compilers and commands to produce meaningfull output.

Of course, you can compile your project directly with FDT or FB, but the way it handles its output (how it is generated, where it is placed, what to do after compilation) is much less under your control.

I wanted FDT to automatically upload the generated SWF and other resources after compilation, but only if they had been changed. My deployment folder consisted of these resources:

Post to Twitter Tweet This Post

6Jun/090

First AS3/Flash project

I started developing with AS3 and Flex some time ago. I really like AS3, Flex and AIR, but I decided it would be good for me to learn how to create Flash applications in AS3, without the Flex framework, and without the Flash CS3/4 authoring tool. So, I installed FDT, and set out to create a website for my freelance activities.

The result can be found at www.aboutcoding.nl. I embedded the following techniques I wanted to learn:

  • Dynamic content through XML
  • Indexable by search engines
  • Deeplinking enabled
  • Embedded fonts with some visual effects
  • Full-window flash with graceful resizing

I hope you enjoy it, I sure did creating it :-)

Post to Twitter Tweet This Post

5Apr/092

Xen benchmark

Contents


Introduction


This post discusses the performance of Linux context switches under the Xen hypervisor. Presence of the Xen hypervisor has an impact on the context switching performance, as is shown by benchmarks. I was interested in these benchmarks since I had the feeling a Xen-enabled machine is in general running slower than a non-Xen machine.

Post to Twitter Tweet This Post

25Mar/090

iPhone tethering in OS 3.0

Yesterday I decided I would try out the new OS 3.0 beta 1. I've been looking forward to having tethering in my iPhone very much, since I'm often traveling and working at the same time. Buying an extra device for laptop internet (like a UMTS USB dongle) doesn't seem right, since I'm already paying for unlimited data access with my iPhone subscription. After having heard some reports of people in NL who got tethering to work with T-Mobile, I decided it was time.

To get to the tethering, one must go through these steps:

  • Register your iPhone's UUID with apple
  • Get the OS 3.0 beta firmware image
  • Install the iPhone SDK or at least the USB System Components package that comes with it
  • Change the carrier information on you iPhone by uploading a modified IPCC file

I'm registered as an Apple developer since the company where I work is starting to build iPhone apps. So, getting the device registered and downloading the OS 3.0 beta image and SDK was easy.

Upgrading to the new image is done by clicking the "Check for Update" button in iTunes while the Option key is pressed. This allows you to select a firmware image to update to. By selecting the 3.0 image, the device is upgraded. Then the SDK story. I already have the iPhone 2.2 SDK, since I've started writing iPhone apps. For tethering to work however, you need the 3.0 SDK or a part of it. I first installed the pkg that only contains the necessary drivers for USB tethering (the whole SDK is a 2.15 GB download). This file can be found here. Later, when tethering didn't work, I installed the whole SDK, and when it still didn't work, I installed the small PKG again. It finally worked :-)

Then, you need an IPCC file. For T-Mobile NL, you can find such a file in this excellent post. It didn't work for me though, I downloaded this file (thanks to Wiebel).

At first, iTunes and my iPhone would crash when I plugged the iPhone in with tethering enabled. After re-installing the PKG with drivers again, this problem was fixed. Tethering via bluetooth worked after using Wiebel's IPCC file. So, now it works both via bluetooth and USB!

Post to Twitter Tweet This Post

Filed under: Life hacking No Comments
23Mar/093

Visim – website visitor simulator

Today I uploaded some code I wrote for my bachelor thesis to Google Code as an open source project. My bachelor thesis was about creating a scalable architecture for heavy-duty web apps. I created such an architecture, and validated it on a prototype. To see what effect changes to the system had on its capacity, I wrote a tool that measures how many users can be served by the system. This tool is now public under the name "visim".

To measure how many users a system can serve, I used the following steps:

  • Set requirements on response times (like 500ms for 90% of the requests, 1 sec for 98% of the request etc)
  • Run the tool with these requirements for a low number of users
  • Increase the number of users until the requirements aren't met anymore

Then you can make changes to the system and re-run the tool to see if it still serves the same amount of users, or perhaps more.

The project is at http://code.google.com/p/visim

Post to Twitter Tweet This Post

Twitter links powered by Tweet This v1.7, a WordPress plugin for Twitter.