Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rust-networking-examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
GitLab upgrade completed. Current version is 17.11.3.
Show more breadcrumbs
WIDE
rust-networking-examples
Commits
dc763b98
Commit
dc763b98
authored
2 months ago
by
WILLIAMS Harvey
Browse files
Options
Downloads
Patches
Plain Diff
Pushing some changes to the main branch
parent
cd3fbd38
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/local/src/main.rs
+15
-4
15 additions, 4 deletions
examples/local/src/main.rs
examples/local/src/net/mod.rs
+4
-3
4 additions, 3 deletions
examples/local/src/net/mod.rs
with
19 additions
and
7 deletions
examples/local/src/main.rs
+
15
−
4
View file @
dc763b98
...
...
@@ -2,13 +2,23 @@ use rust_networking_examples::Network;
mod
net
;
use
net
::
Net
;
use
net
::{
DispatchHandle
,
Net
};
/*
main.rs
A simple test program demonstrating sending a message between two instances of Net.
*/
/// Our main function decorated with the tokio::main attribute to setup an async runtime.
#[tokio::main]
async
fn
main
()
{
let
mut
net_a
=
Net
::
new
(
"a"
)
.await
;
let
mut
net_b
=
Net
::
new
(
"b"
)
.await
;
// Create a single dispatch actor, which will handle message routing
let
dispatcher
=
DispatchHandle
::
new
();
// Create two network instances, with addresses "a" and "b"
let
mut
net_a
=
Net
::
new
(
"a"
,
dispatcher
.clone
())
.await
;
let
mut
net_b
=
Net
::
new
(
"b"
,
dispatcher
)
.await
;
// Channel for b to receive messages
let
mut
recv_b
=
net_b
.recv
()
.await
;
...
...
@@ -16,7 +26,8 @@ async fn main() {
// Send message from a to b
net_a
.send
(
"b"
,
"Hello world!"
.as_bytes
()
.to_vec
());
// See what b receives
while
let
Some
((
from
,
msg
))
=
recv_b
.recv
()
.await
{
println!
(
"
R
eceived {:?} from {}"
,
String
::
from_utf8
(
msg
),
from
);
println!
(
"
b r
eceived {:?} from {}"
,
String
::
from_utf8
(
msg
)
.unwrap
()
,
from
);
}
}
This diff is collapsed.
Click to expand it.
examples/local/src/net/mod.rs
+
4
−
3
View file @
dc763b98
mod
dispatch
;
use
dispatch
::{
DispatchHandle
,
DISPATCHER
};
use
rust_networking_examples
::
Network
;
use
tokio
::
sync
::
mpsc
;
pub
use
dispatch
::
DispatchHandle
;
/// Handle for sending and receiving on our network simulated using async channels.
#[derive(Clone)]
pub
struct
Net
{
...
...
@@ -15,8 +16,8 @@ pub struct Net {
}
impl
Net
{
pub
async
fn
new
(
address
:
&
str
)
->
Net
{
Net
{
dispatcher
:
DISPATCHER
.clone
()
,
address
:
address
.to_string
()
}
pub
async
fn
new
(
address
:
&
str
,
dispatcher
:
DispatchHandle
)
->
Net
{
Net
{
dispatcher
,
address
:
address
.to_string
()
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment