var util = require('util');
var EventEmitter = require('events').EventEmitter;
var exec = require('child_process').exec;


var IpPoll = function(switchaddr, hostsaddr) {
    var self = this;

    var regexp = /\(([0-9]+) hosts* up\)/;
    var nmap = "nmap -n -sP ";

    this.pollState = function() {
        exec(nmap + switchaddr, function (error, stdout, stderr) {
            if(error == null) {
                var matches = regexp.exec(stdout);
                if(matches != null && matches.length === 2) {
                    self.emit('doneState', matches[1] === "1");
                }
            } else {
                self.emit('doneState', "unknown");
            }
        });
    };

};

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