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 collapse

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

#initialize(caller_id, service_name, service_type, callback, port = 0, host = ::GServer::DEFAULT_HOST) ⇒ ServiceServer

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

  • port (Integer) (defaults to: 0)

    port number

  • host (host) (defaults to: ::GServer::DEFAULT_HOST)

    host name



31
32
33
34
35
36
37
38
39
40
# File 'lib/ros/tcpros/service_server.rb', line 31

def initialize(caller_id, service_name, service_type, callback,
               port=0, host=::GServer::DEFAULT_HOST)
  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

#byte_receivedInteger (readonly)

received data amout for slave api

Returns:

  • (Integer)

    byte received



125
126
127
# File 'lib/ros/tcpros/service_server.rb', line 125

def byte_received
  @byte_received
end

#byte_sentInteger (readonly)

sent data amout for slave api

Returns:

  • (Integer)

    byte sent



129
130
131
# File 'lib/ros/tcpros/service_server.rb', line 129

def byte_sent
  @byte_sent
end

Instance Method Details

#build_headerHeader

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

Returns:



115
116
117
118
119
120
121
# File 'lib/ros/tcpros/service_server.rb', line 115

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

#check_header(header) ⇒ Boolean

check header. check md5sum only.

Parameters:

  • header (Header)

    header for checking

Returns:

  • (Boolean)

    check result (true means ok)



107
108
109
# File 'lib/ros/tcpros/service_server.rb', line 107

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

#read_and_callback(socket) ⇒ Boolean

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

Parameters:

  • socket (IO)

Returns:

  • (Boolean)

    result of callback



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ros/tcpros/service_server.rb', line 61

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

#send_ng_byte(socket) ⇒ Object

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

Parameters:

  • socket (IO)


52
53
54
# File 'lib/ros/tcpros/service_server.rb', line 52

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

#send_ok_byte(socket) ⇒ Object

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

Parameters:

  • socket (IO)


45
46
47
# File 'lib/ros/tcpros/service_server.rb', line 45

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

#serve(socket) ⇒ Object

this is called by socket accept

Parameters:

  • socket (IO)

    given socket



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ros/tcpros/service_server.rb', line 83

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