Class: ROS::TCPROS::ServiceClient

Inherits:
Object
  • Object
show all
Includes:
Message
Defined in:
lib/ros/tcpros/service_client.rb

Overview

TCPROS protocol service client connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Message

#protocol, #read_all, #read_header, #write_header, #write_msg

Constructor Details

#initialize(host, port, caller_id, service_name, service_type, persistent) ⇒ ServiceClient

Returns a new instance of ServiceClient.

Parameters:

  • host (String)

    host name

  • port (Integer)

    port number

  • caller_id (String)

    caller_id of this node

  • service_name (String)

    name of service

  • service_type (Class)

    class of this service

  • persistent (Boolean)

    use persistent connection or not



26
27
28
29
30
31
32
33
34
35
# File 'lib/ros/tcpros/service_client.rb', line 26

def initialize(host, port, caller_id, service_name, service_type, persistent)
  @caller_id = caller_id
  @service_name = service_name
  @service_type = service_type
  @port = port
  @host = host
  @socket = TCPSocket.open(@host, @port)
  @persistent = persistent
  @socket.setsockopt(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
end

Instance Attribute Details

#hostString (readonly)

host of this connection

Returns:

  • (String)

    host name



101
102
103
# File 'lib/ros/tcpros/service_client.rb', line 101

def host
  @host
end

#portInteger (readonly)

port number of this socket

Returns:

  • (Integer)

    port number



97
98
99
# File 'lib/ros/tcpros/service_client.rb', line 97

def port
  @port
end

Instance Method Details

#build_headerHeader

build henader message for service client. It contains callerid, service, md5sum, type, persistent.

Returns:



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ros/tcpros/service_client.rb', line 41

def build_header
  header = Header.new
  header.push_data("callerid", @caller_id)
  header.push_data("service", @service_name)
  header.push_data("md5sum", @service_type.md5sum)
  header.push_data("type", @service_type.type)
  if @persistent
	header.push_data("persistent", '1')
  end
  header
end

#call(srv_request, srv_response) ⇒ Boolean

call the service by sending srv request message, and receive response message.

Parameters:

  • srv_request (Message)

    call with this request

  • srv_response (Message)

    response is stored in this message

Returns:

  • (Boolean)

    result of call



59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ros/tcpros/service_client.rb', line 59

def call(srv_request, srv_response)
  write_header(@socket, build_header)
  if check_header(read_header(@socket))
	write_msg(@socket, srv_request)
	@socket.flush
	ok_byte = read_ok_byte
	if ok_byte == 1
	  srv_response.deserialize(read_all(@socket))
	  return true
	end
	false
  end
  false
end

#check_header(header) ⇒ Boolean

check md5sum only.

Parameters:

  • header (Header)

    received header

Returns:

  • (Boolean)

    true if it is ok.



85
86
87
# File 'lib/ros/tcpros/service_client.rb', line 85

def check_header(header)
  header.valid?('md5sum', @service_type.md5sum)
end

#read_ok_byteInteger

read ok byte for boolean service result.

Returns:

  • (Integer)

    1 for OK, 0 for NG



77
78
79
# File 'lib/ros/tcpros/service_client.rb', line 77

def read_ok_byte
  @socket.recv(1).unpack('c')[0]
end

#shutdownObject

close the socket



91
92
93
# File 'lib/ros/tcpros/service_client.rb', line 91

def shutdown
  @socket.close
end