You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@celix.apache.org by "pnoltes (via GitHub)" <gi...@apache.org> on 2023/08/29 18:08:28 UTC
[PR] Feature/599 provide and use c service in rust (celix)
pnoltes opened a new pull request, #627:
URL: https://github.com/apache/celix/pull/627
# Intro
This PR completes the Rust feasibility issue (#599) by providing a proof of concept (PoC) implementation for Apache Celix with Rust.
I invested more time in this than I had initially planned. However, as a result, this PoC IMO demonstrates nicely the feasibility of integrating Rust support into Apache Celix.
It's good to highlight that this implementation serves as a proof of concept and is not ready for production. The code lacks consistent error checks, documentation, unit tests, and is not feature-complete.
# Changes
In this PR, the following are introduced:
- A `celix` crate, which builds upon the `celix_bindings` crate, offering a Rust-native API for:
- `Erno` enum module
- `BundleActivator` module
- `LogHelper` module
- `BundleContext` module
- The `BundleContext` module provides the following features:
- Logging functions
- Service registration builder
- Service use builder
- Service tracker builder
- `LogHelper` uses a service tracker to fetch an on-demand C `log_service`.
- An updated Rust "hello world" bundle that utilizes the BundleActivator
- A Rust Shell API crate that:
- Introduces a `RustShellCommand` type.
- A Rust Shell command bundle that:
- Utilizes the `BundleActivator`
- Supplies both a C shell command and a Rust shell command (Note: The Rust shell command serves merely as a demonstration and is not actively utilized)
- Uses the `BundleContext::use_service` to access both the Rust and C shell commands
The `rust_shell_cnt` Apache Celix container/executable can be used to try-out Rust integration. With the `query -v` command it should give a nice overview of provided and used service in Rust.
# Interesting Observations
Rust's templating system, especially its type inference capabilities, is quite powerful. However, working with templates and traits can be - based on my experience - complex. The is mainly because traits aren't complete types. As a result, template methods cannot seamlessly move a provided trait type from a Box (pointer) to a different container type, like Arc (atomic reference count (shared_ptr)). My assumption is that this constraint is because of potential slicing and memory allocation/deallocation challenges. But, I am still learning the ins and outs of Rust, so maybe more is possible. Consequently, for this PoC, I decided to make type containers (e.g., Box, Arc, Rc) part of the service type. As result, from a Rust perspective, one does not register a 'RustShellCommand' service type but an `Arc<RustShellCommand>` service type.
While integrating Rust support into Apache Celix is feasible, I'm uncertain whether it's appropriate to include it directly within the Apache Celix main git repository, especially when leveraging CMake Corrosion. Though this method functions, in my experience is does not really integrate well with an IDE.
Lastly, Rust, would arguably be a better fit, if Apache Celix could support both Static Bundles and Shared Object Bundles, in addition to Zip Bundles (#94).
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `celix_bindings` error.
However, removing `extern crate bindgen;` in build.rs will not cause any errors.
I also found that it is claimed that "extern crate is no longer needed in 99% of circumstances"(https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html).
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `rust_shell_api` error.
However, removing `extern crate bindgen;` in build.rs will not cause any errors.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328037813
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
It is indeed an edition issue:
https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field
https://doc.rust-lang.org/edition-guide/editions/
As a new project, I think we shall follow the upstream advice to use 2021 edition.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328037813
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
It is indeed an edition issue: https://doc.rust-lang.org/cargo/reference/manifest.html#the-edition-field
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328043877
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -17,23 +17,37 @@
* under the License.
*/
-extern crate bindgen;
-
+use std::env;
use std::error::Error;
-use std::path::PathBuf;
use std::fs::File;
use std::io::{self, BufRead};
-use std::env;
+use std::path::PathBuf;
-fn print_include_paths() -> Result<Vec<String>, Box<dyn Error>> {
- let build_dir = PathBuf::from(env::var("CORROSION_BUILD_DIR").unwrap());
- let include_path_file = build_dir.join("include_paths.txt");
+fn open_include_paths_file() -> Result<File, Box<dyn Error>> {
+ let file: File;
- //let include_path_file = Path::new("include_paths.txt");
- let file = File::open(&include_path_file)?;
+ let corrosion_build_dir = env::var("CORROSION_BUILD_DIR");
+ if corrosion_build_dir.is_ok() {
+ let build_dir = PathBuf::from(corrosion_build_dir.unwrap());
+ let include_path_file = build_dir.join("include_paths.txt");
+ file = File::open(&include_path_file)?;
+ } else {
+ println!("include_paths.txt not found in CORROSION_BUILD_DIR. Failing back to CELIX_RUST_INCLUDE_PATHS_FILE env value");
Review Comment:
If the message expresses the real intention, then the currently implementation, which fails back to `CELIX_RUST_INCLUDE_PATHS_FILE` if `CORROSION_BUILD_DIR` is not **set**, is wrong.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "kulst (via GitHub)" <gi...@apache.org>.
kulst commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2110255869
Two weeks have passed and I wanted to share again my progress :)
**BundleContext lifetime**
Another way I came up with is to hand out a BundleContext to the bundle and having an Arc<(Condvar, Mutex<Bool>)> flag in the BundleContext (not accessible by the BundleContexts API). When the BundleContext is dropped, the flag is set and the Condvar is notified.
A copy of that Arc is stored in the internal part of the activator. When the stop/delete function of the bundle is invoked the flag is checked periodically if it is set. By using the Condvar we can block between these checks (and also log an error similar to the cpp API).
However I postponed this topic a little bit as I think there are a few feasible solutions. We can later review these solutions and settle on one of them.
**Services**
The next topic I wanted to approach is services. When using a service a bundle calls the function of another bundle. Both bundles need to agree upon the the API of the service and also on the ABI of the service.
In my eyes the Celix Rust API should be safe to use for the user. This means it should not be possible to cause undefined behaviour without using unsafe code in Rust. But we can not enforce during compilation that the API and ABI of the service we want to use is the same as we think it is.
So I think we have two options here:
1. Using a service is inherently unsafe and invariants must be upheld by the user. For C and C++ services this will probably be the only solution, so using these services from Rust will allways be unsafe. However for Rust services I think there is another way!
2. Checking ABI and API compatibility at runtime. If we can achieve this, using a service is safe, incompatible ABI/API would not cause undefined behaviour, returning an Error would be enough.
** How can we achieve ABI and API checks at runtime?**
There is one Rust crate that already tries to deal with this issue. Only a very small part of the Rust API is already stable. Most part of the API is allowed to change between compiler versions but also between different compiler runs.
[abi_stable](https://docs.rs/abi_stable/latest/abi_stable/) provides ABI stability by transforming Rust types with unstable ABI into types with a stable ABI (#[repr(C)]) types. It utilizes the Rust procedural macro system for this which can transform abstract syntax trees into other abstract syntax trees.
To get API stability the crate provides type information of these types, which can be checked at runtime.
To make sure that the API and ABI of the abi_stable crate is compatible a static variable is provided which can also be checked against at runtime.
I think we can utilize this crate to provide safe services.
** How could this look like?**
A service would be an object that implements a trait (the service interface). This is similar to C++ services. The trait is defined in an Service API crate.
The bundle that provides the service statically links against this API crate. When registering this service a pointer to the bundle-static type information and abi_stable library information is stored in the framework (for that it is necessary to implement one or two functions that are only usable internally by the Celix Rust API).
When another bundle now wants to use the service it must statically link against the API crate as well. When using the service it is checked that the type information and abi_stable library information are compatible.
** Additional opportunities**
It would be great if we could use Rust services from C and C++. For this the Service API crate must export some C/C++ header files. Luckily there is a crate that already does this: [cglue](https://docs.rs/cglue/latest/cglue/). On top of that it is compatible and works together with abi_stable.
This makes it possible to easily create services that are usable from C, C++ and Rust and provide additional guaranties.
**Final hurdles**
I would like to rewrite the POC in the next step to make sure all of this works. However there is one problem I could not solve yet:
What if a service of a bundle provides a 'static reference in its interface? The reference could point to 'static data of the bundle. After unloading the providing bundle this reference would be invalid.
There should be a way to constrain the lifetimes to the lifetime of the service use callback.
However I do not know how yet.
Maybe there is a way by checking the service API with an additional proc macro.
**Summary**
Again a lot of text. I hope you are fine with documenting my thoughts and progress here. There are a lot of topics that need to be considered. At the moment Rust is not perfectly well suited for dynamic linking. Its mostly unstable ABI makes it necessary to heavily use the macro system to provide a safe interface. If you want to have all of the Rust safety guaranties as well, a lot of possibilites need to be considered.
But I still think it is possible and I will continue working on it.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2041540655
Sound like a plan :+1: .
> For now I would start with making myself more familiar with Celix. I was already able to develop a basic understanding of most of the concepts of Celix. But I definitely need to further enhance this. I will start by installing Celix and trying to get your Rust examples to work, lets see if I can also make it work with the latest version 2.4.
>
Trying Apache Celix using version 2.4 is fine, but generally speaking the master branch is also quite stable for exploring Apache Celix.
1 note: We are currently working towards a Apache Celix 3.0.0, so a major update. I expect this
has no real impact on learning Apache Celix, because a major part of the backwards incompatible updates focus on dropping older APIs, which are not or not well documented anyways. For a complete overview of the backwards incompatible changes see the top level `CHANGES.md` file.
> The next step would be to get a better understanding of the invariants that need to be upheld when using the C API. I saw, that a lot of these are already documented, but I assume there are more and maybe also implicit ones. When this is done, the Rust API can be designed. Of course I want to leverage the Rust type system to make sure that the invariants of the C API are upheld. Your POC is also a great starting point.
> For now I think this is already a lot to do. Let's see where this leads me. :) In any case, an ASF Contributor Agreement will not be an obstacle.
Great to hear, when this is needed I will give an additional trigger.
> Topics I stumbled upon but which I have put in my backlog for now:
>
> **Allocation:** by default allocating and deallocating on different sides of the ffi might lead to undefined behaviour. There are several possible solutions to this
>
> 1. implementing a (global) allocator in Rust that delegates to the Celix allocator
>
> 2. making sure that both happens on the same side of the FFI by the Rust type system
>
>
> **Type conversion:** There are various types in the C API that are similar to Rust types but can not be used as smoothly without further adjustments on the Rust side. For example the `array_list` type is similar to a vector (with some additions).
>
> 1. So one solution to use such a type smoothly in Rust, you basically would have to reimplement the interface of the `std::vec` type.
>
> 2. Another solution is to just copy the elements to the corresponding type before/after passing the ffi boundary. This might be inefficient however.
>
> 3. A third solution might only be possible when allocation and deallocation happen on the same side of the ffi boundary. I think it could then be possible to create a `std::vec` from the internals of an `array_list` without another allocation just by using the pointer, the size and the capacity an `array_list` already provides. Together with the additions of an `array_list` this could be packaged in a simple wrapper type.
Good to hear, you are already seeing some topics and I think the topics are a good starting point. I was not aware that a separate way of mem allocation was needed/desired. But as stated, I am not a Rust expert, so help is welcome :)
I will also try to eventual get this PR merged. Not as a stable or even completely usable addition, but then we at least have a Rust starting point in the master branch. But I am currently focusing on something else, so I am not sure yet when I have to pick up this PR.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "kulst (via GitHub)" <gi...@apache.org>.
kulst commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2076589287
I wanted to give a little update on my progress :)
I was able to get your POC running with the latest master branch. Only some small changes were necessary. However there were some error messages I could track down to filter compilation. In `celix_version_parse` "Invalid version component" gets logged. I do not know if this is intentional.
I also made some progress towards the API and wanted to share my thoughts.
I heavily thought about the I think most important invariant. `celix_bundle_context_t` (and all other framework objects) must not be used when the Bundle is stopped. From my point of view this means that the activator must invalidate all Framework objects that are floating around.
**POC approach using Weak and Arc**
In the POC I think this was tried to achieve by using `std::sync::Weak` pointers. However I am not sure if this is entirely sound. For example when building the Servicetracker we upgrade the `Weak` to an `Arc`. What happens if this is done in another thread and immediately after upgrading the `Weak` and before actually tracking the service the bundle gets stopped and destroyed. I think we would have a use after free situation here.
**Approach using Arc<Mutex<Option<T>>>**
So I thought about a solution having two types both storing `Arc<Mutex<Option<*mut celix_bundle_context>>>`. One type is private to the library and invalidates each `celix_bundle_context_t` simply by taking it out of the `Option` when the bundle gets stopped. The other type is public with a private implementation and uses the stored `celix_bundle_context_t` as long as it is available. With this solution the whole API of the `PublicCelixBundleContext` would always return a `Result<T,E>`. Also for each access there is additional locking necessary because of the `Mutex`.
Then I thought about using lifetimes. The problem with lifetimes is that they need a scope where the Owner lifes. But we have two (four) functions that are called after each other from the framework. The compiler does not know that these are called one after another. Also we convert the `Activator` to a raw pointer. There do no lifetimes exist for raw pointers. So what can we do about it?
**First lifetime approach: Static services**
The framework provides the `celix_bundle_context_t` in each of the four functions. We could now create a `BundleContext` in the start function from it and store it in the activator. The library user could now get a `&BundleContext`. During the start function the user could register services, track services and so on with it. This can be also stored in the activator. However he can not create a new thread with it that outlives the start function. This is because the `&BundleContext` has a non-static lifetime.
In the stop function the user simply takes its user data from the activator and undos what he did in the start function.
Overall this can only be used for static handling of services.
**Second lifetime approach: Spawn a thread**
My second approach is to just spawn a thread in the start function and join it in the stop function. By this we get a scope that lifes from start to stop. We send the created `BundleContext` into the thread but also here we only provide the user a `&BundleContext`. Also here the user can not spawn thread that outlive the scope. But as the scope is from start to stop it is possible to create threads that also live as long.
In the stop function we signal the thread that it must stop and join it. The user is responsible to make sure the thread stops.
With this approach we do not need to store user data in the activator. We simply put a `JoinHandle` there to join it and the `Sender` to signal the thread it must stop.
Overall this method is suited for static services but dynamic services as well.
In the following repository I show how I think this could look like:
https://github.com/kulst/celix_rust_test
I hope I was able to make everything clear to you.
I would be glad to get some feedback, to know if I am on the right way.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-1724716479
> I think it is maybe better to merge this PR after 2.4.0. Then some additional work can be done to prevent/refactor some strange constructions (e.g. the log_helper <-> service tracker).
I agree. Improving the C implementation and the Rust design at the same time may produce the best results.
For a stable 2.4.0, we'd better leave the current C implementation untouched.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-1712789987
I'll finish the review before Wednesday.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1329162954
##########
misc/experimental/rust/rust_shell_api/src/lib.rs:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+
+extern crate celix;
+
+use celix::Error;
+pub const COMMAND_NAME: &str = "command.name";
+pub const COMMAND_USAGE: &str = "command.usage";
+pub const COMMAND_DESCRIPTION: &str = "command.description";
+
+#[doc = "A trait to implement a Celix Shell Command"]
+pub trait RustShellCommand {
+ fn execute_command(&self, command_line: &str) -> Result<(), Error>;
Review Comment:
This needs IMO some more experimenting and/or discussion.
But a `&self` argument - so not mutable - in a (service) trait can be used to change a service state, but it requires the the implementing object uses a Mutex to change the state.
An other option could be to register a services as Arc<Mutex<T>> or maybe require that a service template argument is `Sync`.
But I think this is something to get more clear later.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `rust_shell_api` error.
However, removing `extern crate bindgen;` in build.rs will not cause any errors.
I also found that it is claimed that "extern crate is no longer needed in 99% of circumstances"(https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html).
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328054070
##########
misc/experimental/rust/celix_bindings/src/celix_bindings.h:
##########
@@ -30,3 +30,6 @@
#include "celix_framework.h"
#include "celix_framework_factory.h"
#include "celix_framework_utils.h"
+
+#include "celix_shell_command.h"
+#include "celix_log_service.h"
Review Comment:
Now all components (including `framework` and `utils`) are optional, these headers may be missing.
For Conan, intra-package and inter-package dependency deduction work like charm. But we need to think about how to make Cargo and Conan work together.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1317614018
##########
misc/experimental/rust/CMakeLists.txt:
##########
@@ -26,13 +26,12 @@ if (CELIX_RUST_EXPERIMENTAL)
FetchContent_MakeAvailable(Corrosion)
Review Comment:
I did a revert on this and merged with #634 instead for the updated `cmake_minimum_required` call
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2111030126
> Two weeks have passed and I wanted to share again my progress :)
>
> **BundleContext lifetime** Another way I came up with is to hand out a BundleContext to the bundle and having an Arc<(Condvar, Mutex)> flag in the BundleContext (not accessible by the BundleContexts API). When the BundleContext is dropped, the flag is set and the Condvar is notified. A copy of that Arc is stored in the internal part of the activator. When the stop/delete function of the bundle is invoked the flag is checked periodically if it is set. By using the Condvar we can block between these checks (and also log an error similar to the cpp API).
>
This is indeed close to what C++ is doing and that case a `weak_ptr` is used during bundle stop a runtime check and wait is made to ensure there are no users of the BundleContext anymore.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327933445
##########
misc/experimental/rust/celix/Cargo.toml:
##########
@@ -0,0 +1,28 @@
+# 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.
+
+[package]
+name = "celix"
+version = "0.0.1"
+
+[dependencies]
+celix_bindings = { path = "../celix_bindings" }
+
+[lib]
+name = "celix"
+path = "src/lib.rs"
+crate-type = ["rlib"]
Review Comment:
Now it's OK to have static libcelix.
Considering its size, 200KB for x64 release build, it might be preferable to built as shared object.
But unfortunately I've not found any way to make BUILD_RPATH work for Corrosion, without which it is quite inconvenient to run the resulting binary in the build tree.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328469387
##########
misc/experimental/rust/shell_command_bundle/src/lib.rs:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ */
+
+use std::ffi::c_char;
+use std::ffi::c_void;
+use std::sync::Arc;
+
+use celix::{BundleActivator, LogHelper};
+use celix::BundleContext;
+use celix::Error;
+use rust_shell_api::RustShellCommand;
+
+use celix_bindings::celix_shell_command_t;
+use celix_bindings::FILE;
+
+struct CShellCommandImpl {
+ ctx: Arc<BundleContext>,
+}
+
+impl CShellCommandImpl {
+ fn new(ctx: Arc<BundleContext>) -> Self {
+ ctx.log_info("Shell Command created");
+ CShellCommandImpl { ctx }
+ }
+
+ extern "C" fn call_execute_command(
+ handle: *mut c_void,
+ command_line: *const c_char,
+ _out_stream: *mut FILE,
+ _error_stream: *mut FILE,
+ ) -> bool {
+ if handle.is_null() || command_line.is_null() {
+ return false;
+ }
+ unsafe {
+ let obj = &mut *(handle as *mut CShellCommandImpl);
+ let str_command_line = std::ffi::CStr::from_ptr(command_line).to_str();
+ if str_command_line.is_err() {
+ return false;
+ }
+ obj.execute_command(str_command_line.unwrap());
+ }
+ true
+ }
+
+ fn execute_command(&mut self, command_line: &str) {
+ self.ctx.log_info(format!("Execute command: \"{}\"", command_line).as_str());
+ }
+}
+
+//temporary, should be moved in a separate API crate
+
+struct RustShellCommandImpl {
+ ctx: Arc<BundleContext>,
+}
+
+impl RustShellCommandImpl {
+ fn new(ctx: Arc<BundleContext>) -> Self {
+ ctx.log_info("Rust Shell Command created");
+ RustShellCommandImpl { ctx }
+ }
+}
+
+impl RustShellCommand for RustShellCommandImpl {
+ fn execute_command(&self, command_line: &str) -> Result<(), Error> {
+ self.ctx
+ .log_info(format!("Execute command: {}.", command_line).as_str());
+ Ok(())
+ }
+}
+
+struct ShellCommandActivator {
+ ctx: Arc<BundleContext>,
+ log_helper: Arc<LogHelper>,
+ shell_command_provider: CShellCommandImpl,
+ registrations: Vec<celix::ServiceRegistration>,
+}
+
+impl ShellCommandActivator {
+
+ fn register_services(&mut self) -> Result<(), Error> {
+ //Register C service registered as value
+ let registration = self.ctx.register_service()
+ .with_service(celix_shell_command_t {
+ handle: &mut self.shell_command_provider as *mut CShellCommandImpl as *mut c_void,
+ executeCommand: Some(CShellCommandImpl::call_execute_command),
+ })
+ .with_service_name("celix_shell_command")
Review Comment:
If `with_service_name` comes before `with_service`, an dynamic memory allocation will be avoided.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `rust_shell_api` error.
However, removing `extern crate bindgen;` in build.rs will not cause any errors.
I also found that it is claimed that "extern crate is no longer needed in 99% of circumstances"(https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html).
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328049324
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -44,19 +58,15 @@ fn main() {
println!("cargo:info=Start build.rs for celix_bindings");
let include_paths = print_include_paths().unwrap();
- let mut builder = bindgen::Builder::default()
- .header("src/celix_bindings.h");
+ let mut builder = bindgen::Builder::default().header("src/celix_bindings.h");
// Add framework and utils include paths
for path in &include_paths {
builder = builder.clang_arg(format!("-I{}", path));
Review Comment:
As mentioned earlier, `-I` is not enough, we may need `-D` and other compile options (like `-pthread`).
It is hard to tell whether it matters for now without checking each target's `INCLUDE_DIRECTORIES`, `COMPILE_DEFINITIONS` and `COMPILE_OPTIONS`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2110990398
> Two weeks have passed and I wanted to share again my progress :)
Very good, keep up the pace :)
I did have a look at the implementation, but was struggling to find some spare time.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2037862920
> Hi, awesome to see Celix is making progress towards Rust π
>
> Are you open to contributions on this topic? Although I am not completely familiar with the internals of Celix yet, I would love to wrap my head around your concepts and see if we can create a nice and secure Rust API for Celix.
Hi,
Firstly thanks for the interest! Additional help is always welcome :smile: .
Although this pull request has remained dormant for some time, I still believe that introducing Rust bindings, alongside the existing C++ bindings, would greatly benefit Apache Celix.
Please note that Rust support, as well as this pull request, is still experimental. Rust is a new for me (and, I believe, for the other committers as well), and we are all in the process of understanding how Rust concepts can be integrated into Apache Celix, or more broadly, how they fit within a dynamic, in-process, service framework.
Another point to consider is that, for now, we have decided to develop Rust support on top of the C framework, thereby leveraging the existing and stable C framework. Theoretically, this approach could be reversed, but in my opinion, that is not our current objective.
A good way to start would be to try out Apache Celix and, specifically, this branch. If you have any questions, feel free to ask them here or on the Apache Celix dev mailing list (see [https://celix.apache.org/support/mailing-list.html](https://celix.apache.org/support/mailing-list.html)). And, of course, it is possible to create a pull request on top of this pull request to introduce some changes.
Apache Celix is part of the [Apache Software Foundation (ASF)](https://www.apache.org/), and in practice, this means that we can accept some small (code) donations, but for more or larger donations, an [ASF Contributor Agreement](https://www.apache.org/licenses/contributor-agreements.html) is needed.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327934956
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -19,21 +19,37 @@
extern crate bindgen;
Review Comment:
Why shall we need it? Seems unnecessary since 2018 edition.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327934956
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -19,21 +19,37 @@
extern crate bindgen;
Review Comment:
Why shall we need it? Seems unnecessary.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-1724224624
> LGTM
>
> Thanks for contributing this eye-opening PR.
>
> Reviewing this PR is not an easy task: familiarizing oneself with Rust's ownership model is not easy, and fitting it with the Celix's service layer which manages dynamic service lifetime is challenging. I was struggling with aligning these two properly. Maybe I need more time to absorb Rust. Never mind. I'll keep looking at the Rust part afterwards.
>
> Let's get this cool addition merged in its current form and get 2.4.0 released ASAP.
Thanks. I agree with a 2.4.0 ASAP. I think it is maybe better to merge this PR after 2.4.0. Then some additional work can be done to prevent/refactor some strange constructions (e.g. the log_helper <-> service tracker).
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328043877
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -17,23 +17,37 @@
* under the License.
*/
-extern crate bindgen;
-
+use std::env;
use std::error::Error;
-use std::path::PathBuf;
use std::fs::File;
use std::io::{self, BufRead};
-use std::env;
+use std::path::PathBuf;
-fn print_include_paths() -> Result<Vec<String>, Box<dyn Error>> {
- let build_dir = PathBuf::from(env::var("CORROSION_BUILD_DIR").unwrap());
- let include_path_file = build_dir.join("include_paths.txt");
+fn open_include_paths_file() -> Result<File, Box<dyn Error>> {
+ let file: File;
- //let include_path_file = Path::new("include_paths.txt");
- let file = File::open(&include_path_file)?;
+ let corrosion_build_dir = env::var("CORROSION_BUILD_DIR");
+ if corrosion_build_dir.is_ok() {
+ let build_dir = PathBuf::from(corrosion_build_dir.unwrap());
+ let include_path_file = build_dir.join("include_paths.txt");
+ file = File::open(&include_path_file)?;
+ } else {
+ println!("include_paths.txt not found in CORROSION_BUILD_DIR. Failing back to CELIX_RUST_INCLUDE_PATHS_FILE env value");
Review Comment:
If the message expresses the real intention, then the currently implementation, which fails back to `CELIX_RUST_INCLUDE_PATHS_FILE` if `CORROSION_BUILD_DIR` is not set, is wrong.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328052508
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -44,19 +58,15 @@ fn main() {
println!("cargo:info=Start build.rs for celix_bindings");
let include_paths = print_include_paths().unwrap();
Review Comment:
Missing `parse_callbacks(Box::new(bindgen::CargoCallbacks))` so that changed Celix C headers will not lead to rebuild of this crate.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328462161
##########
misc/experimental/rust/celix/src/bundle_context.rs:
##########
@@ -0,0 +1,669 @@
+/*
+ * 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.
+ */
+
+use std::any::type_name;
+use std::any::Any;
+use std::collections::HashMap;
+use std::ffi::c_void;
+use std::ops::Deref;
+use std::ptr::null_mut;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::Weak;
+
+use celix_bindings::{celix_bundleContext_log, celix_bundleContext_stopTracker, celix_bundleContext_stopTrackerAsync, celix_bundleContext_trackServicesWithOptions, celix_bundleContext_trackServicesWithOptionsAsync, celix_properties_t};
+use celix_bindings::celix_service_filter_options;
+use celix_bindings::celix_bundleContext_registerServiceWithOptionsAsync;
+use celix_bindings::celix_bundleContext_unregisterServiceAsync;
+use celix_bindings::celix_service_tracking_options_t;
+use celix_bindings::celix_service_use_options_t;
+use celix_bindings::celix_service_filter_options_t;
+use celix_bindings::celix_bundleContext_useServicesWithOptions;
+use celix_bindings::celix_bundleContext_useServiceWithOptions;
+use celix_bindings::celix_bundleContext_registerServiceWithOptions;
+use celix_bindings::celix_bundleContext_unregisterService;
+use celix_bindings::celix_bundle_context_t;
+use celix_bindings::celix_properties_create;
+use celix_bindings::celix_properties_set;
+use celix_bindings::celix_service_registration_options_t;
+
+use super::Error;
+use super::LogLevel;
+
+pub struct ServiceRegistration {
+ service_id: i64,
+ service_name: String,
+ unregister_async: bool,
+ weak_ctx: Weak<BundleContext>,
+ svc: Option<Box<dyn Any>>,
+ unmanaged_svc: Option<*mut dyn Any>,
+}
+
+impl ServiceRegistration {
+ pub fn get_service_id(&self) -> i64 {
+ self.service_id
+ }
+
+ pub fn get_service_name(&self) -> &str {
+ self.service_name.as_str()
+ }
+
+ pub fn get_service(&self) -> Option<&dyn Any> {
+ if let Some(boxed_svc) = self.svc.as_ref() {
+ Some(boxed_svc.as_ref())
+ } else if let Some(unmanaged_svc) = self.unmanaged_svc {
+ unsafe {
+ Some(&*unmanaged_svc)
+ }
+ } else {
+ None
+ }
+ }
+}
+
+impl Drop for ServiceRegistration {
+ fn drop(&mut self) {
+ let ctx = self.weak_ctx.upgrade();
+ match ctx {
+ Some(ctx) => ctx.unregister_service(self.service_id, self.unregister_async),
+ None => println!("Cannot unregister ServiceRegistration: BundleContext is gone"),
+ }
+ }
+}
+
+pub struct ServiceRegistrationBuilder<'a, T: ?Sized + 'static> {
+ ctx: &'a BundleContext,
+ register_async: bool,
+ unregister_async: bool,
+ svc: Option<Box<T>>, //note box is needed for stable pointer value
+ unmanaged_svc: Option<*mut dyn Any>,
+ service_name: String,
+ service_version: String,
+ service_properties: HashMap<String, String>,
+}
+
+impl<T> ServiceRegistrationBuilder<'_, T> {
+ fn new(ctx: &BundleContext) -> ServiceRegistrationBuilder<T> {
+ ServiceRegistrationBuilder {
+ ctx,
+ register_async: false,
+ unregister_async: false,
+ svc: None,
+ unmanaged_svc: None,
+ service_name: "".to_string(),
+ service_version: "".to_string(),
+ service_properties: HashMap::new(),
+ }
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ fn with_service_name_if_not_set(&mut self) -> &mut Self {
+ if self.service_name.is_empty() {
+ self.service_name = type_name::<T>().to_string();
+ }
+ self
+ }
+
+ pub fn with_service(&mut self, svc: T) -> &mut Self {
+ self.svc = Some(Box::new(svc));
+ self.with_service_name_if_not_set()
+ }
+
+ pub fn with_unmanaged_service(&mut self, svc: *mut T) -> &mut Self {
+ self.unmanaged_svc = Some(svc);
+ self.with_service_name_if_not_set()
+ }
+
+ pub fn with_version(&mut self, version: &str) -> &mut Self {
+ self.service_version = version.to_string();
+ self
+ }
+
+ pub fn with_properties(&mut self, properties: HashMap<String, String>) -> &mut Self {
+ self.service_properties = properties;
+ self
+ }
+
+ pub fn with_property(&mut self, key: &str, value: &str) -> &mut Self {
+ self.service_properties
+ .insert(key.to_string(), value.to_string());
+ self
+ }
+
+ pub fn with_register_async(&mut self) -> &mut Self {
+ self.register_async = true;
+ self
+ }
+
+ pub fn with_register_sync(&mut self) -> &mut Self {
+ self.register_async = false;
+ self
+ }
+
+ pub fn with_unregister_async(&mut self) -> &mut Self {
+ self.unregister_async = true;
+ self
+ }
+
+ pub fn with_unregister_sync(&mut self) -> &mut Self {
+ self.unregister_async = false;
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ let mut valid = true;
+ if self.service_name.is_empty() {
+ self.ctx
+ .log_error("Cannot register service. Service name is empty");
+ valid = false;
+ }
+ if self.svc.is_none() && self.unmanaged_svc.is_none() {
+ self.ctx
+ .log_error("Cannot register service. No instance provided");
+ valid = false;
+ }
+ match valid {
+ true => Ok(()),
+ false => Err(Error::BundleException),
+ }
+ }
+
+ fn get_c_svc(svc_reg: &ServiceRegistration) -> *mut c_void {
+ if let Some(boxed_svc) = svc_reg.svc.as_ref() {
+ boxed_svc.deref() as *const dyn Any as *mut c_void
+ } else if let Some(unmanaged_svc) = svc_reg.unmanaged_svc {
+ unmanaged_svc as *mut c_void
+ } else {
+ null_mut()
+ }
+ }
+
+ pub fn build(&mut self) -> Result<ServiceRegistration, Error> {
+ self.validate()?;
+
+ let mut svc_reg = ServiceRegistration {
+ service_id: -1,
+ service_name: self.service_name.clone(),
+ unregister_async: self.unregister_async,
+ weak_ctx: self.ctx.get_self().clone(),
+ svc: if self.svc.is_none() { None } else { Some(self.svc.take().unwrap()) },
+ unmanaged_svc: self.unmanaged_svc,
+ };
+
+ let svc_ptr = Self::get_c_svc(&svc_reg);
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_service_version = std::ffi::CString::new(self.service_version.as_str()).unwrap();
+ let c_service_properties: *mut celix_properties_t;
+ unsafe {
+ c_service_properties = celix_properties_create();
+ for (key, value) in self.service_properties.iter() {
+ let c_key = std::ffi::CString::new(key.as_str()).unwrap();
+ let c_value = std::ffi::CString::new(value.as_str()).unwrap();
+ celix_properties_set(c_service_properties, c_key.as_ptr(), c_value.as_ptr());
+ }
+ }
+
+ let opts = celix_service_registration_options_t {
+ svc: svc_ptr,
+ factory: null_mut(),
+ serviceName: c_service_name.as_ptr() as *const i8,
+ properties: c_service_properties,
+ serviceLanguage: null_mut(),
+ serviceVersion: if self.service_version.is_empty() {
+ null_mut()
+ } else {
+ c_service_version.as_ptr() as *const i8
+ },
+ asyncData: null_mut(),
+ asyncCallback: None,
+ };
+
+ if self.register_async {
+ unsafe {
+ svc_reg.service_id = celix_bundleContext_registerServiceWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ }
+ } else {
+ unsafe {
+ svc_reg.service_id = celix_bundleContext_registerServiceWithOptionsAsync(self.ctx.get_c_bundle_context(), &opts);
+ }
+ }
+ if svc_reg.service_id >= 0 {
+ Ok(svc_reg)
+ } else {
+ Err(Error::BundleException)
+ }
+ }
+}
+
+pub struct ServiceUseBuilder<'a, T> {
+ ctx: &'a BundleContext,
+ many: bool,
+ service_name: String,
+ filter: String,
+ callback: Option<Box<dyn Fn(&T)>>, //note double boxed
+}
+
+impl<T> ServiceUseBuilder<'_, T> {
+ fn new(ctx: &BundleContext, many: bool) -> ServiceUseBuilder<T> {
+ ServiceUseBuilder {
+ ctx,
+ many,
+ service_name: type_name::<T>().to_string(),
+ filter: "".to_string(),
+ callback: None,
+ }
+ }
+
+ pub fn with_callback(&mut self, closure: Box<dyn Fn(&T)>)-> &mut Self {
+ self.callback = Some(closure);
+ self
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ pub fn with_filter(&mut self, filter: &str) -> &mut Self {
+ self.filter = filter.to_string();
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ if self.service_name.is_empty() {
+ return Err(Error::BundleException);
+ }
+ Ok(())
+ }
+
+ unsafe extern "C" fn use_service_c_callback(handle: *mut c_void, svc: *mut c_void) {
+ let closure = handle as *const Box<dyn Fn(&T)>;
+ let closure = closure.as_ref().unwrap();
+
+ let typed_svc = svc as *const T;
+ let typed_svc = typed_svc.as_ref().unwrap();
+
+ closure(typed_svc);
+ }
+
+ pub fn build(&mut self) -> Result<isize, Error> {
+ self.validate()?;
+
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_filter = std::ffi::CString::new(self.filter.as_str()).unwrap();
+ let c_service_name_ptr: *const i8 = c_service_name.as_ptr();
+ let c_filter_ptr: *const i8 = if self.filter.is_empty() { null_mut()} else {c_filter.as_ptr() };
+
+ let c_closure_ptr = self.callback.as_ref().unwrap() as *const Box<dyn Fn(&T)> as *mut c_void;
+
+ let opts = celix_service_use_options_t {
+ filter: celix_service_filter_options_t {
+ serviceName: c_service_name_ptr,
+ versionRange: null_mut(),
+ filter: c_filter_ptr,
+ serviceLanguage: null_mut(),
+ ignoreServiceLanguage: false,
+ },
+ waitTimeoutInSeconds: 0.0,
+ callbackHandle: c_closure_ptr,
+ use_: Some(Self::use_service_c_callback),
+ useWithProperties: None,
+ useWithOwner: None,
+ flags: 0,
+ };
+
+ if self.many {
+ unsafe {
+ let count = celix_bundleContext_useServicesWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ Ok(count as isize)
+ }
+ } else {
+ unsafe {
+ let called = celix_bundleContext_useServiceWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ if called {
+ Ok(1)
+ } else {
+ Ok(0)
+ }
+ }
+ }
+ }
+}
+
+struct ServiceTrackerCallbacks<T> {
+ set_callback: Option<Box<dyn Fn(Option<&T>)>>,
+ add_callback: Option<Box<dyn Fn(&T)>>,
+ remove_callback: Option<Box<dyn Fn(&T)>>,
+}
+
+pub struct ServiceTracker<T> {
+ ctx: Arc<BundleContext>,
+ tracker_id: i64,
+ // shared_data: Mutex<SharedServiceTrackerData<T>>,
+ // data_condition: Condvar,
+ callbacks: Box<ServiceTrackerCallbacks<T>>, //Note in a box to ensure pointer value is stable after move
+ stop_async: bool,
+}
+
+impl<T> ServiceTracker<T> {
+ pub fn close(&mut self) {
+ self.ctx.stop_tracker(self.tracker_id, self.stop_async);
+ self.tracker_id = -1;
+ }
+}
+
+impl<T> Drop for ServiceTracker<T> {
+ fn drop(&mut self) {
+ self.close();
+ }
+}
+
+pub struct ServiceTrackerBuilder<'a, T> {
+ ctx: &'a BundleContext,
+ service_name: String,
+ filter: String,
+ track_async: bool,
+ stop_async: bool,
+ set_callback: Option<Box<dyn Fn(Option<&T>)>>,
+ add_callback: Option<Box<dyn Fn(&T)>>,
+ remove_callback: Option<Box<dyn Fn(&T)>>,
+}
+
+impl<T> ServiceTrackerBuilder<'_, T> {
+ fn new(ctx: &BundleContext) -> ServiceTrackerBuilder<T> {
+ ServiceTrackerBuilder {
+ ctx,
+ service_name: type_name::<T>().to_string(),
+ filter: "".to_string(),
+ track_async: false,
+ stop_async: false,
+ set_callback: None,
+ add_callback: None,
+ remove_callback: None,
+ }
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ pub fn with_filter(&mut self, filter: &str) -> &mut Self {
+ self.filter = filter.to_string();
+ self
+ }
+
+ pub fn with_set_callback(&mut self, closure: Box<dyn Fn(Option<&T>)>) -> &mut Self {
+ self.set_callback = Some(closure);
+ self
+ }
+
+ pub fn with_add_callback(&mut self, closure: Box<dyn Fn(&T)>) -> &mut Self {
+ self.add_callback = Some(closure);
+ self
+ }
+
+ pub fn with_remove_callback(&mut self, closure: Box<dyn Fn(&T)>) -> &mut Self {
+ self.remove_callback = Some(closure);
+ self
+ }
+
+ pub fn with_track_async(&mut self) -> &mut Self {
+ self.track_async = true;
+ self
+ }
+
+ pub fn with_track_sync(&mut self) -> &mut Self {
+ self.track_async = false;
+ self
+ }
+
+ pub fn with_stop_async(&mut self) -> &mut Self {
+ self.stop_async = true;
+ self
+ }
+
+ pub fn with_stop_sync(&mut self) -> &mut Self {
+ self.stop_async = false;
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ if self.service_name.is_empty() {
+ return Err(Error::BundleException);
+ }
+ Ok(())
+ }
+
+ unsafe extern "C" fn set_callback_for_c(handle: *mut ::std::os::raw::c_void, svc: *mut ::std::os::raw::c_void) {
+ let callbacks = handle as *const ServiceTrackerCallbacks<T>;
+ let callbacks = callbacks.as_ref().unwrap();
+
+ if svc.is_null() {
+ if let Some(set_callback) = callbacks.set_callback.as_ref() {
+ set_callback(None);
+ }
+ } else {
+ let typed_svc = svc as *const T;
+ let typed_svc = typed_svc.as_ref().unwrap();
+ if let Some(set_callback) = callbacks.set_callback.as_ref() {
+ set_callback(Some(typed_svc));
+ }
+ }
+ }
+
+ unsafe extern "C" fn add_callback_for_c(handle: *mut ::std::os::raw::c_void, svc: *mut ::std::os::raw::c_void) {
+ let callbacks = handle as *const ServiceTrackerCallbacks<T>;
+ let callbacks = callbacks.as_ref().unwrap();
+
+ let typed_svc = svc as *const T;
+ let typed_svc = typed_svc.as_ref().unwrap();
+
+ if let Some(add_callback) = callbacks.add_callback.as_ref() {
+ add_callback(typed_svc);
+ }
+ }
+
+ unsafe extern "C" fn remove_callback_for_c(handle: *mut ::std::os::raw::c_void, svc: *mut ::std::os::raw::c_void) {
+ let callbacks = handle as *const ServiceTrackerCallbacks<T>;
+ let callbacks = callbacks.as_ref().unwrap();
+
+ let typed_svc = svc as *const T;
+ let typed_svc = typed_svc.as_ref().unwrap();
+
+ if let Some(remove_callback) = callbacks.remove_callback.as_ref() {
+ remove_callback(typed_svc);
+ }
+ }
+
+ pub fn build(&mut self) -> Result<ServiceTracker<T>, Error> {
+ self.validate()?;
+
+ let mut svc_tracker = ServiceTracker {
+ ctx: self.ctx.get_self().upgrade().unwrap(),
+ tracker_id: -1,
+ callbacks: Box::new(ServiceTrackerCallbacks {
+ set_callback: self.set_callback.take(),
+ add_callback: self.add_callback.take(),
+ remove_callback: self.remove_callback.take(),
+ }),
+ stop_async: self.stop_async,
+ };
+
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_filter = std::ffi::CString::new(self.filter.as_str()).unwrap();
+ let c_callback_handle = svc_tracker.callbacks.as_ref() as *const ServiceTrackerCallbacks<T> as *mut c_void;
+
+ let opts = celix_service_tracking_options_t{
+ filter: celix_service_filter_options {
+ serviceName: c_service_name.as_ptr(),
+ versionRange: null_mut(),
+ filter: if self.filter.is_empty() { null_mut() } else { c_filter.as_ptr() },
+ serviceLanguage: null_mut(),
+ ignoreServiceLanguage: false,
+ },
+ callbackHandle: c_callback_handle,
+ set: Some(Self::set_callback_for_c),
+ setWithProperties: None,
+ setWithOwner: None,
+ add: Some(Self::add_callback_for_c),
+ addWithProperties: None,
+ addWithOwner: None,
+ remove: Some(Self::remove_callback_for_c),
+ removeWithProperties: None,
+ removeWithOwner: None,
+ trackerCreatedCallbackData: null_mut(),
+ trackerCreatedCallback: None,
+ };
+
+ let svc_tracker_id: i64;
+ unsafe {
+ if self.track_async {
+ svc_tracker_id = celix_bundleContext_trackServicesWithOptionsAsync(self.ctx.get_c_bundle_context(), &opts);
+ } else {
+ svc_tracker_id = celix_bundleContext_trackServicesWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ }
+ }
+
+ if svc_tracker_id >= 0 {
+ svc_tracker.tracker_id = svc_tracker_id;
+ Ok(svc_tracker)
+ } else {
+ Err(Error::BundleException)
+ }
+ }
+}
+
+pub struct BundleContext {
+ c_bundle_context: *mut celix_bundle_context_t,
+ weak_self: Mutex<Option<Weak<BundleContext>>>,
+}
+
+impl BundleContext {
+ fn new(c_bundle_context: *mut celix_bundle_context_t) -> Arc<Self> {
+ let ctx = Arc::new(BundleContext {
+ c_bundle_context,
+ weak_self: Mutex::new(None),
+ });
+ let weak_ref = Arc::downgrade(&ctx);
+ ctx.set_self(weak_ref);
+ ctx
+ }
+
+ fn set_self(&self, weak_self: Weak<BundleContext>) {
+ let mut guard = self.weak_self.lock().unwrap();
+ *guard = Some(weak_self);
+ }
+
+ fn get_self(&self) -> Weak<BundleContext> {
+ self.weak_self.lock().unwrap().clone().unwrap()
+ }
+
+ fn log_to_c(&self, level: LogLevel, message: &str) {
+ unsafe {
+ let result = std::ffi::CString::new(message);
+ match result {
+ Ok(c_str) => {
+ celix_bundleContext_log(
+ self.c_bundle_context,
+ level.into(),
+ c_str.as_ptr() as *const i8,
+ );
+ }
+ Err(e) => {
+ println!("Error creating CString: {}", e);
Review Comment:
Use `eprintln!` rather than `println!`?
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "kulst (via GitHub)" <gi...@apache.org>.
kulst commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2038429466
Hi, thank you for the detailed explanations!
I aswell think that Rust would really benefit Celix.
The exclusive use of safe Rust guarantees that memory and type conversion errors as well as undefined behavior are excluded - through compile time and runtime checks.
This should also be the goal for the Rust API of Celix. If any of these errors occur to the user of the API when using safe Rust only, it should be considered a bug in the Rust API or Celix. However, whether it is possible to offer a Celix API that consists exclusively of safe Rust is still unclear to me.
I think the existing C API is a great starting point for now, as Rust interopperates usually well with C. As you already showed :)
For now I would start with making myself more familiar with Celix. I was already able to develop a basic understanding of most of the concepts of Celix. But I definitely need to further enhance this.
I will start by installing Celix and trying to get your Rust examples to work, lets see if I can also make it work with the latest version 2.4.
The next step would be to get a better understanding of the invariants that need to be upheld when using the C API. I saw, that a lot of these are already documented, but I assume there are more and maybe also implicit ones.
When this is done, the Rust API can be designed. Of course I want to leverage the Rust type system to make sure that the invariants of the C API are upheld.
Your POC is also a great starting point.
For now I think this is already a lot to do. Let's see where this leads me. :) In any case, an ASF Contributor Agreement will not be an obstacle.
Topics I stumbled upon but which I have put in my backlog for now:
**Allocation:** by default allocating and deallocating on different sides of the ffi might lead to undefined behaviour. There are several possible solutions to this
1. implementing a (global) allocator in Rust that delegates to the Celix allocator
2. making sure that both happens on the same side of the FFI by the Rust type system
**Type conversion:** There are various types in the C API that are similar to Rust types but can not be used as smoothly without further adjustments on the Rust side. For example the `array_list` type is similar to a vector (with some additions).
1. So one solution to use such a type smoothly in Rust, you basically would have to reimplement the interface of the `std::vec` type.
2. Another solution is to just copy the elements to the corresponding type before/after passing the ffi boundary. This might be inefficient however.
3. A third solution might only be possible when allocation and deallocation happen on the same side of the ffi boundary. I think it could then be possible to create a `std::vec` from the internals of an `array_list` without another allocation just by using the pointer, the size and the capacity an `array_list` already provides. Together with the additions of an `array_list` this could be packaged in a simple wrapper type.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2111017532
> **Second lifetime approach: Spawn a thread** My second approach is to just spawn a thread in the start function and join it in the stop function. By this we get a scope that lifes from start to stop. We send the created `BundleContext` into the thread but also here we only provide the user a `&BundleContext`. Also here the user can not spawn thread that outlive the scope. But as the scope is from start to stop it is possible to create threads that also live as long. In the stop function we signal the thread that it must stop and join it. The user is responsible to make sure the thread stops. With this approach we do not need to store user data in the activator. We simply put a `JoinHandle` there to join it and the `Sender` to signal the thread it must stop. Overall this method is suited for static services but dynamic services as well.
>
> In the following repository I show how I think this could look like: https://github.com/kulst/celix_rust_test
>
In my opinion I would be great if the BundleContext and everything created from the bundle context can be controlled by lifecycles. This could ensure that we can have static checks for correctly handling service registrations and service usage within the lifecycle of a bundle.
For now I also think it is fine to do this with spawning threads, but ideally a thread is not needed. I have experience with complex applications using about 100 bundles and this would also mean a 100 thread (including a 100 extra stack stack) purely for lifecycle management.
If I understand it correctly a thread is used to ensure a object lifecycle from BundleActivtor::start until BundleActivator::stop. Is it not possible to somehow create a rust object that outlives the celix C framework, maybe even using a custom rust celix launcher and use that rust object to store "bundle lifecycle control" storage objects?
But I am guessing here.. I am still learning rust.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "kulst (via GitHub)" <gi...@apache.org>.
kulst commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2111046448
> > Two weeks have passed and I wanted to share again my progress :)
> > **BundleContext lifetime** Another way I came up with is to hand out a BundleContext to the bundle and having an Arc<(Condvar, Mutex)> flag in the BundleContext (not accessible by the BundleContexts API). When the BundleContext is dropped, the flag is set and the Condvar is notified. A copy of that Arc is stored in the internal part of the activator. When the stop/delete function of the bundle is invoked the flag is checked periodically if it is set. By using the Condvar we can block between these checks (and also log an error similar to the cpp API).
>
> This is indeed close to what C++ is doing and that case a `weak_ptr` is used during bundle stop a runtime check and wait is made to ensure there are no users of the BundleContext anymore.
Exactly, the C++ solution is where I took inspiration from. But by doing it with an Arc<(Condvar, Mutex<bool>)> inside of the BundleContext we can utilize lifetimes and could also create the Rust API with more flexibility for the user.
For example instead of explicitly storing an Arc<BundleContext> or an &BundleContext in each data structure that depends on the BundleContext we could also store an T : AsRef<BundleContext> in the data structures and let the user decide.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327974586
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -19,21 +19,37 @@
extern crate bindgen;
Review Comment:
Removing it is OK, while removing other `extern crate`s will cause build errors, though all of them have been specified in the manifest's dependencies.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328469387
##########
misc/experimental/rust/shell_command_bundle/src/lib.rs:
##########
@@ -0,0 +1,180 @@
+/*
+ * 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.
+ */
+
+use std::ffi::c_char;
+use std::ffi::c_void;
+use std::sync::Arc;
+
+use celix::{BundleActivator, LogHelper};
+use celix::BundleContext;
+use celix::Error;
+use rust_shell_api::RustShellCommand;
+
+use celix_bindings::celix_shell_command_t;
+use celix_bindings::FILE;
+
+struct CShellCommandImpl {
+ ctx: Arc<BundleContext>,
+}
+
+impl CShellCommandImpl {
+ fn new(ctx: Arc<BundleContext>) -> Self {
+ ctx.log_info("Shell Command created");
+ CShellCommandImpl { ctx }
+ }
+
+ extern "C" fn call_execute_command(
+ handle: *mut c_void,
+ command_line: *const c_char,
+ _out_stream: *mut FILE,
+ _error_stream: *mut FILE,
+ ) -> bool {
+ if handle.is_null() || command_line.is_null() {
+ return false;
+ }
+ unsafe {
+ let obj = &mut *(handle as *mut CShellCommandImpl);
+ let str_command_line = std::ffi::CStr::from_ptr(command_line).to_str();
+ if str_command_line.is_err() {
+ return false;
+ }
+ obj.execute_command(str_command_line.unwrap());
+ }
+ true
+ }
+
+ fn execute_command(&mut self, command_line: &str) {
+ self.ctx.log_info(format!("Execute command: \"{}\"", command_line).as_str());
+ }
+}
+
+//temporary, should be moved in a separate API crate
+
+struct RustShellCommandImpl {
+ ctx: Arc<BundleContext>,
+}
+
+impl RustShellCommandImpl {
+ fn new(ctx: Arc<BundleContext>) -> Self {
+ ctx.log_info("Rust Shell Command created");
+ RustShellCommandImpl { ctx }
+ }
+}
+
+impl RustShellCommand for RustShellCommandImpl {
+ fn execute_command(&self, command_line: &str) -> Result<(), Error> {
+ self.ctx
+ .log_info(format!("Execute command: {}.", command_line).as_str());
+ Ok(())
+ }
+}
+
+struct ShellCommandActivator {
+ ctx: Arc<BundleContext>,
+ log_helper: Arc<LogHelper>,
+ shell_command_provider: CShellCommandImpl,
+ registrations: Vec<celix::ServiceRegistration>,
+}
+
+impl ShellCommandActivator {
+
+ fn register_services(&mut self) -> Result<(), Error> {
+ //Register C service registered as value
+ let registration = self.ctx.register_service()
+ .with_service(celix_shell_command_t {
+ handle: &mut self.shell_command_provider as *mut CShellCommandImpl as *mut c_void,
+ executeCommand: Some(CShellCommandImpl::call_execute_command),
+ })
+ .with_service_name("celix_shell_command")
Review Comment:
From the current implementation, I can tell if `with_service_name` comes before `with_service`, an dynamic memory allocation will be avoided.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328059334
##########
misc/experimental/rust/celix/src/errno.rs:
##########
@@ -0,0 +1,59 @@
+/*
+ * 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.
+ */
+
+use std::fmt::{Debug, Formatter};
+use celix_bindings::celix_status_t;
+
+pub const CELIX_SUCCESS: celix_status_t = celix_bindings::CELIX_SUCCESS as celix_status_t;
+
+//Note compile-time defined constants are not available in rust generated bindings, so
+//these are defined with literal values.
+pub const BUNDLE_EXCEPTION: celix_status_t = 70001;
+
+pub enum Error {
+ BundleException,
+ CelixStatusError(celix_status_t), // Represent not explicitly mapped celix_status_t values
+}
+
+impl From<celix_status_t> for Error {
+ fn from(status: celix_status_t) -> Self {
+ match status {
+ BUNDLE_EXCEPTION => Error::BundleException,
+ _ => Error::CelixStatusError(status),
+ }
+ }
+}
+
+impl Into<celix_status_t> for Error {
+ fn into(self) -> celix_status_t {
+ match self {
+ Error::BundleException => BUNDLE_EXCEPTION,
+ Error::CelixStatusError(status) => status,
+ }
+ }
+}
+
+impl Debug for Error {
+ fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+ match self {
Review Comment:
Is `celix_strerror` helpful for more useful Debug information?
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `rust_shell_api` error.
However, removing `extern crate bindgen;` will not cause any errors.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2111057796
> **Services** The next topic I wanted to approach is services. When using a service a bundle calls the function of another bundle. Both bundles need to agree upon the the API of the service and also on the ABI of the service. In my eyes the Celix Rust API should be safe to use for the user. This means it should not be possible to cause undefined behaviour without using unsafe code in Rust. But we can not enforce during compilation that the API and ABI of the service we want to use is the same as we think it is.
>
> So I think we have two options here:
>
> 1. Using a service is inherently unsafe and invariants must be upheld by the user. For C and C++ services this will probably be the only solution, so using these services from Rust will allways be unsafe. However for Rust services I think there is another way!
First of, my complements about taking API **and** ABI compatibility into account. For C and C++ we currently only support source compatibility, so everything should be build with the same compiler using the same sources (include service interface headers).
But the idea to do some API compatibility checking is not new. Apache Celix also has a [`libffi` library](https://github.com/apache/celix/tree/master/libs/dfi) (recently updated for stability and documentation).
An idea back when libffi was first created, was to use the `libclang` to parse C service interface headers to create descriptors (small and easy parseable files describing C structures and interfaces). Note: using libclang as lib and not necessary also using the clang compiler. And then adding these generated dfi descriptors in bundle zips to that they can be referred to during service registration and service usage. The Celix framework could then use these descriptors and libffi to check compatibility.
I still think this would be a very welcome addition to Apache Celix, but I would still require significant effort to develop, test and make user friendly.
>
> 2. Checking ABI and API compatibility at runtime. If we can achieve this, using a service is safe, incompatible ABI/API would not cause undefined behaviour, returning an Error would be enough.
>
>
> **How can we achieve ABI and API checks at runtime?** There is one Rust crate that already tries to deal with this issue. Only a very small part of the Rust API is already stable. Most part of the API is allowed to change between compiler versions but also between different compiler runs. [abi_stable](https://docs.rs/abi_stable/latest/abi_stable/) provides ABI stability by transforming Rust types with unstable ABI into types with a stable ABI (#[repr(C)]) types. It utilizes the Rust procedural macro system for this which can transform abstract syntax trees into other abstract syntax trees. To get API stability the crate provides type information of these types, which can be checked at runtime. To make sure that the API and ABI of the abi_stable crate is compatible a static variable is provided which can also be checked against at runtime.
>
> I think we can utilize this crate to provide safe services.
I have a short look at abi_stable and mainly looked for the license. Maybe I missing something, but I could not find under which license the code is provided.
But this does indeed sound like a good addition for runtime compatibility checking.
>
> **How could this look like?** A service would be an object that implements a trait (the service interface). This is similar to C++ services. The trait is defined in an Service API crate. The bundle that provides the service statically links against this API crate. When registering this service a pointer to the bundle-static type information and abi_stable library information is stored in the framework (for that it is necessary to implement one or two functions that are only usable internally by the Celix Rust API).
>
> When another bundle now wants to use the service it must statically link against the API crate as well. When using the service it is checked that the type information and abi_stable library information are compatible.
>
> **Additional opportunities** It would be great if we could use Rust services from C and C++. For this the Service API crate must export some C/C++ header files. Luckily there is a crate that already does this: [cglue](https://docs.rs/cglue/latest/cglue/). On top of that it is compatible and works together with abi_stable. This makes it possible to easily create services that are usable from C, C++ and Rust and provide additional guaranties.
>
Nice, I will have a deeper look into this. Currently when combining C and C++, it possible to use C service in C++ but not the other way around. If rust service can be more flexible that would be great.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2111071301
> **Final hurdles** I would like to rewrite the POC in the next step to make sure all of this works. However there is one problem I could not solve yet: What if a service of a bundle provides a 'static reference in its interface? The reference could point to 'static data of the bundle. After unloading the providing bundle this reference would be invalid. There should be a way to constrain the lifetimes to the lifetime of the service use callback. However I do not know how yet. Maybe there is a way by checking the service API with an additional proc macro.
>
Indeed. The integral dynamic nature of dynamic services in Apache Celix is that they can come and go and users should be protected against misuse as much as possible. For C / C++ this is arranged by providing a "use service" API based on callbacks so that the framework can ensure services are kept "alive" during callbacks. But it can also be done by service trackers combined with set/add/remove callbacks and in that case the user is responsible for correctly using the callbacks and protection service usage.
I agree that ideally in Rust this should be done in a safer way, but lack to rust experience to know how or even if this is feasible.
> **Summary** Again a lot of text. I hope you are fine with documenting my thoughts and progress here. There are a lot of topics that need to be considered. At the moment Rust is not perfectly well suited for dynamic linking. Its mostly unstable ABI makes it necessary to heavily use the macro system to provide a safe interface. If you want to have all of the Rust safety guaranties as well, a lot of possibilites need to be considered. But I still think it is possible and I will continue working on it.
No worries, I find very interesting to read up your thoughts, ideas and POCs. IMO communication through PR comments are ok :)
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328338319
##########
misc/experimental/rust/celix/src/bundle_activator.rs:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+
+use std::sync::Arc;
+
+use super::BundleContext;
+use super::Error;
+
+pub trait BundleActivator {
+ fn new(ctx: Arc<BundleContext>) -> Self;
+ fn start(&mut self) -> Result<(), Error> {
+ /* Default implementation */
+ Ok(())
+ }
+ fn stop(&mut self) -> Result<(), Error> {
+ /* Default implementation */
+ Ok(())
+ }
+}
+
+#[macro_export]
+macro_rules! generate_bundle_activator {
+ ($activator:ty) => {
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_create(
+ ctx: *mut $crate::details::CBundleContext,
+ out: *mut *mut ::std::ffi::c_void,
+ ) -> $crate::details::CStatus {
+ let boxed_context = $crate::bundle_context_new(ctx);
+ let mut arc_context = Arc::from(boxed_context);
+ let activator = <$activator>::new(arc_context);
+ *out = Box::into_raw(Box::new(activator)) as *mut ::std::ffi::c_void;
+ $crate::CELIX_SUCCESS
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_start(
+ handle: *mut ::std::ffi::c_void,
+ ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let activator = &mut *(handle as *mut $activator);
+ let result = activator.start();
+ match result {
+ Ok(_) => $crate::CELIX_SUCCESS,
+ Err(e) => e.into(),
+ }
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_stop(
+ handle: *mut ::std::ffi::c_void,
+ ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let activator = &mut *(handle as *mut $activator);
+ let result = activator.stop();
+ match result {
+ Ok(_) => $crate::CELIX_SUCCESS,
+ Err(e) => e.into(),
+ }
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_destroy(
+ handle: *mut ::std::ffi::c_void,
+ _ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let reclaimed_activator = Box::from_raw(handle as *mut $activator);
Review Comment:
This does not feel right. In the C version, activator is dropped after `celix_bundleContext_waitForEvents(ctx)`.
IMHO, the best fix is to change the corresponding logic in `celix_framework_stopBundleEntryInternal`:
From
```C
status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
```
To
```C
status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
```
So that we can remove `celix_bundleContext_waitForEvents(ctx)` from the generated `celix_bundleActivator_destroy`. Also we re-promote usage of create/start/stop/destroy rather than `CELIX_GEN_BUNDLE_ACTIVATOR`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `rust_shell_api` error.
However, removing `extern crate bindgen;` in build.rs will not cause any errors.
I also found that it is claimed that "extern crate is no longer needed in 99% of circumstances"(https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html).
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327977210
##########
misc/experimental/rust/rust_shell_api/src/lib.rs:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+
+extern crate celix;
+
+use celix::Error;
+pub const COMMAND_NAME: &str = "command.name";
+pub const COMMAND_USAGE: &str = "command.usage";
+pub const COMMAND_DESCRIPTION: &str = "command.description";
+
+#[doc = "A trait to implement a Celix Shell Command"]
+pub trait RustShellCommand {
+ fn execute_command(&self, command_line: &str) -> Result<(), Error>;
Review Comment:
`&self` or `&mut self`? What if a command execution changes the internal states?
So we have to use a dummy `RustShellCommandImpl` to hold a mutable reference to the object we really want to change its states with such a command execution.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328072986
##########
misc/experimental/rust/celix/src/log_helper.rs:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.
+ */
+
+use std::sync::{Arc, Mutex, RwLock};
+
+use super::BundleContext;
+use super::LogLevel;
+
+use celix_bindings::celix_log_service_t;
+use crate::ServiceTracker;
+
+pub struct LogHelper {
+ name: String,
+ tracker: Mutex<Option<ServiceTracker<celix_log_service_t>>>,
+ log_svc: RwLock<Option<*const celix_log_service_t>>,
+}
+
+impl LogHelper {
+
+ pub fn new(ctx: Arc<BundleContext>, name: &str) -> Arc<Self> {
+ let helper = Arc::new(LogHelper{
+ name: name.to_string(),
+ tracker: Mutex::new(None),
+ log_svc: RwLock::new(None),
+ });
+ let filter = format!("(name={})", name);
+ let weak_helper = Arc::downgrade(&helper);
+ let tracker = ctx.track_services::<celix_log_service_t>()
Review Comment:
I think the weirdness of the tracker mutex comes from this: providing a half-constructed object (log helper) to another object (tracker).
If we permit a new log helper holding a None tracker and opening the tracker after new, then `LogHelper::new` returns Self rather than `Arc<Self>`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1312714746
##########
misc/experimental/rust/CMakeLists.txt:
##########
@@ -26,13 +26,12 @@ if (CELIX_RUST_EXPERIMENTAL)
FetchContent_MakeAvailable(Corrosion)
Review Comment:
According to Corrosion documentation, this method only works for CMake >= 3.19.
Our current requirement is `cmake_minimum_required (VERSION 3.18)`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "kulst (via GitHub)" <gi...@apache.org>.
kulst commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2033959308
Hi, awesome to see Celix is making progress towards Rust :+1:
Are you open to contributions on this topic? Although I am not completely familiar with the internals of Celix yet, I would love to wrap my head around your concepts and see if we can create a nice and secure Rust API for Celix.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1317609429
##########
misc/experimental/rust/CMakeLists.txt:
##########
@@ -26,13 +26,12 @@ if (CELIX_RUST_EXPERIMENTAL)
FetchContent_MakeAvailable(Corrosion)
Review Comment:
Good point, I add a cmake_minimum_required for the Rust PoC. I think in this case it is not needed to require on top level.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328338319
##########
misc/experimental/rust/celix/src/bundle_activator.rs:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+
+use std::sync::Arc;
+
+use super::BundleContext;
+use super::Error;
+
+pub trait BundleActivator {
+ fn new(ctx: Arc<BundleContext>) -> Self;
+ fn start(&mut self) -> Result<(), Error> {
+ /* Default implementation */
+ Ok(())
+ }
+ fn stop(&mut self) -> Result<(), Error> {
+ /* Default implementation */
+ Ok(())
+ }
+}
+
+#[macro_export]
+macro_rules! generate_bundle_activator {
+ ($activator:ty) => {
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_create(
+ ctx: *mut $crate::details::CBundleContext,
+ out: *mut *mut ::std::ffi::c_void,
+ ) -> $crate::details::CStatus {
+ let boxed_context = $crate::bundle_context_new(ctx);
+ let mut arc_context = Arc::from(boxed_context);
+ let activator = <$activator>::new(arc_context);
+ *out = Box::into_raw(Box::new(activator)) as *mut ::std::ffi::c_void;
+ $crate::CELIX_SUCCESS
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_start(
+ handle: *mut ::std::ffi::c_void,
+ ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let activator = &mut *(handle as *mut $activator);
+ let result = activator.start();
+ match result {
+ Ok(_) => $crate::CELIX_SUCCESS,
+ Err(e) => e.into(),
+ }
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_stop(
+ handle: *mut ::std::ffi::c_void,
+ ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let activator = &mut *(handle as *mut $activator);
+ let result = activator.stop();
+ match result {
+ Ok(_) => $crate::CELIX_SUCCESS,
+ Err(e) => e.into(),
+ }
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_destroy(
+ handle: *mut ::std::ffi::c_void,
+ _ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let reclaimed_activator = Box::from_raw(handle as *mut $activator);
Review Comment:
This does not feel right. In the C version, activator is dropped after `celix_bundleContext_waitForEvents(ctx)`.
IMHO, the best fix is to change the corresponding logic in `celix_framework_stopBundleEntryInternal`:
From
```C
status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
```
To
```C
status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
```
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328068416
##########
misc/experimental/rust/celix/src/log_helper.rs:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.
+ */
+
+use std::sync::{Arc, Mutex, RwLock};
+
+use super::BundleContext;
+use super::LogLevel;
+
+use celix_bindings::celix_log_service_t;
+use crate::ServiceTracker;
+
+pub struct LogHelper {
+ name: String,
+ tracker: Mutex<Option<ServiceTracker<celix_log_service_t>>>,
Review Comment:
With `LogHelper` as the sole owner, the use of `Mutex` is weird, whose purpose is to suppress compiler's complain of `helper.tracker.lock().unwrap().replace(tracker)`?
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328378358
##########
misc/experimental/rust/celix/src/bundle_context.rs:
##########
@@ -0,0 +1,669 @@
+/*
+ * 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.
+ */
+
+use std::any::type_name;
+use std::any::Any;
+use std::collections::HashMap;
+use std::ffi::c_void;
+use std::ops::Deref;
+use std::ptr::null_mut;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::Weak;
+
+use celix_bindings::{celix_bundleContext_log, celix_bundleContext_stopTracker, celix_bundleContext_stopTrackerAsync, celix_bundleContext_trackServicesWithOptions, celix_bundleContext_trackServicesWithOptionsAsync, celix_properties_t};
+use celix_bindings::celix_service_filter_options;
+use celix_bindings::celix_bundleContext_registerServiceWithOptionsAsync;
+use celix_bindings::celix_bundleContext_unregisterServiceAsync;
+use celix_bindings::celix_service_tracking_options_t;
+use celix_bindings::celix_service_use_options_t;
+use celix_bindings::celix_service_filter_options_t;
+use celix_bindings::celix_bundleContext_useServicesWithOptions;
+use celix_bindings::celix_bundleContext_useServiceWithOptions;
+use celix_bindings::celix_bundleContext_registerServiceWithOptions;
+use celix_bindings::celix_bundleContext_unregisterService;
+use celix_bindings::celix_bundle_context_t;
+use celix_bindings::celix_properties_create;
+use celix_bindings::celix_properties_set;
+use celix_bindings::celix_service_registration_options_t;
+
+use super::Error;
+use super::LogLevel;
+
+pub struct ServiceRegistration {
+ service_id: i64,
+ service_name: String,
+ unregister_async: bool,
+ weak_ctx: Weak<BundleContext>,
+ svc: Option<Box<dyn Any>>,
+ unmanaged_svc: Option<*mut dyn Any>,
+}
+
+impl ServiceRegistration {
+ pub fn get_service_id(&self) -> i64 {
+ self.service_id
+ }
+
+ pub fn get_service_name(&self) -> &str {
+ self.service_name.as_str()
+ }
+
+ pub fn get_service(&self) -> Option<&dyn Any> {
+ if let Some(boxed_svc) = self.svc.as_ref() {
+ Some(boxed_svc.as_ref())
+ } else if let Some(unmanaged_svc) = self.unmanaged_svc {
+ unsafe {
+ Some(&*unmanaged_svc)
+ }
+ } else {
+ None
+ }
+ }
+}
+
+impl Drop for ServiceRegistration {
+ fn drop(&mut self) {
+ let ctx = self.weak_ctx.upgrade();
+ match ctx {
+ Some(ctx) => ctx.unregister_service(self.service_id, self.unregister_async),
+ None => println!("Cannot unregister ServiceRegistration: BundleContext is gone"),
+ }
+ }
+}
+
+pub struct ServiceRegistrationBuilder<'a, T: ?Sized + 'static> {
+ ctx: &'a BundleContext,
+ register_async: bool,
+ unregister_async: bool,
+ svc: Option<Box<T>>, //note box is needed for stable pointer value
+ unmanaged_svc: Option<*mut dyn Any>,
+ service_name: String,
+ service_version: String,
+ service_properties: HashMap<String, String>,
+}
+
+impl<T> ServiceRegistrationBuilder<'_, T> {
+ fn new(ctx: &BundleContext) -> ServiceRegistrationBuilder<T> {
+ ServiceRegistrationBuilder {
+ ctx,
+ register_async: false,
+ unregister_async: false,
+ svc: None,
+ unmanaged_svc: None,
+ service_name: "".to_string(),
+ service_version: "".to_string(),
+ service_properties: HashMap::new(),
+ }
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ fn with_service_name_if_not_set(&mut self) -> &mut Self {
+ if self.service_name.is_empty() {
+ self.service_name = type_name::<T>().to_string();
+ }
+ self
+ }
+
+ pub fn with_service(&mut self, svc: T) -> &mut Self {
+ self.svc = Some(Box::new(svc));
+ self.with_service_name_if_not_set()
+ }
+
+ pub fn with_unmanaged_service(&mut self, svc: *mut T) -> &mut Self {
+ self.unmanaged_svc = Some(svc);
+ self.with_service_name_if_not_set()
+ }
+
+ pub fn with_version(&mut self, version: &str) -> &mut Self {
+ self.service_version = version.to_string();
+ self
+ }
+
+ pub fn with_properties(&mut self, properties: HashMap<String, String>) -> &mut Self {
+ self.service_properties = properties;
+ self
+ }
+
+ pub fn with_property(&mut self, key: &str, value: &str) -> &mut Self {
+ self.service_properties
+ .insert(key.to_string(), value.to_string());
+ self
+ }
+
+ pub fn with_register_async(&mut self) -> &mut Self {
+ self.register_async = true;
+ self
+ }
+
+ pub fn with_register_sync(&mut self) -> &mut Self {
+ self.register_async = false;
+ self
+ }
+
+ pub fn with_unregister_async(&mut self) -> &mut Self {
+ self.unregister_async = true;
+ self
+ }
+
+ pub fn with_unregister_sync(&mut self) -> &mut Self {
+ self.unregister_async = false;
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ let mut valid = true;
+ if self.service_name.is_empty() {
+ self.ctx
+ .log_error("Cannot register service. Service name is empty");
+ valid = false;
+ }
+ if self.svc.is_none() && self.unmanaged_svc.is_none() {
+ self.ctx
+ .log_error("Cannot register service. No instance provided");
+ valid = false;
+ }
+ match valid {
+ true => Ok(()),
+ false => Err(Error::BundleException),
+ }
+ }
+
+ fn get_c_svc(svc_reg: &ServiceRegistration) -> *mut c_void {
+ if let Some(boxed_svc) = svc_reg.svc.as_ref() {
+ boxed_svc.deref() as *const dyn Any as *mut c_void
+ } else if let Some(unmanaged_svc) = svc_reg.unmanaged_svc {
+ unmanaged_svc as *mut c_void
+ } else {
+ null_mut()
+ }
+ }
+
+ pub fn build(&mut self) -> Result<ServiceRegistration, Error> {
+ self.validate()?;
+
+ let mut svc_reg = ServiceRegistration {
+ service_id: -1,
+ service_name: self.service_name.clone(),
+ unregister_async: self.unregister_async,
+ weak_ctx: self.ctx.get_self().clone(),
+ svc: if self.svc.is_none() { None } else { Some(self.svc.take().unwrap()) },
+ unmanaged_svc: self.unmanaged_svc,
+ };
+
+ let svc_ptr = Self::get_c_svc(&svc_reg);
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_service_version = std::ffi::CString::new(self.service_version.as_str()).unwrap();
+ let c_service_properties: *mut celix_properties_t;
+ unsafe {
+ c_service_properties = celix_properties_create();
+ for (key, value) in self.service_properties.iter() {
+ let c_key = std::ffi::CString::new(key.as_str()).unwrap();
+ let c_value = std::ffi::CString::new(value.as_str()).unwrap();
+ celix_properties_set(c_service_properties, c_key.as_ptr(), c_value.as_ptr());
+ }
+ }
+
+ let opts = celix_service_registration_options_t {
+ svc: svc_ptr,
+ factory: null_mut(),
+ serviceName: c_service_name.as_ptr() as *const i8,
+ properties: c_service_properties,
+ serviceLanguage: null_mut(),
+ serviceVersion: if self.service_version.is_empty() {
+ null_mut()
+ } else {
+ c_service_version.as_ptr() as *const i8
+ },
+ asyncData: null_mut(),
+ asyncCallback: None,
+ };
+
+ if self.register_async {
+ unsafe {
+ svc_reg.service_id = celix_bundleContext_registerServiceWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ }
+ } else {
+ unsafe {
+ svc_reg.service_id = celix_bundleContext_registerServiceWithOptionsAsync(self.ctx.get_c_bundle_context(), &opts);
+ }
+ }
+ if svc_reg.service_id >= 0 {
+ Ok(svc_reg)
+ } else {
+ Err(Error::BundleException)
+ }
+ }
+}
+
+pub struct ServiceUseBuilder<'a, T> {
+ ctx: &'a BundleContext,
+ many: bool,
+ service_name: String,
+ filter: String,
+ callback: Option<Box<dyn Fn(&T)>>, //note double boxed
+}
+
+impl<T> ServiceUseBuilder<'_, T> {
+ fn new(ctx: &BundleContext, many: bool) -> ServiceUseBuilder<T> {
+ ServiceUseBuilder {
+ ctx,
+ many,
+ service_name: type_name::<T>().to_string(),
+ filter: "".to_string(),
+ callback: None,
+ }
+ }
+
+ pub fn with_callback(&mut self, closure: Box<dyn Fn(&T)>)-> &mut Self {
+ self.callback = Some(closure);
+ self
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ pub fn with_filter(&mut self, filter: &str) -> &mut Self {
+ self.filter = filter.to_string();
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ if self.service_name.is_empty() {
+ return Err(Error::BundleException);
+ }
+ Ok(())
+ }
+
+ unsafe extern "C" fn use_service_c_callback(handle: *mut c_void, svc: *mut c_void) {
+ let closure = handle as *const Box<dyn Fn(&T)>;
+ let closure = closure.as_ref().unwrap();
+
+ let typed_svc = svc as *const T;
+ let typed_svc = typed_svc.as_ref().unwrap();
+
+ closure(typed_svc);
+ }
+
+ pub fn build(&mut self) -> Result<isize, Error> {
+ self.validate()?;
+
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_filter = std::ffi::CString::new(self.filter.as_str()).unwrap();
+ let c_service_name_ptr: *const i8 = c_service_name.as_ptr();
+ let c_filter_ptr: *const i8 = if self.filter.is_empty() { null_mut()} else {c_filter.as_ptr() };
+
+ let c_closure_ptr = self.callback.as_ref().unwrap() as *const Box<dyn Fn(&T)> as *mut c_void;
+
+ let opts = celix_service_use_options_t {
+ filter: celix_service_filter_options_t {
+ serviceName: c_service_name_ptr,
+ versionRange: null_mut(),
+ filter: c_filter_ptr,
+ serviceLanguage: null_mut(),
+ ignoreServiceLanguage: false,
+ },
+ waitTimeoutInSeconds: 0.0,
+ callbackHandle: c_closure_ptr,
+ use_: Some(Self::use_service_c_callback),
+ useWithProperties: None,
+ useWithOwner: None,
+ flags: 0,
+ };
+
+ if self.many {
+ unsafe {
+ let count = celix_bundleContext_useServicesWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ Ok(count as isize)
+ }
+ } else {
+ unsafe {
+ let called = celix_bundleContext_useServiceWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ if called {
+ Ok(1)
+ } else {
+ Ok(0)
+ }
+ }
+ }
+ }
+}
+
+struct ServiceTrackerCallbacks<T> {
+ set_callback: Option<Box<dyn Fn(Option<&T>)>>,
+ add_callback: Option<Box<dyn Fn(&T)>>,
+ remove_callback: Option<Box<dyn Fn(&T)>>,
+}
+
+pub struct ServiceTracker<T> {
+ ctx: Arc<BundleContext>,
Review Comment:
This does not feel right, since `BundleContext` has longer lifetime than any tracker associated with it.
This weirdness leads to unnatural `BundleContext::get_self`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327933445
##########
misc/experimental/rust/celix/Cargo.toml:
##########
@@ -0,0 +1,28 @@
+# 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.
+
+[package]
+name = "celix"
+version = "0.0.1"
+
+[dependencies]
+celix_bindings = { path = "../celix_bindings" }
+
+[lib]
+name = "celix"
+path = "src/lib.rs"
+crate-type = ["rlib"]
Review Comment:
Now it's OK to have static libcelix.
Considering its size, 200KB for x64 release build, it might be preferable to built as shared object.
But unfortunately I've not found any way to make BUILD_RPATH work for Corrosion, without which it is quite inconvenient to run the resulting binary in the build tree.
Even if we can add `BUILD_RPATH` via build.rs, I don't know how to make RPATH rewrite work.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328049031
##########
misc/experimental/rust/CMakeLists.txt:
##########
@@ -26,13 +26,12 @@ if (CELIX_RUST_EXPERIMENTAL)
FetchContent_MakeAvailable(Corrosion)
#Prepare a list of include paths needed to generating bindings for the Apache Celix C API
- file(GENERATE
- OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include_paths.txt"
- CONTENT "$<TARGET_PROPERTY:framework,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:utils,INTERFACE_INCLUDE_DIRECTORIES>"
- )
+ #Note for now this includes framework, utils and shell_api maybe this should be separated in the future.
+ file(GENERATE OUTPUT
+ "${CMAKE_CURRENT_BINARY_DIR}/include_paths.txt" CONTENT
+ "$<TARGET_PROPERTY:framework,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:utils,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:shell_api,INTERFACE_INCLUDE_DIRECTORIES>;$<TARGET_PROPERTY:Celix::log_service_api,INTERFACE_INCLUDE_DIRECTORIES>")
Review Comment:
`INCLUDE_DIRECTORIES` alone is not enough, we shall also have `COMPILE_DEFINITIONS` and `COMPILE_OPTIONS`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "codecov-commenter (via GitHub)" <gi...@apache.org>.
codecov-commenter commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-1697920904
## [Codecov](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
> Merging [#627](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (a5dca52) into [master](https://app.codecov.io/gh/apache/celix/commit/6cde7b48e6d6e34b22eee7d3f9fa73aa93d1663d?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) (6cde7b4) will **increase** coverage by `1.86%`.
> Report is 50 commits behind head on master.
> The diff coverage is `97.93%`.
> :exclamation: Current head a5dca52 differs from pull request most recent head 88deb83. Consider uploading reports for the commit 88deb83 to get more accurate results
```diff
@@ Coverage Diff @@
## master #627 +/- ##
==========================================
+ Coverage 79.71% 81.58% +1.86%
==========================================
Files 259 260 +1
Lines 34681 34680 -1
==========================================
+ Hits 27645 28292 +647
+ Misses 7036 6388 -648
```
| [Files Changed](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | Coverage Ξ | |
|---|---|---|
| [bundles/shell/shell/src/dm\_shell\_list\_command.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-YnVuZGxlcy9zaGVsbC9zaGVsbC9zcmMvZG1fc2hlbGxfbGlzdF9jb21tYW5kLmM=) | `0.00% <0.00%> (ΓΈ)` | |
| [libs/dfi/src/json\_serializer.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy9kZmkvc3JjL2pzb25fc2VyaWFsaXplci5j) | `82.83% <0.00%> (ΓΈ)` | |
| [libs/framework/include/celix\_bundle\_context.h](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy9mcmFtZXdvcmsvaW5jbHVkZS9jZWxpeF9idW5kbGVfY29udGV4dC5o) | `100.00% <ΓΈ> (ΓΈ)` | |
| [libs/framework/src/manifest\_parser.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy9mcmFtZXdvcmsvc3JjL21hbmlmZXN0X3BhcnNlci5j) | `92.85% <ΓΈ> (ΓΈ)` | |
| [libs/utils/src/celix\_err.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy91dGlscy9zcmMvY2VsaXhfZXJyLmM=) | `88.17% <93.75%> (+2.62%)` | :arrow_up: |
| [bundles/logging/log\_helper/src/celix\_log\_helper.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-YnVuZGxlcy9sb2dnaW5nL2xvZ19oZWxwZXIvc3JjL2NlbGl4X2xvZ19oZWxwZXIuYw==) | `100.00% <100.00%> (ΓΈ)` | |
| [...sub\_admin\_udp\_mc/src/pubsub\_udpmc\_topic\_receiver.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-YnVuZGxlcy9wdWJzdWIvcHVic3ViX2FkbWluX3VkcF9tYy9zcmMvcHVic3ViX3VkcG1jX3RvcGljX3JlY2VpdmVyLmM=) | `73.68% <100.00%> (+73.68%)` | :arrow_up: |
| [libs/framework/include/celix/BundleContext.h](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy9mcmFtZXdvcmsvaW5jbHVkZS9jZWxpeC9CdW5kbGVDb250ZXh0Lmg=) | `100.00% <100.00%> (ΓΈ)` | |
| [libs/framework/include\_deprecated/manifest.h](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy9mcmFtZXdvcmsvaW5jbHVkZV9kZXByZWNhdGVkL21hbmlmZXN0Lmg=) | `100.00% <100.00%> (ΓΈ)` | |
| [libs/framework/src/bundle\_archive.c](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache#diff-bGlicy9mcmFtZXdvcmsvc3JjL2J1bmRsZV9hcmNoaXZlLmM=) | `100.00% <100.00%> (ΓΈ)` | |
| ... and [5 more](https://app.codecov.io/gh/apache/celix/pull/627?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) | |
... and [10 files with indirect coverage changes](https://app.codecov.io/gh/apache/celix/pull/627/indirect-changes?src=pr&el=tree-more&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
:mega: Weβre building smart automated test selection to slash your CI/CD build times. [Learn more](https://about.codecov.io/iterative-testing/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1312714746
##########
misc/experimental/rust/CMakeLists.txt:
##########
@@ -26,13 +26,12 @@ if (CELIX_RUST_EXPERIMENTAL)
FetchContent_MakeAvailable(Corrosion)
Review Comment:
According to documentation, this method only work for CMake >= 3.19.
Our current requirement is `cmake_minimum_required (VERSION 3.18)`.
##########
misc/experimental/rust/CMakeLists.txt:
##########
@@ -26,13 +26,12 @@ if (CELIX_RUST_EXPERIMENTAL)
FetchContent_MakeAvailable(Corrosion)
Review Comment:
According to documentation, this method only works for CMake >= 3.19.
Our current requirement is `cmake_minimum_required (VERSION 3.18)`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1329166795
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -17,23 +17,37 @@
* under the License.
*/
-extern crate bindgen;
-
+use std::env;
use std::error::Error;
-use std::path::PathBuf;
use std::fs::File;
use std::io::{self, BufRead};
-use std::env;
+use std::path::PathBuf;
-fn print_include_paths() -> Result<Vec<String>, Box<dyn Error>> {
- let build_dir = PathBuf::from(env::var("CORROSION_BUILD_DIR").unwrap());
- let include_path_file = build_dir.join("include_paths.txt");
+fn open_include_paths_file() -> Result<File, Box<dyn Error>> {
+ let file: File;
- //let include_path_file = Path::new("include_paths.txt");
- let file = File::open(&include_path_file)?;
+ let corrosion_build_dir = env::var("CORROSION_BUILD_DIR");
+ if corrosion_build_dir.is_ok() {
+ let build_dir = PathBuf::from(corrosion_build_dir.unwrap());
+ let include_path_file = build_dir.join("include_paths.txt");
+ file = File::open(&include_path_file)?;
+ } else {
+ println!("include_paths.txt not found in CORROSION_BUILD_DIR. Failing back to CELIX_RUST_INCLUDE_PATHS_FILE env value");
Review Comment:
This is intentionally, but does not really work. So I will remove the else branch.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327977210
##########
misc/experimental/rust/rust_shell_api/src/lib.rs:
##########
@@ -0,0 +1,30 @@
+/*
+ * 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.
+ */
+
+extern crate celix;
+
+use celix::Error;
+pub const COMMAND_NAME: &str = "command.name";
+pub const COMMAND_USAGE: &str = "command.usage";
+pub const COMMAND_DESCRIPTION: &str = "command.description";
+
+#[doc = "A trait to implement a Celix Shell Command"]
+pub trait RustShellCommand {
+ fn execute_command(&self, command_line: &str) -> Result<(), Error>;
Review Comment:
`&self` or `&mut self`? What if a command execution changes the internal states?
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328052333
##########
misc/experimental/rust/celix_bindings/build.rs:
##########
@@ -44,19 +58,15 @@ fn main() {
println!("cargo:info=Start build.rs for celix_bindings");
let include_paths = print_include_paths().unwrap();
Review Comment:
Misisng `cargo:rustc-link-lib=` leads to no DT_NEEDED for `framework` and `utils` shared objects.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328072986
##########
misc/experimental/rust/celix/src/log_helper.rs:
##########
@@ -0,0 +1,113 @@
+/*
+ * 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.
+ */
+
+use std::sync::{Arc, Mutex, RwLock};
+
+use super::BundleContext;
+use super::LogLevel;
+
+use celix_bindings::celix_log_service_t;
+use crate::ServiceTracker;
+
+pub struct LogHelper {
+ name: String,
+ tracker: Mutex<Option<ServiceTracker<celix_log_service_t>>>,
+ log_svc: RwLock<Option<*const celix_log_service_t>>,
+}
+
+impl LogHelper {
+
+ pub fn new(ctx: Arc<BundleContext>, name: &str) -> Arc<Self> {
+ let helper = Arc::new(LogHelper{
+ name: name.to_string(),
+ tracker: Mutex::new(None),
+ log_svc: RwLock::new(None),
+ });
+ let filter = format!("(name={})", name);
+ let weak_helper = Arc::downgrade(&helper);
+ let tracker = ctx.track_services::<celix_log_service_t>()
Review Comment:
I think the weirdness of the tracker mutex comes from this: providing a half-constructed object (log helper) to another object (tracker).
If we permit a new log helper holding a None tracker and opening the tracker after new, then `LogHelper::new` returns Self rather than `Arc<Self>`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328338319
##########
misc/experimental/rust/celix/src/bundle_activator.rs:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.
+ */
+
+use std::sync::Arc;
+
+use super::BundleContext;
+use super::Error;
+
+pub trait BundleActivator {
+ fn new(ctx: Arc<BundleContext>) -> Self;
+ fn start(&mut self) -> Result<(), Error> {
+ /* Default implementation */
+ Ok(())
+ }
+ fn stop(&mut self) -> Result<(), Error> {
+ /* Default implementation */
+ Ok(())
+ }
+}
+
+#[macro_export]
+macro_rules! generate_bundle_activator {
+ ($activator:ty) => {
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_create(
+ ctx: *mut $crate::details::CBundleContext,
+ out: *mut *mut ::std::ffi::c_void,
+ ) -> $crate::details::CStatus {
+ let boxed_context = $crate::bundle_context_new(ctx);
+ let mut arc_context = Arc::from(boxed_context);
+ let activator = <$activator>::new(arc_context);
+ *out = Box::into_raw(Box::new(activator)) as *mut ::std::ffi::c_void;
+ $crate::CELIX_SUCCESS
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_start(
+ handle: *mut ::std::ffi::c_void,
+ ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let activator = &mut *(handle as *mut $activator);
+ let result = activator.start();
+ match result {
+ Ok(_) => $crate::CELIX_SUCCESS,
+ Err(e) => e.into(),
+ }
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_stop(
+ handle: *mut ::std::ffi::c_void,
+ ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let activator = &mut *(handle as *mut $activator);
+ let result = activator.stop();
+ match result {
+ Ok(_) => $crate::CELIX_SUCCESS,
+ Err(e) => e.into(),
+ }
+ }
+
+ #[no_mangle]
+ pub unsafe extern "C" fn celix_bundleActivator_destroy(
+ handle: *mut ::std::ffi::c_void,
+ _ctx: *mut $crate::details::CBundleContext,
+ ) -> $crate::details::CStatus {
+ let reclaimed_activator = Box::from_raw(handle as *mut $activator);
Review Comment:
This does not feel right. In the C version, activator is dropped after `celix_bundleContext_waitForEvents(ctx)`.
IMHO, the best fix is to change the corresponding logic in `celix_framework_stopBundleEntryInternal`:
From
```C
status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
```
To
```C
status = CELIX_DO_IF(status, bundle_getContext(bndEntry->bnd, &context));
if (status == CELIX_SUCCESS) {
if (activator->stop != NULL) {
status = CELIX_DO_IF(status, activator->stop(activator->userData, context));
if (status == CELIX_SUCCESS) {
celix_dependency_manager_t *mng = celix_bundleContext_getDependencyManager(context);
celix_dependencyManager_removeAllComponents(mng);
}
}
}
if (bndEntry->bndId >= CELIX_FRAMEWORK_BUNDLE_ID) {
//framework and "normal" bundle
celix_framework_waitUntilNoEventsForBnd(framework, bndEntry->bndId);
celix_bundleContext_cleanup(bndEntry->bnd->context);
}
if (status == CELIX_SUCCESS) {
if (activator->destroy != NULL) {
status = CELIX_DO_IF(status, activator->destroy(activator->userData, context));
}
}
```
So that we can remove `celix_bundleContext_waitForEvents(ctx)` from the generated `celix_bundleActivator_destroy`.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1328459473
##########
misc/experimental/rust/celix/src/bundle_context.rs:
##########
@@ -0,0 +1,669 @@
+/*
+ * 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.
+ */
+
+use std::any::type_name;
+use std::any::Any;
+use std::collections::HashMap;
+use std::ffi::c_void;
+use std::ops::Deref;
+use std::ptr::null_mut;
+use std::sync::Arc;
+use std::sync::Mutex;
+use std::sync::Weak;
+
+use celix_bindings::{celix_bundleContext_log, celix_bundleContext_stopTracker, celix_bundleContext_stopTrackerAsync, celix_bundleContext_trackServicesWithOptions, celix_bundleContext_trackServicesWithOptionsAsync, celix_properties_t};
+use celix_bindings::celix_service_filter_options;
+use celix_bindings::celix_bundleContext_registerServiceWithOptionsAsync;
+use celix_bindings::celix_bundleContext_unregisterServiceAsync;
+use celix_bindings::celix_service_tracking_options_t;
+use celix_bindings::celix_service_use_options_t;
+use celix_bindings::celix_service_filter_options_t;
+use celix_bindings::celix_bundleContext_useServicesWithOptions;
+use celix_bindings::celix_bundleContext_useServiceWithOptions;
+use celix_bindings::celix_bundleContext_registerServiceWithOptions;
+use celix_bindings::celix_bundleContext_unregisterService;
+use celix_bindings::celix_bundle_context_t;
+use celix_bindings::celix_properties_create;
+use celix_bindings::celix_properties_set;
+use celix_bindings::celix_service_registration_options_t;
+
+use super::Error;
+use super::LogLevel;
+
+pub struct ServiceRegistration {
+ service_id: i64,
+ service_name: String,
+ unregister_async: bool,
+ weak_ctx: Weak<BundleContext>,
+ svc: Option<Box<dyn Any>>,
+ unmanaged_svc: Option<*mut dyn Any>,
+}
+
+impl ServiceRegistration {
+ pub fn get_service_id(&self) -> i64 {
+ self.service_id
+ }
+
+ pub fn get_service_name(&self) -> &str {
+ self.service_name.as_str()
+ }
+
+ pub fn get_service(&self) -> Option<&dyn Any> {
+ if let Some(boxed_svc) = self.svc.as_ref() {
+ Some(boxed_svc.as_ref())
+ } else if let Some(unmanaged_svc) = self.unmanaged_svc {
+ unsafe {
+ Some(&*unmanaged_svc)
+ }
+ } else {
+ None
+ }
+ }
+}
+
+impl Drop for ServiceRegistration {
+ fn drop(&mut self) {
+ let ctx = self.weak_ctx.upgrade();
+ match ctx {
+ Some(ctx) => ctx.unregister_service(self.service_id, self.unregister_async),
+ None => println!("Cannot unregister ServiceRegistration: BundleContext is gone"),
+ }
+ }
+}
+
+pub struct ServiceRegistrationBuilder<'a, T: ?Sized + 'static> {
+ ctx: &'a BundleContext,
+ register_async: bool,
+ unregister_async: bool,
+ svc: Option<Box<T>>, //note box is needed for stable pointer value
+ unmanaged_svc: Option<*mut dyn Any>,
+ service_name: String,
+ service_version: String,
+ service_properties: HashMap<String, String>,
+}
+
+impl<T> ServiceRegistrationBuilder<'_, T> {
+ fn new(ctx: &BundleContext) -> ServiceRegistrationBuilder<T> {
+ ServiceRegistrationBuilder {
+ ctx,
+ register_async: false,
+ unregister_async: false,
+ svc: None,
+ unmanaged_svc: None,
+ service_name: "".to_string(),
+ service_version: "".to_string(),
+ service_properties: HashMap::new(),
+ }
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ fn with_service_name_if_not_set(&mut self) -> &mut Self {
+ if self.service_name.is_empty() {
+ self.service_name = type_name::<T>().to_string();
+ }
+ self
+ }
+
+ pub fn with_service(&mut self, svc: T) -> &mut Self {
+ self.svc = Some(Box::new(svc));
+ self.with_service_name_if_not_set()
+ }
+
+ pub fn with_unmanaged_service(&mut self, svc: *mut T) -> &mut Self {
+ self.unmanaged_svc = Some(svc);
+ self.with_service_name_if_not_set()
+ }
+
+ pub fn with_version(&mut self, version: &str) -> &mut Self {
+ self.service_version = version.to_string();
+ self
+ }
+
+ pub fn with_properties(&mut self, properties: HashMap<String, String>) -> &mut Self {
+ self.service_properties = properties;
+ self
+ }
+
+ pub fn with_property(&mut self, key: &str, value: &str) -> &mut Self {
+ self.service_properties
+ .insert(key.to_string(), value.to_string());
+ self
+ }
+
+ pub fn with_register_async(&mut self) -> &mut Self {
+ self.register_async = true;
+ self
+ }
+
+ pub fn with_register_sync(&mut self) -> &mut Self {
+ self.register_async = false;
+ self
+ }
+
+ pub fn with_unregister_async(&mut self) -> &mut Self {
+ self.unregister_async = true;
+ self
+ }
+
+ pub fn with_unregister_sync(&mut self) -> &mut Self {
+ self.unregister_async = false;
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ let mut valid = true;
+ if self.service_name.is_empty() {
+ self.ctx
+ .log_error("Cannot register service. Service name is empty");
+ valid = false;
+ }
+ if self.svc.is_none() && self.unmanaged_svc.is_none() {
+ self.ctx
+ .log_error("Cannot register service. No instance provided");
+ valid = false;
+ }
+ match valid {
+ true => Ok(()),
+ false => Err(Error::BundleException),
+ }
+ }
+
+ fn get_c_svc(svc_reg: &ServiceRegistration) -> *mut c_void {
+ if let Some(boxed_svc) = svc_reg.svc.as_ref() {
+ boxed_svc.deref() as *const dyn Any as *mut c_void
+ } else if let Some(unmanaged_svc) = svc_reg.unmanaged_svc {
+ unmanaged_svc as *mut c_void
+ } else {
+ null_mut()
+ }
+ }
+
+ pub fn build(&mut self) -> Result<ServiceRegistration, Error> {
+ self.validate()?;
+
+ let mut svc_reg = ServiceRegistration {
+ service_id: -1,
+ service_name: self.service_name.clone(),
+ unregister_async: self.unregister_async,
+ weak_ctx: self.ctx.get_self().clone(),
+ svc: if self.svc.is_none() { None } else { Some(self.svc.take().unwrap()) },
+ unmanaged_svc: self.unmanaged_svc,
+ };
+
+ let svc_ptr = Self::get_c_svc(&svc_reg);
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_service_version = std::ffi::CString::new(self.service_version.as_str()).unwrap();
+ let c_service_properties: *mut celix_properties_t;
+ unsafe {
+ c_service_properties = celix_properties_create();
+ for (key, value) in self.service_properties.iter() {
+ let c_key = std::ffi::CString::new(key.as_str()).unwrap();
+ let c_value = std::ffi::CString::new(value.as_str()).unwrap();
+ celix_properties_set(c_service_properties, c_key.as_ptr(), c_value.as_ptr());
+ }
+ }
+
+ let opts = celix_service_registration_options_t {
+ svc: svc_ptr,
+ factory: null_mut(),
+ serviceName: c_service_name.as_ptr() as *const i8,
+ properties: c_service_properties,
+ serviceLanguage: null_mut(),
+ serviceVersion: if self.service_version.is_empty() {
+ null_mut()
+ } else {
+ c_service_version.as_ptr() as *const i8
+ },
+ asyncData: null_mut(),
+ asyncCallback: None,
+ };
+
+ if self.register_async {
+ unsafe {
+ svc_reg.service_id = celix_bundleContext_registerServiceWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ }
+ } else {
+ unsafe {
+ svc_reg.service_id = celix_bundleContext_registerServiceWithOptionsAsync(self.ctx.get_c_bundle_context(), &opts);
+ }
+ }
+ if svc_reg.service_id >= 0 {
+ Ok(svc_reg)
+ } else {
+ Err(Error::BundleException)
+ }
+ }
+}
+
+pub struct ServiceUseBuilder<'a, T> {
+ ctx: &'a BundleContext,
+ many: bool,
+ service_name: String,
+ filter: String,
+ callback: Option<Box<dyn Fn(&T)>>, //note double boxed
+}
+
+impl<T> ServiceUseBuilder<'_, T> {
+ fn new(ctx: &BundleContext, many: bool) -> ServiceUseBuilder<T> {
+ ServiceUseBuilder {
+ ctx,
+ many,
+ service_name: type_name::<T>().to_string(),
+ filter: "".to_string(),
+ callback: None,
+ }
+ }
+
+ pub fn with_callback(&mut self, closure: Box<dyn Fn(&T)>)-> &mut Self {
+ self.callback = Some(closure);
+ self
+ }
+
+ pub fn with_service_name(&mut self, name: &str) -> &mut Self {
+ self.service_name = name.to_string();
+ self
+ }
+
+ pub fn with_filter(&mut self, filter: &str) -> &mut Self {
+ self.filter = filter.to_string();
+ self
+ }
+
+ fn validate(&self) -> Result<(), Error> {
+ if self.service_name.is_empty() {
+ return Err(Error::BundleException);
+ }
+ Ok(())
+ }
+
+ unsafe extern "C" fn use_service_c_callback(handle: *mut c_void, svc: *mut c_void) {
+ let closure = handle as *const Box<dyn Fn(&T)>;
+ let closure = closure.as_ref().unwrap();
+
+ let typed_svc = svc as *const T;
+ let typed_svc = typed_svc.as_ref().unwrap();
+
+ closure(typed_svc);
+ }
+
+ pub fn build(&mut self) -> Result<isize, Error> {
+ self.validate()?;
+
+ let c_service_name = std::ffi::CString::new(self.service_name.as_str()).unwrap();
+ let c_filter = std::ffi::CString::new(self.filter.as_str()).unwrap();
+ let c_service_name_ptr: *const i8 = c_service_name.as_ptr();
+ let c_filter_ptr: *const i8 = if self.filter.is_empty() { null_mut()} else {c_filter.as_ptr() };
+
+ let c_closure_ptr = self.callback.as_ref().unwrap() as *const Box<dyn Fn(&T)> as *mut c_void;
+
+ let opts = celix_service_use_options_t {
+ filter: celix_service_filter_options_t {
+ serviceName: c_service_name_ptr,
+ versionRange: null_mut(),
+ filter: c_filter_ptr,
+ serviceLanguage: null_mut(),
+ ignoreServiceLanguage: false,
+ },
+ waitTimeoutInSeconds: 0.0,
+ callbackHandle: c_closure_ptr,
+ use_: Some(Self::use_service_c_callback),
+ useWithProperties: None,
+ useWithOwner: None,
+ flags: 0,
+ };
+
+ if self.many {
+ unsafe {
+ let count = celix_bundleContext_useServicesWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ Ok(count as isize)
+ }
+ } else {
+ unsafe {
+ let called = celix_bundleContext_useServiceWithOptions(self.ctx.get_c_bundle_context(), &opts);
+ if called {
+ Ok(1)
+ } else {
+ Ok(0)
+ }
+ }
+ }
+ }
+}
+
+struct ServiceTrackerCallbacks<T> {
+ set_callback: Option<Box<dyn Fn(Option<&T>)>>,
+ add_callback: Option<Box<dyn Fn(&T)>>,
+ remove_callback: Option<Box<dyn Fn(&T)>>,
+}
+
+pub struct ServiceTracker<T> {
+ ctx: Arc<BundleContext>,
Review Comment:
Lifetime Order: BundleContext > Activator > Loggerhelper > ServiceTracker ...
If we can enforce such order using Rust type system, then lots of weirdness will disappear.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327933445
##########
misc/experimental/rust/celix/Cargo.toml:
##########
@@ -0,0 +1,28 @@
+# 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.
+
+[package]
+name = "celix"
+version = "0.0.1"
+
+[dependencies]
+celix_bindings = { path = "../celix_bindings" }
+
+[lib]
+name = "celix"
+path = "src/lib.rs"
+crate-type = ["rlib"]
Review Comment:
Now it's OK to have static libcelix.
Considering its size, 200KB for x64 release build, it might be preferable to built as shared object.
But unfortunately I've not found any way to make BUILD_RPATH work for Corrosion.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
Given that it has already been specified in the manifest, I'm really confused why it was needed here?
If the above line is removed, then it fails to build with unresolved import `rust_shell_api` error.
However, removing `extern crate bindgen;` in build.rs will not cause any errors.
I also found that it is claimed that "extern crate is no longer needed in 99% of circumstances"(https://doc.rust-lang.org/edition-guide/rust-2018/path-changes.html). My Rust installation is the latest.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "PengZheng (via GitHub)" <gi...@apache.org>.
PengZheng commented on code in PR #627:
URL: https://github.com/apache/celix/pull/627#discussion_r1327970443
##########
misc/experimental/rust/celix/src/lib.rs:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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.
+ */
+
+extern crate celix_bindings;
Review Comment:
I'm really confused why it was needed here?
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Feature/599 provide and use c service in rust (celix)
Posted by "pnoltes (via GitHub)" <gi...@apache.org>.
pnoltes commented on PR #627:
URL: https://github.com/apache/celix/pull/627#issuecomment-2081638679
Thanks for the update.
>
> **POC approach using Weak and Arc** In the POC I think this was tried to achieve by using `std::sync::Weak` pointers. However I am not sure if this is entirely sound. For example when building the Servicetracker we upgrade the `Weak` to an `Arc`. What happens if this is done in another thread and immediately after upgrading the `Weak` and before actually tracking the service the bundle gets stopped and destroyed. I think we would have a use after free situation here.
You are correct, the usage of `Weak` mimics what is done in C++. Note that in C++ de bundle deactivates waits until all upgraded `Weak` (`std::weak_ptr`) to `Arc` (`std::shared_ptr`) are out of scope and prints an error when this takes too long.
But I agree, ideally this is improved using Rust lifecycles.
And great to see, and impressive, that you already made some progress with Rust lifecycles and Apache Celix.
The coming week I will not have the time have a look at the code and give feedback, but when I have to time I will pick this up.
--
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.
To unsubscribe, e-mail: dev-unsubscribe@celix.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org