var util = require('util');
var EventEmitter = require('events').EventEmitter;
var http = require("http");

var Flukso = function(hostname, pathname) {
    var self = this;
    var regexp = /([0-9]+)\]\]$/; // /\(([0-9]+) hosts* up\)/;

    this.pollPower = function() {
        http.request({ host: hostname, path: pathname }, function(response) {
            var str = '';

            response.on('data', function(chunk) {
                str += chunk;
            });

            response.on('end', function () {
                var matches = regexp.exec(str);
                if(matches != null && matches.length == 2) {
                    var time = Date.now();
                    var num = matches[1];
                    self.emit('done', parseInt(num));
                }
            });
        }).end();
    };
};

util.inherits(Flukso, EventEmitter);
module.exports = Flukso;
