Skip to content

Commit

Permalink
Update rust library examples
Browse files Browse the repository at this point in the history
  • Loading branch information
metaclips committed Aug 25, 2023
1 parent 28cc3d2 commit c94077f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 6 additions & 0 deletions reference/libraries/rust/credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ In a later guide, we'll explore how Ockam enables you to define various pluggabl

{% code lineNumbers="true" %}
```rust
// examples/06-credentials-exchange-issuer.rs
use ockam::access_control::AllowAll;
use ockam::access_control::IdentityIdAccessControl;
use ockam::identity::CredentialsIssuer;
Expand Down Expand Up @@ -112,6 +113,7 @@ async fn main(ctx: Context) -> Result<()> {
println!("issuer started");
Ok(())
}

```
{% endcode %}

Expand All @@ -127,6 +129,7 @@ touch examples/06-credential-exchange-server.rs

{% code lineNumbers="true" %}
```rust
// examples/06-credentials-exchange-server.rs
// This node starts a tcp listener, a secure channel listener, and an echoer worker.
// It then runs forever waiting for messages.
use hello_ockam::Echoer;
Expand Down Expand Up @@ -222,6 +225,7 @@ async fn main(ctx: Context) -> Result<()> {
println!("server started");
Ok(())
}

```
{% endcode %}

Expand All @@ -237,6 +241,7 @@ touch examples/06-credential-exchange-client.rs

{% code lineNumbers="true" %}
```rust
// examples/06-credentials-exchange-client.rs
use ockam::identity::{AuthorityService, CredentialsIssuerClient, SecureChannelOptions, TrustContext};
use ockam::TcpTransportExtension;
use ockam::{node, route, Context, Result, TcpConnectionOptions};
Expand Down Expand Up @@ -318,6 +323,7 @@ async fn main(ctx: Context) -> Result<()> {

node.stop().await
}

```
{% endcode %}

Expand Down
7 changes: 4 additions & 3 deletions reference/libraries/rust/nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ Add the following code to this file:

```rust
// src/echoer.rs

use ockam::{Context, Result, Routed, Worker};

pub struct Echoer;
Expand All @@ -157,6 +156,7 @@ impl Worker for Echoer {
ctx.send(msg.return_route(), msg.body()).await
}
}

```

Note that we define the `Message` associated type of the worker as `String`, which specifies that this worker expects to handle `String` messages. We then go on to define a `handle_message(..)` function that will be called whenever a new message arrives for this worker.
Expand All @@ -166,10 +166,10 @@ In the Echoer's `handle_message(..)`, we print any incoming message, along with
To make this Echoer type accessible to our main program, export it from `src/lib.rs` file by adding the following to it:

```rust
// src/lib.rs

mod echoer;

pub use echoer::*;

```

#### App worker
Expand Down Expand Up @@ -211,6 +211,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

To run this new node program:
Expand Down
19 changes: 14 additions & 5 deletions reference/libraries/rust/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ Add the following code to this file:

```rust
// src/hop.rs

use ockam::{Any, Context, Result, Routed, Worker};
use ockam::{Any, Context, LocalMessage, Result, Routed, Worker};

pub struct Hop;

Expand All @@ -92,19 +91,22 @@ impl Worker for Hop {
println!("Address: {}, Received: {}", ctx.address(), msg);

// Some type conversion
let mut message = msg.into_local_message();
let transport_message = message.transport_mut();
let mut transport_message = msg.into_local_message().into_transport_message();

// Remove my address from the onward_route
transport_message.onward_route.step()?;

// Insert my address at the beginning return_route
transport_message.return_route.modify().prepend(ctx.address());

// Wipe all local info (e.g. transport types)
let message = LocalMessage::new(transport_message, vec![]);

// Send the message on its onward_route
ctx.forward(message).await
}
}

```

To make this `Hop` type accessible to our main program, export it from `src/lib.rs` by adding the following to it:
Expand Down Expand Up @@ -161,6 +163,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

To run this new node program:
Expand Down Expand Up @@ -216,6 +219,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

To run this new node program:
Expand Down Expand Up @@ -270,6 +274,7 @@ async fn main(ctx: Context) -> Result<()> {
// Don't call node.stop() here so this node runs forever.
Ok(())
}

```

#### Initiator node
Expand Down Expand Up @@ -309,6 +314,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

#### Run
Expand Down Expand Up @@ -351,7 +357,6 @@ Add the following code to this file:

```rust
// src/forwarder.rs

use ockam::{Address, Any, Context, LocalMessage, Result, Routed, Worker};

pub struct Forwarder(pub Address);
Expand Down Expand Up @@ -393,6 +398,7 @@ impl Worker for Forwarder {
ctx.forward(message).await
}
}

```

To make this `Forwarder` type accessible to our main program, export it from `src/lib.rs` by adding the following to it:
Expand Down Expand Up @@ -440,6 +446,7 @@ async fn main(ctx: Context) -> Result<()> {
// Don't call node.stop() here so this node runs forever.
Ok(())
}

```

#### Middle node
Expand Down Expand Up @@ -487,6 +494,7 @@ async fn main(ctx: Context) -> Result<()> {
// Don't call node.stop() here so this node runs forever.
Ok(())
}

```

#### Initiator node
Expand Down Expand Up @@ -525,6 +533,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

#### Run
Expand Down
3 changes: 3 additions & 0 deletions reference/libraries/rust/secure-channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ async fn main(ctx: Context) -> Result<()> {
// Don't call node.stop() here so this node runs forever.
Ok(())
}

```

### Middle node
Expand Down Expand Up @@ -112,6 +113,7 @@ async fn main(ctx: Context) -> Result<()> {
// Don't call node.stop() here so this node runs forever.
Ok(())
}

```

### Initiator node
Expand Down Expand Up @@ -160,6 +162,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

### Run
Expand Down
4 changes: 3 additions & 1 deletion reference/libraries/rust/vaults-and-identities.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ description: >-
# Vaults and Identities

```rust
// examples/vault-and-identities.rs
use ockam::node;
use ockam::{Context, Result};

Expand All @@ -16,9 +17,10 @@ async fn main(ctx: Context) -> Result<()> {
let mut node = node(ctx);

// Create an Identity to represent Alice.
let alice = node.create_identity().await?;
let _alice = node.create_identity().await?;

// Stop the node.
node.stop().await
}

```

0 comments on commit c94077f

Please sign in to comment.