Created
May 8, 2011 23:57
-
-
Save cobracmder/961820 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env node | |
| try { | |
| var util = require('util'); | |
| } catch(err) { | |
| var util = require('sys'); | |
| } | |
| try { | |
| var args = require('arguments'); | |
| } catch(err) { | |
| console.log('Error: missing arguments module'); | |
| console.log('install via: npm install arguments'); | |
| process.exit(1); | |
| } | |
| var proto = 'tcp'; | |
| var svc = 'http'; | |
| args.parse([ | |
| {'name': /^(-h|--help)$/, 'expected': null, 'callback': printHelp}, | |
| {'name': /^(-p|--proto)$/, 'expected': /^(tcp|udp)$/, 'callback': setupProto}, | |
| {'name': /^(-s|--service)$/, 'expected': /^([a-z]{1,})$/, 'callback': setupService} | |
| ],main,invalidArgument); | |
| function printHelp (end) { | |
| console.log('browse-mdns.js [OPTIONS]'); | |
| console.log('\t-p|--proto [PROTOCOL]....PROTOCOL is either tcp or udp (default: tcp)'); | |
| console.log('\t-s|--service [SVC].......SVC is the service to browse for (default: http)'); | |
| end(); | |
| process.exit(); | |
| } | |
| function setupProto (end,proto) { | |
| protocol = proto; | |
| end(); | |
| } | |
| function setupService (end,service) { | |
| svc = service; | |
| end(); | |
| } | |
| function invalidArgument (arg,value_missing) { | |
| console.log('Error: the argument %s %s', arg, (value_missing?'expects a value':'is not valid')); | |
| } | |
| function main () { | |
| try { | |
| var mdns = require('mdns'); | |
| } catch(err) { | |
| console.log('Error: missing mdns module'); | |
| console.log('install via: npm install mdns'); | |
| process.exit(1); | |
| } | |
| var browser = mdns.createBrowser(svc,proto); | |
| browser.on('serviceUp', function(info,flags){ | |
| //util.puts('Found: ' + util.inspect(info)); | |
| util.puts('Found: ' + info.name + ' - ' + svc + '://' + info.hosttarget + ':' + info.port); | |
| }); | |
| browser.on('serviceDown', function(info,flags){ | |
| //util.puts('Leaving: ' + util.inspect(info)); | |
| util.puts('Leaving: ' + info.name + ' - ' + svc + '://' + info.hosttarget + ':' + info.port); | |
| }); | |
| browser.start(); | |
| setTimeout(function(){ | |
| browser.stop(); | |
| },5000); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment