 url  = require 'url'
 qs   = require 'querystring'
 fs   = require 'fs'
+bencode = require '../../lib/bencode'
 
 # defines
 ANNOUNCE_INTERVAL = 60

    
    # http server
    http.createServer (req, res) =>
      req_url = url.parse req.url
      if req_url.pathname.substr(-9) is '/announce'
        if query = req_url.query
          @announce query, req, res
        else
          res.end '!?'

    obj[kvp.substr(0, x)] = kvp.substr(x + 1) if ~(x = kvp.indexOf '=')
  obj

#bencode.bencode = (o) -> new Buffer 1
#bencode = (o) ->
#  bstring = (s) -> s.length + ':' + s
#  bint = (n) -> "i#{n}e"
#  blist = (l) ->
#    buf = 'l'
#    buf += bencode i for i in l
#    buf + 'e'
#  bdict = (d) ->
#    buf = 'd'
#    buf += bstring(k) + bencode(d[k]) for k in Object.keys(d).sort()
#    buf + 'e'
#  
#  switch typeof o
#    when 'string' then bstring o
#    when 'number' then bint    o
#    when 'object'
#      if o instanceof Array
#        blist o
#      else if o?
#        bdict o
#      else
#        throw new Error('cannot encode element null')
#    else
#      throw new Error('cannot encode element ' + o)

decodeURLtoHex = (str) ->
  output = ''
  for i in [0..str.length-1]
    if (str.charCodeAt i)== 37
      output += str[i+1] + str[i+2]
      i += 2
    else
      output += (str.charCodeAt i).toString 16
  return output.toLowerCase()

decodeURLtoBinary = (str, len) ->
  b = new Buffer len
  j = 0
  for i in [0..str.length-1]
    if (str.charCodeAt i)== 37
      b[j] = parseInt(str[i+1] + str[i+2], 16)
      i+=2
    else
      b[j] = str.charCodeAt i
    j += 1
  return b

# Tracker class
class Tracker
  constructor: (port) ->
    # redis
    @redis = require('redis').createClient 6379, '127.0.0.1', {'return_buffers' : true}
    @redis.on 'ready', -> console.log 'connected to redis'
    
    # mongodb
