Class: Gt06Server::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/gt06_server/session.rb

Defined Under Namespace

Classes: SessionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, logger: Logger.new(STDOUT)) ⇒ Session

Returns a new instance of Session.

Parameters:

  • socket (TCPSocket)


9
10
11
12
13
14
15
16
17
# File 'lib/gt06_server/session.rb', line 9

def initialize(socket, logger: Logger.new(STDOUT))
  @socket      = socket
  @addr        = socket.peeraddr
  @terminal_id = ''
  @info        = { received_count: 0, sent_count: 0, last_received_at: Time.now}
  @logger      = logger

  logger.debug 'New session has been created'
end

Instance Attribute Details

#addrObject (readonly)

Returns the value of attribute addr.



6
7
8
# File 'lib/gt06_server/session.rb', line 6

def addr
  @addr
end

#infoObject (readonly)

Returns the value of attribute info.



6
7
8
# File 'lib/gt06_server/session.rb', line 6

def info
  @info
end

#loggerObject (readonly)

Returns the value of attribute logger.



6
7
8
# File 'lib/gt06_server/session.rb', line 6

def logger
  @logger
end

#socketObject (readonly)

Returns the value of attribute socket.



6
7
8
# File 'lib/gt06_server/session.rb', line 6

def socket
  @socket
end

#terminal_idObject (readonly)

Returns the value of attribute terminal_id.



6
7
8
# File 'lib/gt06_server/session.rb', line 6

def terminal_id
  @terminal_id
end

Instance Method Details

#inspectObject



29
30
31
# File 'lib/gt06_server/session.rb', line 29

def inspect
  "#{object_id} Terminal id:#{@terminal_id}, ip: #{@addr}, #{@info}"
end

#run {|Hash| ... } ⇒ Object

Yields:

  • (Hash)

    information_content of packet

Raises:

  • EOF



21
22
23
24
25
26
27
# File 'lib/gt06_server/session.rb', line 21

def run(&block)
  handle_head_pack(Protocol.read_pack(@socket))

  loop do
    handle_main_pack(Protocol.read_pack(@socket), &block)
  end
end