2018-01-05 23:01:19 +01:00
|
|
|
#[macro_use] extern crate actix;
|
2017-10-21 02:16:17 +02:00
|
|
|
extern crate bytes;
|
|
|
|
extern crate byteorder;
|
|
|
|
extern crate futures;
|
|
|
|
extern crate tokio_io;
|
|
|
|
extern crate tokio_core;
|
|
|
|
extern crate serde;
|
|
|
|
extern crate serde_json;
|
|
|
|
#[macro_use] extern crate serde_derive;
|
|
|
|
|
|
|
|
use std::{io, net, process, thread};
|
|
|
|
use std::str::FromStr;
|
|
|
|
use std::time::Duration;
|
|
|
|
use futures::Future;
|
2018-01-28 10:04:58 +01:00
|
|
|
use tokio_io::AsyncRead;
|
2017-10-21 02:16:17 +02:00
|
|
|
use tokio_core::net::TcpStream;
|
|
|
|
use actix::prelude::*;
|
|
|
|
|
|
|
|
mod codec;
|
|
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let sys = actix::System::new("chat-client");
|
|
|
|
|
|
|
|
// Connect to server
|
|
|
|
let addr = net::SocketAddr::from_str("127.0.0.1:12345").unwrap();
|
|
|
|
Arbiter::handle().spawn(
|
|
|
|
TcpStream::connect(&addr, Arbiter::handle())
|
|
|
|
.and_then(|stream| {
|
2018-01-28 10:04:58 +01:00
|
|
|
let addr: SyncAddress<_> = ChatClient::create_with(
|
|
|
|
stream.framed(codec::ClientChatCodec),
|
|
|
|
|_, framed| ChatClient{framed: framed});
|
2017-10-21 02:16:17 +02:00
|
|
|
|
|
|
|
// 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.send(ClientCommand(cmd));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
futures::future::ok(())
|
|
|
|
})
|
|
|
|
.map_err(|e| {
|
|
|
|
println!("Can not connect to server: {}", e);
|
|
|
|
process::exit(1)
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
println!("Running chat client");
|
|
|
|
sys.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-28 10:04:58 +01:00
|
|
|
struct ChatClient {
|
|
|
|
framed: FramedCell<TcpStream, codec::ClientChatCodec>,
|
|
|
|
}
|
2017-10-21 02:16:17 +02:00
|
|
|
|
2018-01-05 23:01:19 +01:00
|
|
|
#[derive(Message)]
|
2017-10-21 02:16:17 +02:00
|
|
|
struct ClientCommand(String);
|
|
|
|
|
|
|
|
impl Actor for ChatClient {
|
2018-01-28 10:04:58 +01:00
|
|
|
type Context = Context<Self>;
|
2017-10-21 02:16:17 +02:00
|
|
|
|
2018-01-28 10:04:58 +01:00
|
|
|
fn started(&mut self, ctx: &mut Context<Self>) {
|
2017-10-21 02:16:17 +02:00
|
|
|
// start heartbeats otherwise server will disconnect after 10 seconds
|
|
|
|
self.hb(ctx)
|
|
|
|
}
|
2018-01-05 23:01:19 +01:00
|
|
|
|
2018-01-28 10:04:58 +01:00
|
|
|
fn stopping(&mut self, _: &mut Context<Self>) -> bool {
|
2018-01-05 23:01:19 +01:00
|
|
|
println!("Disconnected");
|
|
|
|
|
|
|
|
// Stop application on disconnect
|
|
|
|
Arbiter::system().send(actix::msgs::SystemExit(0));
|
2018-01-07 08:22:10 +01:00
|
|
|
|
|
|
|
true
|
2018-01-05 23:01:19 +01:00
|
|
|
}
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl ChatClient {
|
2018-01-28 10:04:58 +01:00
|
|
|
fn hb(&self, ctx: &mut Context<Self>) {
|
2017-10-21 02:16:17 +02:00
|
|
|
ctx.run_later(Duration::new(1, 0), |act, ctx| {
|
2018-01-28 10:04:58 +01:00
|
|
|
act.framed.send(codec::ChatRequest::Ping);
|
|
|
|
act.hb(ctx);
|
2017-10-21 02:16:17 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Handle stdin commands
|
2018-01-05 23:01:19 +01:00
|
|
|
impl Handler<ClientCommand> for ChatClient {
|
|
|
|
type Result = ();
|
|
|
|
|
2018-01-28 10:04:58 +01:00
|
|
|
fn handle(&mut self, msg: ClientCommand, _: &mut Context<Self>) {
|
2017-10-21 02:16:17 +02:00
|
|
|
let m = msg.0.trim();
|
|
|
|
if m.is_empty() {
|
2018-01-05 23:01:19 +01:00
|
|
|
return
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// we check for /sss type of messages
|
|
|
|
if m.starts_with('/') {
|
|
|
|
let v: Vec<&str> = m.splitn(2, ' ').collect();
|
|
|
|
match v[0] {
|
|
|
|
"/list" => {
|
2018-01-28 10:04:58 +01:00
|
|
|
let _ = self.framed.send(codec::ChatRequest::List);
|
2017-10-21 02:16:17 +02:00
|
|
|
},
|
|
|
|
"/join" => {
|
|
|
|
if v.len() == 2 {
|
2018-01-28 10:04:58 +01:00
|
|
|
let _ = self.framed.send(codec::ChatRequest::Join(v[1].to_owned()));
|
2017-10-21 02:16:17 +02:00
|
|
|
} else {
|
|
|
|
println!("!!! room name is required");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => println!("!!! unknown command"),
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-28 10:04:58 +01:00
|
|
|
let _ = self.framed.send(codec::ChatRequest::Message(m.to_owned()));
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Server communication
|
|
|
|
|
2018-01-28 17:26:36 +01:00
|
|
|
impl FramedHandler<TcpStream, codec::ClientChatCodec> for ChatClient {
|
2017-10-21 02:16:17 +02:00
|
|
|
|
2018-01-28 10:04:58 +01:00
|
|
|
fn handle(&mut self, msg: io::Result<codec::ChatResponse>, ctx: &mut Context<Self>) {
|
2017-10-21 02:16:17 +02:00
|
|
|
match msg {
|
2018-01-05 23:01:19 +01:00
|
|
|
Err(_) => ctx.stop(),
|
|
|
|
Ok(msg) => match msg {
|
|
|
|
codec::ChatResponse::Message(ref msg) => {
|
|
|
|
println!("message: {}", msg);
|
|
|
|
}
|
|
|
|
codec::ChatResponse::Joined(ref msg) => {
|
|
|
|
println!("!!! joined: {}", msg);
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
2018-01-05 23:01:19 +01:00
|
|
|
codec::ChatResponse::Rooms(rooms) => {
|
|
|
|
println!("\n!!! Available rooms:");
|
|
|
|
for room in rooms {
|
|
|
|
println!("{}", room);
|
|
|
|
}
|
|
|
|
println!("");
|
|
|
|
}
|
|
|
|
_ => (),
|
2017-10-21 02:16:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|