Module: Fargo::Protocol::PeerUpload

Included in:
Peer
Defined in:
lib/fargo/protocol/peer_upload.rb

Constant Summary collapse

CHUNKSIZE =
16 * 1024

Instance Method Summary collapse

Instance Method Details

#receive_message(type, message) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/fargo/protocol/peer_upload.rb', line 7

def receive_message type, message
  case type
    when :adcget, :getblock, :get
      if @handshake_step == 5
        if message[:file] == 'files.xml.bz2'
          @listing = 'filelist'
        else
          @listing = @client.listing_for message[:file].gsub("\\", '/')
        end

        if message[:size] == -1
          if @listing == 'filelist'
            @size = File.size @client.local_file_list_path
          else
            @size = @listing.try :size
          end
        else
          @size = message[:size]
        end

        @offset = message[:offset]
        @zlib   = message[:zlib]

        if @listing.nil?
          if type == :getblock
            send_message 'Failed', 'File Not Available'
          else
            send_message 'Error', 'File Not Available'
          end
        elsif @client.open_upload_slots == 0 && @listing != 'filelist'
          send_message 'MaxedOut'
        elsif type == :adcget
          zl = @zlib ? ' ZL1' : ''
          send_message 'ADCSND',
            "#{message[:kind]} #{message[:file]} #{@offset} #{@size}#{zl}"

          begin_streaming
        elsif type == :getblock
          if message[:size] == -1
            send_message 'Sending'
          else
            send_message 'Sending', @size
          end

          begin_streaming
        else
          @handshake_step = 10

          send_message 'FileLength', @size
        end
      else
        error "Premature disconnect when #{type} received"
      end

    when :send
      if @handshake_step == 10
        begin_streaming
      else
        error "Premature disconnect when #{type} received"
      end

    when :cancel
      if @handshake_step == 11
        cancel_streaming
      else
        error "Premature disconnect when cancel received"
      end

    else
      super
  end
end

#unbindObject



80
81
82
83
84
85
86
87
# File 'lib/fargo/protocol/peer_upload.rb', line 80

def unbind
  super

  if @listing
    Fargo.logger.debug "Upload disconnected"
    finish_streaming
  end
end