Skip to content

Commit 7bd08c6

Browse files
Use generic server interfaces in generated code (#485)
* Refs #23325. Update server header template. Signed-off-by: Miguel Company <[email protected]> * Refs #23325. Update server source template. Signed-off-by: Miguel Company <[email protected]> * Refs #23325. Update server implementation template. Signed-off-by: Miguel Company <[email protected]> * Refs #23325. Update swig template. Signed-off-by: Miguel Company <[email protected]> * Refs #23325. Silence false positive memory leak warnings. Signed-off-by: Miguel Company <[email protected]> --------- Signed-off-by: Miguel Company <[email protected]>
1 parent 8ab13be commit 7bd08c6

File tree

4 files changed

+42
-132
lines changed

4 files changed

+42
-132
lines changed

src/main/java/com/eprosima/fastcdr/idl/templates/TypesSwigInterface.stg

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,15 @@ $endif$
3232

3333
// If using windows in debug, it would try to use python_d, which would not be found.
3434
%begin %{
35+
$if(ctx.thereIsInterface)$
36+
/*
37+
* From: https://github.com/swig/swig/issues/2638
38+
* When a module uses a type in a module that is defined in a different module,
39+
* a false positive memory leak is detected.
40+
* The following line silences this warning.
41+
*/
42+
#define SWIG_PYTHON_SILENT_MEMLEAK
43+
$endif$
3544
#ifdef _MSC_VER
3645
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
3746
#endif
@@ -86,6 +95,7 @@ $endif$
8695
$if(ctx.thereIsInterface)$
8796
%import(module="fastdds") "fastdds/dds/rpc/exceptions/RpcException.hpp"
8897
%import(module="fastdds") "fastdds/dds/rpc/exceptions/RpcOperationError.hpp"
98+
%import(module="fastdds") "fastdds/dds/rpc/interfaces/RpcServer.hpp"
8999

90100
%exception {
91101
try
@@ -386,17 +396,6 @@ $export_list$
386396

387397
%shared_ptr($interface.scopedname$);
388398
$if(!interface.annotatedAsNested)$
389-
%shared_ptr($interface.scopedname$Server);
390-
%extend $interface.scopedname$Server
391-
{
392-
void run()
393-
{
394-
Py_BEGIN_ALLOW_THREADS
395-
self->run();
396-
Py_END_ALLOW_THREADS
397-
}
398-
}
399-
400399
%shared_ptr($interface.scopedname$Server_IServerImplementation);
401400
%shared_ptr($interface.scopedname$ServerImplementation);
402401
%feature("director") $interface.scopedname$ServerImplementation;

src/main/java/com/eprosima/fastdds/idl/templates/ServerHeader.stg

Lines changed: 10 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -51,104 +51,15 @@ $definition_list$
5151

5252
interface(ctx, parent, interface, export_list) ::= <<
5353
$if(!interface.annotatedAsNested)$
54-
/**
55-
* @brief Context for a client request.
56-
*/
57-
struct $interface.name$Server_ClientContext
58-
{
59-
virtual ~$interface.name$Server_ClientContext() = default;
60-
61-
/**
62-
* @brief Get the GUID of the client that made the request.
63-
*
64-
* @return The GUID of the client that made the request.
65-
*/
66-
virtual const eprosima::fastdds::rtps::GUID_t& get_client_id() const = 0;
67-
68-
/**
69-
* @brief Get the locators of the client that made the request.
70-
*
71-
* @return The locators of the client that made the request.
72-
*/
73-
virtual const eprosima::fastdds::rtps::RemoteLocatorList& get_client_locators() const = 0;
74-
};
75-
7654
struct $interface.name$Server_IServerImplementation
7755
{
7856
virtual ~$interface.name$Server_IServerImplementation() = default;
7957

8058
$interface.all_operations:{op | $operation_prototype(op)$}; separator="\n\n"$
8159
};
8260

83-
struct $interface.name$Server
84-
{
85-
virtual ~$interface.name$Server() = default;
86-
87-
/**
88-
* @brief Run the server.
89-
*
90-
* This method starts the server and begins processing requests.
91-
* The method will block until the server is stopped.
92-
*/
93-
virtual void run() = 0;
94-
95-
/**
96-
* @brief Stop the server.
97-
*
98-
* This method stops the server and releases all resources.
99-
* It will cancel all pending requests, and then @c call server_stopped on the request scheduler to let
100-
* it release any resources associated to this server.
101-
*
102-
* When the server has been created with the factory method that receives a @c thread_pool_size argument,
103-
* it will wait for all threads in the pool to finish before returning.
104-
*/
105-
virtual void stop() = 0;
106-
107-
/**
108-
* @brief Perform execution of a client request.
109-
*
110-
* @param request The client request to execute.
111-
*/
112-
virtual void execute_request(
113-
const std::shared_ptr<$interface.name$Server_ClientContext>& request) = 0;
114-
115-
};
116-
117-
struct $interface.name$ServerSchedulingStrategy
118-
{
119-
virtual ~$interface.name$ServerSchedulingStrategy() = default;
120-
121-
/**
122-
* @brief Schedule a request for processing.
123-
*
124-
* This method is called when a request is received and should be processed by the server.
125-
* The implementation should decide how to handle the request, whether to process it immediately,
126-
* or to queue it for later processing.
127-
*
128-
* A call to server->execute_request(request) should eventually be made to process the request.
129-
*
130-
* @note This method is called from the thread that takes requests and input feed values, so it
131-
* should not directly execute the request for operations that have input feed parameters.
132-
*
133-
* @param request The request to schedule.
134-
* @param server The server instance that should process the request.
135-
*/
136-
virtual void schedule_request(
137-
const std::shared_ptr<$interface.name$Server_ClientContext>& request,
138-
const std::shared_ptr<$interface.name$Server>& server) = 0;
139-
140-
/**
141-
* @brief Informs that a server has been stopped and all its requests have been cancelled.
142-
*
143-
* @param server The server instance that has been stopped.
144-
*/
145-
virtual void server_stopped(
146-
const std::shared_ptr<$interface.name$Server>& server) = 0;
147-
148-
};
149-
15061
/**
151-
* @brief Create a $interface.name$Server instance.
62+
* @brief Create a $interface.name$ server instance.
15263
*
15364
* @param part The DomainParticipant to use for the server.
15465
* @param service_name The name of the service.
@@ -157,35 +68,35 @@ struct $interface.name$ServerSchedulingStrategy
15768
* When set to 0, a pool with a single thread will be created.
15869
* @param implementation The implementation of the server interface.
15970
*/
160-
extern eProsima_user_DllExport std::shared_ptr<$interface.name$Server> create_$interface.name$Server(
71+
extern eProsima_user_DllExport std::shared_ptr<eprosima::fastdds::dds::rpc::RpcServer> create_$interface.name$Server(
16172
eprosima::fastdds::dds::DomainParticipant& part,
16273
const char* service_name,
16374
const eprosima::fastdds::dds::ReplierQos& qos,
16475
size_t thread_pool_size,
16576
std::shared_ptr<$interface.name$Server_IServerImplementation> implementation);
16677

16778
/**
168-
* @brief Create a $interface.name$Server instance.
79+
* @brief Create a $interface.name$ server instance.
16980
*
17081
* @param part The DomainParticipant to use for the server.
17182
* @param service_name The name of the service.
17283
* @param qos The QoS settings for the server.
17384
* @param scheduler The request scheduling strategy to use for the server.
17485
* @param implementation The implementation of the server interface.
17586
*/
176-
extern eProsima_user_DllExport std::shared_ptr<$interface.name$Server> create_$interface.name$Server(
87+
extern eProsima_user_DllExport std::shared_ptr<eprosima::fastdds::dds::rpc::RpcServer> create_$interface.name$Server(
17788
eprosima::fastdds::dds::DomainParticipant& part,
17889
const char* service_name,
17990
const eprosima::fastdds::dds::ReplierQos& qos,
180-
std::shared_ptr<$interface.name$ServerSchedulingStrategy> scheduler,
91+
std::shared_ptr<eprosima::fastdds::dds::rpc::RpcServerSchedulingStrategy> scheduler,
18192
std::shared_ptr<$interface.name$Server_IServerImplementation> implementation);
18293
$endif$
18394
>>
18495

18596
operation_prototype(op) ::= <<
18697
$if(op.annotationFeed)$
18798
virtual void $op.name$(
188-
const $interface.name$Server_ClientContext& info,
99+
const eprosima::fastdds::dds::rpc::RpcRequest& info,
189100
$if(op.parameters)$
190101
$operation_parameters(op.parameters)$,
191102
$endif$
@@ -194,18 +105,18 @@ $else$
194105
$if(op.outputparam)$
195106
virtual $paramRetType(op.outTypeCode)$ $op.name$(
196107
$if(op.inputparam)$
197-
const $interface.name$Server_ClientContext& info,
108+
const eprosima::fastdds::dds::rpc::RpcRequest& info,
198109
$operation_parameters(op.inputparam)$) = 0;
199110
$else$
200-
const $interface.name$Server_ClientContext& info) = 0;
111+
const eprosima::fastdds::dds::rpc::RpcRequest& info) = 0;
201112
$endif$
202113
$elseif(op.inputparam)$
203114
virtual $paramRetType(op.rettype)$ $op.name$(
204-
const $interface.name$Server_ClientContext& info,
115+
const eprosima::fastdds::dds::rpc::RpcRequest& info,
205116
$operation_parameters(op.inputparam)$) = 0;
206117
$else$
207118
virtual $paramRetType(op.rettype)$ $op.name$(
208-
const $interface.name$Server_ClientContext& info) = 0;
119+
const eprosima::fastdds::dds::rpc::RpcRequest& info) = 0;
209120
$endif$
210121
$endif$
211122
>>

src/main/java/com/eprosima/fastdds/idl/templates/ServerImplementation.stg

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ $endif$
6060
operation_implementation(interface, op) ::= <<
6161
$if(op.annotationFeed)$
6262
void $op.name$(
63-
const $interface.name$Server_ClientContext& info,
63+
const eprosima::fastdds::dds::rpc::RpcRequest& info,
6464
$if(op.parameters)$
6565
$operation_parameters(op.parameters)$,
6666
$endif$
@@ -72,10 +72,10 @@ $else$
7272
$paramRetType(op.rettype)$ $op.name$(
7373
$endif$
7474
$if(op.inputparam)$
75-
const $interface.name$Server_ClientContext& info,
75+
const eprosima::fastdds::dds::rpc::RpcRequest& info,
7676
$operation_parameters(op.inputparam)$) override
7777
$else$
78-
const $interface.name$Server_ClientContext& info) override
78+
const eprosima::fastdds::dds::rpc::RpcRequest& info) override
7979
$endif$
8080
$endif$
8181
{

src/main/java/com/eprosima/fastdds/idl/templates/ServerSource.stg

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ namespace frpc = eprosima::fastdds::dds::rpc;
7777
namespace frtps = eprosima::fastdds::rtps;
7878

7979
class $interface.name$ServerLogic
80-
: public $interface.name$Server
80+
: public frpc::RpcServer
8181
, public std::enable_shared_from_this<$interface.name$ServerLogic>
8282
{
8383
using RequestType = $interface.name$_Request;
@@ -104,9 +104,9 @@ public:
104104
fdds::DomainParticipant& part,
105105
const char* service_name,
106106
const fdds::ReplierQos& qos,
107-
std::shared_ptr<$interface.name$ServerSchedulingStrategy> scheduler,
107+
std::shared_ptr<frpc::RpcServerSchedulingStrategy> scheduler,
108108
std::shared_ptr<$interface.name$Server_IServerImplementation> implementation)
109-
: $interface.name$Server()
109+
: frpc::RpcServer()
110110
, participant_(part)
111111
, request_scheduler_(scheduler)
112112
, implementation_(std::move(implementation))
@@ -206,7 +206,7 @@ public:
206206
}
207207

208208
void execute_request(
209-
const std::shared_ptr<$interface.name$Server_ClientContext>& request) override
209+
const std::shared_ptr<frpc::RpcRequest>& request) override
210210
{
211211
auto ctx = std::dynamic_pointer_cast<RequestContext>(request);
212212
if (ctx)
@@ -245,7 +245,7 @@ private:
245245

246246
$interface.all_operations:{op | $operation_declarations(op)$}; separator="\n\n"$
247247

248-
struct RequestContext : $interface.name$Server_ClientContext
248+
struct RequestContext : frpc::RpcRequest
249249
{
250250
RequestType request;
251251
frpc::RequestInfo info;
@@ -374,7 +374,7 @@ $endif$
374374
};
375375

376376
struct ThreadPool
377-
: public $interface.name$ServerSchedulingStrategy
377+
: public frpc::RpcServerSchedulingStrategy
378378
{
379379
ThreadPool(
380380
$interface.name$ServerLogic& server,
@@ -391,7 +391,7 @@ $endif$
391391
{
392392
while (!finished_)
393393
{
394-
std::shared_ptr<$interface.name$Server_ClientContext> req;
394+
std::shared_ptr<frpc::RpcRequest> req;
395395
{
396396
std::unique_lock<std::mutex> lock(mtx_);
397397
cv_.wait(lock, [this]()
@@ -418,8 +418,8 @@ $endif$
418418
}
419419

420420
void schedule_request(
421-
const std::shared_ptr<$interface.name$Server_ClientContext>& req,
422-
const std::shared_ptr<$interface.name$Server>& server) override
421+
const std::shared_ptr<frpc::RpcRequest>& req,
422+
const std::shared_ptr<frpc::RpcServer>& server) override
423423
{
424424
static_cast<void>(server);
425425

@@ -432,7 +432,7 @@ $endif$
432432
}
433433

434434
void server_stopped(
435-
const std::shared_ptr<$interface.name$Server>& server) override
435+
const std::shared_ptr<frpc::RpcServer>& server) override
436436
{
437437
static_cast<void>(server);
438438

@@ -457,7 +457,7 @@ $endif$
457457
$interface.name$ServerLogic& server_;
458458
std::mutex mtx_;
459459
std::condition_variable cv_;
460-
std::queue<std::shared_ptr<$interface.name$Server_ClientContext\>> requests_;
460+
std::queue<std::shared_ptr<frpc::RpcRequest\>> requests_;
461461
bool finished_{ false };
462462
std::vector<std::thread> threads_;
463463
};
@@ -526,16 +526,16 @@ $endif$
526526
fdds::GuardCondition finish_condition_;
527527
std::mutex mtx_;
528528
std::map<frtps::SampleIdentity, std::shared_ptr<RequestContext\>> processing_requests_;
529-
std::shared_ptr<$interface.name$ServerSchedulingStrategy> request_scheduler_;
529+
std::shared_ptr<frpc::RpcServerSchedulingStrategy> request_scheduler_;
530530
std::shared_ptr<$interface.name$Server_IServerImplementation> implementation_;
531531

532532
};
533533

534534
struct $interface.name$ServerProxy
535-
: public $interface.name$Server
535+
: public frpc::RpcServer
536536
{
537537
$interface.name$ServerProxy(
538-
std::shared_ptr<$interface.name$Server> impl)
538+
std::shared_ptr<frpc::RpcServer> impl)
539539
: impl_(std::move(impl))
540540
{
541541
}
@@ -559,19 +559,19 @@ struct $interface.name$ServerProxy
559559
}
560560

561561
void execute_request(
562-
const std::shared_ptr<$interface.name$Server_ClientContext>& request) override
562+
const std::shared_ptr<frpc::RpcRequest>& request) override
563563
{
564564
impl_->execute_request(request);
565565
}
566566

567567
private:
568568

569-
std::shared_ptr<$interface.name$Server> impl_;
569+
std::shared_ptr<frpc::RpcServer> impl_;
570570
};
571571

572572
} // namespace detail
573573

