The making of MBChat v3 – part 1 introducing Nested Closure Programming

MB chat is an Ajax chat application that I built for Melinda’s Backups. It was originally so that the leadership team could hold meetings without resorting to e-mail, but it became more of a general social thing for the members of the web site. In order for the software to work out if someone had said anything new, it polled a database every two seconds. Not something that would scale particularly but OK for v1. That was until Melinda herself came to chat one time, and a large number of people tried to come in at the same time.

MB chat is an Ajax chat application that I built for Melinda’s Backups. It was originally so that the leadership team could hold meetings without resorting to e-mail, but it became more of a general social thing for the members of the web site. In order for the software to work out if someone had said anything new, it polled a database every two seconds. Not something that would scale particularly but OK for v1. That was until Melinda herself came to chat one time, and a large number of people tried to come in at the same time. We found out that the scale limit was about 20 people. Beyond that and chat fell over.

In v2 I explored techniques for avoiding the need to poll a database and developed a solution based around the same technique as I mention in providing a high speed link between web clients using named pipes. Unfortunately the slight variation on this technique where essentially a message generated by one user has to be passed to all other users rather than just the one other, meant that you couldn’t wait for the two sides to synchronise (as is important in that solution). This means in some circumstances a message could get missed – so even with this technique a level of database interaction was necessary. I therefore also explored switching to the SQLite database as it seemed to offer a potential speed advantage. However I soon discovered that whilst it was almost faster the lock out mechanisms when multiple users attempted to access the database at the same time were crude and actually slowed things down. I got to 50 people, but that was the best I could achieve.

One thing I did discover in this test was that although SQLite was fairly fast with a single transaction per user interaction, it was lighteningly fast if multiple accesses could be done within the same transaction. I started thinking about a solution that could make that happen.

During this time, my daughter told me how they were considering a secure version of a chat program at her work. This got me thinking about how I could make MBChat secure. I set myself the following challenge:-

  • The chat server and the clients had to communicate with each other over the open internet
  • Only authorised users should be able to access the functionality or see any of the conversations
  • Clients can be sure they are talking to the server they think they are (to prevent an imposter capturing the messages)

A moments thought will immediately dictate that we should be encrypting at least the conversational messages with a key, but that since the software is in essence being downloaded from the server, it cannot contain that key within it as anyone watching the traffic would immediately be able to sniff out the key and use it to decrypt the conversation that was supposed to be secure .

In fact I decided that the only way to do this properly is for the client to generate a public/private key pair dynamically once it has started and transmit the public key of that pair to the server. The server will have to encrypt the key used to encrypt messages with that public key and pass it back to the client.  The client (and only that client) who will be able to decrypt it with his private key and start using it in the subsequent client server communication.

Generating a public/private key pair turns out to be a non trivial task.  If we are performing the task at the client end, inside the browser, then Javascript will most likely have to be the programming language which will be used to run the software which generates the public/private key pair.  Even with a reasonably fast processor, it takes several seconds to calculate an appropriate 64bit key (which we use).  If this was a normal script the browser would lock up solid, and would soon report a script failure.

I found Atsushi Oka had already solved this problem and provided a set of javascript routines that could generate the RSA public/private key pair. His solution he called “Nested Closure Oriented Progarmming”. If you take a look at the Nested Closure Oriented Programing module he has produced, you will see that part of his approach is to modify the base Object prototype to add the additional routines in. It was only after I built my first attempt to use his software to produce the key did I discover that there was major conflict with the mootools framework library that was an integral base for the rest of MBchat.

However after a detailed analysis of how the RSA key generation routines where built, I believed it was possible to make a mootools class that did almost the same job, but avoiding the need to alter the object prototype. I developed my own Nested Closure class which turned out to be considerably simpler that the library it replaced.

The key approach of nested closure programming is to have STEPS which are encapsulated within a function. A function can have part of its process broken into sub-steps, the key being that at the lowest level of steps the code can complete within the time allowed by the browser. Then all that is needed is an engine that runs one step, waits for a tick (a millisecond) and then executes the next step. Finally we need a mechanism to provide loops, for steps to indicate when they complete whether to exit this loop or to repeat the step or to continue to the next step (or loop if it was the last step of the loop), and a mechanism for passing results between steps.

Lets look at some small fragments of the RSA generation which show how it is used:-

Firstly – declaring the generator class, dealing with the result and starting the processes

