Sha256: a574a697ac19a3fdbb834bd6e48d373194e0df016cb0e47581f93bcbd3e778d0

Contents?: true

Size: 1.21 KB

Versions: 3

Compression:

Stored size: 1.21 KB

Contents

module ChainReactor

  # Exception used by the ClientConnection class, signifying incorrect connection
  # details for a client.
  class ClientConnectionError < StandardError
  end

  # Holds information about the connected client, and provides access to the
  # socket.
  #
  # The host, ip and port of the client are provided as attributes.
  class ClientConnection

    # Client's IP address.
    attr_reader :ip
    # Client's port.
    attr_reader :port

    # Create the ClientConnection with a TCPSocket. This socket holds connection
    # parameters and allows data transfer both ways.
    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

    # Write a string to the client socket, using <tt>TCPSocket.puts</tt>.
    def say(string)
      @socket.puts string
    end

    # Read from the client socket, using <tt>TCPSocket.gets</tt>.
    def read
      @socket.gets
    end

    # Close the socket connection, using <tt>TCPSocket.close</tt>.
    def close
      @log.info { "Closing connection to client #{@ip}" }
      @socket.close
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
chain-reactor-0.2.2 lib/chain-reactor/client_connection.rb
chain-reactor-0.2.1 lib/chain-reactor/client_connection.rb
chain-reactor-0.2.0 lib/chain-reactor/client_connection.rb