Author Archive for Marc

Talking at BFUG 9/8/2008

Tomorrow I’ll be giving a talk to BFUG, the Boston Flex Users Group on Degrafa and ObjectHandles.  I’ll be giving an introduction to both Degrafa and ObjectHandles, and then building a simple charting application that uses them together.

So if you’re in the Boston area, come on down!

(If you’ve seen my previous BFAIG talk, you’ve already seen about half of this. )


Wordle

I ran across Wordle today, a pretty neat concept. It reads in an RSS feed and creates a word-cloud of all the words in it.  Here’s one for this blog:



The cool thing about it is how easy it is to create one of these.  Just paste in a URL and click the button.  Bamn, you got something to start with.  No login, no signup.  You can tweak some settings after that if you like.  And you can save it just as easily.

Amazon EC2 - I’m sold…

Since finding out about EBS, I’ve been playing with it and Amazon’s EC2 (their Elastic Compute Cloud) and am very impressed.

After my intial tinkering I’m to the point where with a couple keystrokes, I can start up an instance of a virtual machine anytime I want.  It can range from a "good" machine to an insanse server with copius amounts of CPU and RAM.  So far I’ve only been using the smallest instance type since that beats out my current dedicated server box by a good margin. 

I’m about 90% sure I’ll be switching my hosting over to this.  Here’s my short analysis that I did while deciding.

Right now I have a dedicated server.  It’s a Celeron 2ghz with a 1GB ram and a single 160GB hard disk.  A comparison between that and EC2 yields the following:

Pros:

Reliability - Right now I’m one hardware fault away from a weekend rebuild of the server.  I’ve got plenty of backups and wouldn’t lose any data, but there would be a royal pain in the ass in reconfiguring a new server.  Under EC2, I’d be one command line away from launching a brand new instance that has the exact configuration as the original server.  The EBS storage is on a more reliable, redundant, platform than the current single-disk configuration that I have.  And even if that were to fail I have the same level of backups that my stand-alone server has.  So worse-case, I have to launch a new instance and restore some databases.

Performance - The smallest EC2 instance has more ram and more CPU power than my dedicated server.  The largest EC2 instance is equivalent to about 15x of my current server which means I can scale quite a bit without even thinking about clustering, which leads to…

Scalability and scaling speed- Imagine I get on the front page of some very busy site and have a traffic explosion.  With EC2, In 20 minutes I could provision a much beefier machine to act as my server.  With the stand-alone option, I’m looking at ordering a new server, waiting for it to become available, configuring it, moving data over, waiting for DNS to propogate to it, that’s probably a few days at the least.  Beyond that, with EC2, I believe I could set up a cluster in a weekend instead of who knows how long.  But more importantly, EC2 allows me to scale back down just as easily when the traffic dies down so I’m not stuck paying for the peak possible usage forever.   This is the "Elastic" part of "Elastic compute cloud"

Cons:

What if EC2 or EBS becomes unavailable for some reason?  Amazon had an S3 outage last month and it could happen to these services.  I have no good answer here.   Amazon doesn’t have an SLA for EC2 yet, I assume they will eventually. The only solutions for a problem like this are:

  1. Create an instance in a different EC2 availability zone (Each AV is supposedly seperate and hopefully outages own’t span them).  Unfortunately the EBS volumes aren’t shared across zones, so I’d have to restore a DB from a backup.
  2. Boot up virtualization software, such as VMWare, on a dedicated standalone server and run it on there.

What if Amazon discontinued EC2?  Luckily, the images can be converted to other virtual machine formats.  So I could go to another hosting cloud provider or get a dedicated server running VMWare and boot up my server on it.

Cost-wise, with EC2, I’ll be spending about $10 less a month than I do now for a dedicated server.  (Sounds like a "Pro", huh?)  But, I’ll be on metered bandwidth so that cost will raise over time whereas the dedicated server had the first 1500GB free so it’ll cost more eventually (assuming my traffic continues to grow).  But then again, the per-gig cost of Amazon is lower than my hosting provider.  So if I started to break the 1500GB barrier (unlikely for the foreseeable future), EC2 would start to win again.  

