Skip to content

Commit

Permalink
cpp: init Client and logger
Browse files Browse the repository at this point in the history
Signed-off-by: Yuki Kishimoto <[email protected]>
  • Loading branch information
yukibtc committed Nov 4, 2024
1 parent 5c8f921 commit c3f4b6d
Show file tree
Hide file tree
Showing 11 changed files with 184 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bindings/nostr-sdk-cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
*.h
3 changes: 3 additions & 0 deletions bindings/nostr-sdk-cpp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ crate-type = ["staticlib"]
anyhow = "1.0"
cxx = "1.0"
nostr-sdk = { workspace = true, features = ["all-nips"] }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true, features = ["std"] }
tracing-subscriber.workspace = true

[build-dependencies]
cxx-build = "1.0"
Expand Down
8 changes: 8 additions & 0 deletions bindings/nostr-sdk-cpp/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@
// Distributed under the MIT software license

use std::fs;
use std::process::Command;

use glob::glob;

fn main() {
if let Ok(output) = Command::new("git").args(["rev-parse", "HEAD"]).output() {
if let Ok(git_hash) = String::from_utf8(output.stdout) {
println!("cargo:rerun-if-changed={git_hash}");
println!("cargo:rustc-env=GIT_HASH={git_hash}");
}
}

let mut files: Vec<String> = Vec::new();

// Recursively find all .rs files in src/ directory
Expand Down
28 changes: 28 additions & 0 deletions bindings/nostr-sdk-cpp/examples/client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <iostream>
#include <string>
#include <memory>

#include <nostr_sdk.h>

int main() {
try {
// Init logger
logger::init(logger::LogLevel::Debug);

// New client without signer
auto client = client::without_signer();

// Add relays
client->add_relay("wss://relay.damus.io");

// Connect
client->connect();

// Send event
std::string jsonString = R"({"content":"Think about this.\n\nThe most powerful centralized institutions in the world have been replaced by a protocol that protects the individual. #bitcoin\n\nDo you doubt that we can replace everything else?\n\nBullish on the future of humanity\nnostr:nevent1qqs9ljegkuk2m2ewfjlhxy054n6ld5dfngwzuep0ddhs64gc49q0nmqpzdmhxue69uhhyetvv9ukzcnvv5hx7un8qgsw3mfhnrr0l6ll5zzsrtpeufckv2lazc8k3ru5c3wkjtv8vlwngksrqsqqqqqpttgr27","created_at":1703184271,"id":"38acf9b08d06859e49237688a9fd6558c448766f47457236c2331f93538992c6","kind":1,"pubkey":"e8ed3798c6ffebffa08501ac39e271662bfd160f688f94c45d692d8767dd345a","sig":"f76d5ecc8e7de688ac12b9d19edaacdcffb8f0c8fa2a44c00767363af3f04dbc069542ddc5d2f63c94cb5e6ce701589d538cf2db3b1f1211a96596fabb6ecafe","tags":[["e","5fcb28b72cadab2e4cbf7311f4acf5f6d1a99a1c2e642f6b6f0d5518a940f9ec","","mention"],["p","e8ed3798c6ffebffa08501ac39e271662bfd160f688f94c45d692d8767dd345a","","mention"],["t","bitcoin"],["t","bitcoin"]]})";
client->send_event(jsonString);
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}
Empty file.
7 changes: 7 additions & 0 deletions bindings/nostr-sdk-cpp/include/nostr_sdk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once

#include <rust/cxx.h>
#include <nostr-sdk-cpp/src/lib.rs.h>
#include <nostr-sdk-cpp/src/logger.rs.h>
#include <nostr-sdk-cpp/src/client/mod.rs.h>
#include <nostr-sdk-cpp/src/protocol/key/mod.rs.h>
7 changes: 7 additions & 0 deletions bindings/nostr-sdk-cpp/justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/env just --justfile

build:
cargo build --release

example: build
g++ examples/client.cpp -o client -I include/ -I ../../target/x86_64-unknown-linux-gnu/cxxbridge/ -L ../../target/x86_64-unknown-linux-gnu/release/ -lnostr_sdk_cpp
57 changes: 57 additions & 0 deletions bindings/nostr-sdk-cpp/src/client/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use anyhow::Result;
use cxx::CxxString;
use nostr_sdk::client;
use nostr_sdk::nostr::{Event, JsonUtil};

use crate::RUNTIME;

#[cxx::bridge(namespace = "client")]
mod ffi {
extern "Rust" {
type Client;

fn without_signer() -> Box<Client>;

fn add_relay(&self, url: &CxxString) -> Result<bool>;

fn connect(&self);

fn send_event(&self, json: &CxxString) -> Result<()>;
}
}

pub struct Client {
inner: client::Client,
}

fn without_signer() -> Box<Client> {
Box::new(Client {
inner: client::Client::default(),
})
}

impl Client {
pub fn add_relay(&self, url: &CxxString) -> Result<bool> {
RUNTIME.block_on(async {
let url: &str = url.to_str()?;
Ok(self.inner.add_relay(url).await?)
})
}

pub fn connect(&self) {
RUNTIME.block_on(async { self.inner.connect().await })
}

pub fn send_event(&self, json: &CxxString) -> Result<()> {
RUNTIME.block_on(async {
let json: &str = json.to_str()?;
let event = Event::from_json(json)?;
self.inner.send_event(event).await?;
Ok(())
})
}
}
20 changes: 20 additions & 0 deletions bindings/nostr-sdk-cpp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,24 @@
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use std::sync::LazyLock;

use tokio::runtime::Runtime;

pub mod client;
pub mod logger;
pub mod protocol;

static RUNTIME: LazyLock<Runtime> =
LazyLock::new(|| Runtime::new().expect("failed to create tokio runtime"));

pub fn git_hash_version() -> String {
option_env!("GIT_HASH").unwrap_or_default().to_owned()
}

#[cxx::bridge]
mod ffi {
extern "Rust" {
fn git_hash_version() -> String;
}
}
51 changes: 51 additions & 0 deletions bindings/nostr-sdk-cpp/src/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

use tracing::Level;

use super::git_hash_version;

#[cxx::bridge(namespace = "logger")]
mod ffi {
enum LogLevel {
Error,
Warn,
Info,
Debug,
Trace,
}

extern "Rust" {
fn init(level: LogLevel);
}
}

impl From<ffi::LogLevel> for Level {
fn from(value: ffi::LogLevel) -> Self {
match value {
ffi::LogLevel::Trace => Self::TRACE,
ffi::LogLevel::Debug => Self::DEBUG,
ffi::LogLevel::Info => Self::INFO,
ffi::LogLevel::Warn => Self::WARN,
ffi::LogLevel::Error => Self::ERROR,
_ => unreachable!(),
}
}
}

pub fn init(level: ffi::LogLevel) {
let level: Level = level.into();
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(level)
.finish();
match tracing::subscriber::set_global_default(subscriber) {
Ok(_) => {
tracing::info!("Desktop logger initialized");

// Log git hash (defined at compile time)
tracing::info!("Git hash: {}", git_hash_version())
}
Err(e) => eprintln!("Impossible to init desktop logger: {e}"),
}
}

0 comments on commit c3f4b6d

Please sign in to comment.