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::BOARD_SIZE, Constants::GAME_IDENTIFIER, Constants::ROUND_LIMIT

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger

Constructor Details

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

Returns a new instance of Network.



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

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)

Returns true, if the client is connected to a server.

Returns:

  • (Boolean)

    true, if the client is connected to a server



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

def connected
  @connected
end

Instance Method Details

#connectBoolean

connects the client with a given server

Returns:

  • (Boolean)

    true, if successfully connected to the server



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

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



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

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

#processMessagesBoolean

processes an incomming message

Returns:

  • (Boolean)

    true, if the processing of a incomming message was successfull



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

def processMessages
  return false unless @connected
  readString
end

#readStringObject

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



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
# File 'lib/software_challenge_client/network.rb', line 67

def readString
  return false unless @connected
  sock_msg = ''

  line = ''
  @socket.each_char do |char|
    line += char
    sock_msg += char
    line = '' if ['\n', ' '].include? char
    break if ['</room>', '</protocol>'].include? line
  end
  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

Parameters:

  • s (String)

    the message, to be sent



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

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

#sendXML(xml) ⇒ Object

sends a xml Document to the buffer

Parameters:

  • xml (REXML::Document)

    the Document, that will be sent



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

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