Conclusion:

For me, the reliability is the tipping point one way or another.  I have more confidence in Amazon’s datacenter than I do in a single stand alone dedicated server so I believe it’ll be more reliable.  But I have no way of knowing that for sure. 

Either way I need a decent disaster recovery plan.  With EC2, that plan can be a couple steps from really-easy (starting a new EC2 instnce), to a medium ground (Starting an new EC2 instance and restoring a database), to fairly difficult (provisioning a physical stand alone server and configuring it).  With a stand alone server, that plan is a single step jumping right to the "configure a new server" stage.


Amazon EBS

I just got an email from Amazon about a new service.  Check it out…

http://www.amazon.com/gp/browse.html/ref=pe_2170_10160930?node=689343011

This new EBS system for E2C just made the whole compute-cloud offering from Amazon an easy port from a regular web app to an Amazon hosted app.  Before, all your persistent data storage had to go to S3 which had a specific API you had to develop for.  Now, it can go to EBS which acts like a block device.  In other words, a virtual disk.  So just about any application should “just work” instead of needing modifications.

I had stayed away from EC2 because I didn’t want to tie my apps to Amazon services.  But perhaps this fixes that problem.  I’ll have to play with it sometime soon.

First AIR app to ship?

This week, one of the projects I’ve been working on for the past year and a half ships.

Now, I have no idea if this next statement is actually true since I haven’t done extensive research.  But TimeLiner XE may be the first AIR app to ship.  And by “ship” I mean the more traditional meaning of the word, from a warehouse, on a CD, in a box.

This is a product that I’m really proud of.  We had a really rocky start in the development process, but I ended up taking the lead of most of the project and we were able to deliver something that I hope will really help kids out in school pretty close to the originally planned ship date.  It’s probably the most complex product I’ve ever worked on, and is definitely the biggest project I’ve managed.

For a little background, I work at an educational software company that sells directly to schools.  Because of the market, we generally need to produce our products on a CD, in a box, and with a (gasp) printed manual.  Sounds old fasion, doesn’t it?

It’s worth reflecting on the physical manifestation of software… It’s amazing how much satisfaction you can get from actually holding that beautifully designed box that is meant to contain your work.  Just to know that it was someone else’s job to go through iteration after iteration of design, and then someone else’s job to put ink on a piece of cardboard, and someone else’s job to assemble that cardboard.  All so the piece of software YOU worked on can be delivered to the customer in style.

The box is also great for those times when relatives ask what you do, and you can just point to that on your shelf and say “I made that.”  Then they can look at the pretty pictures on the box, or even crack the manual to see more pretty pictures.  It’s a lot easier to explain than giving out a URL to an AIR install badge or even giving a quick demo.

Unfortunately, there’s also an impending feeling of doom when you realize that you don’t get to update that software for a long time.  What if you missed some critical bug?  You lie awake at night wondering if you remembered to take out that September 1st expiration date that the Beta’s had and you wonder if QA ever tested that.   Those CD’s will last a long time and you can’t just throw up a new version of a web page and make all of the old versions disappear.  Sure we can do downloadble patches, and sure we’ll have a 1.1 version someday. But there is a certain feeling of permanency to the whole process.  I mean a major screwup means pressing new CD’s and packing them, and paying for postage.  That could be thousands of dollars.

I’m not sure if I dare to go check that expiration thing now…

OpenID from AIR movie

Here’s a quick movie I just recorded showing the user interaction of logging into a web service with OpenID and then loading up some data.

The flow goes like this:

  1. When accessing the web services the user is first presented with a normal login dialog.
  2. From there he chooses to use an OpenID login.
  3. He types in his OpenID identity.
  4. He is forwarded to his OpenID provider’s page.
  5. He clicks the "Allow" button, and is brought to a list of all the projects he’s previously saved.
  6. Upon clicking a project, the browser portion of the application closes and the program loads up the data over AMF

