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 Jul 25, 2023
1 parent e467568 commit 0ff8ca9
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 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
8 changes: 5 additions & 3 deletions reference/libraries/rust/nodes.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop the node as soon as it starts.
node.stop().await
}

```

Here we add the `#[ockam::node]` attribute to an `async` main function that receives the node execution context as a parameter and returns `ockam::Result` which helps make our error reporting better.
Expand Down Expand Up @@ -97,7 +98,6 @@ Add the following code to this file:

```rust
// src/echoer.rs

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

pub struct Echoer;
Expand All @@ -114,6 +114,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 @@ -123,10 +124,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 @@ -168,6 +169,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
21 changes: 14 additions & 7 deletions reference/libraries/rust/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,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 @@ -95,19 +94,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 @@ -164,6 +166,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 @@ -221,6 +224,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 @@ -253,7 +257,6 @@ Add the following code to this file:
// It then runs forever waiting for messages.

use hello_ockam::Echoer;
use ockam::flow_control::FlowControlPolicy;
use ockam::{node, Context, Result, TcpListenerOptions, TcpTransportExtension};

#[ockam::node]
Expand All @@ -276,6 +279,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 @@ -315,6 +319,7 @@ async fn main(ctx: Context) -> Result<()> {
// Stop all workers, stop the node, cleanup and return.
node.stop().await
}

```

#### Run
Expand Down Expand Up @@ -357,7 +362,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 @@ -399,6 +403,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 All @@ -424,7 +429,6 @@ Add the following code to this file:
// It then runs forever waiting for messages.

use hello_ockam::Echoer;
use ockam::flow_control::FlowControlPolicy;
use ockam::{node, Context, Result, TcpListenerOptions, TcpTransportExtension};

#[ockam::node]
Expand All @@ -447,6 +451,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 @@ -494,6 +499,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 @@ -532,6 +538,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 0ff8ca9

Please sign in to comment.