Connection

Struct Connection 

Source
pub struct Connection { /* private fields */ }
Expand description

A connection to the HyveOS runtime.

This struct provides access to the various services provided by HyveOS.

By default, the connection to the HyveOS runtime will be made through the scripting bridge, i.e., the Unix domain socket specified by the HYVEOS_BRIDGE_SOCKET environment variable (hyveos_core::BRIDGE_SOCKET_ENV_VAR) will be used to communicate with the runtime. If another connection type is desired, use the Self::builder function to get a ConnectionBuilder and use the ConnectionBuilder::custom or ConnectionBuilder::uri methods.

§Example

use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut discovery_service = connection.discovery();
let peer_id = discovery_service.get_own_id().await.unwrap();

println!("My peer id: {peer_id}");

Implementations§

Source§

impl Connection

Source

pub async fn new() -> Result<Self>

Establishes a connection to the HyveOS runtime through the scripting bridge.

The Unix domain socket specified by the HYVEOS_BRIDGE_SOCKET environment variable (hyveos_core::BRIDGE_SOCKET_ENV_VAR) will be used to communicate with the runtime. If another connection type is desired, use the Self::builder function to get a ConnectionBuilder and use the ConnectionBuilder::custom or ConnectionBuilder::uri methods.

§Errors

Returns an error if the connection could not be established.

§Example
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut discovery_service = connection.discovery();
let peer_id = discovery_service.get_own_id().await.unwrap();

println!("My peer id: {peer_id}");
Source

pub fn builder() -> ConnectionBuilder<BridgeConnection>

Creates a new builder for configuring a connection to the HyveOS runtime.

By default, the connection to the HyveOS runtime will be made through the scripting bridge, i.e., the Unix domain socket specified by the HYVEOS_BRIDGE_SOCKET environment variable (hyveos_core::BRIDGE_SOCKET_ENV_VAR) will be used to communicate with the runtime. If another connection type is desired, use the ConnectionBuilder::custom or ConnectionBuilder::uri methods.

§Example
use hyveos_sdk::Connection;

let connection = Connection::builder()
    .custom("/path/to/hyveos.sock", "/path/to/shared/dir")
    .connect()
    .await
    .unwrap();
let mut discovery_service = connection.discovery();
let peer_id = discovery_service.get_own_id().await.unwrap();

println!("My peer id: {peer_id}");
Source

pub fn db(&self) -> DbService

Returns a handle to the database service.

§Example
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut db_service = connection.db();
assert!(db_service.put("key", b"value").await.unwrap().is_none());

let value = db_service.get("key").await.unwrap().unwrap();
assert_eq!(value, b"value");
Source

pub fn debug(&self) -> DebugService

Returns a handle to the debug service.

§Example
use futures::TryStreamExt as _;
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut debug_service = connection.debug();
let mut events = debug_service.subscribe_mesh_topology().await.unwrap();

while let Some(event) = events.try_next().await.unwrap() {
    println!("{event:?}");
}
Source

pub fn dht(&self) -> DhtService

Returns a handle to the DHT service.

§Example
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut dht_service = connection.dht();
let value = dht_service.get_record("topic", "key").await.unwrap();

if let Some(value) = value.and_then(|value| String::from_utf8(value).ok()) {
    println!("Record has value: {value}");
} else {
   println!("Record not found");
}
Source

pub fn discovery(&self) -> DiscoveryService

Returns a handle to the discovery service.

§Example
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut discovery_service = connection.discovery();
let peer_id = discovery_service.get_own_id().await.unwrap();

println!("My peer id: {peer_id}");
Source

pub fn file_transfer(&self) -> FileTransferService

Returns a handle to the file transfer service.

§Example
use std::path::Path;

use hyveos_sdk::Connection;

let shared_dir = std::env::var(hyveos_core::BRIDGE_SHARED_DIR_ENV_VAR).unwrap();
let file_path = Path::new(&shared_dir).join("example.txt");
tokio::fs::write(&file_path, "Hello, world!").await.unwrap();

let connection = Connection::new().await.unwrap();
let mut file_transfer_service = connection.file_transfer();
let cid = file_transfer_service.publish_file(&file_path).await.unwrap();

println!("Content ID: {cid:?}");
Source

pub fn gossipsub(&self) -> GossipSubService

Returns a handle to the gossipsub service.

§Example
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut gossipsub_service = connection.gossipsub();
let id = gossipsub_service.publish("topic", "Hello, world!").await.unwrap();

println!("Published message with id: {id}");
Source

pub fn req_resp(&self) -> ReqRespService

Returns a handle to the request-response service.

§Example
use futures::StreamExt as _;
use hyveos_sdk::Connection;

let connection = Connection::new().await.unwrap();
let mut dht_service = connection.dht();
let peer_id = dht_service
    .get_providers("identification", "example")
    .await
    .unwrap()
    .next()
    .await
    .unwrap()
    .unwrap();

let mut req_resp_service = connection.req_resp();
let response = req_resp_service
    .send_request(peer_id, "Hello, world!", None)
    .await
    .unwrap();

let data = Vec::try_from(response).unwrap();
println!("Received response: {}", String::from_utf8(data).unwrap());
Source

pub fn req_resp_json<Req, Resp>(&self) -> JsonReqRespService<Req, Resp>

Available on crate feature json only.

Returns a handle to the request-response service with JSON-encoded requests and responses.

§Example
use futures::StreamExt as _;
use hyveos_sdk::Connection;
use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
struct ExampleRequest {
   message: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct ExampleResponse {
   message: String,
}

let connection = Connection::new().await.unwrap();
let mut dht_service = connection.dht();
let peer_id = dht_service
    .get_providers("identification", "example")
    .await
    .unwrap()
    .next()
    .await
    .unwrap()
    .unwrap();

let mut req_resp_service = connection.req_resp_json();
let request = ExampleRequest { message: "Hello, world!".to_string() };
let response = req_resp_service
    .send_request(peer_id, &request, None)
    .await
    .unwrap();

let data: ExampleResponse = Result::from(response).unwrap();
println!("Received response: {data:?}");
Source

pub fn req_resp_cbor<Req, Resp>(&self) -> CborReqRespService<Req, Resp>

Available on crate feature cbor only.

Returns a handle to the request-response service with JSON-encoded requests and responses.

§Example
use futures::StreamExt as _;
use hyveos_sdk::Connection;
use serde::{Serialize, Deserialize};

#[derive(Debug, Serialize, Deserialize)]
struct ExampleRequest {
   message: String,
}

#[derive(Debug, Serialize, Deserialize)]
struct ExampleResponse {
   message: String,
}

let connection = Connection::new().await.unwrap();
let mut dht_service = connection.dht();
let peer_id = dht_service
    .get_providers("identification", "example")
    .await
    .unwrap()
    .next()
    .await
    .unwrap()
    .unwrap();

let mut req_resp_service = connection.req_resp_cbor();
let request = ExampleRequest { message: "Hello, world!".to_string() };
let response = req_resp_service
    .send_request(peer_id, &request, None)
    .await
    .unwrap();

let data: ExampleResponse = Result::from(response).unwrap();
println!("Received response: {data:?}");

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,