Class: Network
- Inherits:
-
Object
- Object
- Network
- 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
-
#connected ⇒ Boolean
readonly
True, if the client is connected to a server.
Instance Method Summary collapse
-
#connect ⇒ Boolean
connects the client with a given server.
-
#disconnect ⇒ Object
disconnects the client from a server.
-
#initialize(host, port, board, client, reservation = nil) ⇒ Network
constructor
A new instance of Network.
-
#processMessages ⇒ Boolean
processes an incomming message.
-
#readString ⇒ Object
reads from the socket until "" is read.
-
#sendString(s) ⇒ Object
sends a string to the socket.
-
#sendXML(xml) ⇒ Object
sends a xml Document to the buffer.
Methods included from Logging
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
#connected ⇒ Boolean (readonly)
Returns 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
#connect ⇒ Boolean
connects the client with a given 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 |
#disconnect ⇒ Object
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 |
#processMessages ⇒ Boolean
processes an incomming message
106 107 108 109 |
# File 'lib/software_challenge_client/network.rb', line 106 def processMessages return false unless @connected readString end |
#readString ⇒ Object
reads from the socket until "" 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
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
97 98 99 100 101 |
# File 'lib/software_challenge_client/network.rb', line 97 def sendXML(xml) text = '' xml.write(text) sendString(text) end |