Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Completed implementation on events 'stopped' and 'completed'

  • Loading branch information...
commit 984c8cdfd12b33263a5dde548178bea0ed43c420 1 parent 2850034
@razielgn authored
View
2  README.markdown
@@ -3,7 +3,7 @@ I'm trying to keep it as simple as possible!
It uses mongodb! :D
__PERFORMANCE__
-I measured 2-4ms responses on Heroku.
+I measured 5-6ms responses on Heroku.
__CONFIG__
To change config see config/tracker.yaml
View
2  config/tracker.yaml
@@ -5,7 +5,7 @@ defaults: &defaults
min_announce_interval: 900
allow_unregistered_torrents: true
allow_noncompact: true
- full_scrape: false
+ full_scrape: true
development:
<<: *defaults
View
2  lib/peer.rb
@@ -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
View
2  lib/request.rb
@@ -10,7 +10,7 @@ def initialize(request)
end
@info_hash = STracker::Tracker.bin2hex(request["info_hash"])
- @peer_id = request["peer_id"]
+ @peer_id = STracker::Tracker.bin2hex(request["peer_id"])
@port = request["port"].to_i
@uploaded = request["uploaded"].to_i
@downloaded = request["downloaded"].to_i
View
18 lib/torrent.rb
@@ -28,8 +28,16 @@ def update_torrent(request, min_announce)
inc(:leechers, 1)
end
else
- if (Time.now - peer.last_announce) < min_announce
- raise TrackerException.new "Respect the announce interval!"
+ if request.event == "stopped"
+ if peer.left == 0
+ inc(:seeders, -1)
+ else
+ inc(:leechers, -1)
+ end
+
+ peer.delete
+
+ return false
end
peer.update_self(request)
@@ -37,9 +45,11 @@ def update_torrent(request, min_announce)
if request.event == "completed"
inc(:completed, 1)
inc(:seeders, 1)
- dec(:leechers, 1)
+ inc(:leechers, -1)
end
end
+
+ true
end
def clear_zombies(cutoff)
@@ -56,8 +66,6 @@ def clear_zombies(cutoff)
zombie.delete
end
-
- save
end
count
View
6 lib/tracker.rb
@@ -46,7 +46,9 @@ def announce(req)
raise TrackerException, "Non-compact response is not supported!"
end
- torrent.update_torrent(request, @min_announce_interval)
+ outcome = torrent.update_torrent(request, @min_announce_interval)
+
+ return "" if not outcome
zombies = torrent.clear_zombies(Time.now - @timeout_interval)
@logger.info "Torrent had #{zombies} in it, removed them." if zombies > 0
@@ -70,7 +72,7 @@ def announce(req)
def scrape(params)
if params.keys.include? "info_hash"
- torrents = [Torrent.find(STracker::Tracker.bin2hex(params["info_hash"]))]
+ torrents = [] << Torrent.where(:_id => STracker::Tracker.bin2hex(params["info_hash"])).only(:seeders, :leechers, :completed).first
elsif @full_scrape
torrents = Torrent.only(:seeders, :leechers, :completed)
else
View
10 server.rb
@@ -1,13 +1,11 @@
require 'sinatra/base'
require 'mongoid'
require 'haml'
-require 'exceptional'
+require 'exceptional' if ENV['HEROKU']
module STracker
class SinatraTracker < Sinatra::Base
- use Rack::Exceptional, ENV["EXCEPTIONAL_API_KEY"] if ENV["HEROKU"]
-
set :root, File.dirname(__FILE__)
set :show_exceptions, true if development?
enable :logging, :raise_errors
@@ -20,9 +18,15 @@ class SinatraTracker < Sinatra::Base
TRACKER = Tracker.new
end
+
+ configure :test do
+ require 'rack/perftools_profiler'
+ use ::Rack::PerftoolsProfiler, :default_printer => 'gif', :mode => :objects
+ end
configure :production do
require 'newrelic_rpm' if ENV['HEROKU']
+ use Rack::Exceptional, ENV["EXCEPTIONAL_API_KEY"] if ENV["HEROKU"]
end
before do
View
3  views/status.haml
@@ -37,5 +37,8 @@
Leechers:
%b= torrent.leechers
%br
+ Completed:
+ %b= torrent.completed
+ %br
Total peers:
%b= torrent.peers.size
Please sign in to comment.
Something went wrong with that request. Please try again.