Skip to content

Commit

Permalink
Completed implementation on events 'stopped' and 'completed'
Browse files Browse the repository at this point in the history
  • Loading branch information
razielgn committed Jan 8, 2011
1 parent 2850034 commit 984c8cd
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion config/tracker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/peer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions lib/torrent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,28 @@ 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)

if request.event == "completed"
inc(:completed, 1)
inc(:seeders, 1)
dec(:leechers, 1)
inc(:leechers, -1)
end
end

true
end

def clear_zombies(cutoff)
Expand All @@ -56,8 +66,6 @@ def clear_zombies(cutoff)

zombie.delete
end

save
end

count
Expand Down
6 changes: 4 additions & 2 deletions lib/tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 7 additions & 3 deletions server.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions views/status.haml
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,8 @@
Leechers:
%b= torrent.leechers
%br
Completed:
%b= torrent.completed
%br
Total peers:
%b= torrent.peers.size

0 comments on commit 984c8cd

Please sign in to comment.