Skip to content

Commit

Permalink
* lib/parkplace/torrent.rb: removed all internal seeder code.
Browse files Browse the repository at this point in the history
 * lib/parkplace.rb: see below.
 * bin/parkplace: moved a bunch of the repetitive config stuff to the ParkPlace module.
 * bin/parkseed: external seeder tool.
 * Rakefile: include parkseed.
  • Loading branch information
_why committed May 3, 2006
1 parent 04b54cc commit 9052542
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 51 deletions.
3 changes: 2 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ spec =
s.summary = "a web file storage service, lovely with BitTorrent support."
s.description = s.summary
s.author = "why the lucky stiff"
s.executables = ['parkplace']
s.executables = ['parkplace', 'parkseed']

s.add_dependency('mongrel', '>= 0.3.12.5')
s.add_dependency('camping', '>= 1.4.1')
s.add_dependency('sqlite3-ruby', '>=1.1.0')
s.add_dependency('rubytorrent', '>= 0.3')
s.required_ruby_version = '>= 1.8.4'

Expand Down
52 changes: 17 additions & 35 deletions bin/parkplace
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
#!/usr/bin/env ruby
$: << "./lib"
require 'ftools'
$:.unshift "./lib"
require 'optparse'
require 'ostruct'
require 'yaml'
require 'parkplace'

DEFAULT_PASSWORD = 'pass@word1'
DEFAULT_SECRET = 'OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV'

options = OpenStruct.new
options = ParkPlace.options
options.host = "127.0.0.1"
options.port = 3002

opts = OptionParser.new do |opts|
opts.banner = "Usage: parkplace [options] [directory]"
opts.banner = "Usage: parkplace [options] [host] [port]"
opts.separator "Default host is #{options.host}, default port is #{options.port}."

opts.separator ""
opts.separator "Specific options:"

opts.on("-d", "--directory DIRECTORY",
"Park Place directory (defaults to #{options.parkplace_dir || 'None'})") do |d|
options.parkplace_dir = d
end

opts.on("-D", "--[no-]daemon", "Daemon mode") do |d|
options.daemon = d
end
Expand All @@ -39,35 +46,10 @@ opts = OptionParser.new do |opts|
end

opts.parse! ARGV
options.parkplace_dir = ARGV[0]
options.host = ARGV[0] if ARGV[0]
options.port = ARGV[1].to_i if ARGV[1]
ParkPlace.config(options)

# locate ~/.parkplace
if options.parkplace_dir.nil?
homes = []
homes << [ENV['HOME'], File.join( ENV['HOME'], '.parkplace' )] if ENV['HOME']
homes << [ENV['APPDATA'], File.join( ENV['APPDATA'], 'ParkPlace' )] if ENV['APPDATA']
homes.each do |home_top, home_dir|
next unless home_top
if File.exists? home_top
options.parkplace_dir = home_dir
break
end
end
end

abort "** No home directory found, please say the directory when you run #$O." unless options.parkplace_dir
File.makedirs( options.parkplace_dir )
conf = File.join( options.parkplace_dir, 'config.yaml' )
if File.exists? conf
YAML.load_file( conf ).each { |k,v| options.__send__("#{k}=", v) if options.__send__(k).nil? }
end
options.storage_dir = File.expand_path(options.storage_dir || 'storage', options.parkplace_dir)
options.database ||= {:adapter => 'sqlite3', :database => File.join(options.parkplace_dir, 'park.db')}

module ParkPlace; end
ParkPlace::STORAGE_PATH = options.storage_dir

require 'parkplace'
require 'parkplace/control'
include ParkPlace

Expand All @@ -88,4 +70,4 @@ if admin.password == hmac_sha1( DEFAULT_PASSWORD, admin.secret )
puts "** You should change the default password or delete the admin at soonest chance!"
end

ParkPlace.serve
ParkPlace.serve(options.host, options.port)
70 changes: 70 additions & 0 deletions bin/parkseed
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env ruby
$:.unshift "./lib"
require 'optparse'
require 'parkplace'
require 'stringio'

options = ParkPlace.options
options.seed_host = "127.0.0.1"
options.seed_port = 3003

opts = OptionParser.new do |opts|
opts.banner = "Usage: parkseed [options] [host] [port]"
opts.separator "Default host is #{options.seed_host}, default port is #{options.seed_port}."

opts.separator ""
opts.separator "Specific options:"

opts.on("-d", "--directory DIRECTORY",
"Park Place directory (defaults to #{options.parkplace_dir || 'None'})") do |d|
options.parkplace_dir = d
end

opts.on("-D", "--[no-]daemon", "Daemon mode") do |d|
options.daemon = d
end

opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options.verbose = v
end

opts.separator ""
opts.separator "Common options:"

opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end

# Another typical switch to print the version.
opts.on_tail("--version", "Show version") do
puts ParkPlace::VERSION
exit
end
end

