class Pokan::Network

Network is a helper class, used by the Pokan::Server class. Its main goal is to provide a higher abstraction for networking methods provided by the Ruby core. It should not be used by user code.

Public Class Methods

tcp(message, host, port) click to toggle source

Sends message to the given host and port. TCP is a connection-oriented protocol, so we need to have a server waiting for the message in the destination passed as parameters. If the connection cannot be established, this method returns false. TCP messages are used for communication with seeds, since we need to be sure the data really reaches its destination (for example, when someone finishes the gossip server manually, pokan sends a message to a seed, telling it is about to die.

# File lib/pokan/network.rb, line 31
def self.tcp(message, host, port)
  begin
    @socket = TCPSocket.open(host, port)
    bytes = @socket.send(message, 0)

    bytes  > 0
  rescue
    false
  end
end
udp(message, host, port) click to toggle source

Sends message to the given host and port. As UDP is connectionless, there’s no way to be sure wheter or not the message will reach its destination. UDP messages are used for communication of peers.

# File lib/pokan/network.rb, line 15
def self.udp(message, host, port)
  @socket = UDPSocket.new
  bytes = @socket.send(message, 0, host, port)

  bytes > 0
end