|
| 1 | +//# send-commands |
| 2 | +use async_std::prelude::*; |
| 3 | +use async_chat::utils::{self, ChatResult}; |
| 4 | +use async_std::io; |
| 5 | +use async_std::net; |
| 6 | + |
| 7 | +async fn send_commands(mut to_server: net::TcpStream) -> ChatResult<()> { |
| 8 | + println!("Commands:\n\ |
| 9 | + join GROUP\n\ |
| 10 | + post GROUP MESSAGE...\n\ |
| 11 | + Type Control-D (on Unix) or Control-Z (on Windows) \ |
| 12 | + to close the connection."); |
| 13 | + |
| 14 | + let mut command_lines = io::BufReader::new(io::stdin()).lines(); |
| 15 | + while let Some(command) = command_lines.next().await { |
| 16 | + let command = command?; |
| 17 | + let request = match parse_command(&command) { |
| 18 | + Some(request) => request, |
| 19 | + None => continue, |
| 20 | + }; |
| 21 | + |
| 22 | + utils::send_as_json(&mut to_server, &request).await?; |
| 23 | + to_server.flush().await?; |
| 24 | + } |
| 25 | + |
| 26 | + Ok(()) |
| 27 | +} |
| 28 | +//#end |
| 29 | + |
| 30 | +//# client-handle-replies |
| 31 | +use async_chat::FromServer; |
| 32 | + |
| 33 | +async fn handle_replies(from_server: net::TcpStream) -> ChatResult<()> { |
| 34 | + let buffered = io::BufReader::new(from_server); |
| 35 | + let mut reply_stream = utils::receive_as_json(buffered); |
| 36 | + |
| 37 | + while let Some(reply) = reply_stream.next().await { |
| 38 | + match reply? { |
| 39 | + FromServer::Message { group_name, message } => { |
| 40 | + println!("message posted to {}: {}", group_name, message); |
| 41 | + } |
| 42 | + FromServer::Error(message) => { |
| 43 | + println!("error from server: {}", message); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + Ok(()) |
| 49 | +} |
| 50 | +//# end |
| 51 | + |
| 52 | +//# client-main |
| 53 | +use async_std::task; |
| 54 | + |
| 55 | +fn main() -> ChatResult<()> { |
| 56 | + let address = std::env::args().nth(1) |
| 57 | + .expect("Usage: client ADDRESS:PORT"); |
| 58 | + |
| 59 | + task::block_on(async { |
| 60 | + let socket = net::TcpStream::connect(address).await?; |
| 61 | + socket.set_nodelay(true)?; |
| 62 | + |
| 63 | + let to_server = send_commands(socket.clone()); |
| 64 | + let from_server = handle_replies(socket); |
| 65 | + |
| 66 | + from_server.race(to_server).await?; |
| 67 | + |
| 68 | + Ok(()) |
| 69 | + }) |
| 70 | +} |
| 71 | +//# end |
| 72 | + |
| 73 | +use async_chat::FromClient; |
| 74 | +use std::sync::Arc; |
| 75 | + |
| 76 | +/// Parse a line (presumably read from the standard input) as a `Request`. |
| 77 | +fn parse_command(line: &str) -> Option<FromClient> { |
| 78 | + let (command, rest) = get_next_token(line)?; |
| 79 | + if command == "post" { |
| 80 | + let (group, rest) = get_next_token(rest)?; |
| 81 | + let message = rest.trim_start().to_string(); |
| 82 | + return Some(FromClient::Post { |
| 83 | + group_name: Arc::new(group.to_string()), |
| 84 | + message: Arc::new(message), |
| 85 | + }); |
| 86 | + } else if command == "join" { |
| 87 | + let (group, rest) = get_next_token(rest)?; |
| 88 | + if !rest.trim_start().is_empty() { |
| 89 | + return None; |
| 90 | + } |
| 91 | + return Some(FromClient::Join { |
| 92 | + group_name: Arc::new(group.to_string()), |
| 93 | + }); |
| 94 | + } else { |
| 95 | + eprintln!("Unrecognized command: {:?}", line); |
| 96 | + return None; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Given a string `input`, return `Some((token, rest))`, where `token` is the |
| 101 | +/// first run of non-whitespace characters in `input`, and `rest` is the rest of |
| 102 | +/// the string. If the string contains no non-whitespace characters, return |
| 103 | +/// `None`. |
| 104 | +fn get_next_token(mut input: &str) -> Option<(&str, &str)> { |
| 105 | + input = input.trim_start(); |
| 106 | + |
| 107 | + if input.is_empty() { |
| 108 | + return None; |
| 109 | + } |
| 110 | + |
| 111 | + match input.find(char::is_whitespace) { |
| 112 | + Some(space) => Some((&input[0..space], &input[space..])), |
| 113 | + None => Some((input, "")), |
| 114 | + } |
| 115 | +} |
0 commit comments