Class: Network

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

Overview

This class handles the socket connection to the server

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.



30
31
32
33
34
35
36
37
# File 'lib/software_challenge_client/network.rb', line 30

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

  @protocol = Protocol.new(self, @client)
  @reservationID = reservation || ''
  @receiveBuffer = ''
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



28
29
30
# File 'lib/software_challenge_client/network.rb', line 28

def connected
  @connected
end

Instance Method Details

#connectBoolean

connects the client with a given server

Returns:

  • (Boolean)

    true, if successfully connected to the server



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/software_challenge_client/network.rb', line 42

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

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

#disconnectObject

disconnects the client from a server



65
66
67
68
69
70
71
72
73
# File 'lib/software_challenge_client/network.rb', line 65

def disconnect

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

#emptyReceiveBufferObject

empties the receive buffer



112
113
114
# File 'lib/software_challenge_client/network.rb', line 112

def emptyReceiveBuffer
  @receiveBuffer = ''
end

#processMessagesBoolean

processes an incomming message

Returns:

  • (Boolean)

    true, if the processing of a incomming message was successfull



119
120
121
122
123
124
# File 'lib/software_challenge_client/network.rb', line 119

def processMessages
  if !@connected
    return false
  end
  return self.readString
end

#readStringObject

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



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/software_challenge_client/network.rb', line 76

def readString
  logger.debug 'reading'
  sockMsg = ''
  if(!@connected)
    return
  end

  line =''
  char = ''
  while (line != "</room>" && line != "</protocol>") && !(char = @socket.getc).nil?
    line+=char
    if char=='\n' || char==' '

      line = ''
    end
    sockMsg += char
  end
  logger.debug 'ended reading'
  if sockMsg != ''

    @receiveBuffer.concat(sockMsg)

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

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

    # Process text
    @protocol.process_string(@receiveBuffer);
    self.emptyReceiveBuffer
  end
  return true
end

#sendString(s) ⇒ Object

sends a string to the socket

Parameters:

  • s (String)

    the message, to be sent



129
130
131
132
133
134
# File 'lib/software_challenge_client/network.rb', line 129

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

#sendXML(xml) ⇒ Object

sends a xml Document to the buffer

Parameters:

  • xml (REXML::Document)

    the Document, that will be sent



139
140
141
142
143
# File 'lib/software_challenge_client/network.rb', line 139

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