Spiking Greedy SWFs

A project we’re working on requires third parties to be able to load SWFs of their own design into an overall “shell” that we’re building. So far, so good - particularly with the new unloadAndStop method that’s part of Flash Player 10. That means I can fully unload the SWFs no matter where they managed to sink their teeth/event listeners.

Then I started to think about a weird edge case… what if the creators of these SWFs accidentally does something spectacularly bad like get stuck in an infinite loop? I had a few ideas for a solution, and so I decided to spike them and see if any of them worked (The link explains what spiking is - but in a nutshell it’s a quick-and-dirty end-to-end solution that tests out an idea).

To test my solutions I made two SWFs - one with a simple spinning animation (spinner.swf) and another that, with the push of a button, would lock up for 5 seconds (greedy.swf). My first idea was to put the SWFs on the same HTML page and have them talk via LocalConnection. The spinner would monitor the greedy SWF (when ever the greedy one started, it would tell the spinner) and if the greedy SWF went on too long the spinner would talk back out to Javascript and tell it to replace the Flash Player instance.

That didn’t work in the slightest.

As soon as the greedy SWF started getting greedy, the spinner would lock up too. I even tried putting the SWFs in separate HTML files and loading in one via an iframe - same result.

The next solution was to have Javascript do the monitoring. So now the greedy SWF would call out to Javascript before it started getting greedy and the Javascript would then start a timer. If that timer wasn’t reset by the greedy SWF finishing it would unload the Flash Player running the greedy SWF. Pretty decent idea, yeah? Nope.

It seems like (at least in the browsers I tested, Safari and Firefox), when Flash is locked up the rest of the browser, including JS is locked up too. The timer would only first AFTER the greedy SWF stopped on it’s own accord.

So where does that bring us? Well, these solutions or something like that may work in IE, particularly since it’s running on a whole different plugin architecture. It may also work in Chrome since it’s whole architecture is based on isolating processes. That being said, a real cross-browser solution remains elusive. I guess I’ll just have to hope the 3rd party SWFs don’t suck and maybe do a bit of testing on them before I roll them out to the public.

LAFlashapaloozastock III

Branden will both be speaking at the free Flash conference LAFlashapaloozastock III in Venice Beach, CA on December 6, 2008.

To help keep the event free Branden will also be teaching a very cool course of his own titled “Creating Killer ActionScript Robots For Fun & Profit” on December 4th and 5th. It’s going to be a two day romp through ActionScript 3 learning advanced programming techniques in order to make software robots with which the students will do battle! The course is $499 per student. If you’re interested click the course title to find out more.

Comments: Leave a comment

SOAP is Just XML

Recently, Branden and I were tasked to build a Flash UI on top of a web service.  Seems like pretty standard development…but, as you have to know by now, nothing is ever “standard” at Automata.  After some research, trial & error, and hand-wringing, we realized that the device we were attempting to connect to was using SOAP 1.2.  Well, unfortunately, Flex only supports up to SOAP 1.1, so we had a bit of a problem.

It didn’t take us long to remember that SOAP was just XML, and that Flex’s web service API was just a nice class that makes it easy to speak that language.  After deciding to nix the Flex solution (we were only using Flex for the WebService API), we moved to an all Flash solution.  We created a connector class to act as a web service API, and simply sent XML packets directly to the server using URLLoader.  One note about this solution is that it will only work if you have knowledge of the web service you are trying to use.  In our case, we had the entire schema at our disposal, so we knew the exact format of the service operations.

The connector class contains public methods to call the individual web service operations.  Additionally, we created individual request classes for each of the operations that extended a class called Abstract Request.  AbstractRequest provides a method send(uri:String, headers:Array) that uses URLLoader to send the request to the server.

The “uri” parameter of send() is the full path to the web service, with the “http://” attached.  The headers are the HTTP headers that you need to send to the web service for such things as authentication.  In our code, these are set in the Connector class, and passed to each request as it is needed.  The “_request” property holds the XML object that you will pass to the server.  This XML is formatted as an XML packet specific for this web service.  The value for this property is defined in the AbstractRequest sub-classes.

