Class: ZSS::Socket

Inherits:
Object
  • Object
show all
Includes:
LoggerFacade::Loggable
Defined in:
lib/zss/socket.rb

Defined Under Namespace

Classes: Error, TimeoutError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Socket

Returns a new instance of Socket.



14
15
16
17
18
# File 'lib/zss/socket.rb', line 14

def initialize config
  @identity = config.identity
  @timeout = config.timeout || 1000
  @socket_address = config.socket_address
end

Instance Attribute Details

#identityObject (readonly)

Returns the value of attribute identity.



12
13
14
# File 'lib/zss/socket.rb', line 12

def identity
  @identity
end

#socket_addressObject (readonly)

Returns the value of attribute socket_address.



12
13
14
# File 'lib/zss/socket.rb', line 12

def socket_address
  @socket_address
end

#timeoutObject (readonly)

Returns the value of attribute timeout.



12
13
14
# File 'lib/zss/socket.rb', line 12

def timeout
  @timeout
end

Instance Method Details

#call(request, call_timeout = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/zss/socket.rb', line 20

def call request, call_timeout = nil
  fail Socket::Error, 'invalid request' unless request

  response = nil
  t = (call_timeout || timeout) / 1000.0

  context do |ctx|
    socket ctx do |sock|
      begin
        ::Timeout.timeout t do
          log.trace("Request #{request.rid} sent to #{request.address} with #{t}s timeout")
          
          send_message sock, request

          log.trace("Waiting for #{request.rid}")
          response = receive_message(sock)

        end
      rescue ::Timeout::Error
        log.info("Request #{request.rid} exit with timeout after #{t}s")
        raise ZSS::Socket::TimeoutError, "call timeout after #{t}s"
      end
    end
  end

  response
end