<commit_before>var http        = require("http");
var ip          = require("ip");
var request     = require("request");
var hostaddress = ip.address();



// remove the first two elements as they contain the node and vf commands
//process.argv.pop()
//process.argv.pop()


process.argv.shift()
process.argv.shift()
var firstArg =  process.argv.shift()

var useHost = hostaddress
var usePort = 80
if (firstArg == '--host') {
    useHost = process.argv.shift()
    firstArg =  process.argv.shift()
}
//console.log("Connecting to: " + useHost + ":" + usePort);


//console.log (process.argv)
if (!firstArg) {
    console.log(`
VisiFile commands:

> vf
This command. Enter this to see this menu. If you are a developer
then you may have enter "vf" as "node src/vf.js"


> vf test
See if VisiFile is installed on your local computer on port 80


> vf ls
See which computers have VisiFile installed on them. Example
output may be:
User: root, 192.168.0.101:80
: in which case you can:
1) enter "vf --host 192.168.0.101:80 main" at the command prompt
2) enter 192.168.0.101:80 into a web Browser to view VisiFile
3) enter 192.168.0.101:80/main into a Rest API client such as Postman


> vf drivers
Show the available drivers


> vf --host 192.168.0.101:80 drivers
Show the drivers on the machine located at 192.168.0.101:80


> vf home
Show the main details of the running VisiFile
`)


} else if (firstArg == 'ls') {

    getListOfHosts(function(serversReturned) {
        if (serversReturned.length == 0) {
            console.log("No VisiFile Servers started on your Intranet")
        } else {
            for (var x = 0; x < serversReturned.length ; x++) {
                console.log("User: "+serversReturned[x].username + ", " + serversReturned[x].internal_host + ":"+serversReturned[x].internal_port)
            }
        }
    })



} else if (firstArg == 'main') {
    findMainServer(function(serverName) {
        console.log(serverName)
    })


} else {
    process.argv.unshift(firstArg)
    //options.query.a = process.argv

    var args = process.argv

    //console.log("Args JSON: " + JSON.stringify(args,null,2))

    var args2 = encodeURI(JSON.stringify(args))
    //console.log("Args seralized: " + args2)

    var options = {
      host: extractHostname(useHost),
      port: getPort(useHost),
      path: '/vf?a=' + args2,
      method: 'GET'
    };


    var req = http.request(options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          var result = JSON.parse(chunk)
        console.log(result.OK);
      });
    });

    req.on('error', function(e) {
      console.log('problem with request: ' + e.message);
    });


    req.end();
}




function serialize(obj) {
  var str = [];
  for (var p in obj)
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  return str.join("&");
}








function lsFn(servers, index, callbackFn) {
    //console.log("Trying lookup server: " + servers[index])

    var remoteServerUrl =   'http://' + servers[index]  +
                            "/get_intranet_servers?time=" + new Date().getTime();

    request({
        uri: remoteServerUrl,
        method: "GET",
        timeout: 10000,
        agent: false,
        followRedirect: true,
        maxRedirects: 10
  },
  function(error, response, body) {
      var servers2 = servers
      if (error) {
          console.log("Error opening central server: " + error);
          var nextIndex = index + 1
          if (nextIndex < servers2.length) {
              lsFn(servers2, index + 1, callbackFn)
          }
    } else {
        var servers2 = []
        var returned= eval( "(" + body + ")");
        for (var i = 0 ; i < returned.allServers.length; i++) {
            //console.log('got server ' + i)
            //console.log(JSON.stringify(returned.allServers[i],null,2))
            var tt = new Object();
            tt.username = returned.allServers[i].client_user_name;
            tt.internal_host = returned.allServers[i].internal_host;
            tt.internal_port = returned.allServers[i].internal_port;
            tt.via           = returned.allServers[i].via;
            servers2.push(tt)
        };
        callbackFn(servers2)

    }
  });


}


function extractHostname(url) {
    var hostname;
    //find & remove protocol (http, ftp, etc.) and get hostname

    if (url.indexOf("://") > -1) {
        hostname = url.split('/')[2];
    }
    else {
        hostname = url.split('/')[0];
    }

    //find & remove port number
    hostname = hostname.split(':')[0];
    //find & remove "?"
    hostname = hostname.split('?')[0];

    return hostname;
}


function getPort(url) {
    url = url.match(/^(([a-z]+:)?(\/\/)?[^\/]+).*$/)[1] || url;
    var parts = url.split(':'),
        port = parseInt(parts[parts.length - 1], 10);
    if(parts[0] === 'http' && (isNaN(port) || parts.length < 3)) {
        return 80;
    }
    if(parts[0] === 'https' && (isNaN(port) || parts.length < 3)) {
        return 443;
    }
    if(parts.length === 1 || isNaN(port)) return 80;
    return port;
}


function findMainServer(callbackFn) {

    getListOfHosts(function(serversReturned) {
        if (serversReturned.length == 0) {
            callbackFn("No VisiFile Servers started on your Intranet")
        } else {
            var mainServer = serversReturned[0].username + ", " + serversReturned[0].internal_host + ":"+serversReturned[0].internal_port

            for (var x = 1; x < serversReturned.length ; x++) {
                if (serversReturned[x].internal_host == hostaddress) {
                     mainServer = serversReturned[x].username + ", " + serversReturned[x].internal_host + ":"+serversReturned[x].internal_port
                }
            }
            callbackFn(mainServer)

        }
    })
}<commit_msg>Better message if no VisiFile server running<commit_after>var http        = require("http");
var ip          = require("ip");
var request     = require("request");
var hostaddress = ip.address();



// remove the first two elements as they contain the node and vf commands
//process.argv.pop()
//process.argv.pop()


