Playing with JavaScript and node.js

Author: lemongrass  |  Category: Technology

I really enjoy learning new things in the field of computer science.  Recently I have been learning some new computer languages and having a great time into the bargain.  Ruby is my favourite, but I’ve also started playing with node.js which is an asynchronous IO library bundled with an implementation of JavaScript (or ECMAscript) because substack has been Plurking about how wonderful it is.  I’m not all that keen on JavaScript so far, partly because I’m so keen on Ruby and partly because of the criticism of it by Douglas Crockford in his book JavaScript: The Good Parts.  However he is not totally anti-JavaScript and has spent a lot of time working on its standardisation as described in a YUI Theatre video called The State and Future of JavaScript.  (This was very interesting for me, for reasons some people may know, though of course I didn’t agree with everything he said.)  There’s a bunch of intriguing talks available at The YUI Theater which I’d love to watch.  I did watch the one by Ryan Dahl on node.js and I expect I will go back for more.

The computer on my desk at home runs Windows (XP SP3 at the moment) rather than my beloved NetBSD or a Linux variant.  Of course my servers run real OSes, but I wanted to work on my desktop machine and so spent a while installing node.js, its package manager and associated packages under Cygwin, which is what makes programming on a Windows machine bearable for me.  The node.js installation instructions cover Cygwin separately and the instructions there worked for me.  However I had a problem using the Node.js Package Manager to install third-party modules.  As described in this email thread, Cygwin pipelines into gzip can hang in certain environments.  I spent hours rebooting, reinstalling and futzing around until I found that thread, which goes on to lay the blame on a “BLODA“.  It turns out that certain antivirus applications can cause this, by hooking themselves into the system call invocation chain in a way which doesn’t actually work perfectly.  I turned my AVG Antivirus Free (v11) off for 30 seconds during the installation of this particular package (called “request“) and the installation went like a dream.

Node.js is young software and under frenetic development.  One promising use-case is the implementation of very scalable web applications, where the asynchronous IO model can yield great efficiencies.  My interest in it is as a web client, where I hope to use it to retrieve and execute JavaScript from websites whose applications depend on it, but with which I want to interact with programatically rather than manually in a browser.  Some such websites (for example Plurk) provide proper APIs for this, whereas others, such as Facebook, do not.  Facebook requires you to write a Facebook application instead.

A consequence of node.js’s youth is the immaturity of adjunct libraries for interaction with other services.  Although HTTP is treated as a first-class application with built-in support, that support is primitive at this stage when compared with Ruby’s net/http.  I’ve been exploring the available libraries for node, which for HTTP client applications include node-httpclient, node-get, node-http-digest, request and abstract-http-request of which the last two look particularly promising.  None of them appear to offer cookie storage between requests, which is something I will need.  At the moment I’m thinking of using one of the available key-value storage libraries which are available to store cookies.

Once one receives the appropriate page from the server, the HTML may need parsing (and there are plenty of HTML parsers for node, such as this), and scripts therein will need executing (aha! That’s why I’m using JavaScript to write the program!).  That’s all in the future.  For now, I’m able to run simple JavaScript programs which fetch pages from websites, and ready to start the real work.

Why write code?

Author: lemongrass  |  Category: Technology

Sitting here in China, wondering what to do, I decided to write some code to help with analysis of moves in a wargame.  Rather than write it though, I started to wonder why people like to write code.

I find writing computer programs fun. When they work, it’s fantastic. When they won’t, it’s frustrating.  I love learning new skills in computing, new languages, environments, toolkits. That’s not to say I like all the ones I come across; there are some which put me off, or which I fail to understand quickly enough to keep my interest. (Ruby on Rails is one such.)  I claim that learning to use these new tools keeps my brain sharp.  Recently I’ve been writing web-hosted database applications, for fun!  I used to think databases were the pits; the boring, commercial end of computing, but now I’ve found a need to use them and find lots of interest, particularly in constructing complicated queries. I never cease to be amazed at how much one can persuade the server to do, rather than have to code into the client application.

This morning though, it wasn’t the challenge which excited me, or even the benefit the new code would bring once it was completed - it was the aesthetics of the code itself.  There are two aspects to that, one serious and one more trivial.   Since I learnt my original computing skills (in assembly language and BASIC), the power of languages has changed greatly.  There’s a good deal of satisfcation in being able to write small chunks of code which do a lot.  I find Ruby particularly rewarding in this respect.  Secondly, editors with syntax highlighting make code so attractive!  Pretty code draws the eye; colours help understanding.  I want to code because the result is pretty!

Making Asterisk (Trixbox) work in the UK

Author: lemongrass  |  Category: Technology

Asterisk is an open-source PABX.  It can connect existing telephones, “soft phones” running on a PC, and physical IP phones to the VOIP networks and to existing “POTS” telephone lines.  Connectivity to existing telephones and exchange lines is provided by either external hardware boxes such as Astribank (for large systems) or PCI plug-in cards such as Digium’s TDM400P.  Cards are also available to connect to digital lines such as T1/E1 to provide multiple lines.

Using Asterisk, I can connect telephones in my children’s bedrooms to the one in the kitchen, and allow them to make phone calls (with restrictions).  I can also connect to my home telephone system from the Internet and call my wife on a real phone from my iPod Touch, or make a call apparently from home when I am in a conference room in the USA.  Also, I can provide new telephone numbers in minutes (via VOIPuser for example) which connect directly to a particular phone in my house, or my PC wherever it is in the world that day, or to an automated message.  I can receive voicemails and have them sent to me by email.  I can also write simple teleohony apps which (for example) tell my wife how many emails she has when she dials *90 on the phone in the kitchen.  The telecoms world becomes my oyster.

Asterisk is available in many forms, both in source and binary distributions designed to run on a PC.  The easiest approach is to dedicate a PC to running Asterisk (though mine also runs a firewall for my home network and a disk server).  Pre-packaged distribtions for this are available and the one I prefer is Trixbox.

Getting Asterisk to work in the UK with caller-id can be tricky.  Having struggled with it several times and found a solution which works, I thought I’d write it down both for my own reference and to help other people struggling with this problem.

  1. Install Trixbox 2.8.0.1 from CD.  Trixbox is based on the CentOS Linux distribution.  Mind, it will find and destroy *all* your hard disks at installation time.
  2. Upgrade to Asterisk 1.6.0.10 (I think)
  3. # yum clean all
    # rpm -del –nodeps kmod-dahdi-linux dahdi-linux
    # yum -y update           (this takes a while)
    # yum install kmod-dahdi-linux dahdi-linux

  4. Then edit /etc/modprobe.d/dahdi to add a line as follows:
  5. options wctdm opermode=UK fwringdetect=1 battthresh=4

  6. Then edit /etc/asterisk/chan_dahdi.conf to add the following lines after “usecallerid=yes”
  7. cidsignalling=v23
    cidstart=polarity

NOTE that Asterisk seems to refuse to use the outgoing line after a reboot until the line is unplugged and plugged back in again!