class Buschtelefon::NetTattler

Attributes

host[RW]
port[RW]
socket[RW]

Public Class Methods

new(host: "127.0.0.1", port: 0) click to toggle source
Calls superclass method
# File lib/buschtelefon/net_tattler.rb, line 8
def initialize(host: "127.0.0.1", port: 0)
  super()
  @socket = UDPSocket.new
  @socket.bind(host, port)
  @host = @socket.local_address.ip_address
  @port = @socket.local_address.ip_port
end

Public Instance Methods

connect_remote(host:, port:) click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 36
def connect_remote(host:, port:)
  find_or_build_remote_tattler(host: host, port: port).tap do |remote_tattler|
    connect(remote_tattler)
  end
end
inquire_remote_neighbors() click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 42
def inquire_remote_neighbors
  remote_connections.each { |remote_tattler| remote_tattler.inquire }
end
listen() { |gossip, remote_tattler| ... } click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 16
def listen(&_callback)
  puts "Started UDP server on #{@host}:#{@port}..."

  Socket.udp_server_loop_on([@socket]) do |message, message_source|
    remote_tattler = find_or_build_remote_tattler(
      host: message_source.remote_address.ip_address,
      port: message_source.remote_address.ip_port
    )

    if message == "\x05"
      # puts "#{@port} got inquiry from #{remote_tattler}. Is connected to #{@connections.inspect}"
      handle_knowledge_inquiry(remote_tattler)
    else
      gossip = Gossip.new(message)
      handle_incoming_gossip(gossip)
      yield(gossip, remote_tattler) if block_given?
    end
  end
end
remote_connections() click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 46
def remote_connections
  @connections.select { |tattler| tattler.is_a?(RemoteTattler) }
end
to_s() click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 50
def to_s
  "#{@host}:#{@port}"
end

Private Instance Methods

build_remote_tattler(host:, port:) click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 69
def build_remote_tattler(host:, port:)
  RemoteTattler.new(host: host, port: port, outbound_socket: @socket)
end
find_or_build_remote_tattler(host:, port:) click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 65
def find_or_build_remote_tattler(host:, port:)
  remote_connections.find { |t| t.host == host && t.port == port } || build_remote_tattler(host: host, port: port)
end
handle_incoming_gossip(gossip) click to toggle source
# File lib/buschtelefon/net_tattler.rb, line 56
def handle_incoming_gossip(gossip)
  feed(gossip)
end
handle_knowledge_inquiry(remote_tattler) click to toggle source

We just give out everything to the outbound port of the inquiry source

# File lib/buschtelefon/net_tattler.rb, line 61
def handle_knowledge_inquiry(remote_tattler)
  transfer_knowledge(remote_tattler)
end