process.argv.shift()
process.argv.shift()
var firstArg =  process.argv.shift()

var useHost = hostaddress
var usePort = 80
if (firstArg == '--host') {
    useHost = process.argv.shift()
    firstArg =  process.argv.shift()
}
//console.log("Connecting to: " + useHost + ":" + usePort);


//console.log (process.argv)
if (!firstArg) {
    console.log(`
VisiFile commands:

> vf
This command. Enter this to see this menu. If you are a developer
then you may have enter "vf" as "node src/vf.js"


> vf test
See if VisiFile is installed on your local computer on port 80


> vf ls
See which computers have VisiFile installed on them. Example
output may be:
User: root, 192.168.0.101:80
: in which case you can:
1) enter "vf --host 192.168.0.101:80 main" at the command prompt
2) enter 192.168.0.101:80 into a web Browser to view VisiFile
3) enter 192.168.0.101:80/main into a Rest API client such as Postman


> vf drivers
Show the available drivers


> vf --host 192.168.0.101:80 drivers
Show the drivers on the machine located at 192.168.0.101:80


> vf home
Show the main details of the running VisiFile
`)


} else if (firstArg == 'ls') {

    getListOfHosts(function(serversReturned) {
        if (serversReturned.length == 0) {
            console.log("No VisiFile Servers started on your Intranet")
        } else {
            for (var x = 0; x < serversReturned.length ; x++) {
                console.log("User: "+serversReturned[x].username + ", " + serversReturned[x].internal_host + ":"+serversReturned[x].internal_port)
            }
        }
    })



} else if (firstArg == 'main') {
    findMainServer(function(serverName) {
        console.log(serverName)
    })


} else {
    process.argv.unshift(firstArg)
    //options.query.a = process.argv

    var args = process.argv

    //console.log("Args JSON: " + JSON.stringify(args,null,2))

    var args2 = encodeURI(JSON.stringify(args))
    //console.log("Args seralized: " + args2)

    var options = {
      host: extractHostname(useHost),
      port: getPort(useHost),
      path: '/vf?a=' + args2,
      method: 'GET'
    };


    var req = http.request(options, function(res) {
      res.setEncoding('utf8');
      res.on('data', function (chunk) {
          var result = JSON.parse(chunk)
        console.log(result.OK);
      });
    });

    req.on('error', function(e) {
      console.log('problem with request: ' + e.message);
    });


    req.end();
}




function serialize(obj) {
  var str = [];
  for (var p in obj)
    if (obj.hasOwnProperty(p)) {
      str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
    }
  return str.join("&");
}








function lsFn(servers, index, callbackFn) {
    //console.log("Trying lookup server: " + servers[index])

    var remoteServerUrl =   'http://' + servers[index]  +
                            "/get_intranet_servers?time=" + new Date().getTime();

    request({
        uri: remoteServerUrl,
        method: "GET",
        timeout: 10000,
        agent: false,
        followRedirect: true,
        maxRedirects: 10
  },
  function(error, response, body) {
      var servers2 = servers
      if (error) {
          console.log("Error opening central server: " + error);
          var nextIndex = index + 1
          if (nextIndex < servers2.length) {
              lsFn(servers2, index + 1, callbackFn)
          }
    } else {
        var servers2 = []
        var returned= eval( "(" + body + ")");
        for (var i = 0 ; i < returned.allServers.length; i++) {
            //console.log('got server ' + i)
            //console.log(JSON.stringify(returned.allServers[i],null,2))
            var tt = new Object();
            tt.username = returned.allServers[i].client_user_name;
            tt.internal_host = returned.allServers[i].internal_host;
            tt.internal_port = returned.allServers[i].internal_port;
            tt.via           = returned.allServers[i].via;
            servers2.push(tt)
        };
        callbackFn(servers2)

    }
  });


}


function extractHostname(url) {
    var hostname;
    //find & remove protocol (http, ftp, etc.) and get hostname

    if (url.indexOf("://") > -1) {
        hostname = url.split('/')[2];
    }
    else {
        hostname = url.split('/')[0];
    }

    //find & remove port number
    hostname = hostname.split(':')[0];
    //find & remove "?"
    hostname = hostname.split('?')[0];

    return hostname;
}


function getPort(url) {
    url = url.match(/^(([a-z]+:)?(\/\/)?[^\/]+).*$/)[1] || url;
    var parts = url.split(':'),
        port = parseInt(parts[parts.length - 1], 10);
    if(parts[0] === 'http' && (isNaN(port) || parts.length < 3)) {
        return 80;
    }
    if(parts[0] === 'https' && (isNaN(port) || parts.length < 3)) {
        return 443;
    }
    if(parts.length === 1 || isNaN(port)) return 80;
    return port;
}


function findMainServer(callbackFn) {

    getListOfHosts(function(serversReturned) {
        if (serversReturned.length == 0) {
            callbackFn("No main VisiFile Server as none running on your Intranet")
        } else {
            var mainServer = serversReturned[0].username + ", " + serversReturned[0].internal_host + ":"+serversReturned[0].internal_port

            for (var x = 1; x < serversReturned.length ; x++) {
                if (serversReturned[x].internal_host == hostaddress) {
                     mainServer = serversReturned[x].username + ", " + serversReturned[x].internal_host + ":"+serversReturned[x].internal_port
                }
            }
            callbackFn(mainServer)

        }
    })
}
{'fraction_non_alphanumeric': 0.14699634633358824, 'fraction_numerical': 0.021921998470558247, 'mean_word_length': 2.3899769585253456, 'pattern_counts': {'":': 10, '<': 15, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 12, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5639821', 'n_tokens_mistral': 3970, 'n_tokens_neox': 3733, 'n_words': 915}