Skip to content

Commit db7fc05

Browse files
#fix REQREPLY-221: Update RPC examples (#724)
* REQREPLY-221: Update RPC examples * REQREPLY-221: Fix precondition error because the participant is stil alive by the time we call finalize factory * REQREPLY-221: Add logging * REQREPLY-221: Remove white spaces * REQREPLY-221: Add timeout to request reply cs example * REQREPLY-221: Update code to use the new apis to wait for service * REQREPLY-221: Undo changes in Inventory.py * REQREPLY-221: Apply PR feedback * REQREPLY-221: Fix format * REQREPLY-221: Add test specification and test implementation * REQREPLY-221: Add makefile for java * REQREPLY-221: add csproj * REQREPLY-221: Undo change in script * REQREPLY-221: Fix tests paths * REQREPLY-221: Improve test scripts * Revert "REQREPLY-221: Improve test scripts" This reverts commit ae0e3c1. * REQREPLY-221: Fix testing * REQREPLY-221: Apply PR feedback
1 parent 94bd07a commit db7fc05

27 files changed

+531
-36
lines changed

examples/connext_dds/remote_procedure_call/c++11/Inventory_service.cxx

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,31 +81,14 @@ class InventoryImpl : public InventoryService {
8181
std::mutex mutex;
8282
};
8383

84-
int main(int argc, char *argv[])
84+
void run_server(int domain_id, unsigned int delay, unsigned int service_timeout)
8585
{
86-
using namespace application;
87-
88-
// Parse arguments
89-
auto arguments = parse_arguments(argc, argv, false);
90-
if (arguments.parse_result == ParseReturn::exit) {
91-
return EXIT_SUCCESS;
92-
} else if (arguments.parse_result == ParseReturn::failure) {
93-
return EXIT_FAILURE;
94-
}
95-
96-
// Sets Connext verbosity to help debugging
97-
rti::config::Logger::instance().verbosity(arguments.verbosity);
98-
99-
// To turn on additional logging, include <rti/config/Logger.hpp> and
100-
// uncomment the following line:
101-
// rti::config::Logger::instance().verbosity(rti::config::Verbosity::STATUS_ALL);
102-
10386
// Create a DomainParticipant with default Qos. The Service will communicate
10487
// only with Clients that join the same domain_id
105-
dds::domain::DomainParticipant participant(arguments.domain_id);
88+
dds::domain::DomainParticipant participant(domain_id);
10689

10790
// Create an instance of the service interface
108-
auto service_impl = std::make_shared<InventoryImpl>(arguments.delay);
91+
auto service_impl = std::make_shared<InventoryImpl>(delay);
10992

11093
// A server provides the execution environment (a thread pool) for one or
11194
// more services
@@ -126,7 +109,38 @@ int main(int argc, char *argv[])
126109
::InventoryServiceService service(service_impl, server, params);
127110

128111
std::cout << "InventoryService running... " << std::endl;
129-
server.run();
112+
server.run(std::chrono::seconds(service_timeout));
113+
}
114+
115+
int main(int argc, char *argv[])
116+
{
117+
using namespace application;
118+
119+
// Parse arguments
120+
auto arguments = parse_arguments(argc, argv, false);
121+
if (arguments.parse_result == ParseReturn::exit) {
122+
return EXIT_SUCCESS;
123+
} else if (arguments.parse_result == ParseReturn::failure) {
124+
return EXIT_FAILURE;
125+
}
126+
127+
// Sets Connext verbosity to help debugging
128+
rti::config::Logger::instance().verbosity(arguments.verbosity);
129+
130+
// To turn on additional logging, include <rti/config/Logger.hpp> and
131+
// uncomment the following line:
132+
// rti::config::Logger::instance().verbosity(rti::config::Verbosity::STATUS_ALL);
133+
134+
try {
135+
run_server(
136+
arguments.domain_id,
137+
arguments.delay,
138+
arguments.service_timeout);
139+
} catch (const std::exception &ex) {
140+
// This will catch DDS exceptions
141+
std::cerr << "Exception in run_server(): " << ex.what() << std::endl;
142+
return EXIT_FAILURE;
143+
}
130144

131145
// Releases the memory used by the participant factory. Optional at
132146
// application exit

examples/connext_dds/remote_procedure_call/c++11/application.hpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ struct ApplicationArguments {
2929
std::string item_name;
3030
unsigned int quantity;
3131
unsigned int delay;
32+
unsigned int service_timeout;
3233
rti::config::Verbosity verbosity;
3334

3435
ApplicationArguments(
@@ -38,13 +39,15 @@ struct ApplicationArguments {
3839
const std::string &item_name_param,
3940
unsigned int quantity_param,
4041
unsigned int delay_param,
42+
unsigned int service_timeout_param,
4143
rti::config::Verbosity verbosity_param)
4244
: parse_result(parse_result_param),
4345
domain_id(domain_id_param),
4446
add(add_param),
4547
item_name(item_name_param),
4648
quantity(quantity_param),
4749
delay(delay_param),
50+
service_timeout(service_timeout_param),
4851
verbosity(verbosity_param)
4952
{
5053
}
@@ -82,6 +85,7 @@ inline ApplicationArguments parse_arguments(int argc, char *argv[], bool client)
8285
unsigned int domain_id = 0;
8386
unsigned int quantity = 1;
8487
unsigned int delay = 0;
88+
unsigned int service_timeout = INT32_MAX;
8589
std::string item_name = "";
8690
bool add = true;
8791
rti::config::Verbosity verbosity(rti::config::Verbosity::EXCEPTION);
@@ -131,6 +135,12 @@ inline ApplicationArguments parse_arguments(int argc, char *argv[], bool client)
131135
|| strcmp(argv[arg_processing], "--delay") == 0)) {
132136
delay = atoi(argv[arg_processing + 1]);
133137
arg_processing += 2;
138+
} else if (
139+
(argc > arg_processing + 1)
140+
&& (strcmp(argv[arg_processing], "-s") == 0
141+
|| strcmp(argv[arg_processing], "--service-timeout") == 0)) {
142+
service_timeout = atoi(argv[arg_processing + 1]);
143+
arg_processing += 2;
134144
} else {
135145
std::cout << "Bad parameter." << std::endl;
136146
show_usage = true;
@@ -147,16 +157,18 @@ inline ApplicationArguments parse_arguments(int argc, char *argv[], bool client)
147157

148158
if (show_usage) {
149159
std::cout << "Usage:\n"\
150-
" -d, --domain <int> Domain ID this application will\n" \
151-
" subscribe in. \n"
152-
" Default: 0\n"\
153-
" -a, --add <item_name> Add an item to the inventory\n"\
154-
" -r, --remove <item_name> Remove an item from the inventory\n"\
155-
" -q, --quantity <int> Number of items to add or remove\n"\
156-
" Default: 1\n"
157-
" -v, --verbosity <int> How much debugging output to show.\n"\
158-
" Range: 0-3 \n"
159-
" Default: 1"
160+
" -d, --domain <int> Domain ID this application will\n" \
161+
" subscribe in. \n"
162+
" Default: 0\n"\
163+
" -a, --add <item_name> Add an item to the inventory\n"\
164+
" -r, --remove <item_name> Remove an item from the inventory\n"\
165+
" -q, --quantity <int> Number of items to add or remove\n"\
166+
" Default: 1\n"
167+
" -s, --service-timeout <int> Numbers of senconds the service will run\n"\
168+
" Default: infinite\n"
169+
" -v, --verbosity <int> How much debugging output to show.\n"\
170+
" Range: 0-3 \n"
171+
" Default: 1"
160172
<< std::endl;
161173
}
162174

@@ -167,6 +179,7 @@ inline ApplicationArguments parse_arguments(int argc, char *argv[], bool client)
167179
item_name,
168180
quantity,
169181
delay,
182+
service_timeout,
170183
verbosity);
171184
}
172185

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************************
2+
(c) 2024 Copyright, Real-Time Innovations, Inc. All rights reserved.
3+
RTI grants Licensee a license to use, modify, compile, and create derivative
4+
works of the Software. Licensee has the right to distribute object form only
5+
for use with RTI products. The Software is provided "as is", with no warranty
6+
of any type, including any warranty for fitness for any purpose. RTI is under
7+
no obligation to maintain or support the Software. RTI shall not be liable for
8+
any incidental or consequential damages arising out of the use or inability to
9+
use the software.
10+
******************************************************************************/
11+
12+
struct Item {
13+
string name;
14+
int64 quantity;
15+
};
16+
17+
struct InventoryContents {
18+
sequence<Item> items;
19+
};
20+
21+
exception UnknownItemError {
22+
string name;
23+
};
24+
25+
@service("DDS")
26+
interface InventoryService {
27+
InventoryContents get_inventory();
28+
void add_item(Item item);
29+
void remove_item(Item item) raises (UnknownItemError);
30+
};

