celix-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [celix] pnoltes commented on a change in pull request #310: Adds initial impl for the C++ headers only wrappers
Date Sun, 21 Feb 2021 16:19:28 GMT

pnoltes commented on a change in pull request #310:
URL: https://github.com/apache/celix/pull/310#discussion_r579829832



##########
File path: examples/celix-examples/services_example_cxx/src/DynamicConsumerBundleActivator.cc
##########
@@ -0,0 +1,159 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <utility>
+
+#include "celix/IShellCommand.h"
+#include "celix/BundleActivator.h"
+#include "examples/ICalc.h"
+
+namespace /*anon*/ {
+
+    class DynamicConsumer {
+    public:
+        explicit DynamicConsumer(int _id) : id{_id} {}
+
+        ~DynamicConsumer() {
+            stop();
+        }
+
+        void start() {
+            std::lock_guard<std::mutex> lck{mutex};
+            calcThread = std::thread{&DynamicConsumer::run, this};
+        }
+
+        void stop() {
+            active = false;
+            if (calcThread.joinable()) {
+                calcThread.join();
+                auto msg = std::string{"Destroying consumer nr "} + std::to_string(id) +
"\n";
+                std::cout << msg;
+            }
+        }
+
+        void setCalc(std::shared_ptr<examples::ICalc> _calc, const std::shared_ptr<const
celix::Properties>& props) {
+            std::lock_guard<std::mutex> lck{mutex};
+            if (_calc) {
+                calc = std::move(_calc);
+                svcId = props->getAsLong(celix::SERVICE_ID, -1);
+                ranking = props->getAsLong(celix::SERVICE_RANKING, 0);
+
+                std::string msg = "Setted calc svc for consumer nr " +  std::to_string(id);
+                msg += ". svc id is  "  + std::to_string(svcId);
+                msg += ", and ranking is " + std::to_string(ranking);
+                msg += "\n";
+                std::cout << msg;
+            } else {
+                std::string msg = "Resetting calc svc for consumer " + std::to_string(id)
 + " to nullptr\n";
+                std::cout << msg;
+
+                calc = nullptr;
+                svcId = -1;
+                ranking = 0;
+            }
+        }
+    private:
+        void run() {
+            std::unique_lock<std::mutex> lck{mutex,  std::defer_lock};
+            int count = 1;
+            while (active) {
+                lck.lock();
+                auto localCalc = calc;
+                lck.unlock();
+
+                /*
+                 * note it is safe to use the localCalc outside a mutex,
+                 * because the shared_prt count will ensure the service cannot be unregistered
while in use.
+                 */
+                if (localCalc) {
+                    auto msg = std::string{"Calc result is "} + std::to_string(localCalc->calc(count))
+ "\n";

Review comment:
       Merged with cout. Although I did not use this originally because print line messages
with cout can get mangled if multiple threads are using cout. 




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



Mime
View raw message