I’ll post code examples of what I did to make this all work as soon as I get some spare time.

AgileAgenda Beta Signups

Quick note, there’s a new signup for AgileAgenda betas.  Towards the end of the month, we’ll probably be releasing a Beta with a ton of improvements including things like fully integrated time tracking, resources across projects, and RSS feeds of tasks.


OpenID From AIR

I’ve been working on some web services recently.  There’s both plain old browser accessed HTML pages plus an AMF interface to them.  One feature of the HTML version is OpenID authentication.  Now OpenID is all fine and dandy for a web application, but we get some problems when we want to use an AIR desktop client to connect and authenticate through OpenID. 

If you’re not familiar with how OpenID works, here’s a quick summary.

  1. User goes to an application (Usually a web-app)
  2. User types in their OpenID URL to that app
  3. They get forwarded to their OpenID page where they can either grant or deny access.
  4. That page then forwards them back to the original web app with an authentication token set.

Obviously, it’s kind of hard to redirect back to a desktop application.  I was thinking up some wacky work-a-rounds for this but then it hit me.

In the AIR app I can just use the HTML component and point it towards the login form of the web services.  The user can then log in using either username/password or OpenID just like they do on the website.  Now, here’s the cool part…  The AIR HTML component shares a network stack with the AIR/Flex NetConnection object.  That means any session/cookies/whatever opened in the HTML component carry through to the remoting calls I want to make from Actionscript. So I can authenticate using a web form, but then consume BlazeDS/LiveCycleDS/AMFPHP/Red5 services using AMF over Netconnection.

I did up a quick proof of concept and it works on both Windows and OSX.  I was able to successfully call a remoting service that requires an authenticated session.  So this was actually a much easier problem to figure out than I had feared.

Now, I’ll just need to make the web page that loads after a successful login somehow indicate to the AIR app that the user is successfully logged in.  I’ll probably either use a tiny Flash component that signals over LocalConnection, or I’ll just make the AIR app watch for when the HTML component gets to a specific URL. I’m pretty sure I can get this to be a completely seamless experience for the user.

Oh, and as a bonus… the user can sign up for a new account instead of log into an existing one from "within the app" instead of going to an external website to do that.


Object Handles Spotted?


I might have spotted another use of ObjectHandles over at the AsButtonGen webpage.  Over on the text and image tabs you can add stuff to your button that can be resized/moved using some familiar looking handles and mouse cursors.  Also, I recognize a bug with resizing the left bottom corner that was in previous versions of the library.

If it is ObjectHandles, this would be the second time I just ran into a use of them, that’s pretty cool. Unfortunately, I couldn’t find an email address on that site.

But that aside, the site is neat.  It lets you create some pretty "web 2.0" style buttons with stripes, highlights, etc.

I spent some time on the library yesterday.  I was tyring to get the rotation+resizing thing working better since it’s been somewhat broken for a while.  I’ve never needed that feature so it’s been neglected.  Unfortunately, I’m running into a bit of a brick wall.  I’m still trying to figure out exactly what it should do, nevermind how.  Luckily, most of my Trigonometry from High School is coming back to me since it’s heavy in that department.

Model Adapters - A binding pattern using an Adapter

Binding in Flex is great. It’s an ultra convienent way to get information from your data model to show up in your views. But it does have some limitations, and to work around those limitations I’ve been using the “Model Adapter” aka the “Wrapper” or just plain “Adapter” pattern (some info, and more).

The basic idea is you shouldn’t have to modify your data model to use it in a specific view. If you need to filter, sort, or summarize the data for a view you can do that through an Adapter so your model doesn’t need to understand that logic and you’re view isn’t reliant on a specific implementation of your model.

Example: If you had a list of books in an array, and you want to filter by some property of books (say publisher) you shouldn’t apply the filter directly to the model. Instead, create an adapter that can watch that array, and have that adapter apply the filter (or sort, or whatever).

