Let's now write a Cplusplus service client directly inside a node that has been written with object
oriented programming.
This way, you can learn how to integrate a service client inside an existing node that may contain,
for example, other publishers, subscribers, other services, etc. so we are going to rewrite that
inside the node.
Let's create a new file.
And you could do that from VSCode directly as well.
But I'm just doing it from the terminal.
So we go back to our cplusplus files inside the package that's going to be add to int client dot cp.
So we have the know OOP which was basically the script.
And that one is going to be the object oriented programming one okay.
And we have a new file there add to int client.
We can use the template.
And let's save.
First get the auto completion and include detected.
Okay.
And then add to INS client node.
We replace it here as well.
And here and then add to INS client.
All right.
So now we have our node structure.
And we can start with well first including the interface that's going to be example interface solve
and add two ints dot HPP.
So we have the interface that's already in the package dot XML and that's already in the cmakelists.txt
okay.
So nothing to do.
And then we are going to create.
So just like we did here, we are going to create a client inside Side our node.
And you see that's an Lclc-rp client with shared pointer.
So I'm going to create that as a private attribute here Lclc-rp client.
We need to provide the interface.
So example.
So you see I don't have the autocompletion because I first need to save.
And then that's going to be detected okay.
So now we have an error here of course example interfaces save and add to it.
So we have a client.
But of course I'm adding shared pointer.
So sharedptr.
And it's going to be client.
Great.
We declare it here and we can initialize it in the constructor.
So client is equal to.
And just like we did here we're going to do.
So I'm just going to take that actually.
So we do This create client with the type and the name add to it.
So we are sure that we don't make any mistake.
Here.
We have the same name and that's the name we have defined in the server right here.
Add to it.
Then once I have the client well I'm going to create a function that I can call from, from the node
from the outside so that I can send a request.
So void let's name it call add to int with two parameters int a int b.
And what do I do?
First I will wait.
So I will use this while the client is waiting for the service.
We are going to print this log every one second.
So I'm going to add here using namespace std Chrono literals, and I'm gonna just take that code here
and put it there.
And it's not just client is the client from.
So the private attribute we have created here and also here, it's not going to be node I'm going to
put this.
All right.
So first things first we make sure the service so the server is up.
So we wait for the service after one second.
If it hasn't found service then we're gonna print this log.
Then when we have found the service we can send the request.
So let's also get that one here.
It's going to be the same as what we've just written before.
And so we create a request which is going to be a shared pointer of the request object inside add to
int.
And then what request a.
I'm going to use the A that I got from the parameters of the function here.
And the same for B.
And then here is going to be quite different.
So what I did was to do.
So I'm going to call this function as well async sendrequest.
But instead of getting a future and then spinning until the future is complete, I'm going to add a
callback here so that I can process the response in another method.
And to do this I will write here.
So I'm going to rewrite client async send request with the request.
And then I'm going to add a callback.
You see I can add a callback to Async send request okay.
And to add a callback what I'm going to first create a callback.
And the callback is going to be in the private, because it's not something we want to call from outside
of the class.
Let's name it void callback.
So for example callback call add to eat.
Or you could name it callback.
Add to its response something like that.
Something that makes sense.
And in this callback we are going to receive our Clcp client.
With the type example, interfaces solve add to int and that's going to be a shared future.
Okay.
That's going to be a future with a shared future.
So we'll just need to use that syntax.
And you see that this shared future is going to create an STD shared future.
You don't really need to worry too much about that.
Okay.
Just know how to use it because then it's very easy.
You do.
Auto response is equal to future.
Dot get as simple as that.
And the response is going to be a shared pointer to the response object.
Of the interface.
All right.
So sometimes with C plus plus the syntax is quite long and quite explicit, which is a bad thing and
a good thing at the same time.
And sometimes, well, you just, you know, you just write it because, you know, you have to write
it like that and it's just working.
So let's not make things more complicated.
All right.
I'm going to finish this callback with a Clcp for this Getlogger.
We're just going to print the result.
And then we can say response.
So response is going to be some we're going to print response dot.
So actually not dot is a pointer.
So some like that.
And let's just cast it as an integer just in case okay.
And the callback is finished.
I'm just going to add it here and how to add a callback.
Now you start to get used to it std bind.
We provide the reference with the class name which is add to insert client node and then callback call
add to insert.
Let's go back to a new line here.
Then we have this which is this object here.
And because we have one parameter I'm going to add a placeholder.
And I'm just going to use using namespace std placeholders.
And I'm going to use here underscore one.
All right.
And that's it for the code.
And if you also wanted to pass the request here to the callback.
Well one way to do this is for example you could create a structure containing the request.
And you save it inside the class in a attribute.
And then you just retrieve it from the callback and you can get access to it.
But I'm going to keep the code like this.
Okay.
Already enough things in this C plus plus code.
So let's do a quick recap.
Before we run that we are going to create a node.
When we create the node, we just create a client okay.
And then you could create all your publisher subscribers, other services, etc. you could register
as many callbacks as you want.
Then we make the node spin and actually well I'm not calling the function.
So it would be nice to call the function from somewhere.
You could call this function from anywhere from any other callback, but I'm going to call it here node
call add to insert with let's say ten and five okay.
So we're going to call the function.
We're going to wait for the service to be up.
That might just return immediately.
If the service is already up then we send the request and we register a callback.
So then when we spin the node and when the server replies we're going to get the response here.
And we can process the response okay let's save.
And let's add an execute table here Add executable add to Int's client from the file which is in src.
Add to int clients.
CPP.
We add the dependencies and target dependencies with the executable name and then Clcp example interfaces.
And we also add the executable name here to be installed.
Great.
So we have the three executables we have created here in this section so far.
Let's save okay.
You save all the files.
Make sure everything is saved.
Let's go back.
Let's build with click on build.
Okay.
It's working and I'm gonna stop that.
Let's just stop and open a new terminals.
So we have everything correctly set up.
So let's start our client roster.
Run my CP package with add to client.
So okay you see it's waiting for the server because we are calling the function to send the request
and then rose to run my CP.
So my CP package with the add to it server okay.
And you can see it's working.
We have the results.
Um the node is still running okay.
So we have sent a request from a node that is still spinning.
And now I can do also an other example like I did before.
If I send multiple requests let's see what's going to happen.
So I'm going to do that.
So 12 and seven for example.
And then one and two Okay, so I sent three requests.
So for that we will need to of course build again.
And then I can go back here.
I will stop the client.
I will make sure that I source my environment because I have just built and we have a new version of
that node.
I can still keep the server running.
Okay, no problem with that.
And let's run the client again.
And you see we have sent three requests okay.
And we have the three responses here.
So you could also print uh when you send the request you could also print the two elements of the request.
And also maybe as I told you before, you keep in a structure and you retrieve it from the callback
as you want, but now you can see that it's correctly working, and you can use this structure in any
node that you've already created.
And congratulations, you have now written a full Cplusplus service server and client.