Class: Network
- Inherits:
-
Object
- Object
- Network
- Defined in:
- lib/software_challenge_client/network.rb
Overview
This class handles the socket connection to the server
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.
-
#emptyReceiveBuffer ⇒ Object
empties the receive buffer.
-
#initialize(host, port, board, client) ⇒ Network
constructor
A new instance of Network.
-
#processMessages ⇒ Boolean
processes an incomming message.
-
#readString ⇒ Object
reads from the socket until “</room>” is read.
-
#sendString(s) ⇒ Object
sends a string to the socket.
-
#sendXML(xml) ⇒ Object
sends a xml Document to the buffer.
Constructor Details
#initialize(host, port, board, client) ⇒ Network
Returns a new instance of Network.
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/software_challenge_client/network.rb', line 26 def initialize(host, port, board, client) @host, @port, @connected, @board, @client = host, port, false, board, client @protocol = Protocol.new(self, @client) @reservationID = '' @receiveBuffer = '' puts '> Network/Socket created.' end |
Instance Attribute Details
#connected ⇒ Boolean (readonly)
Returns true, if the client is connected to a server.
24 25 26 |
# File 'lib/software_challenge_client/network.rb', line 24 def connected @connected end |
Instance Method Details
#connect ⇒ Boolean
connects the client with a given server
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/software_challenge_client/network.rb', line 40 def connect @socket = TCPSocket.open(@host, @port) @connected = true self.sendString('<protocol>') if @reservationID != '' document = REXML::Docuent.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_2016_twixt') document.add(element) self.sendXML(document) end return @connected end |
#disconnect ⇒ Object
disconnects the client from a server
62 63 64 65 66 67 68 69 70 |
# File 'lib/software_challenge_client/network.rb', line 62 def disconnect if @connected sendString("</protocol>") @connected = false @socket.close end puts '> Disconnected.' end |
#emptyReceiveBuffer ⇒ Object
empties the receive buffer
111 112 113 |
# File 'lib/software_challenge_client/network.rb', line 111 def emptyReceiveBuffer @receiveBuffer = '' end |
#processMessages ⇒ Boolean
processes an incomming message
118 119 120 121 122 123 |
# File 'lib/software_challenge_client/network.rb', line 118 def processMessages if !@connected return false end return self.readString end |
#readString ⇒ Object
reads from the socket until “</room>” is read
73 74 75 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 |
# File 'lib/software_challenge_client/network.rb', line 73 def readString puts 'reading' sockMsg = '' if(!@connected) return end line ='' char = '' while line!="</room>" char = @socket.getc line+=char if char=='\n' || char==' ' line = '' end sockMsg += char end puts 'ended reading' if sockMsg != '' @receiveBuffer.concat(sockMsg) # Remove <protocol> tag @receiveBuffer = @receiveBuffer.gsub('<protocol>', '') puts 'Receive:' puts '' #puts @receiveBuffer # Process text @protocol.processString('<msg>'+@receiveBuffer+'</msg>'); self.emptyReceiveBuffer end return true end |
#sendString(s) ⇒ Object
sends a string to the socket
128 129 130 131 132 133 134 135 |
# File 'lib/software_challenge_client/network.rb', line 128 def sendString(s) if(@connected) @socket.print(s); puts 'Send:' puts '' puts(s); end end |
#sendXML(xml) ⇒ Object
sends a xml Document to the buffer
140 141 142 143 144 |
# File 'lib/software_challenge_client/network.rb', line 140 def sendXML(xml) text = '' xml.write(text) self.sendString(text); end |