In this lesson you will learn how to create a Cplusplus service server.
We will do the exact same thing as we did for Python.
So let's start very quickly with the interface we have used.
So Ros2 interface show we have used the example interfaces and then save and then add two ints which
contains a request that's a and b.
So we have two fields.
Each one is an int 64.
Then three dashes to separate the request from the response.
And then we have a some field in the response okay.
So we have two different messages request and then response.
And we are going to use this interface inside our service server and client.
So let's create a node for the service server.
We are going to our C plus plus package this time and in the source folder of that C plus plus package
where we have all our CPP files.
So let's create a new one.
Let's call it add to int server dot cpp.
And then we can open VSCode.
I'm going to open it once again from the same place, which is the source folder of the roster workspace
with code dot.
Okay let's open that file.
And I don't need that.
And we can get the template.
Okay.
So I'm going to use the C plus plus template for nodes.
Let's save.
So we have the auto completion and the include is found.
Okay.
And let's rename that add to int server node.
We replace it here and there and there we can also rename the node add to its server.
Okay.
Just like that.
And then first things first, let's include the interface that we will need for the server.
So include example interfaces slash.
And you see now it's not going to be MSG.
It's going to be S r v.
And then add to hints dot http.
All right.
We import that one and let's save again.
So that is going to be found.
Now we can create our service server.
And first we are going to declare it as a private attribute.
So how to create a service server.
You do Clcp.
And then that's going to be service.
You need to give the type here with a template, just like we did for publishers and subscribers.
It's going to be the same.
So what is the type.
It's example interfaces solve and add to ints.
Okay, I close the bracket.
And actually that's not all.
We are not just going to create a service, we are going to create a guess what a shared pointer to
the service.
Okay.
So we use once again the shared PTR helper here for most things we have in rust.
And let's name that server.
So we have the declaration here.
Let's initialize it in the constructor.
Server is equal to and I'm using this.
Okay no need but it's to make this explicit.
Create service.
Okay.
So to create a service we use the create service we need to provide once again the interface.
Example interfaces solve add to ints.
All right.
And then open and close the parentheses.
Semicolon.
And what do we need to provide.
Well we've already provided the interface here.
So now we need to provide the name.
And the name is well we choose the name because we create the service here.
We choose the name.
Let's choose add to insert to make the same as what we did with Python.
And then we need to provide a callback okay.
So let's create the callback now.
And the callback is simply because well once again you create a server.
But the server is not going to do anything on its own.
You need to send a request from the outside of this node to this server.
And when you get a request, you need to be able to process it.
And for that, we just create a callback so that when the node is spinning, we can execute that callback.
And I'm going to add it here in private because we don't want to call this callback directly ourselves.
It's going to be called by the spinning mechanism.
And so that's going to be void.
It's not going to return anything.
Here is the difference with Python.
In Python you had to return the response from the from the function from the callback.
Here it's going to be void.
So we don't return anything.
Let's call it callback add to int.
And here you will receive also two things.
You will receive the request and the response.
And actually you receive a const shared pointer of the request and a const shared pointer of the response.
And so that's going to be const.
Example interfaces solve add to ints and request okay.
So you see we have the request message inside it.
Twins.
This is not a message.
This is a combination.
This is a pair of messages.
And then we have shared pointer plus let's name it request.
So you can see it's quite long but that's the full type you need to provide.
We have the request.
And then comma, I'm going to go back to a new line here.
And let's do const example interfaces salve add to int.
This one is going to be the response and also a shared pointer to the response response okay.
And we can open and close the curly brackets for the method.
So what do we do in this service callback.
Well we are going to process the request.
Maybe you need to validate some data or check stuff.
Maybe you need to do an action like turn on an LED, activate a motor, etc..
Here we just want to return a sum.
So the sum of the two elements we have in the request.
So we are going to do response.
And because this is a pointer we use an arrow response sum is equal to request a plus, request A row
B.
Okay.
And that's basically it for this.
You don't need to return anything.
Now I'm also going to print a log here so we know what's happening.
Club info with.
So this get logger.
And then.
So we're going to have a integer.
So that's percentage D plus integer is equal to integer.
And I provide the three elements here.
So just in case I'm going to cast it to an integer there's going to be request a and then int request
b and then int response some which is already set, and a semicolon.
And maybe let's just go back to a new line here.
Okay.
So that's it for the callback.
Now we need to go back here to the constructor.
So we come back to this create service where we have the the type the name.
And we need to provide the callback.
So I'm going to do that on a new line.
And then this also on a new line.
And we need to do STD bind okay.
So for a callback you will use Stdbind.
Once again the first thing is the reference to the function.
So add two ints seven node that's the class name.
And then callback add two ints.
That's the name of the function here with no parentheses.
Second argument in the stdbind is this to link to that object here.
And then because we have two parameters, we need to place two placeholders.
So placeholder one and placeholder two.
Instead of doing STD placeholders one directly here I'm going to be using the namespace using namespace
std placeholders.
Okay.
So I can just do underscore one and then underscore two.
Okay.
So we have two arguments.
We use underscore one and then underscore two.
So in the STD bind you have four parameters to provide.
Okay.
And let's finish this constructor with a log sleep.
And for this get logger and service.
So add to insert a service has been studied for example.
All right let's do a quick recap here.
So we're going to initialize roster communications.
And then we create a shared pointer to the node.
When we get to the constructor we create a server.
And we're going to register that callback for the add to server.
Then the node is going to spin and when the client.
So when one client sends a request it's going to be processed.
Inside this callback we receive the request.
We also have the response object directly.
And what we need to do is to fill.
So to do an action and to fill the response object and that's it.
After this function will exit, then the response is going to be sent back automatically to the client.
All right.
So let's save this file.
And let's go to the Cmakelists.txt to build and install an executable.
So let's do that here.
Add executable.
Add to INS severe from ad to int server dot cp.
All right.
That's the file here.
I'm going a bit faster now because we've done already.
You see that.
We've done already that five times payment target dependencies with the executable name.
And what do we need Lclc-rp and example interfaces.
As you can see in the code we import the two packages and then we have the executable.
I'm going to add it here add two int server.
Now I can save and let's build that.
So let's go back one directory and let's do a call on build packages.
Select.
And let's only build the my CPP package.
So it's quite useful to use this packages select here.
Because if you remember we had built the Python package with Simulink install.
So if you build all the packages again without Simulink install, then you're going to lose the Simulink
install option for the Python package.
So that's why here we just build the Cplusplus one and we don't lose that benefit for the other packages.
All right so I will just a quick parenthesis here.
So let's build.
And then let's solve the terminal or open new ones.
And let's do a Ros to run my CPP package with add to in server okay.
Add to in service has been started.
I can do Ros two.
Well I can do Ros two node list.
Once again I find the node Ros two node info with that name in the service servers I see my add to int
with the example interfaces.
So we add to int.
Great.
And now what I could do is I could do Ros two service Call to test this service from the terminal.
But if you remember, what we just did in the previous videos was to create a Python service client.
So we already have a service client for that service and it's the same name.
So why not just use the client that we already have.
Let's do Ros to run my Python package.
We've had two ints client.
We can use the one with object oriented programming or the one with.
So the script one with no op.
And let's see if that works.
And you can see it's working.
So the Python client here sent a request that was processed by the server here.
And we got the response.
So you can see once again you can have a for example a Python client in one node and a C plus plus server
in another node.
And that's no problem.
It's going to work the same.
And with this we can also validate that our service server is correctly working.