// Kiel Host Scanner
// =================
//
// Usage
// -----
//
// Scan all 65536 ports in a host:
//
// ```javascript
// var HostScanner = require("kiel/src/host-scanner")
// var parsePattern = require("object-pattern").parse
// var hostScanner = new HostScanner
//
// hostScanner.add(parsePattern({
//   method: "GET",
//   resource: ["scan", "google.com", "*"]
// }), function (event) {
//   console.log("order to scan " + event.resource[2])
// })
//
// hostScanner.send({
//   method: "GET",
//   resource: ["scan", "google.com"]
// })
// ```
"use strict"


var Sydney  = require("sydney")
var OP      = require("object-pattern")
var example = require("washington")


var HostScanner = function () {}

HostScanner.prototype = Object.create(Sydney.prototype)

HostScanner.prototype.endpoint = OP.parse({
  method: "GET",
  resource: ["scan", "*"]
})

HostScanner.prototype.callback = function (event, venue) {
  for (var i = 0; i < 65536; i ++) {
    venue.broadcast({
      method: "GET",
      resource: ["scan", event.resource[1], i]
    })
  }
}


example("GET/scan/localhost calls port 0", function (done) {
  var hostScanner = new HostScanner

  hostScanner.add(function (event) {
    done(event.resource[2], 0)
  })

  hostScanner.send({method: "GET", resource: ["scan", "localhost"]})
})


example("GET/scan/localhost calls port 65536", function (done) {
  var hostScanner = new HostScanner
  var index = 0

  hostScanner.add(function (event) {
    if (index == 65535)
      done(event.resource[2], 65535)

    index ++
  })

  hostScanner.send({method: "GET", resource: ["scan", "localhost"]})
})


module.exports = HostScanner

@@ -561,40 +561,8 @@
 %22%0A%0A%0A
-var Sydney  = require(%22sydney%22)%0A
 var 
@@ -673,65 +673,8 @@
 %7B%7D%0A%0A
-HostScanner.prototype = Object.create(Sydney.prototype)%0A%0A
 Host
@@ -960,16 +960,175 @@
 %0A  %7D%0A%7D%0A%0A
+example(%22Matches GET/scan/localhost%22, function () %7B%0A  return new HostScanner().endpoint.match(%7B%0A    method: %22GET%22,%0A    resource: %5B%22scan%22, %22localhost%22%5D%0A  %7D)%0A%7D)%0A
 %0Aexample
@@ -1217,35 +1217,47 @@
 Scanner%0A
-%0A
   
-hostScanner.add(
+var venue = %7B%0A    broadcast: 
 function
@@ -1267,24 +1267,26 @@
 vent) %7B%0A    
+ 
+ 
 done(event.r
@@ -1306,32 +1306,36 @@
 )%0A  
-%7D)%0A%0A  hostScanner.send(%7B
+  %7D%0A  %7D%0A  var event = %7B%0A    
 meth
@@ -1336,32 +1336,36 @@
   method: %22GET%22,
+%0A   
  resource: %5B%22sca
@@ -1376,25 +1376,65 @@
 %22localhost%22%5D
-%7D
+%0A  %7D%0A%0A  hostScanner.callback(event, venue
 )%0A%7D)%0A%0A%0Aexamp
@@ -1549,27 +1549,39 @@
 = 0%0A
-%0A
   
-hostScanner.add(
+var venue = %7B%0A    broadcast: 
 func
@@ -1599,16 +1599,18 @@
 ) %7B%0A    
+ 
+ 
 if (inde
@@ -1621,16 +1621,18 @@
  65535)%0A
+  
       do
@@ -1665,16 +1665,18 @@
 5)%0A%0A    
+  
 index ++
@@ -1682,32 +1682,36 @@
 +%0A  
-%7D)%0A%0A  hostScanner.send(%7B
+  %7D%0A  %7D%0A  var event = %7B%0A    
 meth
@@ -1720,16 +1720,20 @@
 : %22GET%22,
+%0A   
  resourc
@@ -1756,17 +1756,57 @@
 alhost%22%5D
-%7D
+%0A  %7D%0A%0A  hostScanner.callback(event, venue
 )%0A%7D)%0A%0A%0Am

Update HostScanner