opts.parse! ARGV
options.seed_host = ARGV[0] if ARGV[0]
options.seed_port = ARGV[1].to_i if ARGV[1]
ParkPlace.config(options)

ParkPlace::Models::Base.establish_connection(options.database)
ParkPlace::Models::Base.logger = Logger.new('camping.log') if $DEBUG
ParkPlace.create

server = RubyTorrent::Server.new(options.seed_host, options.seed_port).start
loop do
ParkPlace::Models::Torrent.find(:all, :include => :bit).each do |trnt|
mi = RubyTorrent::MetaInfo.from_stream(StringIO.new(trnt.metainfo))
bit = trnt.bit
unless server.instance_variable_get("@controllers").has_key? mi.info.sha1
begin
puts "SEEDING #{bit.name}"
server.add_torrent(mi, RubyTorrent::Package.new(mi, bit.fullpath))
rescue Exception => e
puts "#{e.class}: #{e.message}"
end
end
end
sleep 2.minutes
end
40 changes: 35 additions & 5 deletions lib/parkplace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
module ParkPlace
VERSION = "1.0"
BUFSIZE = (4 * 1024)
STORAGE_PATH ||= File.join(Dir.pwd, 'storage')
STORAGE_PATH = File.join(Dir.pwd, 'storage')
STATIC_PATH = File.expand_path('../static', File.dirname(__FILE__))
RESOURCE_TYPES = %w[acl torrent]
CANNED_ACLS = {
Expand All @@ -43,21 +43,51 @@ def create
Camping::Models::Session.create_schema
ParkPlace::Models.create_schema
end
def serve
def options
require 'ostruct'
options = OpenStruct.new
if options.parkplace_dir.nil?
homes = []
homes << [ENV['HOME'], File.join( ENV['HOME'], '.parkplace' )] if ENV['HOME']
homes << [ENV['APPDATA'], File.join( ENV['APPDATA'], 'ParkPlace' )] if ENV['APPDATA']
homes.each do |home_top, home_dir|
next unless home_top
if File.exists? home_top
options.parkplace_dir = home_dir
break
end
end
end
options
end
def config(options)
require 'ftools'
require 'yaml'
abort "** No home directory found, please say the directory when you run #$O." unless options.parkplace_dir
File.makedirs( options.parkplace_dir )
conf = File.join( options.parkplace_dir, 'config.yaml' )
if File.exists? conf
YAML.load_file( conf ).each { |k,v| options.__send__("#{k}=", v) if options.__send__(k).nil? }
end
options.storage_dir = File.expand_path(options.storage_dir || 'storage', options.parkplace_dir)
options.database ||= {:adapter => 'sqlite3', :database => File.join(options.parkplace_dir, 'park.db')}
ParkPlace::STORAGE_PATH.replace options.storage_dir
end
def serve(host, port)
require 'mongrel'
require 'mongrel/camping'

# Use the Configurator as an example rather than Mongrel::Camping.start
config = Mongrel::Configurator.new :host => "0.0.0.0" do
listener :port => 3002 do
config = Mongrel::Configurator.new :host => host do
listener :port => port do
uri "/", :handler => Mongrel::Camping::CampingHandler.new(ParkPlace)
uri "/favicon", :handler => Mongrel::Error404Handler.new("")
trap("INT") { stop }
run
end
end

puts "** ParkPlace example is running at http://localhost:3002/"
puts "** ParkPlace example is running at http://#{host}:#{port}/"
config.join
end
end
Expand Down
11 changes: 1 addition & 10 deletions lib/parkplace/torrent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ def from_hex_s
end

module ParkPlace
# TORRENT_SERVER = RubyTorrent::Server.new("192.168.0.4", 3003).start
TRACKER_INTERVAL = 10.minutes

# All tracker errors are thrown as this class.
Expand All @@ -25,16 +24,8 @@ def torrent bit
info_hash = Digest::SHA1.digest(mi.info.to_bencoding).to_hex_s
unless t and t.info_hash == info_hash
t ||= Models::Torrent.new
t.update_attributes(:info_hash => info_hash, :bit_id => bit.id, :metainfo => "X!X NOT CACHED X!X")
t.update_attributes(:info_hash => info_hash, :bit_id => bit.id, :metainfo => mi.to_bencoding)
end
# unless TORRENT_SERVER.instance_variable_get("@controllers").has_key? mi.info.sha1
# begin
# puts "SEEDING..."
# p TORRENT_SERVER.add_torrent(mi, RubyTorrent::Package.new(mi, bit.fullpath))
# rescue Exception => e
# puts "#{e.class}: #{e.message}"
# end
# end
r(200, mi.to_bencoding, 'Content-Disposition' => "attachment; filename=#{bit.name}.torrent;",
'Content-Type' => 'application/x-bittorrent')
end
Expand Down

0 comments on commit 9052542

Please sign in to comment.