diff options
author | Eliza Weisman <eliza@buoyant.io> | 2019-03-22 15:25:42 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-03-22 15:25:42 -0700 |
commit | 30330da11a56dfdd11bdbef50dba073a9edc36b2 (patch) | |
tree | bf4e8e90293a3c75a2bf5281572e1c01eceab3cb /examples/udp-codec.rs | |
parent | 6e4945025cdc6f2b71d9b30aaa23c5517cca1504 (diff) |
chore: Fix examples not working with `cargo run` (#998)
* chore: Fix examples not working with `cargo run`
## Motivation
PR #991 moved the `tokio` crate to its own subdirectory, but did not
move the `examples` directory into `tokio/examples`. While attempting to
use the examples for testing another change, I noticed that #991 had
broken the ability to use `cargo run`, as the examples were no longer
considered part of a crate that cargo was aware of:
```
tokio on master [$] via 🦀v1.33.0 at ☸️ aks-eliza-dev
➜ cargo run --example chat
error: no example target named `chat`
Did you mean `echo`?
```
## Solution
This branch moves the examples into the `tokio` directory, so cargo is
now once again aware of them:
```
tokio on eliza/fix-examples [$] via 🦀v1.33.0 at ☸️ aks-eliza-dev
➜ cargo run --example chat
Compiling tokio-executor v0.1.7 (/Users/eliza/Code/tokio/tokio-executor)
Compiling tokio-reactor v0.1.9
Compiling tokio-threadpool v0.1.13
Compiling tokio-current-thread v0.1.6
Compiling tokio-timer v0.2.10
Compiling tokio-uds v0.2.5
Compiling tokio-udp v0.1.3
Compiling tokio-tcp v0.1.3
Compiling tokio-fs v0.1.6
Compiling tokio v0.1.18 (/Users/eliza/Code/tokio/tokio)
Finished dev [unoptimized + debuginfo] target(s) in 7.04s
Running `target/debug/examples/chat`
server running on localhost:6142
```
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Diffstat (limited to 'examples/udp-codec.rs')
-rw-r--r-- | examples/udp-codec.rs | 65 |
1 files changed, 0 insertions, 65 deletions
diff --git a/examples/udp-codec.rs b/examples/udp-codec.rs deleted file mode 100644 index 3657d8cc..00000000 --- a/examples/udp-codec.rs +++ /dev/null @@ -1,65 +0,0 @@ -//! This example leverages `BytesCodec` to create a UDP client and server which -//! speak a custom protocol. -//! -//! Here we're using the codec from tokio-io to convert a UDP socket to a stream of -//! client messages. These messages are then processed and returned back as a -//! new message with a new destination. Overall, we then use this to construct a -//! "ping pong" pair where two sockets are sending messages back and forth. - -#![deny(warnings)] - -extern crate env_logger; -extern crate tokio; -extern crate tokio_codec; -extern crate tokio_io; - -use std::net::SocketAddr; - -use tokio::net::{UdpFramed, UdpSocket}; -use tokio::prelude::*; -use tokio_codec::BytesCodec; - -fn main() -> Result<(), Box<std::error::Error>> { - let _ = env_logger::init(); - - let addr: SocketAddr = "127.0.0.1:0".parse()?; - - // Bind both our sockets and then figure out what ports we got. - let a = UdpSocket::bind(&addr)?; - let b = UdpSocket::bind(&addr)?; - let b_addr = b.local_addr()?; - - // We're parsing each socket with the `BytesCodec` included in `tokio_io`, and then we - // `split` each codec into the sink/stream halves. - let (a_sink, a_stream) = UdpFramed::new(a, BytesCodec::new()).split(); - let (b_sink, b_stream) = UdpFramed::new(b, BytesCodec::new()).split(); - - // Start off by sending a ping from a to b, afterwards we just print out - // what they send us and continually send pings - // let pings = stream::iter((0..5).map(Ok)); - let a = a_sink.send(("PING".into(), b_addr)).and_then(|a_sink| { - let mut i = 0; - let a_stream = a_stream.take(4).map(move |(msg, addr)| { - i += 1; - println!("[a] recv: {}", String::from_utf8_lossy(&msg)); - (format!("PING {}", i).into(), addr) - }); - a_sink.send_all(a_stream) - }); - - // The second client we have will receive the pings from `a` and then send - // back pongs. - let b_stream = b_stream.map(|(msg, addr)| { - println!("[b] recv: {}", String::from_utf8_lossy(&msg)); - ("PONG".into(), addr) - }); - let b = b_sink.send_all(b_stream); - - // Spawn the sender of pongs and then wait for our pinger to finish. - tokio::run({ - b.join(a) - .map(|_| ()) - .map_err(|e| println!("error = {:?}", e)) - }); - Ok(()) -} |