WEBVTT

00:00.050 --> 00:06.650
Let's now write a Cplusplus service client directly inside a node that has been written with object

00:06.680 --> 00:08.000
oriented programming.

00:08.000 --> 00:14.000
This way, you can learn how to integrate a service client inside an existing node that may contain,

00:14.000 --> 00:20.390
for example, other publishers, subscribers, other services, etc. so we are going to rewrite that

00:20.390 --> 00:21.530
inside the node.

00:21.530 --> 00:24.050
Let's create a new file.

00:24.260 --> 00:26.330
And you could do that from VSCode directly as well.

00:26.330 --> 00:29.750
But I'm just doing it from the terminal.

00:29.750 --> 00:40.700
So we go back to our cplusplus files inside the package that's going to be add to int client dot cp.

00:40.910 --> 00:44.270
So we have the know OOP which was basically the script.

00:44.270 --> 00:49.040
And that one is going to be the object oriented programming one okay.

00:49.040 --> 00:54.860
And we have a new file there add to int client.

00:54.890 --> 00:58.190
We can use the template.

01:02.390 --> 01:03.710
And let's save.

01:03.860 --> 01:07.400
First get the auto completion and include detected.

01:07.430 --> 01:07.910
Okay.

01:07.910 --> 01:13.640
And then add to INS client node.

01:14.030 --> 01:16.070
We replace it here as well.

01:16.070 --> 01:22.460
And here and then add to INS client.

01:23.450 --> 01:24.320
All right.

01:24.920 --> 01:29.270
So now we have our node structure.

01:29.270 --> 01:39.440
And we can start with well first including the interface that's going to be example interface solve

01:39.440 --> 01:43.040
and add two ints dot HPP.

01:43.190 --> 01:50.600
So we have the interface that's already in the package dot XML and that's already in the cmakelists.txt

01:50.630 --> 01:50.930
okay.

01:50.960 --> 01:52.040
So nothing to do.

01:52.040 --> 01:54.080
And then we are going to create.

01:54.110 --> 01:59.640
So just like we did here, we are going to create a client inside Side our node.

01:59.640 --> 02:03.750
And you see that's an Lclc-rp client with shared pointer.

02:03.750 --> 02:10.740
So I'm going to create that as a private attribute here Lclc-rp client.

02:10.770 --> 02:12.390
We need to provide the interface.

02:12.390 --> 02:13.800
So example.

02:13.830 --> 02:18.030
So you see I don't have the autocompletion because I first need to save.

02:18.180 --> 02:20.610
And then that's going to be detected okay.

02:20.640 --> 02:28.260
So now we have an error here of course example interfaces save and add to it.

02:28.350 --> 02:29.970
So we have a client.

02:29.970 --> 02:34.620
But of course I'm adding shared pointer.

02:34.620 --> 02:36.300
So sharedptr.

02:37.230 --> 02:38.790
And it's going to be client.

02:38.820 --> 02:39.510
Great.

02:39.510 --> 02:45.450
We declare it here and we can initialize it in the constructor.

02:45.450 --> 02:49.110
So client is equal to.

02:49.140 --> 02:52.080
And just like we did here we're going to do.

02:52.080 --> 02:54.150
So I'm just going to take that actually.

02:55.980 --> 03:04.410
So we do This create client with the type and the name add to it.

03:04.410 --> 03:06.090
So we are sure that we don't make any mistake.

03:06.090 --> 03:06.570
Here.

03:06.990 --> 03:11.970
We have the same name and that's the name we have defined in the server right here.

03:12.000 --> 03:13.020
Add to it.

03:13.080 --> 03:19.620
Then once I have the client well I'm going to create a function that I can call from, from the node

03:19.620 --> 03:22.980
from the outside so that I can send a request.

03:22.980 --> 03:34.590
So void let's name it call add to int with two parameters int a int b.

03:35.670 --> 03:37.290
And what do I do?

03:37.470 --> 03:39.750
First I will wait.

03:39.750 --> 03:44.670
So I will use this while the client is waiting for the service.

03:44.700 --> 03:48.720
We are going to print this log every one second.

03:48.720 --> 04:04.860
So I'm going to add here using namespace std Chrono literals, and I'm gonna just take that code here

04:05.250 --> 04:06.720
and put it there.

04:09.150 --> 04:12.240
And it's not just client is the client from.

04:12.240 --> 04:17.850
So the private attribute we have created here and also here, it's not going to be node I'm going to

04:17.850 --> 04:18.780
put this.

04:18.810 --> 04:19.260
All right.

04:19.260 --> 04:23.580
So first things first we make sure the service so the server is up.

04:23.580 --> 04:25.860
So we wait for the service after one second.

04:25.860 --> 04:30.960
If it hasn't found service then we're gonna print this log.

04:30.990 --> 04:34.650
Then when we have found the service we can send the request.