The _request property is an XML object that defines the format, and uses Flex-style binding to add the actual data from the constructor parameters.  This is a little-known XML trick you can do in AS3.  Branden found it in the Help section of the Flash CS3 IDE.  Pretty neat if you ask me.

Back to the AbstractRequest class, we simply set the data property of the URLRequest intstance to be the string representaton of the request XML, and call URLLoader.load(), passing in the URLRequest instance.  This sends the XML packet to the server.  The handler for Event.COMPLETE will receive the XML returned from the server.

Our connector class has methods like:

1
2
3
4
5
6
public function addGroup(groupId:String, groupName:String, flag:String):void {
	var request:AddGroupRequest = new AddGroupRequest(groupId, groupName, flag);
	request.addEventListener(ISYResultEvent.EVENT, onResult);
	request.addEventListener(ISYFaultEvent.EVENT, onFault);
	request.send(_uri, _headers);
}

This actually executes the web service call.

And just like that, you’ve build your own web service class.  While it’s quite specific to the web service you are using, you could certainly expand it to consume a WSDL file so you can use it for any web service.

Flash on the Beach 2008, Brighton, UK

Branden will be giving his talk on “Brilliant Ideas that I’ve Blatently Stolen” at Flash on the Beach which runs from September 28 - October 1. Branden will be speaking on Monday, September 29 so if you are there, be sure to catch his session!

Simplifying code with finite state machines

Yes, it’s been a while. We’ve been busy. The good news (for you) is that we’ve been making fun new things (i.e. code libraries) that we’ll be sharing soon!

Now, on to the actual topic of this post - finite state machines. What is a finite state machine you ask? Well it’s just a construct that lets you track the states within a piece of code and ensure that you only do what you’re allowed to do when you’re allowed to do it.

A while back I wrote a generic finite state machine (I called it PastaMachine - since every time I wrote FSM I thought of the Flying Spaghetti Monster). It is exceptionally generic - you just define states, define the allowed transitions between those states (state A can go to state B, but B can’t go back to A, etc) and the actions that each state may take (an action may trigger a transition, but it doesn’t have to). The states themselves can have attached to them an arbitrary amount of metadata and in less than 340 lines of code you have a simple finite state machine.

What can you do with it? Well, originally it was written to handle a branching logic game where depending on the users success at a simple game they went down multiple paths and were shown various different videos (yeah… it was a PG-13 strip poker game). But recently Keenan has been using PastaMachine to track the logic of an application to assist doctors in treating patients with HPV. If that’s not a nice range of usefulness, I don’t know what is!

One nice extra trick PastaMachine has up it’s sleeve is that once you’ve defined a state machine with it you can export the information about that state machine to data in DOT format. That DOT data can be loaded into the ever-so-awesome GraphViz and BAM! you have a directed graph diagram of your state machine! It’s actually really useful in debugging the state machine’s connections, but beyond that it adds a very nice “WOW!” factor when you present it to your client.

Over the next few weeks we’ll be cleaning up PastaMachine and sharing it with everyone. Then, if everything goes smoothly, there will be a number of other Automata code libraries following along behind it.

Charles - A great web application debugging tool!

During a recent project of ours we found ourselves in the position where the developer creating the server-side code was a full 5 time zones away. This meant that we often had to do debugging over email. This was, as one would expect, excruciatingly slow. The issues that arose during integration almost always resolved around how we were mapping the servers .Net classes to our AS3 classes - one small change to the server class could break things on our end in a non-obvious way.

Luckily early in this process we discovered a piece of shareware called Charles, a web debugging proxy that’s available for Mac, PC, and Linux. Once you fire Charles up it acts as a proxy for all of your web traffic. This lets you capture, view, and even throttle the data that’s flowing between your machine and the web server. And best of all for us, it lets you view inside of AMF packets and see exactly what’s coming over the wire. Once we got Charles up and working we able to tackle kind of issues that used to take us hours and resolve them in minutes.