More Examples: Consider Timeliner XE, a product I’ve been working on at my day-job. The main data model is a list of events. There are several views for that data. We have a text based / grid view, and some graphical views. Here’s a couple screenshots:

Each of those screenshots has 2 views active at a time, the grid, and then a seperate graphical view. That makes 3 views that all want to bind to our data model. But, notice the grid has 5 events in it, while the graphical views only have 3. This is because only 3 of those events are valid to plot (they have a date). It’d be nice if we only had to bind to a list of events that actually has the data we want.

Now take a look at these three screenshots from AgileAgenda, my project scheduling application.

In all of these the data we have is a list of tasks. The first two show one view with two different filters applied to the data. The third shows a large grid with all of our tasks, and a much shorter pulldown that only has the tasks that are also milestones. (A milestone is a specific type of task)

To create an adapter:

  • Create a new adapter class
  • Create a constructor for that class that takes in the “source” data model, and any options that might be specific to the adapter.
  • Add event listeners to the “source” model.
  • Write event handlers in the adapter to update the adapter’s internal state when the source changes.
  • Write accessors in the adapter, so other components can get data from it.

A simple example…

Click here to run a simple example. View-source is enabled in that. Here’s a screenshot of the example:

When you run the example, it creates a simple data model, populates that data model with 4 sample items, and then creates 4 panels. Each of those panels represents a view. The example also creates 4 different model adapters all from the same data model, but with different options set. Then each panel gets a different adapter.

As you add items to the data model, you can see that the 4 views update depending on whether or not they are filtered and sorted.

Our Data Model:

package
{
    public  DataItemExample
    {
        public var name:String;
        public var amount:Number;
        public var active:Boolean;
    }
}

—-

package
{
    import mx.collections.ArrayCollection;

    public  DataModelExample
    {
        [Bindable] public var myDataItems:ArrayCollection = new ArrayCollection();

    }
}

As you can see, it’s a pretty simple data model. There are items with a name, amount and active properties, and then there is DataModelExample class with an array of those. Notice that no view-specific data is in there.

Now, lets create our adapter and name it “AdapterExample”

