var http = require('http');
var util = require('./cp-util');
var HEARTBEAT_INTERVAL = util.HEARTBEAT_INTERVAL;
var HEARTBEAT_TIMEOUT = util.HEARTBEAT_TIMEOUT;
var MESSAGE = util.MESSAGE;
var MAX_PORT = 65535;
var curPort = 60000;
var DELAY = 600;
var options = JSON.parse(decodeURIComponent(process.argv[2]));
var execPlugin = require(options.pluginPath);
var timeout, heartbeatTimeout;

getServer(function(server, port) {
	execPlugin(server);
	process.send(JSON.stringify({
		type: 'port',
		port: port
	}));
});
heartbeat();
process.on('message', heartbeat);

function heartbeat() {
	clearTimeout(heartbeatTimeout);
	clearTimeout(timeout);
	timeout = setTimeout(function() {
		process.send(MESSAGE);
	}, HEARTBEAT_INTERVAL);
	heartbeatTimeout = setTimeout(handleError, HEARTBEAT_TIMEOUT);
}

function handleError() {
	process.exit(1);
}

function getServer(callback) {
	if (curPort > MAX_PORT) {
		curPort = 40000;
	}
	
	var server = http.createServer();
	var port = curPort++;
	var next = function() {
		getServer(callback);
	};
	server.on('error', next);
	server.listen(port, function() {
		server.removeListener('error', next);
		callback(server, port);
	});
}






@@ -1,12 +1,44 @@
+var assert = require('assert');%0A
 var http = r
@@ -413,16 +413,106 @@
 meout;%0A%0A
+assert(typeof execPlugin == 'function', options.pluginPath + ' must export a function');%0A%0A
 getServe

check plugin