Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ authors = ["lærling <laerling@posteo.de>"]
conch-parser = "^0.1.0"
conch-runtime = "^0.1.4"
tokio-core = "^0.1.17"
vt6 = { path = "../vt6.rs/vt6" }
90 changes: 90 additions & 0 deletions src/connection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*******************************************************************************
*
* Copyright 2018 lærling <laerling@posteo.de>
*
* 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 <http://www.gnu.org/licenses/>.
*
*******************************************************************************/

use std::env::vars;
use std::io;
use std::io::Write;
use std::os::unix::net::UnixStream;
use std::path::PathBuf;
use std::io::Error;
use std::io::ErrorKind;
use vt6::common::core::msg::Message;
use std::io::Read;

const BUF_SIZE: usize = 1024;

/// Encapsulates the server connection
pub struct Connection {
stream: UnixStream,
buffer: [u8; BUF_SIZE],
}

impl Connection {

/// finds the vt6 socket and connects to it, returning a new connection object
pub fn new() -> Result<Connection, io::Error> {

if let Some(vt6_socket_var) = vars().find(|var| var.0 == "VT6") {
let con = Connection {
stream: match UnixStream::connect(PathBuf::from(&vt6_socket_var.1)) {
Ok(stream) => stream,
Err(e) => {
eprintln!("Cannot connect to socket '{}'", vt6_socket_var.1);
panic!(e); // TODO: Only in debug profile, else exit with 1
},
},
buffer: [0; BUF_SIZE],
};
return Ok(con);
}

Err(Error::new(ErrorKind::NotFound, "VT6 server socket not found."))
}

/// sends a message and waits for the response (synchronously)
pub fn send_and_receive(&mut self, msg: &str) -> (Message, usize) {

// send
self.stream.write_all(msg.as_bytes()).unwrap(); // TODO: Use vt6 messages

// read until there is something that can be parsed
let mut buffer_offset: usize = 0;
loop {

// read into buffer...
let bytes_read = match self.stream.read(&mut self.buffer[buffer_offset..]) {
Ok(b) => b,
Err(e) => {
eprintln!("Connection to server socket lost");
panic!(e); // TODO: Only in debug profile, else exit with 1
}
};

// adjust offset for next read
buffer_offset += bytes_read;

// ...until there is something that can be parsed
match Message::parse(&self.buffer[..buffer_offset]) {
Ok(_) => break,
Err(_) => {}, // continue
}
}

return Message::parse(&self.buffer[..buffer_offset]).unwrap();
}
}
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
extern crate conch_parser;
extern crate conch_runtime;
extern crate tokio_core;
extern crate vt6;

use conch_parser::lexer::Lexer;
use conch_parser::parse::DefaultParser;
Expand All @@ -32,6 +33,8 @@ use std::option::Option;
use tokio_core::reactor::Core;
use std::process::exit;

mod connection;

fn repl<T: io::BufRead>(script: &mut T) -> io::Result<()> {

// make event loop
Expand Down Expand Up @@ -79,6 +82,12 @@ fn repl<T: io::BufRead>(script: &mut T) -> io::Result<()> {

fn main() {

// FIXME temp
let mut con = connection::Connection::new().unwrap();
println!("main: core: {}", con.send_and_receive("{3|4:want,4:core,1:1,}").0);
println!("main: LmAo: {}", con.send_and_receive("{3|4:want,4:LmAo,1:1,}").0);
std::process::exit(0);

// evaluate command line argument
let eval_result = match env::args().nth(1) {

Expand Down