Changeset View
Changeset View
Standalone View
Standalone View
native/native_rust_library/src/lib.rs
use crate::ffi::get42Callback; | |||||
use lazy_static::lazy_static; | use lazy_static::lazy_static; | ||||
use std::sync::Arc; | use std::sync::Arc; | ||||
use tokio::runtime::{Builder, Runtime}; | use tokio::runtime::{Builder, Runtime}; | ||||
use tokio::sync::mpsc::{Receiver, Sender}; | |||||
use tokio::sync::Mutex; | |||||
use tokio::time::{sleep, Duration}; | |||||
use tonic::{transport::Channel, Status}; | use tonic::{transport::Channel, Status}; | ||||
use tracing::instrument; | use tracing::instrument; | ||||
use tunnelbroker::tunnelbroker_service_client::TunnelbrokerServiceClient; | use tunnelbroker::tunnelbroker_service_client::TunnelbrokerServiceClient; | ||||
mod crypto_tools; | mod crypto_tools; | ||||
mod identity_client; | mod identity_client; | ||||
mod identity { | mod identity { | ||||
tonic::include_proto!("identity.keyserver"); | tonic::include_proto!("identity.keyserver"); | ||||
Show All 9 Lines | lazy_static! { | ||||
pub static ref RUNTIME: Arc<Runtime> = Arc::new( | pub static ref RUNTIME: Arc<Runtime> = Arc::new( | ||||
Builder::new_multi_thread() | Builder::new_multi_thread() | ||||
.worker_threads(1) | .worker_threads(1) | ||||
.max_blocking_threads(1) | .max_blocking_threads(1) | ||||
.enable_all() | .enable_all() | ||||
.build() | .build() | ||||
.unwrap() | .unwrap() | ||||
); | ); | ||||
pub static ref CHANNEL: Mutex<(Sender<(f64, u32)>, Receiver<(f64, u32)>)> = | |||||
Mutex::new(tokio::sync::mpsc::channel(100)); | |||||
} | } | ||||
#[cxx::bridge] | #[cxx::bridge] | ||||
mod ffi { | mod ffi { | ||||
enum DeviceType { | enum DeviceType { | ||||
KEYSERVER, | KEYSERVER, | ||||
WEB, | WEB, | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | extern "Rust" { | ||||
// Tunnelbroker Service Client | // Tunnelbroker Service Client | ||||
type TunnelbrokerClient; | type TunnelbrokerClient; | ||||
#[cxx_name = "TunnelbrokerInitializeClient"] | #[cxx_name = "TunnelbrokerInitializeClient"] | ||||
fn initialize_tunnelbroker_client(addr: String) -> Box<TunnelbrokerClient>; | fn initialize_tunnelbroker_client(addr: String) -> Box<TunnelbrokerClient>; | ||||
// Crypto Tools | // Crypto Tools | ||||
fn generate_device_id(device_type: DeviceType) -> Result<String>; | fn generate_device_id(device_type: DeviceType) -> Result<String>; | ||||
// Test | |||||
fn get_42(counter: u32); | |||||
} | } | ||||
unsafe extern "C++" { | |||||
include!("RustCallback.h"); | |||||
#[namespace = "comm"] | |||||
fn get42Callback(error: String, counter: u32, ret: f64); | |||||
} | |||||
} | |||||
fn get_42(counter: u32) { | |||||
println!("I SEE YOU!!!"); | |||||
get_42_helper(counter); | |||||
} | |||||
fn get_42_helper(counter: u32) { | |||||
RUNTIME.spawn(async move { | |||||
sleep(Duration::from_secs(10)).await; | |||||
println!("awake now!"); | |||||
get42Callback("".to_string(), counter, 42.0); | |||||
}); | |||||
} | } | ||||
#[derive(Debug)] | #[derive(Debug)] | ||||
pub struct IdentityClient { | pub struct IdentityClient { | ||||
identity_client: IdentityKeyserverServiceClient<Channel>, | identity_client: IdentityKeyserverServiceClient<Channel>, | ||||
} | } | ||||
fn initialize_identity_client(addr: String) -> Box<IdentityClient> { | fn initialize_identity_client(addr: String) -> Box<IdentityClient> { | ||||
▲ Show 20 Lines • Show All 92 Lines • Show Last 20 Lines |