Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

gRPC Examples

Establishing a Connection

#![allow(unused)]
fn main() {
use smartforge::generated::memory_appliance::*;

let mut client = MemoryApplianceServiceClient::connect(
    "http://127.0.0.1:50051"
).await?;
}

Getting Chassis Information

#![allow(unused)]
fn main() {
let chassis = client.get_chassis(()).await?;
println!("Serial: {:?}", chassis.serial_number);
println!("Model: {:?}", chassis.model);
}

Bidirectional Streaming

#![allow(unused)]
fn main() {
let (tx, rx) = tokio::sync::mpsc::channel(32);
let stream = tokio_stream::wrappers::ReceiverStream::new(rx);
let response = client.send_command(stream).await?;
let mut inbound = response.into_inner();

// Send a command
tx.send(SendCommandRequest {
    header: Some(MessageHeader {
        sequence_number: Some(1),
        correlation_id: Some("req-1".to_string()),
        sent_timestamp: Some(100),
        sender: Some("client".to_string()),
    }),
    command_type: Some(send_command_request::CommandType::GetTasks(
        GetTasks { empty_payload: None }
    )),
}).await?;

// Receive responses
while let Some(response) = inbound.message().await? {
    println!("Response: {:?}", response);
}
}

Error Handling

#![allow(unused)]
fn main() {
match client.get_chassis(()).await {
    Ok(chassis) => println!("Success: {:?}", chassis),
    Err(e) => eprintln!("Error: {}", e),
}
}