The making of MBChat v3 – part 2 coordinating multiple activities

In the previous blog article I discuss a process which would take several seconds to run. Obviously it is started as soon in the load process for the initial web page as possible.

Several other activities have to happen, such as loading the sound libraries, the dom has to be ready, the user has to have entered his username and password and it has to have been validated before the user can enter chat.

In the previous blog article I discuss a process which would take several seconds to run. Obviously it is started as soon in the load process for the initial web page as possible.

Several other activities have to happen, such as loading the sound libraries, the DOM has to be ready, the user has to have entered his username and password and it has to have been validated before the user can enter chat.

So several of the start up steps in getting the user properly logged on to chat and with the sound system fully working have to be co-ordinated. In order to avoid having to poll some flag to see if an item had completed, I decided to write a mootools class to handle this situation.

In order to create a coordination point for several activities, the programmer declares a new instance of the coordinator class, thus:-

var coordinator = new Coordinator(['rsa','login','dom','verify'],function(activity){
    loginRequestOptions.e = activity.get('rsa').e.toString();
    loginRequestOptions.n = activity.get('rsa').n.toString(10);
    loginRequestOptions.msg = 'MBChat version:'+MBChatVersion+'   using:'+Browser.Engine.name+Browser.Engine.version;
    loginRequestOptions.msg += ' on:'+Browser.Platform.name;
MBchat.init(loginRequestOptions,activity.get('rsa'));
    window.addEvent('beforeunload', function() {
         MBchat.logout(); //Will send you back from whence you came (if you are not already on the way)
    });
    soundcoord.done('chat',{});
});

In this instance, I am creating a coordinator which will coordinate the activities with the names of the first array parameter. When all the activities are complete the second parameter is a function which gets called with the activity object. This consists of a set of objects keyed by the activity name which are parameters which can be used. In this instance we are getting the public key components of the ‘rsa’ activity to pass to loginRequestOptions.

Note also at the end of the function a call to soundcoord.done. This is another coordinator I set up to coordinate the initialisation of the sound system. We can’t do anything with the sound system until both it is ready, and the user has logged into the chat (through this current activity). The done method is called with the name of the activity and an object (in this case empty) to pass into the final function in the activity parameter.

Just for completeness, this is the call to coordinate.done for the rsa activity so you can see how parameters (in this case the RSA key pair) are passed

var rsa = new RSA();
function genResult (key,rsa) {
    coordinator.done('rsa',key);
};

/*
We are kicking off a process to generate a rsa public/private key pair.  Typically this
takes about 1.2 seconds or so to run to completion with this key length, so should be done
before the user has completed his input - which is when we will need the result.  The genResult
function will be called when complete.
*/

rsa.generateAsync(64,65537,genResult);

This class itself is very very simple. You can download the latest release from my git repository as shown here. Although you might like to just copy and paste it from here

/*
 	Copyright (c) 2011 Alan Chandler
    This file is part of Coordinator.

    Coordinator is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Coordinator is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Coordinator(file COPYING.txt).  If not, 
    see <http://www.gnu.org/licenses/>.

*/

var Coordinator = new Class({
    initialize: function(activities,callback) { //an array of activity names
        this.activities = new Hash();
        activities.each(function(activity) {
            this.activities.set(activity,false);
        }.bind(this));
        this.callback = callback;
    },
    done: function(activity,parameters) {
        this.activities.set(activity,parameters);
        if (this.activities.every(function(activity) {
            return activity;
            })) this.callback(this.activities);
    }
});

AKC Money is born

At work I got my new laptop last October. But I couldn’t get my Palm Pilot to Sync with it, so was getting increasingly worried that my data, particularly my diary, was getting out of date. Also, I had an application for managing my accounts (Micromoney) that contains absolutely critical financial data holding the state of my personal finances.

Near the end of November I decided to try and sync with firstly my Linux Desktop, and then when that didn’t work my old laptop. None of them worked anylonger. It looks as though something has gone wrong with the Palm.

At work I got my new laptop last October. But I couldn’t get my Palm Pilot to Sync with it, so was getting increasingly worried that my data, particularly my diary, was getting out of date. Also, I had an application for managing my accounts (Micromoney) that contains absolutely critical financial data holding the state of my personal finances.

Near the end of November I decided to try and sync with firstly my Linux Desktop, and then when that didn’t work my old laptop. None of them worked anylonger. It looks as though something has gone wrong with the Palm.

A bit of searching on the internet found that a possible cause is static charge buildup, but the only way out of this is to let the battery run down and destroy the contents.

So I decided on a different approach. Build an application to run on my web site that replicated the functionality. From a security point of view that required both login and https – so that I could get at the data from work when filling in my expenses, but I also felt I could improve the application in a few areas (such as the management of foreign currency) that the existing application was poor in.

So I have taken a step sideways and stopped working on my blog, to urgently build this new application.

Its taken about one month – which was longer than I had hoped, but its working. Of course I don’t want you all to see my financial data, so I also installed demo copy with its own database and not protected via login and https.

Things I added

  • The ability to work in Foreign Currencies for transactions. I must admit the way I have done this is slightly strange, but it matches my own workflow, in which I claim my expenses either as
    • the result of transactions where I have paid for cash (and therefore need to remember the exchange rate at the last converion, or
    • the result of credit card transactions where I will initially guess at the conversion rate using the same rate as the cash transactions, but which will be accurately reflected in actual GBP figures when the statement comes through
  • Items have to be separately cleared from both the source and destination accounts. My old version tried to implement this with a single cleared flag and it didn’t always work. I have two specific flags for each end of the transaction
  • recognise that some accounts normally have the majority of transactions as debit (e.g. my bank account where there is a single salary credit, and then lots and lots of debit transactions. On the other hand my Company Expenses account is a credit account (I credit it with debits from other accounts for every expense, its debited – and normally by bank acount is credited when the company repays me those expenses.

I will release source code soon (under the GPL) – as soon as I can get the site tidied up