We haven’t done a done with Charles yet, but so far our only wish is that we knew about it sooner. Suffice it to say that we found the $50 registration fee to be well worth it!

Does Your Tween Stop In The Middle?

Branden and I were working in the office today when Branden came over and asked me if I’ve ever run into the following problem:

You are creating tweens using the Tween class in AS3. These tweens are inside of a method (or a loop). The tweens appear to work fine, except, they stop in the middle without explanation. As it turns out, I had run into this problem before. While working on a previous project, I had a series of tweens moving around a Rectangle object inside a class method. Halfway through the tween, it stopped for no apparent reason. After using everyone’s favorite inter-web fast-food chain, Google, I determined that the issue must be due to garbage collection. I typed in “AS3 tween garbage collection”, and found that my problem had been solved and discussed by Scott Morgan. Here’s his blog post about it.

The solution, as described by Scott is to create class properties for your tweens instead of local variables. As I described this to Branden, he noted a potential problem with this solution. When you use class properties, the Flash player will allocate space for those properties, even when you are not using them. Why is this a problem? Well, imagine you can potentially have hundreds of instances of a class, where each instance contains private properties for 5 tweens. This could significantly hurt the performance of your application since you will have allocated space for all of those tweens, even though the tweens are not running. Needless to say, while this is a good solution, and great in a pinch, ultimately, you will have problems for large numbers of tweens.

So, how do you solve the secondary problem of allocating space for tweens that are not running? Is there a way to use a class property to store the tween (so it is not killed by the garbage collector), without actually allocating space until the tween is needed?

The answer: DICTIONARY!

Instead of creating a property for each tween you use, instead, create a single property of type flash.utils.Dictionary. The Dictionary class allows you to create a set of dynamic properties, without allocating the space for them until the property is actually created. Additionally, you can also use an object reference as a key in a dictionary, which can be a very powerful tool. Once you have your dictionary, inside the method (or loop) that creates the tweens, store the tween object reference as the key in the dictionary. Once your tween is complete, delete the key and your tween will be collected by the garbage collector.

Here’s an example:

How to pronounce our name…

Just as a public service message - it’s not “auto-mata” - it rhymes with “manamana” as demonstrated by the Muppet Show:

(Thanks to Tom Pizer over at Performtech who dug up the video)

Comments: 1 comment

Making Leopard’s Quicklook Feature (more) Useful

I’ve been dealing with piles of two types of files that are somewhat awkward to work with on the Mac - ZIP files and EPS files. On the PC I can just double click on a ZIP file and peer into what’s inside without actually unzipping it, on the Mac, not so much. When it comes to EPS files, I’ll open then with preview and then get a lovely spinning beachball while it is “Converting Postscript to PDF…”. Now admittedly it’s faster on Leopard that it was on Tiger, but still, that sucks. Luckily these are exactly the kinds of problems Quicklook was made to handle.

If you haven’t played with Quicklook, you just select a file and hit the spacebar - you then get a preview of the contents of the file. By default it works with iWork formats, and all the usual MS Office formats. The best part though is that third parties can write their own plugins for Quicklook - and developers have already made some for ZIP and EPS! Now I just select either file type and smack the spacebar and BAM! almost immediately I can peer inside of ZIP files or preview EPS files.

It’s times like these I *really* love the Mac developer community (and Apple ain’t half bad either, though I’m still not fully digging Stacks!).

tags: ,

Comments: Leave a comment

The Nooks and Crannies of ActionScript 3

The slides from Branden’s presentation at FITC Hollywood 2007, Flash on the Beach 2007, and FITC Winnipeg 2007 - “The Nooks and Crannies of ActionScript 3″ are now available for download. Enjoy - and if you have any questions or comments drop us a line.

Comments: Leave a comment

Older Posts »

Built by Automata Studios