2018-05-08 11:08:43 -07:00
|
|
|
use actix::prelude::*;
|
2018-04-13 09:18:42 +08:00
|
|
|
use std::str::FromStr;
|
|
|
|
use std::time::Duration;
|
2019-12-16 13:09:54 +06:00
|
|
|
use std::{io, net, thread};
|
|
|
|
use tokio::io::{split, WriteHalf};
|
|
|
|
use tokio::net::TcpStream;
|
|
|
|
use tokio_util::codec::FramedRead;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
|
|
|
mod codec;
|
|
|
|
|
2019-12-16 13:09:54 +06:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() {
|
2018-04-13 09:18:42 +08:00
|
|
|
// Connect to server
|
|
|
|
let addr = net::SocketAddr::from_str("127.0.0.1:12345").unwrap();
|
2018-05-08 11:08:43 -07:00
|
|
|
|
2019-12-16 13:09:54 +06:00
|
|
|
println!("Running chat client");
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-12-16 13:09:54 +06:00
|
|
|
let stream = TcpStream::connect(&addr).await.unwrap();
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-12-16 13:09:54 +06:00
|
|
|
let addr = ChatClient::create(|ctx| {
|
|
|
|
let (r, w) = split(stream);
|
|
|
|
ChatClient::add_stream(FramedRead::new(r, codec::ClientChatCodec), ctx);
|
|
|
|
ChatClient {
|
|
|
|
framed: actix::io::FramedWrite::new(w, codec::ClientChatCodec, ctx),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// start console loop
|
|
|
|
thread::spawn(move || loop {
|
|
|
|
let mut cmd = String::new();
|
|
|
|
if io::stdin().read_line(&mut cmd).is_err() {
|
|
|
|
println!("error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
addr.do_send(ClientCommand(cmd));
|
|
|
|
});
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ChatClient {
|
|
|
|
framed: actix::io::FramedWrite<WriteHalf<TcpStream>, codec::ClientChatCodec>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Message)]
|
2019-12-16 13:09:54 +06:00
|
|
|
#[rtype(result = "()")]
|
2018-04-13 09:18:42 +08:00
|
|
|
struct ClientCommand(String);
|
|
|
|
|
|
|
|
impl Actor for ChatClient {
|
|
|
|
type Context = Context<Self>;
|
|
|
|
|
|
|
|
fn started(&mut self, ctx: &mut Context<Self>) {
|
|
|
|
// start heartbeats otherwise server will disconnect after 10 seconds
|
|
|
|
self.hb(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stopped(&mut self, _: &mut Context<Self>) {
|
|
|
|
println!("Disconnected");
|
|
|
|
|
|
|
|
// Stop application on disconnect
|
2018-07-16 12:36:53 +06:00
|
|
|
System::current().stop();
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatClient {
|
|
|
|
fn hb(&self, ctx: &mut Context<Self>) {
|
|
|
|
ctx.run_later(Duration::new(1, 0), |act, ctx| {
|
|
|
|
act.framed.write(codec::ChatRequest::Ping);
|
|
|
|
act.hb(ctx);
|
2018-09-27 22:37:19 +03:00
|
|
|
|
|
|
|
// client should also check for a timeout here, similar to the
|
|
|
|
// server code
|
2018-04-13 09:18:42 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl actix::io::WriteHandler<io::Error> for ChatClient {}
|
|
|
|
|
|
|
|
/// Handle stdin commands
|
|
|
|
impl Handler<ClientCommand> for ChatClient {
|
|
|
|
type Result = ();
|
|
|
|
|
|
|
|
fn handle(&mut self, msg: ClientCommand, _: &mut Context<Self>) {
|
|
|
|
let m = msg.0.trim();
|
|
|
|
if m.is_empty() {
|
2018-05-08 11:08:43 -07:00
|
|
|
return;
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// we check for /sss type of messages
|
|
|
|
if m.starts_with('/') {
|
|
|
|
let v: Vec<&str> = m.splitn(2, ' ').collect();
|
|
|
|
match v[0] {
|
|
|
|
"/list" => {
|
|
|
|
self.framed.write(codec::ChatRequest::List);
|
2018-05-08 11:08:43 -07:00
|
|
|
}
|
2018-04-13 09:18:42 +08:00
|
|
|
"/join" => {
|
|
|
|
if v.len() == 2 {
|
2018-05-20 21:03:29 -07:00
|
|
|
self.framed.write(codec::ChatRequest::Join(v[1].to_owned()));
|
2018-04-13 09:18:42 +08:00
|
|
|
} else {
|
|
|
|
println!("!!! room name is required");
|
|
|
|
}
|
2018-05-08 11:08:43 -07:00
|
|
|
}
|
2018-04-13 09:18:42 +08:00
|
|
|
_ => println!("!!! unknown command"),
|
|
|
|
}
|
|
|
|
} else {
|
2018-05-20 21:03:29 -07:00
|
|
|
self.framed.write(codec::ChatRequest::Message(m.to_owned()));
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Server communication
|
|
|
|
|
2019-12-16 13:09:54 +06:00
|
|
|
impl StreamHandler<Result<codec::ChatResponse, io::Error>> for ChatClient {
|
|
|
|
fn handle(
|
|
|
|
&mut self,
|
|
|
|
msg: Result<codec::ChatResponse, io::Error>,
|
|
|
|
ctx: &mut Context<Self>,
|
|
|
|
) {
|
2018-04-13 09:18:42 +08:00
|
|
|
match msg {
|
2019-12-16 13:09:54 +06:00
|
|
|
Ok(codec::ChatResponse::Message(ref msg)) => {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("message: {}", msg);
|
|
|
|
}
|
2019-12-16 13:09:54 +06:00
|
|
|
Ok(codec::ChatResponse::Joined(ref msg)) => {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("!!! joined: {}", msg);
|
|
|
|
}
|
2019-12-16 13:09:54 +06:00
|
|
|
Ok(codec::ChatResponse::Rooms(rooms)) => {
|
2018-04-13 09:18:42 +08:00
|
|
|
println!("\n!!! Available rooms:");
|
|
|
|
for room in rooms {
|
|
|
|
println!("{}", room);
|
|
|
|
}
|
2019-09-05 00:04:57 +09:00
|
|
|
println!();
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
2019-12-16 13:09:54 +06:00
|
|
|
_ => ctx.stop(),
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|