Permalink
Browse files

Bugfixes and began writing tests.

  • Loading branch information...
1 parent be75d54 commit 49302155db2f965e6b603c3f5fec8f7d39dda319 @razielgn committed Mar 10, 2011
View
BIN .DS_Store
Binary file not shown.
View
@@ -32,3 +32,9 @@
ehthumbs.db
Icon?
Thumbs.db
+
+# Directories #
+###############
+tmp/
+coverage/
+coverage.data
View
@@ -4,7 +4,7 @@ gem "sinatra"
gem "haml"
gem "bencode"
gem "bson_ext"
-gem "mongoid", "2.0.0.beta.20"
-gem "exceptional"
+gem "mongoid", "2.0.0.rc.6"
+gem "exceptional" if ENV['HEROKU']
-gem "newrelic_rpm"
+gem "newrelic_rpm" if ENV['HEROKU']
View
@@ -1,33 +1,30 @@
GEM
remote: http://rubygems.org/
specs:
- activemodel (3.0.3)
- activesupport (= 3.0.3)
+ activemodel (3.0.5)
+ activesupport (= 3.0.5)
builder (~> 2.1.2)
i18n (~> 0.4)
- activesupport (3.0.3)
+ activesupport (3.0.5)
bencode (0.6.0)
- bson (1.1.5)
- bson_ext (1.1.5)
+ bson (1.2.4)
+ bson_ext (1.2.4)
builder (2.1.2)
- exceptional (2.0.32)
- rack
haml (3.0.25)
i18n (0.5.0)
- mongo (1.1.5)
- bson (>= 1.1.5)
- mongoid (2.0.0.beta.20)
+ mongo (1.2.4)
+ bson (>= 1.2.4)
+ mongoid (2.0.0.rc.6)
activemodel (~> 3.0)
- mongo (~> 1.1)
+ mongo (~> 1.2)
tzinfo (~> 0.3.22)
will_paginate (~> 3.0.pre)
- newrelic_rpm (2.13.4)
rack (1.2.1)
- sinatra (1.1.2)
+ sinatra (1.2.0)
rack (~> 1.1)
- tilt (~> 1.2)
- tilt (1.2.1)
- tzinfo (0.3.23)
+ tilt (>= 1.2.2, < 2.0)
+ tilt (1.2.2)
+ tzinfo (0.3.24)
will_paginate (3.0.pre2)
PLATFORMS
@@ -36,8 +33,6 @@ PLATFORMS
DEPENDENCIES
bencode
bson_ext
- exceptional
haml
- mongoid (= 2.0.0.beta.20)
- newrelic_rpm
+ mongoid (= 2.0.0.rc.6)
sinatra
View
@@ -5,7 +5,7 @@ It uses mongodb! :D
__PERFORMANCE__
I measured 5-6ms responses on Heroku.
-__CONFIG__
+__CONFIG__
To change config see config/tracker.yaml
* **tracker_id**: String. It is also reported to the clients.
* **announce_interval**: In seconds.
View
@@ -0,0 +1,7 @@
+require 'rubygems'
+require 'spec/rake/spectask'
+
+desc "Run all tests"
+Spec::Rake::SpecTask.new('spec') do |t|
+ t.pattern = 'spec/**/*_spec.rb'
+end
View
Binary file not shown.
View
@@ -1,7 +1,9 @@
require 'logger'
-class CustomLogger < Logger
- def format_message(severity, timestamp, progname, msg)
- "#{timestamp.strftime("%Y-%m-%d %H:%M:%S")} #{severity} - #{msg}\n"
- end
+module STracker
+ class CustomLogger < Logger
+ def format_message(severity, timestamp, progname, msg)
+ "#{timestamp.strftime("%Y-%m-%d %H:%M:%S")} #{severity} - #{msg}\n"
+ end
+ end
end
View
@@ -0,0 +1,13 @@
+class String
+ def to_hex
+ (self.unpack "H*").first
+ end
+
+ def to_bin
+ [self].pack "H*"
+ end
+
+ def to_bool
+ self.to_i == 0 ? false : true
+ end
+end
View
@@ -13,7 +13,7 @@ class Peer
embedded_in :torrent, :inverse_of => :peers
- def update_self(request)
+ def update_self(request)
self.ip = request.ip
self.port = request.port
self.downloaded = request.downloaded
@@ -28,7 +28,7 @@ def get_compact
ip = self.ip.split('.').collect{|n| [n.to_i.to_s(16)].pack("H2")}.join
port = [self.port.to_s(16)].pack("H4")
- "#{ip}#{port}"
+ ip + port
end
def get_noncompact
View
@@ -1,44 +1,40 @@
module STracker
class Request
- REQ_KEYS = ["info_hash", "peer_id", "port", "uploaded", "downloaded", "left", "compact", "ip"]
-
- attr_reader :info_hash, :peer_id, :port, :uploaded, :downloaded, :left, :ip, :event, :key, :tracker_id, :compact, :numwant
-
- def initialize(request)
- if !validate(request)
- raise TrackerException, "Request was invalid!"
- end
+ @@req_keys = ["info_hash", "peer_id", "port", "uploaded", "downloaded", "left", "compact", "ip"]
+ @@pars_keys = ["to_hex", "to_hex", "to_i", "to_i", "to_i", "to_i", "to_bool", "to_s"]
- @info_hash = STracker::Tracker.bin2hex(request["info_hash"])
- @peer_id = STracker::Tracker.bin2hex(request["peer_id"])
- @port = request["port"].to_i
- @uploaded = request["uploaded"].to_i
- @downloaded = request["downloaded"].to_i
- @left = request["left"].to_i
- @ip = request["ip"]
- @compact = !!(request["compact"] == 1)
+ attr_reader :numwant, :event
- if request["event"]
- @event = request["event"]
- end
- if request["key"]
- @key = request["key"]
- end
- if request["tracker id"]
- @tracker_id = request["tracker id"]
+ def initialize(request)
+ if not valid?(request)
+ raise TrackerException, "Request was invalid!"
end
- if request["numwant"]
- @numwant = request["numwant"]
+
+ request.each_pair do |key, value|
+ meta.send :attr_reader, key.to_sym
+
+ if @@req_keys.include? key
+ value = value.send @@pars_keys[@@req_keys.index key]
+ end
+
+ instance_variable_set("@#{key}", value)
end
- @numwant ||= 50
+ @event ||= "started"
+ @numwant ||= 30
end
private
- def validate(request)
+ def valid?(request)
keys = request.keys
- !REQ_KEYS.collect{|key| keys.include?(key)}.include?(false)
+ not @@req_keys.collect{|key| keys.include?(key)}.include?(false)
+ end
+
+ def meta
+ class << self
+ self
+ end
end
end
end
View
@@ -1,6 +1,7 @@
module STracker
class Torrent
include Mongoid::Document
+ store_in :torrents
identity :type => String
field :seeders, :type => Integer, :default => 0
@@ -9,7 +10,7 @@ class Torrent
embeds_many :peers, :class_name => "STracker::Peer"
- def update_torrent(request, min_announce)
+ def update_torrent(request)
# Checks if the peer was already in list
peer = peers.select{|p| p.id == request.peer_id}.first
@@ -22,22 +23,11 @@ def update_torrent(request, min_announce)
peers << peer
peer.save
- if peer.left == 0
- inc(:seeders, 1)
- else
- inc(:leechers, 1)
- end
+ inc(peer.left == 0 ? :seeders : :leechers, 1)
else
if request.event == "stopped"
- if peer.left == 0
- inc(:seeders, -1)
- else
- inc(:leechers, -1)
- end
-
+ inc(peer.left == 0 ? :seeders : :leechers, -1)
peer.delete
-
- return false
end
peer.update_self(request)
@@ -48,8 +38,6 @@ def update_torrent(request, min_announce)
inc(:leechers, -1)
end
end
-
- true
end
def clear_zombies(cutoff)
@@ -58,12 +46,7 @@ def clear_zombies(cutoff)
if count > 0
zombies.each do |zombie|
- if zombie.left == 0
- inc(:seeders, -1)
- else
- inc(:leechers, -1)
- end
-
+ inc(zombie.left == 0 ? :seeders : :leechers, -1)
zombie.delete
end
end
@@ -92,17 +75,13 @@ def get_peers(numwant, compact)
limit -= 1
end
- if compact
- compact_s
- else
- noncompact
- end
+ compact ? compact_s : noncompact
end
private
- def min(n1, n2)
- if n1 > n2; n2; else n1; end;
+ def min(value1, value2)
+ (value1 > value2) ? value2 : value1
end
end
end
Oops, something went wrong.

0 comments on commit 4930215

Please sign in to comment.