Class: Pokan::ServerConfiguration

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

Overview

Class used to store and manage server configuration. Should not be used by user code, just by the Pokan::Cluster class. It loads a YAML configuration file located in the user home directory or in the .config directory or in /etc/pokan.yml. Optionally, a different config file can be passed.

Usage

s = Pokan::ServerConfiguration.new("pokan.yml")
s.tcp_port #=> 8888 (depending on your configuration options)

A general configuration file has the following skeleton:

pokan:
  connection:
    udp: 88885
    tcp: 7777

  seed: "10.10.10.8"

  log:
    enabled: true
    log_file: "/var/log/pokan.log"

Constant Summary collapse

YAML_ROOT =
'pokan'

Instance Method Summary collapse

Constructor Details

#initialize(config_file = nil) ⇒ ServerConfiguration

Creates a new instance of Pokan::ServerConfiguration. You can pass an optional parameter indicating the location of the configuration file (must be a valid YAML file)

If no config file is passed, we first look if there is a .pokan.yml file in the user’s home directory. If there isn’t, then we search for a .config/pokan/pokan.yml. If none of these are found, then we set the default values (see Pokan::ServerConfiguration#set_defaults for further details)



40
41
42
43
44
# File 'lib/server_configuration.rb', line 40

def initialize(config_file=nil)
  @config_file = config_file || search_config

  @config_file.nil? ? set_defaults : load_config(@config_file)
end

Instance Method Details

#file_pathObject

Returns the absolute path of the configuration file, if any.



70
71
72
# File 'lib/server_configuration.rb', line 70

def file_path
  @config_file
end

#has_config_file?Boolean

Returns true if a configuration file was found, or if the user explicitly passed a file.

Returns:

  • (Boolean)


65
66
67
# File 'lib/server_configuration.rb', line 65

def has_config_file?
  !@config_file.nil?
end

#load_config(config_file) ⇒ Object



79
80
81
82
83
84
# File 'lib/server_configuration.rb', line 79

def load_config(config_file)
  raise "YAML file not found: #{config_file}" unless File.exists? config_file

  @data = YAML.load_file(config_file)
  define_methods_for(@data[YAML_ROOT].keys)
end

#logging?Boolean

Returns true if we are logging activities on the server

Returns:

  • (Boolean)


75
76
77
# File 'lib/server_configuration.rb', line 75

def logging?
  @logging
end

#search_configObject

Searches for a valid YAML configuration file in .pokan.yml located in the user’s home directory or in .config/pokan/pokan.yml', or in /etc/pokan.yml. Returns the file name, if found, or nil if no configuration file was found.



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/server_configuration.rb', line 51

def search_config
  home_file   = File.absolute_path '~/.pokan.yml'
  config_file = File.absolute_path '~/.config/pokan/pokan.yml'
  etc_file    = '/etc/pokan.yml'

  [home_file, config_file, etc_file].each do |config|
    return config if File.exists? config
  end

  nil
end

#set_defaultsObject

Sets the default values for all the attributes that can be set with the configuration file. These are:

  • TCP port: 5555

  • UDP port: 5555

  • no seeds information

  • logger enabled

  • log_file ./pokan.log



93
94
95
96
97
98
99
# File 'lib/server_configuration.rb', line 93

def set_defaults
  @tcp_port   = 5555
  @udp_port   = 5555
  @seeds      = []
  @logging    = true
  @log_file   = './pokan.log'
end