Sha256: 44a2bdd693824b93b53e36be8a6af26aca59e981a388e973570437f8bd25da2a

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

require 'socket'

module Plug

  class Peer
    attr_reader :addr, :transport, :name, :owner, :host, :port
    attr_accessor :mute

    def initialize(addr, owner)
      @addr = addr
      @owner = owner
      @transport = @owner.transport

      @port, @host = Socket.unpack_sockaddr_in(@addr)
      @name = "PEER-#{@host}:#{@port}(#{@transport})"
    end

    def say(dat, sender)
      UI.dump(sender.name, self.name, dat)

      if @transport == :UDP
        @owner.send_datagram(dat, @host, @port)
      else
        @owner.send_data(dat)
      end
    end

    def close
      @owner.unbind unless @transport == :UDP
    end
  end


  class PeerList < Array
    def initialize(owner, *args)
      @owner = owner
      @transport = @owner.transport
      
      super(*args)
    end

    def find_peer(addr)
      self.find {|p| p.addr == addr }
      return nil
    end

    def add_peer(addr)
      self << Peer.new(addr, @owner)
      self.last
    end

    def add_peer_manually(host, port)
      addr = Socket.pack_sockaddr_in(port, host)
      return (find_peer(addr) || add_peer(addr))
    end

    def delete(addr)
      if p=find_peer(addr)
        p.close
        super(p)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
emonti-rbkb-0.6.1.1 lib/rbkb/plug/peer.rb
emonti-rbkb-0.6.1.2 lib/rbkb/plug/peer.rb
emonti-rbkb-0.6.1.3 lib/rbkb/plug/peer.rb
emonti-rbkb-0.6.2 lib/rbkb/plug/peer.rb