Skip to content

Commit

Permalink
added -v cmdline option (for debug)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromeetienne committed Jul 14, 2010
1 parent 08fa44e commit dda597c
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions node-chargen.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
var sys = require('sys');
var http = require('http');

var create = function(){
var create = function(opts){
// alias opts for readability and default values
var verbose = opts['verbose'] || 0;
// pattern which gonna be displayed
var pattern = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefg";
// create the http server
var server = http.createServer(function (request, response) {
if( verbose > 0 ) console.log("Client connected");
response.writeHead(200, {
'Content-Type' : 'text/plain',
'Transfer-Encoding' : 'identity' // to force a non-chunked response
Expand All @@ -24,6 +27,7 @@ var create = function(){
var main_loop = function(){
// if the client closes the connection, stop looping
if(stoploop){
if( verbose > 0 ) console.log("Client disconnected");
response.end();
return;
}
Expand All @@ -33,6 +37,8 @@ var create = function(){
nb_line++;
// write the content to the response
response.write(content);
// log to debug
if( verbose > 1 ) console.log("One line written");
// defer the next iteration
setTimeout(main_loop, 10);
}
Expand All @@ -51,7 +57,33 @@ exports.create = create;
// Main code //
//////////////////////////////////////////////////////////////////////////////////
if( process.argv[1] == __filename ){
server = create();
var verbose = 0;

//////////////////////////////////////////////////////////////////////////////////
// parse cmdline //
//////////////////////////////////////////////////////////////////////////////////
var optind = 2;
for(;optind < process.argv.length; optind++){
var key = process.argv[optind];
var val = process.argv[optind+1];
//sys.puts("key="+key+" val="+val);
if( key == "-v" || key == "--verbose" ){
verbose += 1;
}else if( key == "-h" || key == "--help" ){
sys.puts("usage: node-chargen [-v]");
sys.puts("");
sys.puts("-v|--verbose\tIncrease the verbose level (for debug).");
process.exit(0);
}else{
// if the option doesnt exist, consider it is the first non-option parameters
break;
}
}

opts = {
"verbose" : verbose
}
server = create(opts);
server.listen(8124, "127.0.0.1");
sys.puts('Server running at http://127.0.0.1:8124/');
}

0 comments on commit dda597c

Please sign in to comment.