Class: RaptorIO::Socket::Comm::SAPNI

Inherits:
RaptorIO::Socket::Comm show all
Defined in:
lib/raptor-io/socket/comm/sapni.rb

Overview

Communication through a SAPRouter

By default SAPRouter listens on port 3299.

Constant Summary collapse

NI_ROUTE_HEADER =

The bits of the packet that don’t change

[
  "NI_ROUTE",
  2,  # route info version
  39, # NI version
  2,  # number of entries
  1,  # talk mode (NI_MSG_IO: 0; NI_RAW_IO; 1; NI_ROUT_IO: 2)
  0,  # unused
  0,  # unused
  1,  # number of rest nodes
].pack("Z*C7")

Instance Method Summary collapse

Methods inherited from RaptorIO::Socket::Comm

#create, #create_tcp_server, #create_udp, #create_udp_server, from_uri, #resolve, #reverse_resolve

Constructor Details

#initialize(options = {}) ⇒ SAPNI

Returns a new instance of SAPNI.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :sap_host (String, IPAddr)
  • :sap_port (Fixnum) — default: 3299
  • :sap_comm (Comm)


26
27
28
29
30
# File 'lib/raptor-io/socket/comm/sapni.rb', line 26

def initialize(options = {})
  @sap_host = options[:sap_host]
  @sap_port = (options[:sap_port] || 3299).to_i
  @sap_comm = options[:sap_comm]
end

Instance Method Details

#create_tcp(options) ⇒ Object

Connect to a SAPRouter and use its routing capabilities to create a TCP connection to ‘:peer_host`.

Parameters:

  • options (Hash)

    a customizable set of options



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/raptor-io/socket/comm/sapni.rb', line 36

def create_tcp(options)
  @sap_socket = @sap_comm.create_tcp(
    peer_host: @sap_host,
    peer_port: @sap_port
  )

  first_route_item = [
    @sap_host, @sap_port.to_s, 0
  ].pack("Z*Z*C")

  second_route_item = [
    options[:peer_host], options[:peer_port].to_s, 0
  ].pack("Z*Z*C")

  route_data =
    # This is *not* a length, it is the
    #   "current position as an offset into the route string"
    # according to
    # http://help.sap.com/saphelp_nwpi711/helpdata/en/48/6a29785bed4e6be10000000a421937/content.htm
    [ first_route_item.length ].pack("N") +
    first_route_item +
    second_route_item
  route_data = [ route_data.length - 4 ].pack("N") + route_data

  ni_packet = NI_ROUTE_HEADER.dup + route_data
  ni_packet = [ni_packet.length].pack('N') + ni_packet

  @sap_socket.write(ni_packet)
  res_length = @sap_socket.read(4)
  res = @sap_socket.read(res_length.unpack("N").first)

  unless res == "NI_PONG\x00"
    raise RaptorIO::Socket::Error::ConnectionError
  end

  RaptorIO::Socket::TCP.new(@sap_socket, options)
end