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(io, logger: Logger.new(STDOUT)) ⇒ Session

Returns a new instance of Session.



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

def initialize(io, logger: Logger.new(STDOUT))
  @io          = io
  @addr        = io.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

#ioObject (readonly)

Returns the value of attribute io.



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

def io
  @io
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
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



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

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

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

Yields:

  • (Hash)

    information_content of packet

Raises:

  • EOF



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

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

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