From 38dea92e4bfa3f44c1f20a97c9d0593097fbdae0 Mon Sep 17 00:00:00 2001 From: laerling Date: Tue, 11 Sep 2018 20:19:09 +0200 Subject: [PATCH 1/4] Find and connect to socket - connection.rs: struct Connection and associated methods - main.rs: Prototype for primitive core1.0 negotiation --- Cargo.lock | 70 ++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/connection.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 12 +++++- 4 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 src/connection.rs diff --git a/Cargo.lock b/Cargo.lock index 343f695..7b518cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5,6 +5,15 @@ dependencies = [ "conch-parser 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "conch-runtime 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)", + "vt6 0.0.1", +] + +[[package]] +name = "aho-corasick" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -227,6 +236,14 @@ dependencies = [ "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "memchr" +version = "2.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "memoffset" version = "0.2.1" @@ -382,6 +399,26 @@ dependencies = [ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "regex" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", + "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", + "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "regex-syntax" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "rental" version = "0.4.16" @@ -481,6 +518,14 @@ dependencies = [ "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "thread_local" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio" version = "0.1.8" @@ -680,6 +725,11 @@ dependencies = [ "tokio-reactor 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "ucd-util" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "unicode-width" version = "0.1.5" @@ -698,6 +748,11 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "utf8-ranges" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "vec_map" version = "0.8.1" @@ -713,6 +768,14 @@ name = "void" version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "vt6" +version = "0.0.1" +dependencies = [ + "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", + "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "winapi" version = "0.2.8" @@ -752,6 +815,7 @@ dependencies = [ ] [metadata] +"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" @@ -779,6 +843,7 @@ dependencies = [ "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54" "checksum log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fcce5fa49cc693c312001daf1d13411c4a5283796bac1084299ea3e567113f" +"checksum memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" @@ -797,6 +862,8 @@ dependencies = [ "checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" +"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" +"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" "checksum rental 0.4.16 (registry+https://github.com/rust-lang/crates.io-index)" = "f098d5bfcbc5b929baf992ca6e09244daf4b47d1aedba3a808364faf48bd036e" "checksum rental-impl 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)" = "cb24fb6b34fdd7b264154ea85c34bf9d3a9ac1355163b6e22ab8262f9f4a73f1" "checksum scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "332ffa32bf586782a3efaeb58f127980944bbc8c4d6913a86107ac2a5ab24b28" @@ -810,6 +877,7 @@ dependencies = [ "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" +"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum tokio 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "fbb6a6e9db2702097bfdfddcb09841211ad423b86c75b5ddaca1d62842ac492c" "checksum tokio-codec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "881e9645b81c2ce95fcb799ded2c29ffb9f25ef5bef909089a420e5961dd8ccb" "checksum tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" @@ -825,9 +893,11 @@ dependencies = [ "checksum tokio-timer 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d03fa701f9578a01b7014f106b47f0a363b4727a7f3f75d666e312ab7acbbf1c" "checksum tokio-udp 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "da941144b816d0dcda4db3a1ba87596e4df5e860a72b70783fe435891f80601c" "checksum tokio-uds 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "424c1ed15a0132251813ccea50640b224c809d6ceafb88154c1a8775873a0e89" +"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" +"checksum utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd70f467df6810094968e2fce0ee1bd0e87157aceb026a8c083bcf5e25b9efe4" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/Cargo.toml b/Cargo.toml index d7ccf4e..adc61de 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ authors = ["lærling "] conch-parser = "^0.1.0" conch-runtime = "^0.1.4" tokio-core = "^0.1.17" +vt6 = { path = "../vt6.rs" } \ No newline at end of file diff --git a/src/connection.rs b/src/connection.rs new file mode 100644 index 0000000..3e0038b --- /dev/null +++ b/src/connection.rs @@ -0,0 +1,100 @@ +/******************************************************************************* + * + * Copyright 2018 lærling + * + * This program is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later + * version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + * A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * this program. If not, see . + * + *******************************************************************************/ + +use std::env::vars; +use std::io; +use std::io::Write; +use std::os::unix::net::UnixStream; +use std::path::PathBuf; +use std::process::exit; +use vt6::core::msg::Message; + +/// Encapsulates all connections to the VT6 server, that is the I/O stream (most +/// commonly stdin and stdout) and the VT6 message stream (message input, +/// message output) and abstracts over the kind of connection (normal mode or +/// multiplexed mode) +pub struct Connection { + mode: ConnectionMode, +} + +/// The mode the client is operating in and, if operating in normal mode, +/// the (connected) VT6 message stream +pub enum ConnectionMode { + Normal(UnixStream), + Multiplexed, +} + +impl Connection { + + pub fn new() -> Result { + + // use normal mode if VT6 environment variable is present + if let Some(vt6_socket_var) = vars().find(|var| var.0 == "VT6") { + return Ok(Connection { mode: ConnectionMode::Normal( + // FIXME: Use SOCK_SEQPACKET to connect + UnixStream::connect(PathBuf::from(vt6_socket_var.1))? + )}); + } + + // use multiplexed mode if TERM environment variable is "vt6" + if let Some(_) = vars().find(|var| *var == ("TERM".to_string(), "vt6".to_string())) { + return Ok(Connection { mode: ConnectionMode::Multiplexed }); + } + + // else server is absent + // for now this means exiting + eprintln!("No VT6 server found. Exiting"); + exit(1); + } + + // FIXME: msg: Message instead of &str + pub fn send(&mut self, msg: &str) { + match self.mode { + + // in normal mode write to VT6 message stream + ConnectionMode::Normal(ref mut stream) => { + // FIXME + stream.write_all(msg.as_bytes()).unwrap(); + }, + + // in multiplexed mode write to stdout + ConnectionMode::Multiplexed => { + // TODO + }, + }; + } + + // FIXME asynchronous reading + pub fn read(&mut self) { + match self.mode { + + // in normal mode read from VT6 message stream + ConnectionMode::Normal(ref mut stream) => { + use std::io::Read; + let mut answer = String::with_capacity(24); + let read_bytes = stream.read_to_string(&mut answer).unwrap(); + println!("Read {} bytes: {}", read_bytes, answer); + }, + + // in multiplexed mode read from stdin + ConnectionMode::Multiplexed => { + panic!("not yet implemented"); + } + }; + } +} diff --git a/src/main.rs b/src/main.rs index 9b05c30..b68e0b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,18 +19,23 @@ extern crate conch_parser; extern crate conch_runtime; extern crate tokio_core; +extern crate vt6; + +mod connection; use conch_parser::lexer::Lexer; use conch_parser::parse::DefaultParser; use conch_runtime::env::DefaultEnv; use conch_runtime::future::EnvFuture; use conch_runtime::spawn::sequence; +use std::env::vars; use std::env; use std::fs::File; use std::io; use std::option::Option; -use tokio_core::reactor::Core; use std::process::exit; +use tokio_core::reactor::Core; +use connection::Connection; fn repl(script: &mut T) -> io::Result<()> { @@ -79,6 +84,11 @@ fn repl(script: &mut T) -> io::Result<()> { fn main() { + // FIXME temp + let mut con = Connection::new().unwrap(); + con.send("{3|4:want,4:core,1:1,}"); + con.read(); + // evaluate command line argument let eval_result = match env::args().nth(1) { From 394d0519e2f3fcf1351b6965aabf26ed11879f7a Mon Sep 17 00:00:00 2001 From: laerling Date: Sat, 15 Sep 2018 16:21:55 +0200 Subject: [PATCH 2/4] Connection: Merge sending and receiving into send_and_receive() --- src/connection.rs | 12 ++++++------ src/main.rs | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 3e0038b..cfddfcd 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -63,7 +63,9 @@ impl Connection { } // FIXME: msg: Message instead of &str - pub fn send(&mut self, msg: &str) { + pub fn send_and_receive(&mut self, msg: &str) -> String { + + // send match self.mode { // in normal mode write to VT6 message stream @@ -77,18 +79,16 @@ impl Connection { // TODO }, }; - } - // FIXME asynchronous reading - pub fn read(&mut self) { + // read match self.mode { // in normal mode read from VT6 message stream ConnectionMode::Normal(ref mut stream) => { use std::io::Read; let mut answer = String::with_capacity(24); - let read_bytes = stream.read_to_string(&mut answer).unwrap(); - println!("Read {} bytes: {}", read_bytes, answer); + let _bytes_read = stream.read_to_string(&mut answer).unwrap(); + return answer; }, // in multiplexed mode read from stdin diff --git a/src/main.rs b/src/main.rs index b68e0b5..222b686 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,8 +86,8 @@ fn main() { // FIXME temp let mut con = Connection::new().unwrap(); - con.send("{3|4:want,4:core,1:1,}"); - con.read(); + let msg = "{3|4:want,4:core,1:1,}"; + println!("Received: {}", con.send_and_receive(msg)); // evaluate command line argument let eval_result = match env::args().nth(1) { From 1b917fbaaf3da87ff7a10d243ccfe9afe983dd41 Mon Sep 17 00:00:00 2001 From: laerling Date: Fri, 14 Sep 2018 23:55:21 +0200 Subject: [PATCH 3/4] Construct vt6 message instead of raw &str --- Cargo.lock | 65 ----------------------------------------------- src/connection.rs | 12 ++++----- src/main.rs | 17 +++++++++---- 3 files changed, 17 insertions(+), 77 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7b518cc..317792b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,14 +8,6 @@ dependencies = [ "vt6 0.0.1", ] -[[package]] -name = "aho-corasick" -version = "0.6.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "ansi_term" version = "0.11.0" @@ -236,14 +228,6 @@ dependencies = [ "cfg-if 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "memchr" -version = "2.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "memoffset" version = "0.2.1" @@ -399,26 +383,6 @@ dependencies = [ "redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "regex" -version = "0.2.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)", - "thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "regex-syntax" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "rental" version = "0.4.16" @@ -518,14 +482,6 @@ dependencies = [ "unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "thread_local" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "tokio" version = "0.1.8" @@ -725,11 +681,6 @@ dependencies = [ "tokio-reactor 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "ucd-util" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "unicode-width" version = "0.1.5" @@ -748,11 +699,6 @@ dependencies = [ "void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "utf8-ranges" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "vec_map" version = "0.8.1" @@ -771,10 +717,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "vt6" version = "0.0.1" -dependencies = [ - "lazy_static 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "winapi" @@ -815,7 +757,6 @@ dependencies = [ ] [metadata] -"checksum aho-corasick 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)" = "68f56c7353e5a9547cbd76ed90f7bb5ffc3ba09d4ea9bd1d8c06c8b1142eeb5a" "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" "checksum arrayvec 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "a1e964f9e24d588183fcb43503abda40d288c8657dfc27311516ce2f05675aef" "checksum atty 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9a7d5b8723950951411ee34d271d99dddcc2035a16ab25310ea2c8cfd4369652" @@ -843,7 +784,6 @@ dependencies = [ "checksum libc 0.2.43 (registry+https://github.com/rust-lang/crates.io-index)" = "76e3a3ef172f1a0b9a9ff0dd1491ae5e6c948b94479a3021819ba7d860c8645d" "checksum lock_api 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "949826a5ccf18c1b3a7c3d57692778d21768b79e46eb9dd07bfc4c2160036c54" "checksum log 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fcce5fa49cc693c312001daf1d13411c4a5283796bac1084299ea3e567113f" -"checksum memchr 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a3b4142ab8738a78c51896f704f83c11df047ff1bda9a92a661aa6361552d93d" "checksum memoffset 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0f9dc261e2b62d7a622bf416ea3c5245cdd5d9a7fcc428c0d06804dfce1775b3" "checksum mio 0.6.16 (registry+https://github.com/rust-lang/crates.io-index)" = "71646331f2619b1026cc302f87a2b8b648d5c6dd6937846a16cc8ce0f347f432" "checksum mio-named-pipes 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "f5e374eff525ce1c5b7687c4cef63943e7686524a387933ad27ca7ec43779cb3" @@ -862,8 +802,6 @@ dependencies = [ "checksum rand_core 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "edecf0f94da5551fc9b492093e30b041a891657db7940ee221f9d2f66e82eef2" "checksum redox_syscall 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)" = "c214e91d3ecf43e9a4e41e578973adeb14b474f2bee858742d127af75a0112b1" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" -"checksum regex 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "9329abc99e39129fcceabd24cf5d85b4671ef7c29c50e972bc5afe32438ec384" -"checksum regex-syntax 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "7d707a4fa2637f2dca2ef9fd02225ec7661fe01a53623c1e6515b6916511f7a7" "checksum rental 0.4.16 (registry+https://github.com/rust-lang/crates.io-index)" = "f098d5bfcbc5b929baf992ca6e09244daf4b47d1aedba3a808364faf48bd036e" "checksum rental-impl 0.4.15 (registry+https://github.com/rust-lang/crates.io-index)" = "cb24fb6b34fdd7b264154ea85c34bf9d3a9ac1355163b6e22ab8262f9f4a73f1" "checksum scoped-tls 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "332ffa32bf586782a3efaeb58f127980944bbc8c4d6913a86107ac2a5ab24b28" @@ -877,7 +815,6 @@ dependencies = [ "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum textwrap 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "307686869c93e71f94da64286f9a9524c0f308a9e1c87a583de8e9c9039ad3f6" -"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b" "checksum tokio 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "fbb6a6e9db2702097bfdfddcb09841211ad423b86c75b5ddaca1d62842ac492c" "checksum tokio-codec 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "881e9645b81c2ce95fcb799ded2c29ffb9f25ef5bef909089a420e5961dd8ccb" "checksum tokio-core 0.1.17 (registry+https://github.com/rust-lang/crates.io-index)" = "aeeffbbb94209023feaef3c196a41cbcdafa06b4a6f893f68779bb5e53796f71" @@ -893,11 +830,9 @@ dependencies = [ "checksum tokio-timer 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "d03fa701f9578a01b7014f106b47f0a363b4727a7f3f75d666e312ab7acbbf1c" "checksum tokio-udp 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "da941144b816d0dcda4db3a1ba87596e4df5e860a72b70783fe435891f80601c" "checksum tokio-uds 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "424c1ed15a0132251813ccea50640b224c809d6ceafb88154c1a8775873a0e89" -"checksum ucd-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd2be2d6639d0f8fe6cdda291ad456e23629558d466e2789d2c3e9892bda285d" "checksum unicode-width 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "882386231c45df4700b275c7ff55b6f3698780a650026380e72dabe76fa46526" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" "checksum unreachable 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "382810877fe448991dfc7f0dd6e3ae5d58088fd0ea5e35189655f84e6814fa56" -"checksum utf8-ranges 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fd70f467df6810094968e2fce0ee1bd0e87157aceb026a8c083bcf5e25b9efe4" "checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" "checksum version_check 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "7716c242968ee87e5542f8021178248f267f295a5c4803beae8b8b7fd9bc6051" "checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d" diff --git a/src/connection.rs b/src/connection.rs index cfddfcd..8190959 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -17,12 +17,12 @@ *******************************************************************************/ use std::env::vars; -use std::io; use std::io::Write; +use std::io; use std::os::unix::net::UnixStream; use std::path::PathBuf; use std::process::exit; -use vt6::core::msg::Message; +use std::vec::Vec; /// Encapsulates all connections to the VT6 server, that is the I/O stream (most /// commonly stdin and stdout) and the VT6 message stream (message input, @@ -62,16 +62,14 @@ impl Connection { exit(1); } - // FIXME: msg: Message instead of &str - pub fn send_and_receive(&mut self, msg: &str) -> String { + pub fn send_and_receive(&mut self, msg: Vec) -> String { // send match self.mode { // in normal mode write to VT6 message stream ConnectionMode::Normal(ref mut stream) => { - // FIXME - stream.write_all(msg.as_bytes()).unwrap(); + stream.write_all(msg.as_slice()).unwrap(); }, // in multiplexed mode write to stdout @@ -80,7 +78,7 @@ impl Connection { }, }; - // read + // receive match self.mode { // in normal mode read from VT6 message stream diff --git a/src/main.rs b/src/main.rs index 222b686..d678d13 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,14 +28,13 @@ use conch_parser::parse::DefaultParser; use conch_runtime::env::DefaultEnv; use conch_runtime::future::EnvFuture; use conch_runtime::spawn::sequence; -use std::env::vars; +use connection::Connection; use std::env; use std::fs::File; use std::io; use std::option::Option; use std::process::exit; use tokio_core::reactor::Core; -use connection::Connection; fn repl(script: &mut T) -> io::Result<()> { @@ -84,10 +83,18 @@ fn repl(script: &mut T) -> io::Result<()> { fn main() { - // FIXME temp + use vt6::core::msg::MessageFormatter; + let mut buf = vec![0; 22]; // 22 is the exact length + { // lifetime of MessageFormatter + let mut mf = MessageFormatter::new(&mut buf, "want", 2); + mf.add_argument("core"); + mf.add_argument(&1); + let _length = mf.finalize().unwrap(); + } // end borrow of buf by MessageFormatter let mut con = Connection::new().unwrap(); - let msg = "{3|4:want,4:core,1:1,}"; - println!("Received: {}", con.send_and_receive(msg)); + println!("Received: {}", con.send_and_receive(buf)); + + std::process::exit(0); // evaluate command line argument let eval_result = match env::args().nth(1) { From 87dd706d945168f30d589f26a352a4ac1163ffc0 Mon Sep 17 00:00:00 2001 From: laerling Date: Sat, 15 Sep 2018 20:39:50 +0200 Subject: [PATCH 4/4] Replace Connection by IO, a general input/output struct Not only message streams, but also the I/O stream has to go through this struct, because in mutliplexed mode it combines the both streams. --- src/connection.rs | 43 ++++++++++++++++++++++++++++++------------- src/main.rs | 2 +- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/connection.rs b/src/connection.rs index 8190959..1135956 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -24,11 +24,11 @@ use std::path::PathBuf; use std::process::exit; use std::vec::Vec; -/// Encapsulates all connections to the VT6 server, that is the I/O stream (most -/// commonly stdin and stdout) and the VT6 message stream (message input, -/// message output) and abstracts over the kind of connection (normal mode or -/// multiplexed mode) -pub struct Connection { +/// All input/output that is directed at the terminal has to go via this object. +/// It abstracts over the kind of connection (normal or multiplexed) and in +/// multiplexed mode joins message stream and I/O stream appropriately. In +/// normal mode it just forwards to stdout and the message stream. +pub struct IO { mode: ConnectionMode, } @@ -39,8 +39,9 @@ pub enum ConnectionMode { Multiplexed, } -impl Connection { +impl IO { + /// Determines the connection mode and returns a new IO object pub fn new() -> Result { // use normal mode if VT6 environment variable is present @@ -62,26 +63,41 @@ impl Connection { exit(1); } - pub fn send_and_receive(&mut self, msg: Vec) -> String { + pub fn is_multiplexed(&self) -> bool { + self.mode == ConnectionMode::Multiplexed + } + + /// Print to I/O stream + pub fn print(&self, &str) { + match self.mode { + ConnectionMode::Normal(_) => print!(&str); + } + } + + /// Print to message stream. For the time being, this is a synchronous + /// function, meaning that after sending it waits until a complete answer + /// message is received. + pub fn send_message(&mut self, msg: Vec) -> String { - // send + // send to the server match self.mode { - // in normal mode write to VT6 message stream + // in normal mode write to message stream ConnectionMode::Normal(ref mut stream) => { stream.write_all(msg.as_slice()).unwrap(); }, - // in multiplexed mode write to stdout + // in multiplexed mode merge in I/O stream ConnectionMode::Multiplexed => { // TODO + panic!("not yet implemented"); }, }; - // receive + // receive from the server match self.mode { - // in normal mode read from VT6 message stream + // in normal mode read from message stream ConnectionMode::Normal(ref mut stream) => { use std::io::Read; let mut answer = String::with_capacity(24); @@ -89,8 +105,9 @@ impl Connection { return answer; }, - // in multiplexed mode read from stdin + // in multiplexed mode parse from I/O stream ConnectionMode::Multiplexed => { + // TODO panic!("not yet implemented"); } }; diff --git a/src/main.rs b/src/main.rs index d678d13..a6fa4ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,7 +85,7 @@ fn main() { use vt6::core::msg::MessageFormatter; let mut buf = vec![0; 22]; // 22 is the exact length - { // lifetime of MessageFormatter + { let mut mf = MessageFormatter::new(&mut buf, "want", 2); mf.add_argument("core"); mf.add_argument(&1);