Module: Contrast::Api::Communication::Socket

Included in:
TcpSocket, UnixSocket
Defined in:
lib/contrast/api/communication/socket.rb

Overview

Behavior common to all sockets used to communicate with the Contrast Service.

Constant Summary collapse

SOCKET_MONITOR =
Monitor.new

Instance Method Summary collapse

Instance Method Details

#new_socketObject

Override this method to return a socket. Should be interface compatible with TCPSocket, UNIXSocket, etc.

Raises:

  • (NoMethodError)


39
40
41
# File 'lib/contrast/api/communication/socket.rb', line 39

def new_socket
  raise NoMethodError, 'This is abstract, override it.'
end

#send_marshaled(marshaled) ⇒ String

Send a message across the socket and read back the response.

Parameters:

  • marshaled (String)

    some marshaled form of data to be sent across the socket. Typically an encoded form of Contrast::Api::Dtm::Message.

Returns:

  • (String)

    some marshalled form of data returned by the socket. Typically an encoded form of Contrast::Api::Settings::AgentSettings.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/contrast/api/communication/socket.rb', line 17

def send_marshaled marshaled
  SOCKET_MONITOR.synchronize do
    socket = new_socket

    # write message length
    len = marshaled.length
    socket.write([len].pack(Contrast::Api::ENCODING_STRING))

    # write the entire message
    socket.write(marshaled)
    socket.close_write

    # read the response length
    len = socket.read(4).unpack1(Contrast::Api::ENCODING_STRING)

    # read expected response to end
    socket.read(len)
  end
end