Class: Lobster::Network::Connection Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/lobster/network/connection.rb,
lib/lobster/network/connection_inbound.rb,
lib/lobster/network/connection_outbound.rb

Overview

This class is abstract.

Underlying class for sending and receiving messages between a client and server.

Instance Method Summary collapse

Instance Method Details

#receiveMessage

Note:

This method will not return a ResponseMessage. It will not interfere with #send_request.

Waits for a request or notification message to arrive.

Returns:

  • (Message)

    Message received from the remote system.


81
82
83
# File 'lib/lobster/network/connection.rb', line 81

def receive
  receive_non_response
end

#send_message(name, payload = nil) ⇒ nil

Note:

If a response is sent to this message, the response will be discarded.

Sends a message to the remote system. This can be a request or just a notification.

Parameters:

  • name (String)

    Message type name.

  • payload (Object) (defaults to: nil)

    Data to send in the message.

Returns:

  • (nil)

See Also:


37
38
39
40
41
42
43
44
# File 'lib/lobster/network/connection.rb', line 37

def send_message(name, payload = nil)
  fail ArgumentError unless name.is_a?(String)

  message_id = next_message_id
  message    = Message.new(message_id, name, payload)
  send_raw_message(message)
  nil # Hide previous return value.
end

#send_request(name, payload = nil) ⇒ ResponseMessage

Sends a message and waits for a response from the remote system. This method will block until a response is received.

Parameters:

  • name (String)

    Message type name.

  • payload (Object) (defaults to: nil)

    Data to send in the message.

Returns:


66
67
68
69
70
71
72
73
74
75
# File 'lib/lobster/network/connection.rb', line 66

def send_request(name, payload = nil)
  fail ArgumentError unless name.is_a?(String)

  message_id = next_message_id
  message    = Message.new(message_id, name, payload)
  expect_response(message_id)
  send_raw_message(message)

  receive_response(request_id) # Blocks until the response is received.
end

#send_response(name, request, payload = nil) ⇒ nil

Sends a response to a previous request made by the remote system.

Parameters:

  • name (String)

    Message type name.

  • request (Message)

    Original request message.

  • payload (Object) (defaults to: nil)

    Data to send in the response.

Returns:

  • (nil)

51
52
53
54
55
56
57
58
59
# File 'lib/lobster/network/connection.rb', line 51

def send_response(name, request, payload = nil)
  fail ArgumentError unless name.is_a?(String)
  fail ArgumentError unless request.is_a?(Message)

  message_id = next_message_id
  message    = ResponseMessage.new(message_id, request.id, name, payload)
  send_raw_message(message)
  nil # Hide previous return value.
end