04:34.650 --> 04:37.890
So let's also get that one here.

04:38.070 --> 04:41.550
It's going to be the same as what we've just written before.

04:42.930 --> 04:49.320
And so we create a request which is going to be a shared pointer of the request object inside add to

04:49.350 --> 04:49.740
int.

04:49.740 --> 04:52.890
And then what request a.

04:52.890 --> 04:58.120
I'm going to use the A that I got from the parameters of the function here.

04:58.150 --> 04:59.890
And the same for B.

04:59.890 --> 05:01.720
And then here is going to be quite different.

05:01.720 --> 05:04.090
So what I did was to do.

05:04.120 --> 05:07.480
So I'm going to call this function as well async sendrequest.

05:07.480 --> 05:13.660
But instead of getting a future and then spinning until the future is complete, I'm going to add a

05:13.660 --> 05:18.490
callback here so that I can process the response in another method.

05:18.490 --> 05:21.130
And to do this I will write here.

05:21.130 --> 05:29.380
So I'm going to rewrite client async send request with the request.

05:29.380 --> 05:31.390
And then I'm going to add a callback.

05:31.420 --> 05:35.530
You see I can add a callback to Async send request okay.

05:35.560 --> 05:39.100
And to add a callback what I'm going to first create a callback.

05:39.100 --> 05:44.800
And the callback is going to be in the private, because it's not something we want to call from outside

05:44.800 --> 05:45.940
of the class.

05:45.970 --> 05:49.150
Let's name it void callback.

05:49.150 --> 05:55.330
So for example callback call add to eat.

05:55.330 --> 05:57.010
Or you could name it callback.

05:57.040 --> 05:59.710
Add to its response something like that.

05:59.980 --> 06:01.390
Something that makes sense.

06:01.390 --> 06:06.850
And in this callback we are going to receive our Clcp client.

06:06.970 --> 06:18.760
With the type example, interfaces solve add to int and that's going to be a shared future.

06:18.820 --> 06:19.180
Okay.

06:19.210 --> 06:21.700
That's going to be a future with a shared future.

06:21.700 --> 06:25.480
So we'll just need to use that syntax.

06:25.510 --> 06:29.710
And you see that this shared future is going to create an STD shared future.

06:29.710 --> 06:32.200
You don't really need to worry too much about that.

06:32.350 --> 06:32.560
Okay.

06:32.590 --> 06:35.350
Just know how to use it because then it's very easy.

06:35.350 --> 06:35.740
You do.

06:35.770 --> 06:39.610
Auto response is equal to future.

06:39.640 --> 06:42.100
Dot get as simple as that.

06:42.100 --> 06:48.130
And the response is going to be a shared pointer to the response object.

06:48.160 --> 06:49.270
Of the interface.

06:49.300 --> 06:49.660
All right.

06:49.660 --> 06:56.260
So sometimes with C plus plus the syntax is quite long and quite explicit, which is a bad thing and

06:56.260 --> 06:57.490
a good thing at the same time.

06:57.490 --> 07:01.180
And sometimes, well, you just, you know, you just write it because, you know, you have to write

07:01.180 --> 07:03.190
it like that and it's just working.

07:03.190 --> 07:05.560
So let's not make things more complicated.

07:05.590 --> 07:05.890
All right.

07:05.890 --> 07:12.940
I'm going to finish this callback with a Clcp for this Getlogger.

07:12.940 --> 07:14.500
We're just going to print the result.

07:16.030 --> 07:19.870
And then we can say response.

07:20.170 --> 07:28.480
So response is going to be some we're going to print response dot.

07:28.660 --> 07:30.940
So actually not dot is a pointer.

07:30.940 --> 07:33.160
So some like that.

07:33.160 --> 07:37.750
And let's just cast it as an integer just in case okay.

07:37.780 --> 07:39.610
And the callback is finished.

07:39.610 --> 07:42.790
I'm just going to add it here and how to add a callback.

07:42.790 --> 07:46.420
Now you start to get used to it std bind.

07:46.690 --> 07:57.310
We provide the reference with the class name which is add to insert client node and then callback call

07:57.340 --> 07:58.780
add to insert.

07:59.110 --> 08:00.820
Let's go back to a new line here.

08:01.420 --> 08:05.800
Then we have this which is this object here.

08:05.830 --> 08:11.020
And because we have one parameter I'm going to add a placeholder.

08:11.020 --> 08:18.220
And I'm just going to use using namespace std placeholders.

08:18.220 --> 08:22.060
And I'm going to use here underscore one.

08:22.810 --> 08:23.260
All right.

08:23.260 --> 08:25.600
And that's it for the code.

08:25.600 --> 08:29.140
And if you also wanted to pass the request here to the callback.

08:29.140 --> 08:33.850
Well one way to do this is for example you could create a structure containing the request.

