ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

But in C++ code it seems that the addition is done within the server and send the result back to the client.

Correct. That is how the service server & service client examples are set up.

Is the python format code against Server/Client rule? [..] It seems that the addition is done within the client code:

add_two_ints = rospy.ServiceProxy('add_two_ints', AddTwoInts)
resp1 = add_two_ints(x, y)
return resp1.sum

I can understand why you'd have the impression that add_two_ints(..) is executed on the client, but if you examine the server code carefully, you'll see the addition is actually performed by the server (from the tutorial you linked):

def handle_add_two_ints(req):
    print "Returning [%s + %s = %s]"%(req.a, req.b, (req.a + req.b))
    return AddTwoIntsResponse(req.a + req.b)

The "extra code" in the Python client example just wraps the following steps:

  1. making sure the add_two_ints service exists and is available
  2. creating the actual service client object (ie: ServiceProxy)
  3. invoking the service
  4. waiting for the result
  5. returning the result
  6. (optionally) handling errors (in this case simply printing the exception)

My guess is that the example author felt it would be cleaner to wrap all of that in a function, instead of placing all of those lines in the main() directly.