Friday, August 30, 2013

And why do we fall, Bruce ? ...

"... So we can learn to pick ourselves up" (From Batman Begins). But it's a good line none the less. Especially when t comes to technology and computers.

I've run into a little problem while running my node.js irrigation control software. Every once in a while I get this:

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: getaddrinfo ENOENT
    at errnoException (dns.js:31:11)
    at Object.onanswer [as oncomplete] (dns.js:123:16)

[1]+  Exit 1                  node ./sched.js

While it's very descriptive in it's message (basically the getaddrinfo failed) it really doesn't tell me anything. I have no code file called events.js (part of node.js). So what does it mean? Well at first I couldn't figure it out. I suspected that it was a problem with the connection to the user (I'm using sockets). So I did a search and found vague references to the problem but no real answers. Until I came upon a bug report where support asked one simple question: "Do you have an 'error' listener set up on your connection instance?" (thanks mscdex for that). It was a 'Duh!' moment for me. I listen for a connect and disconnect (and a few other things) but not an error. So I added:

socket.on('error', function(){
    --userCount;

    consolelog("Socket error");
    consolelog("Error from " + address.address + ":"                + address.port + "(" + userCount + ")");

    if(userCount < 0) {
        userCount = 0;
    }
});

So far it hasn't occurred again, which doesn't mean that it won't but I am hopeful. At the moment I am in the middle of setting and relearning how to use Eclipse. I'm making the effort so I can use Eclipse to debug node.js programs as well as some of my embedded stuff. I don't expect that I will totally give up on emacs as I'm always using that but I expect I'll be using Eclipse a lot more with my work so it's best to get adjusted to the environment now.

Monday, August 19, 2013

Node.js and the single thread

For a few weeks I've been playing with Node.js and my irrigation controller. So far it was worked very well and I'm beginning to get the hang of 'async programming' (I think). While mowing the lawn today I gave the issue of Node.js and a home automation controller more thought. In home automation, most things are asynchronous so Node.js seems like a perfect fit, we'll see. The model I want to use is that of the HCS II but update the user language and allow for user developed devices. The thing that gives me the most trouble is the user language which basically runs in a loop, continuously. I was thinking of using one of the threading libraries to put the user language in it's own thread and the rest of the device handing in another. But then it dawned on me that most code isn't really a program that runs as a sequential program but rather as blobs that need to run in no particular order (mostly). So instead of a loop I can take the user code and do a single run through on a timer (Misterhouse is like this except it's a loop) and repeat at regularly timed intervals. Right now I'm thinking about how to implement this with a Javascript based user language. I've already done the equivalent of a for loop with timers (and it works well).