Class: Sip2::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/sip2/connection.rb

Overview

Sip2 Connection

Constant Summary collapse

LINE_SEPARATOR =
"\r"

Instance Method Summary collapse

Constructor Details

#initialize(socket:, ignore_error_detection: false) ⇒ Connection

Returns a new instance of Connection.



10
11
12
13
14
# File 'lib/sip2/connection.rb', line 10

def initialize(socket:, ignore_error_detection: false)
  @socket = socket
  @ignore_error_detection = ignore_error_detection
  @sequence = 1
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/sip2/connection.rb', line 28

def method_missing(method_name, *args, **kwargs)
  message_class = Messages::Base.message_class_for_method(method_name)
  if message_class.nil?
    super
  else
    message_class.new(self).action_message(*args, **kwargs)
  end
end

Instance Method Details

#respond_to_missing?(method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/sip2/connection.rb', line 37

def respond_to_missing?(method_name, _include_private = false)
  !Messages::Base.message_class_for_method(method_name).nil? || super
end

#send_message(message) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sip2/connection.rb', line 16

def send_message(message)
  message = with_checksum with_error_detection message
  write_with_timeout message
  # Read the response and strip any leading newline
  # - Some ACS terminate messages with /r/n by mistake.
  #   We need to remove from the front (i.e. buffer remnant from the previous message)
  response = read_with_timeout&.[](/\A\n?(.*)\z/, 1)
  response if sequence_and_checksum_valid? response
ensure
  @sequence += 1
end