@@ -102,38 +102,19 @@ static void sendMsgWithIpc(const celix::LogHelper& logHelper, int qidSender, con
102102 }
103103}
104104
105- static int getConsumer2ProviderChannelId (std::string& scope, std::string& topic) {
106- auto c2pChannel = std::string{" c2p" } + " /" + scope + " /" + topic; // client 2 provider channel
107- auto c2pId = (int )celix_utils_stringHash (c2pChannel.c_str ());
108- return c2pId;
109- }
110-
111- static long getProvider2ConsumerChannelId (std::string& scope, std::string& topic) {
112- auto p2cChannel = std::string{" p2c" } + " /" + scope + " /" + topic; // provider 2 client channel
113- int p2cId = (int )celix_utils_stringHash (p2cChannel.c_str ());
114- return p2cId;
115- }
116-
117105/* *
118106 * A importedCalculater which acts as a pubsub proxy to a imported remote service.
119107 */
120108class ImportedCalculator final : public ICalculator {
121109public:
122- explicit ImportedCalculator (celix::LogHelper _logHelper, int c2pChannelId, int p2cChannelId) : logHelper{std::move (_logHelper)} {
110+ explicit ImportedCalculator (celix::LogHelper _logHelper, long c2pChannelId, long p2cChannelId) : logHelper{std::move (_logHelper)} {
123111 setupMsgIpc (c2pChannelId, p2cChannelId);
124112 }
125113
126- ~ImportedCalculator () noexcept override {
127- // failing al leftover deferreds
128- {
129- std::lock_guard lock{mutex};
130- for (auto & pair : deferreds) {
131- pair.second .fail (celix::rsa::RemoteServicesException{" Shutting down proxy" });
132- }
133- }
134- };
114+ ~ImportedCalculator () noexcept override = default ;
135115
136116 std::shared_ptr<celix::PushStream<double >> result () override {
117+ std::lock_guard lock{mutex};
137118 return stream;
138119 }
139120
@@ -158,6 +139,7 @@ class ImportedCalculator final : public ICalculator {
158139 }
159140
160141 int start () {
142+ std::lock_guard lock{mutex};
161143 ses = psp->createSynchronousEventSource <double >(factory);
162144 stream = psp->createStream <double >(ses, factory);
163145 running.store (true , std::memory_order::memory_order_release);
@@ -176,6 +158,8 @@ class ImportedCalculator final : public ICalculator {
176158 running.store (false , std::memory_order::memory_order_release);
177159 receiveThread.join ();
178160 receiveThread = {};
161+ cleanupDeferreds ();
162+ std::lock_guard lock{mutex};
179163 ses->close ();
180164 ses.reset ();
181165 stream.reset ();
@@ -197,15 +181,30 @@ class ImportedCalculator final : public ICalculator {
197181 }
198182
199183private:
200- void setupMsgIpc (int c2pChannelId, int p2cChannelId) {
184+ void setupMsgIpc (long c2pChannelId, long p2cChannelId) {
185+ logHelper.debug (" Creating msg queue for ImportedCalculator with c2pChannelId=%li and p2cChannelId=%li" ,
186+ c2pChannelId,
187+ p2cChannelId);
201188 int keySender = (int )c2pChannelId;
202189 int keyReceiver = (int )p2cChannelId;
203190 qidSender = msgget (keySender, 0666 | IPC_CREAT);
204191 qidReceiver = msgget (keyReceiver, 0666 | IPC_CREAT);
205192
206193 if (qidSender == -1 || qidReceiver == -1 ) {
207194 throw std::logic_error{" RsaShmClient: Error creating msg queue." };
195+ } else {
196+ logHelper.info (" Created msg queue for ImportedCalculator with qidSender=%i and qidReceiver=%i" ,
197+ qidSender,
198+ qidReceiver);
199+ }
200+ }
201+
202+ void cleanupDeferreds () {
203+ std::lock_guard lock{mutex};
204+ for (auto & pair : deferreds) {
205+ pair.second .tryFail (celix::rsa::RemoteServicesException{" Shutting down proxy" });
208206 }
207+ deferreds.clear ();
209208 }
210209
211210 void receiveMessages () {
@@ -233,9 +232,9 @@ class ImportedCalculator final : public ICalculator {
233232 lock.unlock ();
234233
235234 if (ret.hasError ) {
236- deferred.fail (celix::rsa::RemoteServicesException{ret.errorMsg });
235+ deferred.tryFail (celix::rsa::RemoteServicesException{ret.errorMsg });
237236 } else {
238- deferred.resolve (ret.result );
237+ deferred.tryResolve (ret.result );
239238 }
240239 } catch (const IpcException& e) {
241240 logHelper.error (" IpcException: %s" , e.what ());
@@ -249,6 +248,7 @@ class ImportedCalculator final : public ICalculator {
249248 return ; // no message available (yet)
250249 }
251250 auto event = msg.value ().mtext ;
251+ logHelper.trace (" Received event %f" , event.eventData );
252252
253253 if (event.hasError ) {
254254 logHelper.error (" Received error event %s" , event.errorMsg );
@@ -312,7 +312,7 @@ class ComponentImportRegistration final : public celix::rsa::IImportRegistration
312312 */
313313class CalculatorImportServiceFactory final : public celix::rsa::IImportServiceFactory {
314314public:
315- static constexpr const char * const CONFIGS = " pubsub " ;
315+ static constexpr const char * const CONFIGS = " ipc-mq " ;
316316
317317 explicit CalculatorImportServiceFactory (std::shared_ptr<celix::BundleContext> _ctx) : ctx{std::move (_ctx)}, logHelper{ctx, " celix::rsa::RemoteServiceFactory" } {}
318318 ~CalculatorImportServiceFactory () noexcept override = default ;
@@ -332,10 +332,11 @@ class CalculatorImportServiceFactory final : public celix::rsa::IImportServiceFa
332332
333333private:
334334 std::string createImportedCalculatorComponent (const celix::rsa::EndpointDescription& endpoint) {
335- auto topic = endpoint.getProperties ().get (" endpoint.topic" );
336- auto scope = endpoint.getProperties ().get (" endpoint.scope" );
337- auto c2pChannelId = getConsumer2ProviderChannelId (scope, topic);
338- auto p2cChannelId = getProvider2ConsumerChannelId (scope, topic);
335+ for (auto it : endpoint.getProperties ()) {
336+ logHelper.info (" Endpoint property %s=%s" , it.first .c_str (), it.second .c_str ());
337+ }
338+ auto c2pChannelId = endpoint.getProperties ().getAsLong (" endpoint.client.to.provider.channel.id" , -1 );
339+ auto p2cChannelId = endpoint.getProperties ().getAsLong (" endpoint.provider.to.client.channel.id" , -1 );
339340
340341 auto & cmp = ctx->getDependencyManager ()->createComponent (std::make_unique<ImportedCalculator>(logHelper, c2pChannelId, p2cChannelId));
341342 cmp.createServiceDependency <celix::PromiseFactory>()
@@ -378,7 +379,7 @@ class CalculatorImportServiceFactory final : public celix::rsa::IImportServiceFa
378379 */
379380class ExportedCalculator final {
380381public:
381- explicit ExportedCalculator (celix::LogHelper _logHelper, int c2pChannelId, int p2cChannelId) : logHelper{std::move (_logHelper)} {
382+ explicit ExportedCalculator (celix::LogHelper _logHelper, long c2pChannelId, long p2cChannelId) : logHelper{std::move (_logHelper)} {
382383 setupMsgIpc (c2pChannelId, p2cChannelId);
383384 }
384385
@@ -434,7 +435,10 @@ class ExportedCalculator final {
434435 calculator = calc;
435436 }
436437private:
437- void setupMsgIpc (int c2pChannelId, int p2cChannelId) {
438+ void setupMsgIpc (long c2pChannelId, long p2cChannelId) {
439+ logHelper.debug (" Creating msg queue for ExportCalculator with c2pChannelId=%li and p2cChannelId=%li" ,
440+ c2pChannelId,
441+ p2cChannelId);
438442 // note reverse order of sender and receiver compared to ImportedCalculator
439443 int keySender = (int )p2cChannelId;
440444 int keyReceiver = (int )c2pChannelId;
@@ -443,6 +447,10 @@ class ExportedCalculator final {
443447
444448 if (qidSender == -1 || qidReceiver == -1 ) {
445449 throw std::logic_error{" RsaShmClient: Error creating msg queue." };
450+ } else {
451+ logHelper.info (" Created msg queue for ExportCalculator with qidSender=%i and qidReceiver=%i" ,
452+ qidSender,
453+ qidReceiver);
446454 }
447455 }
448456
@@ -518,7 +526,7 @@ class ComponentExportRegistration final : public celix::rsa::IExportRegistration
518526 */
519527class CalculatorExportServiceFactory final : public celix::rsa::IExportServiceFactory {
520528public:
521- static constexpr const char * const CONFIGS = " pubsub " ;
529+ static constexpr const char * const CONFIGS = " ipc-mq " ;
522530 static constexpr const char * const INTENTS = " osgi.async" ;
523531
524532 explicit CalculatorExportServiceFactory (std::shared_ptr<celix::BundleContext> _ctx) : ctx{std::move (_ctx)},
@@ -544,13 +552,12 @@ class CalculatorExportServiceFactory final : public celix::rsa::IExportServiceFa
544552
545553private:
546554 std::string createExportedCalculatorComponent (const celix::Properties& serviceProperties) {
547- auto topic = serviceProperties.get (" endpoint.topic" );
548- auto scope = serviceProperties.get (" endpoint.scope" );
549- auto c2pChannelId = getConsumer2ProviderChannelId (scope, topic);
550- auto p2cChannelId = getProvider2ConsumerChannelId (scope, topic);
555+ auto c2pChannelId = serviceProperties.getAsLong (" endpoint.client.to.provider.channel.id" , -1 );
556+ auto p2cChannelId = serviceProperties.getAsLong (" endpoint.provider.to.client.channel.id" , -1 );
551557 auto svcId = serviceProperties.get (celix::SERVICE_ID);
552558
553- auto & cmp = ctx->getDependencyManager ()->createComponent (std::make_unique<ExportedCalculator>(logHelper, c2pChannelId, p2cChannelId));
559+ auto & cmp = ctx->getDependencyManager ()->createComponent (
560+ std::make_unique<ExportedCalculator>(logHelper, c2pChannelId, p2cChannelId));
554561
555562 cmp.createServiceDependency <celix::PromiseFactory>()
556563 .setRequired (true )
0 commit comments