Class: AcpcDealer::DealerRunner

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

Constant Summary collapse

DEALER_COMMAND_FORMAT =

> array should correspond to those in the dealer_arguments of ::start.

> (see ::start)

Returns:

  • (Array<Symbol>)

    The format of the dealer command. The symbols in this

[
  # @note The name of the match to start.
  :match_name,
  # @note The path of the game definition to use.
  :game_def_file_name,
  # @note The number of hands to play.
  :hands,
  # @note The random seed to use.
  :random_seed,
  # @note The names of the players in the game. Should be specified as a space delimited string of names.
  :player_names,
  # @note Should be specified as an option string that the dealer will understand.
  :options
]

Class Method Summary collapse

Class Method Details

.ports_for_players(number_of_players) ⇒ Array<Integer>

> so it’s possible that the ports will come into use between calling this method and

> using them.

Returns:

  • (Array<Integer>)

    List of random open ports. Does NOT reserve the ports though,



68
69
70
71
72
# File 'lib/acpc_dealer/dealer_runner.rb', line 68

def self.ports_for_players(number_of_players)
  number_of_players.times.inject([]) do |ports, i|
    ports << TCPServer.open('localhost', 0) { |s| s.addr[1] }
  end
end

.start(dealer_arguments, log_directory = nil, port_numbers = nil) ⇒ Hash

> Defaults to <dealer_arguments>.logs.

> Defaults to random.

> use to connect to the new dealer instance (key :port_numbers).

Parameters:

  • dealer_arguments (Array)

    Arguments to the new dealer instance.

  • log_directory (String) (defaults to: nil)

    The directory in which logs will be placed.

  • port_numbers (Array) (defaults to: nil)

    The port numbers to which each player will connect.

Returns:

  • (Hash)

    The process ID of the started dealer (key :pid) and the array of ports that players may



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/acpc_dealer/dealer_runner.rb', line 36

def self.start(dealer_arguments, log_directory=nil, port_numbers=nil)
  dealer_start_command = DEALER_COMMAND_FORMAT.inject([AcpcDealer::DEALER_PATH]) do |command, parameter|
    command << dealer_arguments[parameter].to_s
  end
  dealer_start_command << "-p #{port_numbers.join(',')}" if port_numbers

  unless log_directory
    log_directory = File.expand_path("../#{dealer_arguments[:match_name]}.logs", __FILE__)
  end

  FileUtils.mkdir_p log_directory unless Dir.exist?(log_directory)

  IO.pipe do |read_io, write_io|
    pid = ProcessRunner.go(
      dealer_start_command,
      err: [
        File.join(log_directory, "#{dealer_arguments[:match_name]}.actions.log"),
        File::CREAT|File::WRONLY
      ],
      out: write_io,
      chdir: log_directory
    )

    {pid: pid, port_numbers: read_io.gets.split, log_directory: log_directory}
  end
end