Class: Network

Inherits:
Object
  • Object
show all
Includes:
Constants, Logging
Defined in:
lib/software_challenge_client/network.rb

Overview

This class handles the socket connection to the server

Constant Summary

Constants included from Constants

Constants::GAME_IDENTIFIER, Constants::ROUND_LIMIT, Constants::SIZE

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

#initialize(host, port, board, client, reservation = nil) ⇒ Network


21
22
23
24
25
26
27
28
29
30
31
# File 'lib/software_challenge_client/network.rb', line 21

def initialize(host, port, board, client, reservation = nil)
  @host = host
  @port = port
  @board = board
  @client = client

  @connected = false
  @protocol = Protocol.new(self, @client)
  @reservation_id = reservation || ''
  @receive_buffer = ''
end

Instance Attribute Details

#connectedBoolean (readonly)


19
20
21
# File 'lib/software_challenge_client/network.rb', line 19

def connected
  @connected
end

Instance Method Details

#connectBoolean

connects the client with a given server


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/software_challenge_client/network.rb', line 36

def connect
  @socket = TCPSocket.open(@host, @port)
  logger.info 'Connection to server established.'
  @connected = true

  sendString('<protocol>')
  document = REXML::Document.new
  if @reservation_id != ''
    element = REXML::Element.new('joinPrepared')
    element.add_attribute('reservationCode', @reservation_id)
  else
    element = REXML::Element.new('join')
    element.add_attribute('gameType', GAME_IDENTIFIER)
  end
  document.add(element)
  sendXML(document)
  @connected
end

#disconnectObject

disconnects the client from a server


56
57
58
59
60
61
62
63
# File 'lib/software_challenge_client/network.rb', line 56

def disconnect
  if @connected
    sendString('</protocol>')
    @connected = false
    @socket.close
  end
  logger.info 'Connection to server closed.'
end

#processMessagesBoolean

processes an incomming message


107
108
109
110
# File 'lib/software_challenge_client/network.rb', line 107

def processMessages
  return false unless @connected
  readString
end

#readStringObject

reads from the socket until “</room>” is read


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/software_challenge_client/network.rb', line 66

def readString
  return false unless @connected
  sock_msg = ''

  line = ''
  logger.debug 'reading'
  @socket.each_char do |char|
    line += char
    sock_msg += char
    line = '' if ['\n', ' '].include? char
    break if ['</room>', '</protocol>'].include? line
  end
  logger.debug 'ended reading'
  if sock_msg != ''
    @receive_buffer.concat(sock_msg)

    # Remove <protocol> tag
    @receive_buffer = @receive_buffer.gsub('<protocol>', '')
    @receive_buffer = @receive_buffer.gsub('</protocol>', '')

    logger.debug "Received XML from server: #{@receive_buffer}"

    # Process text
    @protocol.process_string(@receive_buffer)
    emptyReceiveBuffer
  end
  true
end

#sendString(s) ⇒ Object

sends a string to the socket


115
116
117
118
119
# File 'lib/software_challenge_client/network.rb', line 115

def sendString(s)
  return unless @connected
  logger.debug "Sending: #{s}"
  @socket.print(s)
end

#sendXML(xml) ⇒ Object

sends a xml Document to the buffer


98
99
100
101
102
# File 'lib/software_challenge_client/network.rb', line 98

def sendXML(xml)
  text = ''
  xml.write(text)
  sendString(text)
end