574-
std::shared_ptr<$interface.name$Server> create_$interface.name$Server(
574+
std::shared_ptr<eprosima::fastdds::dds::rpc::RpcServer> create_$interface.name$Server(
575575
eprosima::fastdds::dds::DomainParticipant& part,
576576
const char* service_name,
577577
const eprosima::fastdds::dds::ReplierQos& qos,
@@ -583,11 +583,11 @@ std::shared_ptr<$interface.name$Server> create_$interface.name$Server(
583583
return std::make_shared<detail::$interface.name$ServerProxy>(ptr);
584584
}
585585

586-
std::shared_ptr<$interface.name$Server> create_$interface.name$Server(
586+
std::shared_ptr<eprosima::fastdds::dds::rpc::RpcServer> create_$interface.name$Server(
587587
eprosima::fastdds::dds::DomainParticipant& part,
588588
const char* service_name,
589589
const eprosima::fastdds::dds::ReplierQos& qos,
590-
std::shared_ptr<$interface.name$ServerSchedulingStrategy> scheduler,
590+
std::shared_ptr<eprosima::fastdds::dds::rpc::RpcServerSchedulingStrategy> scheduler,
591591
std::shared_ptr<$interface.name$Server_IServerImplementation> implementation)
592592
{
593593
auto ptr = std::make_shared<detail::$interface.name$ServerLogic>(

0 commit comments

Comments
 (0)