examples/connext_dds/remote_procedure_call/py/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,14 @@ Updated inventory: InventoryContents(items=[Item(name='apples', quantity=100),
104104
For a description of the concurrency model, see the
105105
[Remote Procedure Calls](https://community.rti.com/static/documentation/connext-dds/7.2.0/doc/api/connext_dds/api_python/rpc.html#remote-procedure-calls)
106106
section of the Connext Python API Reference.
107+
108+
## Code Generation
109+
110+
If you want to modify the DDS Service in the idl you will have to re-generate
111+
the service code using **rtiddsgen**
112+
113+
```sh
114+
<install dir>/bin/rtiddsgen -language Python -update typefiles Inventory.idl
115+
```
116+
117+
Where `<install dir>` refers to your RTI Connext installation.

examples/connext_dds/remote_procedure_call/py/inventory_client.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ async def run_client(args):
3131
client = InventoryClient(
3232
participant, "Inventory", max_wait_per_call=dds.Duration(20)
3333
)
34+
35+
# For versions 7.4.0 and below:
3436
await wait_for_service(client)
37+
# For newer versions you can use the following:
38+
# await client.wait_for_service_async(dds.Duration(20))
3539

3640
print("Initial inventory: ", await client.get_inventory())
3741

examples/connext_dds/remote_procedure_call/py/inventory_service.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#
1111

1212
import argparse
13+
import asyncio
14+
import sys
1315
import rti.connextdds as dds
1416
import rti.rpc as rpc
1517
import rti.asyncio
@@ -60,6 +62,13 @@ async def remove_item(self, item: Item):
6062
del self.inventory[item.name]
6163

6264

65+
async def run_service(service: rpc.Service):
66+
try:
67+
await service.run(close_on_cancel=True)
68+
except asyncio.CancelledError:
69+
pass
70+
71+
6372
async def main():
6473
parser = argparse.ArgumentParser(description="Inventory client")
6574
parser.add_argument(
@@ -78,16 +87,30 @@ async def main():
7887
help="Delay in seconds for the add and remove operations (default: 0)",
7988
)
8089

90+
parser.add_argument(
91+
"-s",
92+
"--service-timeout",
93+
type=int,
94+
default=sys.maxsize,
95+
help="Numbers of senconds the service will run (default: infinite)",
96+
)
97+
8198
args = parser.parse_args()
8299

83100
participant = dds.DomainParticipant(args.domain)
84101
service = rpc.Service(InventoryImpl(args.delay), participant, "Inventory")
85102

86-
await service.run()
103+
print("InventoryService running...")
104+
service_task = asyncio.create_task(run_service(service))
105+
106+
await asyncio.sleep(args.service_timeout)
107+
108+
service_task.cancel()
87109

88110

89111
if __name__ == "__main__":
90-
dds.Logger.instance.verbosity = dds.Verbosity.WARNING
112+
# Uncomment this to turn on additional logging
113+
# dds.Logger.instance.verbosity = dds.Verbosity.WARNING
91114
try:
92115
# Run the service until Ctrl-C is pressed
93116
rti.asyncio.run(main())

examples/connext_dds/request_reply/cs/PrimesProgram.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ public static async Task Main(string[] args)
6363
}
6464

6565
// Set up signal handler to Dispose the DDS entities
66-
var cancellationSource = new CancellationTokenSource();
66+
var cancellationSource = arguments.Timeout > 0
67+
? new CancellationTokenSource(TimeSpan.FromSeconds(arguments.Timeout))
68+
: new CancellationTokenSource();
6769
Console.CancelKeyPress += (_, eventArgs) =>
6870
{
6971
Console.WriteLine("Shutting down...");
@@ -100,6 +102,7 @@ private class Arguments
100102
public int Domain { get; set; }
101103
public int N { get; set; } = int.MaxValue;
102104
public int PrimesPerReply { get; set; }
105+
public int Timeout { get; set; }
103106
}
104107

105108
// Uses the System.CommandLine package to parse the program arguments.
@@ -124,7 +127,11 @@ private static Arguments ParseArguments(string[] args)
124127
description: "The number to calculate primes up to (only applicable with --requester)"),
125128
new System.CommandLine.Option<int>(
126129
new string[] { "--primes-per-reply", "-p" },
127-
getDefaultValue: () => 5)
130+
getDefaultValue: () => 5),
131+
new System.CommandLine.Option<int>(
132+
new string[] { "--timeout" },
133+
getDefaultValue: () => 0,
134+
description: "Timeout in seconds to wait for the application to finish (default: infinite)"),
128135
};
129136

