Class: ROS::TCPROS::ServiceServer

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

Overview

TCPROS protocol Service Server

Constant Summary

MAX_CONNECTION =

max number of connection with clients

100

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Message

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

Constructor Details

- (ServiceServer) initialize(caller_id, service_name, service_type, callback, options = {})

Returns a new instance of ServiceServer

Parameters:

  • caller_id (String)

    caller_id of this node

  • service_name (String)

    name of this service

  • service_type (Class)

    class of this service message

  • callback (Proc)

    of this service

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

    options

Options Hash (options):

  • port (Integer)

    port number (default: 0)

  • host (host)

    host name (defualt: Socket.gethostname)



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ros/tcpros/service_server.rb', line 32

def initialize(caller_id, service_name, service_type, callback,
               options={})
  if options[:host]
    host = options[:host]
  else
    host = Socket.gethostname
  end
  if options[:port]
    port = options[:port]
  else
    port = 0
  end

  super(port, host, MAX_CONNECTION)
  @caller_id = caller_id
  @service_name = service_name
  @service_type = service_type
  @callback = callback
  @byte_received = 0
  @byte_sent = 0
end

Instance Attribute Details

- (Integer) byte_received (readonly)

received data amout for slave api

Returns:

  • (Integer)

    byte received



137
138
139
# File 'lib/ros/tcpros/service_server.rb', line 137

def byte_received
  @byte_received
end

- (Integer) byte_sent (readonly)

sent data amout for slave api

Returns:

  • (Integer)

    byte sent



141
142
143
# File 'lib/ros/tcpros/service_server.rb', line 141

def byte_sent
  @byte_sent
end

Instance Method Details

- (Header) build_header

build header message for service server. It contains callerid, type, md5sum.

Returns:



127
128
129
130
131
132
133
# File 'lib/ros/tcpros/service_server.rb', line 127

def build_header
  header = Header.new
  header["callerid"] = @caller_id
  header['type'] = @service_type.type
  header['md5sum'] = @service_type.md5sum
  header
end

- (Boolean) check_header(header)

check header. check md5sum only.

Parameters:

  • header (Header)

    header for checking

Returns:

  • (Boolean)

    check result (true means ok)



119
120
121
# File 'lib/ros/tcpros/service_server.rb', line 119

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

- (Boolean) read_and_callback(socket)

main loop of this connection. read data and do callback.

Parameters:

  • socket (IO)

Returns:

  • (Boolean)

    result of callback



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ros/tcpros/service_server.rb', line 73

def read_and_callback(socket)
  request = @service_type.request_class.new
  response = @service_type.response_class.new
  data = read_all(socket)
  @byte_received += data.length
  request.deserialize(data)
  result = @callback.call(request, response)
  if result
    send_ok_byte(socket)
    data = write_msg(socket, response)
    @byte_sent += data.length
  else
    send_ng_byte(socket)
    write_header(socket, build_header)
    # write some message
  end
  result
end

- (Object) send_ng_byte(socket)

message must send 1 byte for service call result (fail)

Parameters:

  • socket (IO)


64
65
66
# File 'lib/ros/tcpros/service_server.rb', line 64

def send_ng_byte(socket)
  socket.write([0].pack('c'))
end

- (Object) send_ok_byte(socket)

message must send 1 byte for service call result (success)

Parameters:

  • socket (IO)


57
58
59
# File 'lib/ros/tcpros/service_server.rb', line 57

def send_ok_byte(socket)
  socket.write([1].pack('c'))
end

- (Object) serve(socket)

this is called by socket accept

Parameters:

  • socket (IO)

    given socket



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ros/tcpros/service_server.rb', line 95

def serve(socket)
  header = read_header(socket)
  # not documented protocol?
  if header['probe'] == '1'
    write_header(socket, build_header)
  elsif check_header(header)
    write_header(socket, build_header)
    read_and_callback(socket)
    if header['persistent'] == '1'
      loop do
        read_and_callback(socket)
      end
    end
  else
    socket.close
    raise 'header check error'
  end
end