Module: Fargo::Protocol::Peer

Includes:
DC, PeerDownload, PeerUpload, Utils
Defined in:
lib/fargo/protocol/peer.rb

Constant Summary

Constants included from PeerUpload

Fargo::Protocol::PeerUpload::CHUNKSIZE

Instance Attribute Summary collapse

Attributes included from PeerDownload

#download

Instance Method Summary collapse

Methods included from PeerUpload

#unbind

Methods included from PeerDownload

#begin_download!, #parse_data?, #receive_data_chunk, #unbind

Methods included from DC

#parse_data?, #receive_data, #receive_data_chunk, #send_message, #unbind

Methods included from Fargo::Parser

#parse_command_message, #parse_message

Methods included from Utils

#encode_char, #generate_key, #generate_lock

Instance Attribute Details

#clientObject

Returns the value of attribute client.



12
13
14
# File 'lib/fargo/protocol/peer.rb', line 12

def client
  @client
end

Instance Method Details

#connection_typeObject



14
15
16
# File 'lib/fargo/protocol/peer.rb', line 14

def connection_type
  :peer
end

#post_initObject



18
19
20
21
22
23
24
25
# File 'lib/fargo/protocol/peer.rb', line 18

def post_init
  super

  set_comm_inactivity_timeout 20

  @lock, @pk      = generate_lock
  @handshake_step = 0
end

#publish_argsObject



100
101
102
# File 'lib/fargo/protocol/peer.rb', line 100

def publish_args
  {:nick => @other_nick}
end

#receive_message(type, message) ⇒ Object



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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/fargo/protocol/peer.rb', line 37

def receive_message type, message
  case type
    when :mynick
      if @handshake_step == 0
        @handshake_step  = 1
        @other_nick      = message[:nick]

        client.channel << [:download_opened,
            publish_args.merge(:connection => self)]
        @download = @client.lock_next_download! @other_nick, self

        @direction = @download.nil? ? 'Upload' : 'Download'
      else
        error 'Premature disconnect when mynick received'
      end

    when :lock
      if @handshake_step == 1
        @remote_lock    = message[:lock]
        @handshake_step = 2

        send_lock unless @lock_sent

        send_message 'Supports', supports
        send_message 'Direction', "#{@direction} #{@my_num = rand(10000)}"
        send_message 'Key', generate_key(@remote_lock)
      else
        error 'Premature disconnect when lock received'
      end

    when :supports
      if @handshake_step == 2
        @client_extensions = message[:extensions]
        @handshake_step    = 3
      else
        error 'Premature disconnect when supports received'
      end

    when :direction
      if @handshake_step == 3
        @client_num     = message[:number]
        @handshake_step = 4
      else
        error 'Premature disconnect when direction received'
      end

    when :key
      if @handshake_step == 4 && generate_key(@lock) == message[:key]
        @handshake_step = 5

        begin_download! if @direction == 'Download'

      else
        error 'Premature disconnect when key received'
      end

    # This wasn't handled by us, proxy it on up to the client
    else
      super

  end
end

#send_lockObject



27
28
29
30
31
# File 'lib/fargo/protocol/peer.rb', line 27

def send_lock
  @lock_sent = true
  send_message 'MyNick', @client.config.nick
  send_message 'Lock', "#{@lock} Pk=#{@pk}"
end

#supportsObject



33
34
35
# File 'lib/fargo/protocol/peer.rb', line 33

def supports
  'MiniSlots XmlBZList ADCGet TTHF ZLIG GetZBlock'
end