Class: ChainReactor::ClientConnection
- Inherits:
-
Object
- Object
- ChainReactor::ClientConnection
- Defined in:
- lib/chain-reactor/client_connection.rb
Overview
Holds information about the connected client, and provides access to the socket.
The host, ip and port of the client are provided as attributes.
Instance Attribute Summary collapse
-
#ip ⇒ Object
readonly
Client’s IP address.
-
#port ⇒ Object
readonly
Client’s port.
Instance Method Summary collapse
-
#close ⇒ Object
Close the socket connection, using
TCPSocket.close
. -
#initialize(socket, logger) ⇒ ClientConnection
constructor
Create the ClientConnection with a TCPSocket.
-
#read ⇒ Object
Read from the client socket, using
TCPSocket.gets
. -
#say(string) ⇒ Object
Write a string to the client socket, using
TCPSocket.puts
.
Constructor Details
#initialize(socket, logger) ⇒ ClientConnection
Create the ClientConnection with a TCPSocket. This socket holds connection parameters and allows data transfer both ways.
21 22 23 24 25 26 27 28 |
# File 'lib/chain-reactor/client_connection.rb', line 21 def initialize(socket,logger) @socket = socket fam, @port, *addr = @socket.getpeername.unpack('nnC4') @ip = addr.join('.') @log = logger @log.info { "New connection from #{@ip}:#{@port}" } end |
Instance Attribute Details
#ip ⇒ Object (readonly)
Client’s IP address.
15 16 17 |
# File 'lib/chain-reactor/client_connection.rb', line 15 def ip @ip end |
#port ⇒ Object (readonly)
Client’s port.
17 18 19 |
# File 'lib/chain-reactor/client_connection.rb', line 17 def port @port end |
Instance Method Details
#close ⇒ Object
Close the socket connection, using TCPSocket.close
.
41 42 43 44 |
# File 'lib/chain-reactor/client_connection.rb', line 41 def close @log.info { "Closing connection to client #{@ip}" } @socket.close end |
#read ⇒ Object
Read from the client socket, using TCPSocket.gets
.
36 37 38 |
# File 'lib/chain-reactor/client_connection.rb', line 36 def read @socket.gets end |
#say(string) ⇒ Object
Write a string to the client socket, using TCPSocket.puts
.
31 32 33 |
# File 'lib/chain-reactor/client_connection.rb', line 31 def say(string) @socket.puts string end |