Class: ChainReactor::ClientConnection

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#ipObject (readonly)

Client’s IP address.



15
16
17
# File 'lib/chain-reactor/client_connection.rb', line 15

def ip
  @ip
end

#portObject (readonly)

Client’s port.



17
18
19
# File 'lib/chain-reactor/client_connection.rb', line 17

def port
  @port
end

Instance Method Details

#closeObject

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

#readObject

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