08:33.850 --> 08:37.390
And you save it inside the class in a attribute.

08:37.420 --> 08:41.380
And then you just retrieve it from the callback and you can get access to it.

08:41.380 --> 08:43.300
But I'm going to keep the code like this.

08:43.390 --> 08:43.960
Okay.

08:44.200 --> 08:48.010
Already enough things in this C plus plus code.

08:48.010 --> 08:49.750
So let's do a quick recap.

08:49.750 --> 08:53.090
Before we run that we are going to create a node.

08:53.090 --> 08:56.420
When we create the node, we just create a client okay.

08:56.450 --> 09:02.420
And then you could create all your publisher subscribers, other services, etc. you could register

09:02.420 --> 09:04.100
as many callbacks as you want.

09:04.130 --> 09:09.200
Then we make the node spin and actually well I'm not calling the function.

09:09.200 --> 09:13.130
So it would be nice to call the function from somewhere.

09:13.130 --> 09:19.490
You could call this function from anywhere from any other callback, but I'm going to call it here node

09:19.490 --> 09:25.430
call add to insert with let's say ten and five okay.

09:25.430 --> 09:27.590
So we're going to call the function.

09:27.620 --> 09:29.930
We're going to wait for the service to be up.

09:29.960 --> 09:31.850
That might just return immediately.

09:31.850 --> 09:36.560
If the service is already up then we send the request and we register a callback.

09:36.560 --> 09:43.460
So then when we spin the node and when the server replies we're going to get the response here.

09:43.460 --> 09:46.520
And we can process the response okay let's save.

09:46.520 --> 09:58.970
And let's add an execute table here Add executable add to Int's client from the file which is in src.

10:00.170 --> 10:03.740
Add to int clients.

10:03.770 --> 10:04.460
CPP.

10:04.730 --> 10:16.370
We add the dependencies and target dependencies with the executable name and then Clcp example interfaces.

10:16.370 --> 10:23.960
And we also add the executable name here to be installed.

10:24.560 --> 10:24.800
Great.

10:24.800 --> 10:28.760
So we have the three executables we have created here in this section so far.

10:28.790 --> 10:30.470
Let's save okay.

10:30.500 --> 10:32.150
You save all the files.

10:32.180 --> 10:33.890
Make sure everything is saved.

10:34.520 --> 10:35.660
Let's go back.

10:36.860 --> 10:40.550
Let's build with click on build.

10:42.890 --> 10:43.340
Okay.

10:43.340 --> 10:47.060
It's working and I'm gonna stop that.

10:47.060 --> 10:50.390
Let's just stop and open a new terminals.

10:50.390 --> 10:53.600
So we have everything correctly set up.

10:53.600 --> 10:57.080
So let's start our client roster.

10:57.110 --> 11:03.440
Run my CP package with add to client.

11:04.490 --> 11:09.350
So okay you see it's waiting for the server because we are calling the function to send the request

11:09.350 --> 11:13.100
and then rose to run my CP.

11:13.100 --> 11:21.200
So my CP package with the add to it server okay.

11:21.200 --> 11:23.210
And you can see it's working.

11:23.210 --> 11:24.170
We have the results.

11:24.170 --> 11:26.750
Um the node is still running okay.

11:26.780 --> 11:31.820
So we have sent a request from a node that is still spinning.

11:31.820 --> 11:36.080
And now I can do also an other example like I did before.

11:36.080 --> 11:40.010
If I send multiple requests let's see what's going to happen.

11:40.340 --> 11:42.050
So I'm going to do that.

11:42.080 --> 11:45.110
So 12 and seven for example.

11:45.350 --> 11:52.530
And then one and two Okay, so I sent three requests.

11:52.530 --> 11:55.740
So for that we will need to of course build again.

11:57.570 --> 12:00.300
And then I can go back here.

12:00.300 --> 12:02.460
I will stop the client.

12:02.460 --> 12:08.490
I will make sure that I source my environment because I have just built and we have a new version of

12:08.520 --> 12:09.330
that node.

12:09.330 --> 12:11.550
I can still keep the server running.

12:11.580 --> 12:13.230
Okay, no problem with that.

12:13.230 --> 12:15.300
And let's run the client again.

12:15.300 --> 12:19.410
And you see we have sent three requests okay.

12:19.440 --> 12:20.940
And we have the three responses here.

12:20.940 --> 12:26.970
So you could also print uh when you send the request you could also print the two elements of the request.

12:26.970 --> 12:32.010
And also maybe as I told you before, you keep in a structure and you retrieve it from the callback

12:32.040 --> 12:40.350
as you want, but now you can see that it's correctly working, and you can use this structure in any

12:40.380 --> 12:43.080
node that you've already created.

12:43.080 --> 12:48.960
And congratulations, you have now written a full Cplusplus service server and client.
