Class: Process::Roulette::Controller::ConnectHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/process/roulette/controller/connect_handler.rb

Overview

Handles the CONNECT state of the controller state machine. Connects to the croupier, performs the handshake, and advances to COMMAND state.

Instance Method Summary collapse

Constructor Details

#initialize(driver) ⇒ ConnectHandler

Returns a new instance of ConnectHandler.



12
13
14
# File 'lib/process/roulette/controller/connect_handler.rb', line 12

def initialize(driver)
  @driver = driver
end

Instance Method Details

#_handshake(socket) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/process/roulette/controller/connect_handler.rb', line 28

def _handshake(socket)
  socket.send_packet(@driver.password || 'OK')

  packet = socket.wait_with_ping
  abort 'lost connection' unless packet
  abort "unexpected packet #{packet.inspect}" unless packet == 'OK'
end

#runObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/process/roulette/controller/connect_handler.rb', line 16

def run
  puts 'connecting...'

  socket = TCPSocket.new(@driver.host, @driver.port)
  Roulette::EnhanceSocket(socket)

  _handshake(socket)
  @driver.socket = socket

  Controller::CommandHandler
end