Module: SteamCondenser::Servers::BaseServer

Included in:
GameServer, MasterServer
Defined in:
lib/steam-condenser/servers/base_server.rb

Overview

This module is included by all classes implementing server functionality

It provides basic name resolution features and the ability to rotate between different IP addresses belonging to a single DNS name.

Author:

  • Sebastian Staudt

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#host_namesArray<String> (readonly)

Returns a list of host names associated with this server


23
24
25
# File 'lib/steam-condenser/servers/base_server.rb', line 23

def host_names
  @host_names
end

#ip_addressesArray<String> (readonly)

Returns a list of IP addresses associated with this server


28
29
30
# File 'lib/steam-condenser/servers/base_server.rb', line 28

def ip_addresses
  @ip_addresses
end

Instance Method Details

#disconnectObject

Note:

In the base implementation this does nothing, only connection-based communication channels have to be disconnected.

Disconnect the connections to this server


62
63
# File 'lib/steam-condenser/servers/base_server.rb', line 62

def disconnect
end

#failsafe(&proc) ⇒ Object (protected)

Execute an action in the context of this server's current IP address

Any failure will cause the IP to rotate to the next IP in the server's IP list and the execution will be repeated for the next IP address. If the IP rotation reaches the end of the list, the error will be reraised.

See Also:


99
100
101
102
103
104
105
106
107
# File 'lib/steam-condenser/servers/base_server.rb', line 99

def failsafe(&proc)
  begin
    proc.call
  rescue
    raise $! if rotate_ip
    log.info "Request failed, retrying for #@ip_address..."
    failsafe &proc
  end
end

#init_socketObject (protected)

This method is abstract.

Must be implemented in including classes to prepare sockets for server communication

Initializes the socket(s) to communicate with the server


113
114
# File 'lib/steam-condenser/servers/base_server.rb', line 113

def init_socket
end

#initialize(address, port = nil) ⇒ Object

Creates a new server instance with the given address and port

Raises:

  • (Error)

    if an host name cannot be resolved

See Also:


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/steam-condenser/servers/base_server.rb', line 38

def initialize(address, port = nil)
  address = address.to_s
  address, port = address.split(':', 2) if address.include? ':'

  @host_names   = []
  @ip_addresses = []
  @ip_index     = 0
  @port         = port

  Socket.getaddrinfo(address, port, Socket::AF_INET, Socket::SOCK_DGRAM).
         each do |address|
    @host_names << address[2]
    @ip_addresses << address[3]
  end

  @ip_address = @ip_addresses.first

  init_socket
end

#rotate_ipBoolean

Rotate this server's IP address to the next one in the IP list

If this method returns true, it indicates that all IP addresses have been used, hinting at the server(s) being unreachable. An appropriate action should be taken to inform the user.

Servers with only one IP address will always cause this method to return true and the sockets will not be reinitialized.

See Also:


78
79
80
81
82
83
84
85
86
87
# File 'lib/steam-condenser/servers/base_server.rb', line 78

def rotate_ip
  return true if @ip_addresses.size == 1

  @ip_index = (@ip_index + 1) % @ip_addresses.size
  @ip_address = @ip_addresses[@ip_index]

  init_socket

  @ip_index == 0
end