Class: PDTP::Client::Connection

Inherits:
Protocol show all
Defined in:
lib/pdtp/client/connection.rb

Overview

Implementation of the PDTP client protocol

Direct Known Subclasses

Server::FileService::Protocol

Instance Attribute Summary collapse

Attributes inherited from Protocol

#user_data

Instance Method Summary collapse

Methods inherited from Protocol

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

Methods inherited from LengthPrefixProtocol

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

Constructor Details

This class inherits a constructor from PDTP::Protocol

Instance Attribute Details

#callbacksObject

Returns the value of attribute callbacks.



33
34
35
# File 'lib/pdtp/client/connection.rb', line 33

def callbacks
  @callbacks
end

#clientObject

Returns the value of attribute client.



32
33
34
# File 'lib/pdtp/client/connection.rb', line 32

def client
  @client
end

Instance Method Details

#connection_completedObject

Called after a connection to the server has been established



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pdtp/client/connection.rb', line 36

def connection_completed
  begin
    #create the client
    #PDTP::Protocol.listener = self

    # Tell the server about ourself
    send_message(:register,
      :listen_port => @client.listen_port, 
      :client_id => @client.client_id
    )
    
    callbacks.connected client
  rescue Exception => e
    puts "Exception in connection_completed: #{e}"
    puts e.backtrace.join("\n")
    exit
  end
end

#finished(transfer) ⇒ Object

Transfers to report themselves finished



117
118
119
# File 'lib/pdtp/client/connection.rb', line 117

def finished(transfer)
  @client.transfers.delete(transfer)
end

Prints the number of transfers associated with this client



112
113
114
# File 'lib/pdtp/client/connection.rb', line 112

def print_stats
  #@@log.debug "client:  num_transfers=#{@client.transfers.size}"
end

#receive_message(command, message) ⇒ Object

Called when any server message is received. This is the brains of the client’s protocol handling.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/pdtp/client/connection.rb', line 66

def receive_message(command, message)
  case command
  when "tell_info" # Receive and store information for this url
    info = @client.file_service.get_info(message["url"])
    #info = FileInfo.new message["url"].split('/').last
    info.file_size = message["size"]
    info.base_chunk_size = message["chunk_size"]
    info.streaming = message["streaming"]
    #@client.file_service.set_info(message["url"], info)   
  when "transfer" # Begin a transfer as a connector
    transfer = Transfer::Connector.new(
      self,
      message,
      @client.file_service
    )
    
    transfer.run

    ##@@log.debug "TRANSFER STARTING"
  when "tell_verify"
    # We are a listener, and asked for verification of a transfer from a server.
    # After asking for verification, we stopped running, and must be restarted
    # if verification is successful

    found=false
    @client.transfers.each do |t|
      if t.matches_message?(message)
        finished(t)
        t.tell_verify(message["authorized"])
        found=true
        break
      end
    end

    unless found
      puts "BUG: Tell verify sent for an unknown transfer"
      exit!
    end
  when "hash_verify"
    #@@log.debug "Hash verified for url=#{message["url"]} range=#{message["range"]} hash_ok=#{message["hash_ok"]}"
  when "protocol_error", "protocol_warn" #ignore
  else raise "Server sent an unknown message type: #{command} "
  end
end

#unbindObject

Called when the connection to the server closes



56
57
58
59
60
61
62
# File 'lib/pdtp/client/connection.rb', line 56

def unbind
  if callbacks
    callbacks.disconnected client
  else
    puts "Disconnected from PDTP server"
  end
end