Class: PDTP::Server::FileService::Protocol

Inherits:
Client::Connection show all
Defined in:
lib/pdtp/server/file_service_protocol.rb

Overview

Implements the file service for the pdtp protocol

Instance Attribute Summary collapse

Attributes inherited from Client::Connection

#callbacks

Instance Method Summary collapse

Methods inherited from Client::Connection

#finished, #print_stats, #receive_message, #unbind

Methods inherited from Protocol

#connection_open?, define_message_params, #error_close_connection, #get_peer_info, #hash_to_range, obj_matches_type?, #post_init, print_info, #range_to_hash, #receive_packet, #remote_peer_id, #send_message, #to_s, #unbind, validate_message

Methods inherited from LengthPrefixProtocol

#prefix_size=, #receive_data, #receive_packet, #send_packet

Constructor Details

#initialize(*args) ⇒ Protocol

Returns a new instance of Protocol.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pdtp/server/file_service_protocol.rb', line 39

def initialize *args
  @transfers = []
  @client = self
  @connection = self
  @client_id = Digest::MD5.hexdigest "#{Time.now.to_f}#{$$}"
  
  @listen_addr = '0.0.0.0'
  @listen_port = 60860
  @vhost = @listen_addr
  super
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



37
38
39
# File 'lib/pdtp/server/file_service_protocol.rb', line 37

def client
  @client
end

#client_idObject (readonly)

Returns the value of attribute client_id.



37
38
39
# File 'lib/pdtp/server/file_service_protocol.rb', line 37

def client_id
  @client_id
end

#connectionObject (readonly)

Returns the value of attribute connection.



37
38
39
# File 'lib/pdtp/server/file_service_protocol.rb', line 37

def connection
  @connection
end

#file_serviceObject (readonly)

Returns the value of attribute file_service.



37
38
39
# File 'lib/pdtp/server/file_service_protocol.rb', line 37

def file_service
  @file_service
end

#lockObject (readonly)

Returns the value of attribute lock.



37
38
39
# File 'lib/pdtp/server/file_service_protocol.rb', line 37

def lock
  @lock
end

#transfersObject (readonly)

Returns the value of attribute transfers.



37
38
39
# File 'lib/pdtp/server/file_service_protocol.rb', line 37

def transfers
  @transfers
end

Instance Method Details

#connection_completedObject

Called after a connection to the server has been established



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/pdtp/server/file_service_protocol.rb', line 52

def connection_completed
  begin
    @http_server.register "/", Client::HttpHandler.new(self)

    # Register our client_id and listen_port
    send_message :register, :listen_port => @listen_port, :client_id => @client_id

    @file_service = PDTP::Server::FileService.new
    @file_service.root = @base_path

    # Provide all the files in the root directory
    files = find_files @base_path
    files.each { |file| send_message :provide, :url => "http://#{@vhost}/#{file}" }
  rescue Exception => e
    puts "Exception in connection_completed: #{e}"
    puts e.backtrace.join("\n")
    exit
  end
end