130137
rootCommand.Description = "Example RTI Connext Requester and Replier";

tests/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
!*/java/makefile*
2+
!*.csproj
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
cd ${BaseExamplePath}/java
3+
java -Djava.library.path=$NDDSHOME/lib/x64Linux4gcc7.3.0 -cp ".:$NDDSHOME/lib/java/nddsjava.jar" DynamicDataNestedStruct $@
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
######################################################################
2+
#
3+
# (c) Copyright, Real-Time Innovations, 2024.
4+
# All rights reserved.
5+
# No duplications, whole or partial, manual or electronic, may be made
6+
# without express written permission. Any such copies, or
7+
# revisions thereof, must display this notice unaltered.
8+
# This code contains trade secrets of Real-Time Innovations, Inc.
9+
#
10+
######################################################################
11+
12+
# RTI_JAVA_OPTION=-d64
13+
14+
SOURCE_DIR =
15+
16+
TARGET_ARCH=x64Linux4gcc7.3.0
17+
18+
ifndef DEBUG
19+
DEBUG=0
20+
endif
21+
ifeq ($(DEBUG),1)
22+
DEBUG_FLAGS += -g
23+
else
24+
DEBUG_FLAGS =
25+
endif
26+
27+
JAVA_PATH = java
28+
JAVAC_PATH = javac
29+
30+
JAVA_SOURCES = $(SOURCE_DIR)./DynamicDataNestedStruct.java
31+
CLASS_FILES = $(JAVA_SOURCES:%.java=%.class)
32+
33+
RTI_CLASSPATH := $(NDDSHOME)/lib/java/nddsjava.jar
34+
35+
%.class : %.java
36+
$(JAVAC_PATH) $(DEBUG_FLAGS) -classpath ".:$(RTI_CLASSPATH)" $<
37+
38+
all: $(CLASS_FILES)

0 commit comments

Comments
 (0)