refactor: rename structs

This commit is contained in:
Fabian Freyer 2019-03-22 10:13:00 -04:00
parent d1d284f995
commit 04f1013f93

View File

@ -100,12 +100,12 @@ impl From<&str> for IO {
} }
} }
struct Ws { struct Websocket {
cons: Option<Addr<Cons>>, cons: Option<Addr<Terminal>>,
hb: Instant, hb: Instant,
} }
impl Actor for Ws { impl Actor for Websocket {
type Context = ws::WebsocketContext<Self>; type Context = ws::WebsocketContext<Self>;
fn started(&mut self, ctx: &mut Self::Context) { fn started(&mut self, ctx: &mut Self::Context) {
@ -113,7 +113,7 @@ impl Actor for Ws {
self.hb(ctx); self.hb(ctx);
// Start PTY // Start PTY
self.cons = Some(Cons::new(ctx.address()).start()); self.cons = Some(Terminal::new(ctx.address()).start());
trace!("Started WebSocket"); trace!("Started WebSocket");
} }
@ -129,20 +129,20 @@ impl Actor for Ws {
} }
} }
impl Handler<IO> for Ws { impl Handler<IO> for Websocket {
type Result = (); type Result = ();
fn handle(&mut self, msg: IO, ctx: &mut <Self as Actor>::Context) { fn handle(&mut self, msg: IO, ctx: &mut <Self as Actor>::Context) {
trace!("Ws <- Cons : {:?}", msg); trace!("Websocket <- Terminal : {:?}", msg);
ctx.binary(msg); ctx.binary(msg);
} }
} }
impl Handler<TerminadoMessage> for Ws { impl Handler<TerminadoMessage> for Websocket {
type Result = (); type Result = ();
fn handle(&mut self, msg: TerminadoMessage, ctx: &mut <Self as Actor>::Context) { fn handle(&mut self, msg: TerminadoMessage, ctx: &mut <Self as Actor>::Context) {
trace!("Ws <- Cons : {:?}", msg); trace!("Websocket <- Terminal : {:?}", msg);
match msg { match msg {
TerminadoMessage::Stdout(_) => { TerminadoMessage::Stdout(_) => {
let json = serde_json::to_string(&msg); let json = serde_json::to_string(&msg);
@ -156,7 +156,7 @@ impl Handler<TerminadoMessage> for Ws {
} }
} }
impl Ws { impl Websocket {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
hb: Instant::now(), hb: Instant::now(),
@ -181,12 +181,12 @@ fn index(_req: &HttpRequest) -> Result<NamedFile> {
Ok(NamedFile::open("static/term.html")?) Ok(NamedFile::open("static/term.html")?)
} }
impl StreamHandler<ws::Message, ws::ProtocolError> for Ws { impl StreamHandler<ws::Message, ws::ProtocolError> for Websocket {
fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) { fn handle(&mut self, msg: ws::Message, ctx: &mut Self::Context) {
let cons: &mut Addr<Cons> = match self.cons { let cons: &mut Addr<Terminal> = match self.cons {
Some(ref mut c) => c, Some(ref mut c) => c,
None => { None => {
error!("Console died, closing websocket."); error!("Terminalole died, closing websocket.");
ctx.stop(); ctx.stop();
return; return;
} }
@ -213,14 +213,14 @@ impl StreamHandler<ws::Message, ws::ProtocolError> for Ws {
} }
} }
struct Cons { struct Terminal {
pty_write: Option<AsyncPtyMasterWriteHalf>, pty_write: Option<AsyncPtyMasterWriteHalf>,
child: Option<Child>, child: Option<Child>,
ws: Addr<Ws>, ws: Addr<Websocket>,
} }
impl Cons { impl Terminal {
pub fn new(ws: Addr<Ws>) -> Self { pub fn new(ws: Addr<Websocket>) -> Self {
Self { Self {
pty_write: None, pty_write: None,
child: None, child: None,
@ -229,17 +229,17 @@ impl Cons {
} }
} }
impl StreamHandler<<BytesCodec as Decoder>::Item, <BytesCodec as Decoder>::Error> for Cons { impl StreamHandler<<BytesCodec as Decoder>::Item, <BytesCodec as Decoder>::Error> for Terminal {
fn handle(&mut self, msg: <BytesCodec as Decoder>::Item, _ctx: &mut Self::Context) { fn handle(&mut self, msg: <BytesCodec as Decoder>::Item, _ctx: &mut Self::Context) {
self.ws.do_send(TerminadoMessage::Stdout(IO(msg))); self.ws.do_send(TerminadoMessage::Stdout(IO(msg)));
} }
} }
impl Actor for Cons { impl Actor for Terminal {
type Context = Context<Self>; type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) { fn started(&mut self, ctx: &mut Self::Context) {
info!("Started Cons"); info!("Started Terminal");
let pty = match AsyncPtyMaster::open() { let pty = match AsyncPtyMaster::open() {
Err(e) => { Err(e) => {
error!("Unable to open PTY: {:?}", e); error!("Unable to open PTY: {:?}", e);
@ -269,7 +269,7 @@ impl Actor for Cons {
} }
fn stopping(&mut self, _ctx: &mut Self::Context) -> Running { fn stopping(&mut self, _ctx: &mut Self::Context) -> Running {
info!("Stopping Cons"); info!("Stopping Terminal");
let child = self.child.take(); let child = self.child.take();
@ -292,18 +292,18 @@ impl Actor for Cons {
} }
fn stopped(&mut self, _ctx: &mut Self::Context) { fn stopped(&mut self, _ctx: &mut Self::Context) {
info!("Stopped Cons"); info!("Stopped Terminal");
} }
} }
impl Handler<IO> for Cons { impl Handler<IO> for Terminal {
type Result = (); type Result = ();
fn handle(&mut self, msg: IO, ctx: &mut <Self as Actor>::Context) { fn handle(&mut self, msg: IO, ctx: &mut <Self as Actor>::Context) {
let pty = match self.pty_write { let pty = match self.pty_write {
Some(ref mut p) => p, Some(ref mut p) => p,
None => { None => {
error!("Write half of PTY died, stopping Cons."); error!("Write half of PTY died, stopping Terminal.");
ctx.stop(); ctx.stop();
return; return;
} }
@ -314,7 +314,7 @@ impl Handler<IO> for Cons {
ctx.stop(); ctx.stop();
} }
trace!("Ws -> Cons : {:?}", msg); trace!("Websocket -> Terminal : {:?}", msg);
} }
} }
@ -333,20 +333,20 @@ impl<T: PtyMaster> Future for Resize<T> {
} }
} }
impl Handler<TerminadoMessage> for Cons { impl Handler<TerminadoMessage> for Terminal {
type Result = (); type Result = ();
fn handle(&mut self, msg: TerminadoMessage, ctx: &mut <Self as Actor>::Context) { fn handle(&mut self, msg: TerminadoMessage, ctx: &mut <Self as Actor>::Context) {
let pty = match self.pty_write { let pty = match self.pty_write {
Some(ref mut p) => p, Some(ref mut p) => p,
None => { None => {
error!("Write half of PTY died, stopping Cons."); error!("Write half of PTY died, stopping Terminal.");
ctx.stop(); ctx.stop();
return; return;
} }
}; };
trace!("Ws -> Cons : {:?}", msg); trace!("Websocket -> Terminal : {:?}", msg);
match msg { match msg {
TerminadoMessage::Stdin(io) => { TerminadoMessage::Stdin(io) => {
if let Err(e) = pty.write(io.as_ref()) { if let Err(e) = pty.write(io.as_ref()) {
@ -379,7 +379,7 @@ fn main() {
.unwrap() .unwrap()
.show_files_listing(), .show_files_listing(),
) )
.resource("/websocket", |r| r.f(|req| ws::start(req, Ws::new()))) .resource("/websocket", |r| r.f(|req| ws::start(req, Websocket::new())))
.resource("/", |r| r.f(index)) .resource("/", |r| r.f(index))
}) })
.bind("127.0.0.1:8080") .bind("127.0.0.1:8080")