class Torrenter::Peer

instead of initialiazing it with the buffer,

Attributes

buffer[R]
ip[R]
peer_state[R]
socket[R]

Public Class Methods

new(ip, port, params={}) click to toggle source
# File lib/torrenter/peer.rb, line 4
def initialize(ip, port, params={})
  @ip = ip
  @port = port
  @info_hash = params[:info_hash]
  @piece_length = params[:left]
  @peer_state   = false
end

Public Instance Methods

connect() click to toggle source
# File lib/torrenter/peer.rb, line 12
def connect
  puts "\nConnecting to IP: #{@ip} PORT: #{@port}"
  begin
    Timeout::timeout(1) { @socket = TCPSocket.new(@ip, @port) }
  rescue Timeout::Error
    puts "Timed out."
  rescue Errno::EADDRNOTAVAIL
    puts "Address not available."
  rescue Errno::ECONNREFUSED
    puts "Connection refused."
  rescue Errno::ECONNRESET
    puts "bastards."
  end

  if @socket
    puts "Connected!"
    @peer_state = true
    @buffer = BufferState.new(@socket, @info_hash)
    @buffer.send(handshake)
  else
    @peer_state = false
  end
end
connected?() click to toggle source
# File lib/torrenter/peer.rb, line 49
def connected?
  @peer_state
end
connection_state(index, blk) click to toggle source
# File lib/torrenter/peer.rb, line 36
def connection_state(index, blk)
  if @socket.closed?
    @peer_state = false
    @buffer.current_piece = :available
  else
    @buffer.messager(index, blk)
  end
end
handshake() click to toggle source
# File lib/torrenter/peer.rb, line 45
def handshake
  "#{PROTOCOL}#{@info_hash}#{PEER_ID}"
end