RSA.prototype.generateAsync = function(keylen,exp,result) {
 var self=this;
 var generator = new NCOP( stepping_generate,true);
 var _result = function(keyPair) {
  result( self );
 };
 generator.EXEC([keylen,exp],_result);
...

We declare an instance of class NCOP (generator in this case) with the name of the highest level function used in the process, together with a flag which indicates that this instance of the NS class is unique. This allows the class to bind its key functions to the window object – making them easier to call (so you call can DO() rather than this.DO() or generator.DO()).

We start the process going and pass the parameters (as an array – which is an easy text transformation from the original code). We also pass the function that is called when the process terminates (_result).

Looking at the head of the stepping_generate function we can see how the parameters are picked up, and with a simple text transformation returned to two variables that were originally the functional parameters of that function

// Generate a new random private key B bits long, using public expt E
function stepping_generate (myArgs) {
 var B,E;
 B = myArgs[0];
 E = myArgs[1];
...

Once this high level routine has done some simple initialisation of variables, it starts the stepping activity (the main process) by returning but calling the DO method (it could have alternatively called the STEP method) of the main NCOP class. We see here how the first few steps are declared by declaring an array (indicated by the “[” at the start of the function parameters) of functions. Notice also a nested arrays starting at step 1.3. Arrays represent loops with a list of steps that are repeated until an action is taken to exit the array. Finally notice in step 1.3.1 that in certain instances the code executes return AGAIN(). This causes that whole loop to be started from the beginning rather than the (default) action of proceeding to the next step.

return DO([
 // Step1.ver2
 function () {
  RSA.log("RSAEngine:1.1");
  self.p = new BigInteger();
  rng = new SecureRandom();
 },
 function () {
  RSA.log("RSAEngine:1.2");
  // return self.p.stepping_fromNumber1( B-qs, 1, rng ).BREAK();
  return self.p.stepping_fromNumber1( qs[0], 1, rng );
 },
 [
  // Step1.3 ver3
  function () {
   RSA.log("RSAEngine:1.3.1");
   if ( self.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) != 0 ) return AGAIN();
  },
  function () {
   RSA.log("RSAEngine:1.3.2 : calling stepping_isProbablePrime");
   return self.p.stepping_isProbablePrime(10);
  },
...

It is also possible to pass the result from one function into the next step. This is shown in step 2.3.2 where a function “stepping_isProbablePrime” is called (it itself is using the NS methods to split its action into steps). The result of this step is then passed into the next step as the (only) parameter to the function which uses it to determine the flow of the following step (either exit the loop by calling DONE() or repeating the loop by calling AGAIN())

function() {
 RSA.log("RSAEngine:2.3.2");
 return self.q.stepping_isProbablePrime(10);
},
function(result) {
 RSA.log( "RSAEngine:2.3.3:result="+result );
 if ( result ) {
  RSA.log("RSAEngine:2.3.3=>EXIT");
  return DONE();
 } else {
  RSA.log("RSAEngine:2.3.3=>AGAIN");
  return AGAIN();
 }
},
...

This works very well. Dependant on the speed of the computer, it can generate a 64bit RSA public/private key pair in 1 to 2 seconds. This happens in the background – in some configurations of chat whilst the user is entering his username and password. In this instance that time delay is not even noticed. More importantly the browser never pops up a message to say that the script is taking too long to run.

Obviously when several things are happening at once (generating the key and awaiting a user to login) there is need to coordinate the completion. A simple mootools class to do that will be the subject of my next blog

An update on Asterisk

This is just a quick blog entry to note that I now have a working environment of a local telephone system, including three telephones inside the house and two located with my daughter some way away.

I initially purchased a linksys spa3102 as the first attempt to get ordinary telephones to work with , but after that I discovered the linksys pap2t, which gives two analogue phone circuits each. The whole setup works fine.

I have even managed to get asterisk to set up conference rooms and voicemail. Quality is really good.

This is just a quick blog entry to note that I now have a working environment of a local telephone system, including three telephones inside the house and two located with my daughter some way away.

I initially purchased a linksys spa3102 as the first attempt to get ordinary telephones to work with , but after that I discovered the linksys pap2t, which gives two analogue phone circuits each. The whole setup works fine.

I have even managed to get asterisk to set up conference rooms and voicemail. Quality is really good.

Playing with Asterisk

Asterisk is the Open Source PBX software that can manage telephone lines. As well as the classical hardware phones, more importantly for me, it also manages VOIP traffic. I decided to give it a go after I received our last quarterly telephone bill and discovered that we had spent £110 talking to my daughter in the period.

Asterisk is the Open Source PBX software that can manage telephone lines. As well as the classical hardware phones, more importantly for me, it also manages VOIP traffic. I decided to give it a go after I received our last quarterly telephone bill and discovered that we had spent £110 talking to my daughter in the period.

An initial trial last week managed to allow my daughter to call in from outside and have a voice conversation with me (who was inside the house). This is with two firewalls (one at her place and one at mine) in the path. Unfortunately, there was a problem with my microphone.

Over the weekend I extended that further, connecting the Free World Dialup and used there echo service to take a call and bounce it back to me.

I have also ordered a linksys SPA3102 so that I can try and connect a real phone into the process.

I was worried that yet another application running on my server would bog it down, but it doesn’t appear to be a problem with CPU load rising from an average of around 2% to about 5% when asterisk is bridging a call including changing codecs (and therefore presumable de-encoding and then re-encoding the voice on the channel).

I still have a problem with sound quality but it seens to get better with every tweak that I do. Lets hope over the next week I can get it working fully.

Free as in Freedom as well as to Free as in Beer

I just came across Aaron J Siego’s post in his blog about the difference between free as in beer versus free as in freedom, and how the open source community has not done enough to espouse the benefits of the second form of freedom.

I just came across Aaron J Siego’s post in his blog about the difference between free as in beer versus free as in freedom, and how the open source community has not done enough to espouse the benefits of the second form of freedom.

Well, I couldn’t agree more. It made me re-read my own Open Source Philosophy post written in March 2004 (and actually culled from words I had written several years before that) and I think that he makes the same point that I was making then (although in a considerable more lucid way).

It was only a few days ago that I replied to a post on Slashdot where someone was saying that because he wasn’t a programmer it didn’t matter to him that the source wasn’t available. My reply to him was yes it did. Maybe he couldn’t fix bugs, or add new features, but because of the availability of the source others could and he would reap the benefit.

What Aaron points out is that the traditional free software foundation view is that the it is a question of ethics, and that all their arguments about why software should remain free is couched in those terms and he thinks that is a mistake. I agree, I think it is the arguing from this direction, whilst technically correct, fails to really motivate the man in the street, who sees the free as in beer as the dominant factor. I think a more persuasive argument is one of enlightened self interest. Free as in Beer is the short term benefit. Free as in Freedom is the longer term. What those people who are choosing free (as in beer) but binary modules are doing choosing a short term benefit against choosing a long term benefit that far outweighs that.

What I think needs to be shouted about much more is that even though a user personally can’t programme, that fact that others have access to the source and can use it gives them

  • ratchet improvements in quality and functionailty over the long term
  • innovation driven from the multitude of difference viewpoints interacting with one another
  • innovation driven by amicable competition between teams who can also share with one another
  • support services that are often personalised and fast, provided you are courteous in asking for them

Software Patents are Bad for Europe

Since man first invented the wheel, society moves forward technologically by inventors standing on the shoulders of those who came before. This advance in our knowledge has improved our lives immeasurably, so much so that society wants to encourage inventiveness, by rewarding those that invent new things a monopoly in that invention (a patent) in exchange for the knowledge that future generations can build upon.

Since man first invented the wheel, society moves forward technologically by inventors standing on the shoulders of those who came before. This advance in our knowledge has improved our lives immeasurably, so much so that society wants to encourage inventiveness, by rewarding those that invent new things a monopoly in that invention (a patent) in exchange for the knowledge that future generations can build upon.

It is important to understand this clear quid quo pro between society and the inventor. Based on that understanding it is not unreasonable to expect that if the patent grant is going to generate a great deal of money for the holder, that society should expect a similar degree of inventiveness for others to build upon.

This to me is the crux of the problem with grants of patents on Business Methods or Software Functions (I use this word function here because the alternative, the actual software itself – which does take considerable time and effort to get right – is already protected by the monopoly rights of copyright. Defining functionality is comparatively a trivial exercise.). The level at which patent protection can be applied for is several thousand times simpler than would be needed in a fully functioning business, or a substantial software programme. Whilst a physical product can be broken down into smaller patentable components, the ratio is nowhere near as great.

This leads to two consequences. Firstly, and this is the most important point, is that to achieve anything useful in writing a software program, you could potentially be effected by thousands of patents on the little individual functions making up software. Secondly, these little individual functions do not provide anything like the benefit to society that would be appropriate for the monopoly position granted to them.

It is also worth asking why patent protection is being considered at all. Given that we already have copyright as a mechanism for society to give a monopoly to encourage invention why do we think that the patent approach is always needed.

It costs money to obtain a patent. This mitigates against the small organisation spending the time an effort to patent all the various small functions that go up to make any useful software program. By contrast, large global corporations can afford to invest to that level. The consequence of that is that only the large mulinationals can really protect themselves via the patent route.

Considering this from a European perspective, all of the substantive software companies with the one exception of SAP are American. Thus allowing patents of software functions in Europe can only harm Europe.

But there is a broader problem. Open source software, developed by the thousands of individual contributors around the globe are able to develop software that can – for the first time – compete against the monopoly stranglehold that Microsoft has on the industry. Unfortunately open source software has a real problem. It has been competing on technical excellence spread by word of mouth, and unlike normal commercial enterprises does not translate that technical advantage into money. This prevents it from either entering into the patent game, or from spending the cash lobbying governments to take decisions in its best interest.

Monopolies are not in the best interests of society (even more so when the monopoly is based on the other side of the world from your society). That is why we have many many mechanisms to prevent monopolies from abusing their monopoly powers. With open source we have another mechanism that can fight against these monopolies. But allowing software patents could so easily destroy that weapon.

My Open Source Philosophy

It may seem strange that someone who has made his living for over 35 years in the software business, with at least 15 of those years in charge of a product for which we charged significant licence fees, should advocate the open source movement and the supply of software for free. This paper clarifies, and then justifies, my stance on this. I hope that in doing so I may add to the debate on the subject by showing why I believe that despite the free availability of software there is still significant revenues and profit to be made by those that wish to supply solutions to the business community for money.

It may seem strange that someone who has made his living for over 35 years in the software business, with at least 15 of those years in charge of a product for which we charged significant licence fees, should advocate the open source movement and the supply of software for free. This paper clarifies, and then justifies, my stance on this. I hope that in doing so I may add to the debate on the subject by showing why I believe that despite the free availability of software there is still significant revenues and profit to be made by those that wish to supply solutions to the business community for money.

Several years ago I was a strong advocate for Microsoft. I was responsible for establishing a PC based office automation environment within my company and I saw important marketing effort advocating a move from the horrors of the DOS environment to the ease of use that the WIMP (windows, icons, mouse, point) environment provided being driven by Microsoft with a pricing strategy that beat its competitors easily. Two specific deals in particular spring to mind. The first was bundling networking within the operating system where previously we had had to purchase a separate licence (DECNET) and the second was to create an office bundle at a price not much above a single application where previously we had been purchasing these items independently. Then, as part of my role to set future direction for a product I was responsible for, I became a beta tester for windows 95 about 18 months before it was released.

So why do I now think differently? The turning point for me came when I discovered Linux and installed it on my PC at home. I had watched Windows 95 move through new releases which added a small amount of new functionality but which required me to pay for another upgrade licence, I had watched licence conditions change so that key office software at work was not available to me at home anymore, I had paid good money (not to Microsoft) for a mail/news reader that didn’t quite do what I wanted but there was no way to modify it and, most importantly, I was suffering from system crashes and there was no way to solve the problem. I realised in Linux I had found a way to provide me with free software and a mechanism for getting complex crashes fixed.

Back in the late 1980s, I had been actively involved in trying to work out how to improve the quality of code that my company produced. I was directed to, and read, Zen and the art of Motorcycle Maintenance. To me it taught an important lesson. Quality cannot be stitched on to the side of software, it has to be built in by each individual working on it. What open source does, is that by exposing the insides of software to peer review, encourages the producers of the code to consider quality as they go along and also enables code that does not match up to be seen and then improved.

Unlike some, I do not despise people who wish to make a living selling software. I do regard those who abuse their monopoly power and break the law in order to keep or improve their competitive position as reprehensible and I expect the authorities to take appropriate steps to prevent further occurance and to penalise those who do so for their behaviour. But that aside, I regard my use and support of open source a competitive issue and one in which I support the supplier who best meets my needs now and in to the future.

From this perspective there is an interesting balance here. On the one side I get a lot of functionality for free. Not just the kernel but the wealth of applications that come with it. But the downsides are also not insignificant. I cannot run many of my excellent games (although some do surprisingly well under wine), configuration of any particular feature can still be a tortuous process (particularly newer peripherals – like my usb palm pilot:-)) , fonts don’t always perform well (particularly in conjunction with printing). What ultimately drives me towards Open Source is that by giving something of my abilities (from writing software and or documentation to simply searching for bugs or critising it) I am both giving something back for the benefit I have received from the community and I am tipping the balance towards a longer term, better cheaper solution to my computing needs.