Permalink
Browse files

adding portchecker lib inside arrow code base

  • Loading branch information...
1 parent da849d7 commit d8b00061476e7797e81d4ce91a40f354352c9194 Pranav Verma committed Nov 4, 2012
Showing with 103 additions and 3 deletions.
  1. +101 −0 ext-lib/portchecker.js
  2. +2 −2 lib/proxy/proxymanager.js
  3. +0 −1 package.json
View
@@ -0,0 +1,101 @@
+/**
+* LICENSE MIT
+* (C) Daniel Zelisko
+* http://github.com/danielzzz/node-portchecker
+*
+* a simple tcp port checker
+* Use it for checking if a service is up or to find available ports on a machine
+*/
+
+var net = require('net');
+var timeout = 400; //miliseconds
+
+
+
+// start port, last port, host, callback
+exports.getFirstAvailable = function (startPort, endPort, host, callback) {
+ if (startPort>endPort) {
+ throw new Error('portchecker: startPort must be lower than endPort');
+ }
+ //console.log('looking for an available port in ' + startPort + '-' + endPort + ' on ' + host);
+ var notFree = false;
+ var currentPort = startPort;
+
+ var onCheckPort = function(isOpen){
+ isOpen && check();
+ !isOpen && callback((currentPort-1), host);
+ }
+
+ var check = function() {
+ //---- return -1 if we checked all ports from the range already
+ if (currentPort>endPort) {callback(-1, host); return; };
+
+ //console.log('checking :' + currentPort);
+ exports.isOpen(currentPort, host, onCheckPort);
+ currentPort++;
+ }
+
+ //----- start checking ----------
+ check();
+
+
+}
+
+exports.getAllOpen = function (startPort, endPort, host, callback) {
+ if (startPort>endPort) {
+ throw new Error('portchecker: startPort must be lower than endPort');
+ }
+ //console.log('looking for open ports between ' + startPort + '-' + endPort + ' on ' + host);
+ var notFree = false,
+ openPorts = [];
+ var currentPort = startPort;
+
+ var onCheckPort = function(isOpen){
+ if (isOpen) {
+ openPorts.push((currentPort-1))
+ }
+ check();
+ }
+
+ var check = function() {
+ //---- return -1 if we checked all ports from the range already
+ if (currentPort>endPort) {callback(openPorts, host); return; };
+
+ //console.log('checking :' + currentPort);
+ exports.isOpen(currentPort, host, onCheckPort);
+ currentPort++;
+ }
+
+ //----- start checking ----------
+ check();
+
+
+}
+
+exports.isOpen = function (port, host, callback) {
+ var isOpen = false;
+ var conn = net.createConnection(port, host);
+
+ var timeoutId = setTimeout(function() {onClose();}, timeout);
+ var onClose = function() {
+ clearTimeout(timeoutId);
+ delete conn;
+ callback(isOpen, port, host);
+ };
+
+ var onOpen = function() {
+ isOpen = true;
+ //console.log(host+":"+p+" is taken");
+ conn.end();
+ };
+
+ conn.on('close', onClose);
+ conn.on('error', function() {conn.end();});
+ conn.on('connect', onOpen);
+}
+
+exports.setTimeout = function(t) {
+ timeout = t;
+}
+
+
@@ -9,8 +9,8 @@
var http = require('http'),
log4js = require("log4js"),
fs = require('fs'),
- portchecker = require('portchecker'),
- path = require('path');
+ path = require('path'),
+ portchecker = require("../../ext-lib/portchecker");
function ProxyManager(routerConfig) {
this.routerJsonPath = routerConfig;
View
@@ -41,7 +41,6 @@
"log4js": "0.5.1",
"clone": "0.1.0",
"useragent": "1.1.0",
- "portchecker": "0.1.0",
"istanbul": "0.1.10",
"uglify-js": "1.3.4"
},

0 comments on commit d8b0006

Please sign in to comment.