First, create a constructor and some variables to hold some information about the adapter. We’ll have 2 options. onlyActiveItems and sorted. For sorted, we’ll also create a Sort object to actually do the sort for us. And we’ll also create an array to hold our filtered/sorted list of items. Note that we add an event listener for the COLLECTION_CHANGE event. This is how we’ll propogate changes from the data model to our adapter. We’ll see the handler for that later.

 public  AdapterExample extends EventDispatcher
    {
        protected var model:DataModelExample;
        protected var filteredDataItems:ArrayCollection = new ArrayCollection();
        protected var _onlyActiveItems:Boolean;
        protected var _sorted:Boolean = false;
        protected var sort:Sort;        

        public function AdapterExample(dataModel:DataModelExample, onlyActiveItems:Boolean, sorted:Boolean)
        {
            _sorted = sorted;
            _onlyActiveItems = onlyActiveItems;

            model = dataModel;
            model.myDataItems.addEventListener(CollectionEvent.COLLECTION_CHANGE, onItemsChanged );    

            if(sorted)
            {
                sort = new Sort();
                sort.fields = [new SortField("name",true)];
            }

            rebuildFilteredArray();
        }

Notice that we called rebuildFilteredArray above. Lets write that next. All this method does is loop through our data model and grab all the items from it (respecting our filtering option) and adds them to our internal array. It also applies the sort if neccessary. At the end we dispatch two events which will be used for binding later.

  protected function rebuildFilteredArray() : void
        {
            var tmp:Array = [];
            for each ( var item:DataItemExample in model.myDataItems )
            {
                if( (! _onlyActiveItems ) || (item.active) )
                {
                    tmp.push(item);
                }
            }                        

            filteredDataItems = new ArrayCollection(tmp);

            if( sort )
            {
                filteredDataItems.sort = sort;
                filteredDataItems.refresh();
            }

            dispatchEvent(new Event("dataItemsUpdated") );
            dispatchEvent(new Event("totalChanged") );
        }

So now if we made an adapter it would start up, read in the source data model, and populate our internal array of items. But it wouldn’t respond to changes in the source data model. So lets create the event handler that we set up in the constructor. We’ll also create a couple helper methods

    protected function onItemsChanged(event:CollectionEvent):void
        {
            switch(event.kind)
            {
                case CollectionEventKind.ADD: addItems(event.items); break;
                case CollectionEventKind.REMOVE: removeItems(event.items); break;

                case CollectionEventKind.MOVE:
                case CollectionEventKind.REFRESH:
                case CollectionEventKind.REPLACE:
                case CollectionEventKind.RESET:    rebuildFilteredArray();
                                                break;

                case CollectionEventKind.UPDATE:  

            }

        }

        protected function addItems(items:Array):void
        {
            for each ( var item:DataItemExample in items )
            {
                if( (! _onlyActiveItems ) || (item.active) )
                {
                    filteredDataItems.addItem(item);
                }
            }
            dispatchEvent(new Event("totalChanged") );
        }

        protected function removeItems(items:Array):void
        {
            for each ( var item:DataItemExample in items )
            {
                var index:int = filteredDataItems.getItemIndex(item);
                if( index != -1 )
                {
                    filteredDataItems.removeItemAt(index);
                }
            }
            dispatchEvent(new Event("totalChanged") );
        }

For adding/removing items we’re going to our internal array and manually adding or removing items from it. We’re making sure to account for filtered items, but the sort object is taking care of the sorting for us.

For the other types of events, we’re kind of cheating. We only really care about adding / removing operations so we’ll just rebuild our entire internal array on other types of events. If your application uses those types of events often, you should implement them in the adapter in a more efficient manner.

Exposing data from the Adapter

We now have the internal state of the adapter updating as the model changes. So the only thing left to do in there is expose some properties so we can get at that info from our view. Let’s write two bindable getters. One of them will summarize the data (get total()) the other will give us our filtered list (get dataItems())

Note that we set the event=”" property in the [Bindable] tags so our views can correctly know when these properties change.

  [Bindable(event="dataItemsUpdated")]
        public function get dataItems() : ArrayCollection
        {
            return filteredDataItems;
        }

        [Bindable(event="totalChanged")]
        public function get total() : Number
        {
            var total:Number = 0;
            for each ( var item:DataItemExample in filteredDataItems )
            {
                total += item.amount;
            }    

            return total;
        }

Using the Adapter

Once you’ve done all of that, you can actually use your adapter. So create your data model, create your adapter, and use it!

 [Bindable] protected var dataModel:DataModelExample = new DataModelExample();
 [Bindable] protected var example1:AdapterExample = new AdapterExample( dataModel, true ,true);

    <mx:Panel x="10" y="218" width="250" height="200" layout="absolute" title="Filtered, Sorted">
        <mx:Label x="10" y="132" text="Total:"/>
        <mx:Label x="56" y="132" text="{example1.total}"/>
        <mx:List x="10" y="4" width="210" height="120" dataProvider="{example1.dataItems}" labelField="name"></mx:List>
    </mx:Panel>

If you look at the source of the example, we actually create 4 adapters with varying options.

Beyond this basic example

If you want your adapter to respect changes to individual items, your items should implement the IPropertyChangeNotifier interface. So in our example if we edited an item so it’s active flag changed, the views would not update. To make that work we would implement that IPropertyChangeNotifier interface, and then write some code for the CollectionEventKind.UPDATE event.

Often times only one or two views are visible at a time and it’d be nice if all the views in the background weren’t madly updating themselves every change. To accomplish that I often write an enable() disable() method on the adapter. They usually look something like this:

protected function enable() : void

{

model.myDataItems.addEventListener(CollectionEvent.COLLECTION_CHANGE, onItemsChanged );

rebuildFilteredArray();

}

protected function disable() : void

{

model.myDataItems.removeEventListener(CollectionEvent.COLLECTION_CHANGE, onItemsChanged );

}