Class: Pokan::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/pokan/network.rb

Overview

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.

Class Method Summary collapse

Class Method Details

.tcp(message, host, port) ⇒ Object

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.



31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object

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.



15
16
17
18
19
20
# 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