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-unsubscr...@celix.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org