Skip to content

Commit

Permalink
🕸 Implementation of work with the network (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
leonovk authored Feb 18, 2024
1 parent a3f7430 commit 062a217
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minicode"
version = "1.4.2"
version = "1.4.3"
authors = ["Kirill Leonov <leonov7632@gmail.com>"]
edition = "2021"
repository = "https://github.com/leonovk/minicode"
Expand Down
5 changes: 3 additions & 2 deletions src/interpreter/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::opcode_result_type::*;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -26,7 +27,7 @@ pub fn push<'a>(

first_value.push(second_value);

Ok(OpCodeResultType::Empty)
Ok(Empty)
}

#[cfg(test)]
Expand Down
5 changes: 3 additions & 2 deletions src/interpreter/calculate.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::opcode_result_type::*;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::OperationType;
use crate::opcode::OperationType::*;
use crate::opcode::ValueType;
Expand Down Expand Up @@ -36,7 +37,7 @@ pub fn calculate<'a>(
calculate_new_value(old_value, operational_meaning, o_type),
);

Ok(OpCodeResultType::Empty)
Ok(Empty)
}

fn calculate_new_value(old_value: &f64, oper_value: &f64, o_type: &OperationType) -> ValueType {
Expand Down
5 changes: 3 additions & 2 deletions src/interpreter/condition.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::opcode_result_type::*;
use crate::opcode::ComparisonOperators;
use crate::opcode::ComparisonOperators::*;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;
Expand All @@ -24,7 +25,7 @@ pub fn condition(
};

match result {
Ok(b) => Ok(OpCodeResultType::Bool(b)),
Ok(b) => Ok(Bool(b)),
Err(e) => Err(e),
}
}
Expand Down
5 changes: 3 additions & 2 deletions src/interpreter/create.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::opcode_result_type::*;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;
Expand All @@ -20,7 +21,7 @@ pub fn create<'a>(
Arr(_arr) => target.insert(key, Arr(Vec::new())),
};

Ok(OpCodeResultType::Empty)
Ok(Empty)
}

fn complex_assignments_value<'a>(
Expand Down
5 changes: 0 additions & 5 deletions src/interpreter/error_printer.rs

This file was deleted.

6 changes: 3 additions & 3 deletions src/interpreter/execute.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;
use std::process::Command;

use super::OpCodeResultType;

pub fn execute<'a>(
key: &'a String,
command: &String,
Expand All @@ -22,5 +22,5 @@ pub fn execute<'a>(

target.insert(key, Line(result));

Ok(OpCodeResultType::Empty)
Ok(Empty)
}
7 changes: 4 additions & 3 deletions src/interpreter/include.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::opcode_result_type::*;
use crate::code_runner::run;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;
Expand Down Expand Up @@ -30,9 +31,9 @@ pub fn include(
run(file_clone, result_args_value);
});

Ok(OpCodeResultType::Thread(Some(handle)))
Ok(Thread(Some(handle)))
} else {
run(file.to_string(), result_args_value);
Ok(OpCodeResultType::Thread(None))
Ok(Thread(None))
}
}
18 changes: 9 additions & 9 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::opcode::OpCode;
use crate::opcode::OpCode::*;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;
Expand All @@ -9,21 +11,18 @@ mod arrays;
mod calculate;
mod condition;
mod create;
mod error_printer;
mod execute;
mod include;
mod opcode_result_type;
mod print_file;
mod print_value;
mod network;
mod print;
use arrays::push;
use calculate::calculate;
use condition::condition;
use create::create;
use execute::execute;
use include::include;
use opcode_result_type::*;
use print_file::print_file;
use print_value::print_value;
use network::*;
use print::*;

pub fn exegete(operations: Vec<OpCode>, args: Vec<String>, file: &String) {
if operations.is_empty() {
Expand Down Expand Up @@ -58,14 +57,15 @@ pub fn exegete(operations: Vec<OpCode>, args: Vec<String>, file: &String) {
Include(p, a, s) => {
push_new_thread(&mut parallel_computing, include(p, a, &addresses, s))
}
SendTcp(addr, mes) => send_tcp(addr, mes, &addresses),
Sleep(i) => go_sleep(i),
EmptyLine => Ok(OpCodeResultType::Empty),
EmptyLine => Ok(Empty),
};

match result {
Ok(_) => {}
Err(e) => {
error_printer::print_error(file, pointer + 1, &e);
print_error(file, pointer + 1, &e);
return;
}
}
Expand Down
50 changes: 50 additions & 0 deletions src/interpreter/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use crate::network::send_tcp_message;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;

pub fn send_tcp(
addr: &String,
mes: &String,
storage: &HashMap<&String, ValueType>,
) -> Result<OpCodeResultType, String> {
let binding_addr = Line(addr.to_string());
let binding_mes = Line(addr.to_string());
let address = storage.get(addr).unwrap_or(&binding_addr);
let message = storage.get(mes).unwrap_or(&binding_mes);

if !all_lines(address, message) {
return Err("Only strings can be either an address or a message".to_string());
}

let data = unpack_strings(address, message);
let result = send_tcp_message(data.0, data.1);

// +_+
match result {
Ok(_ok) => Ok(Empty),
Err(er) => Err(er.to_string()),
}
}

fn all_lines(adr: &ValueType, mes: &ValueType) -> bool {
match (adr, mes) {
(ValueType::Line(_), ValueType::Line(_)) => true,
_ => false,
}
}

fn unpack_strings<'a>(adr: &'a ValueType, mes: &'a ValueType) -> (&'a String, &'a String) {
let adr_string = match adr {
Line(line) => line,
_ => panic!("address parsing error"),
};
let mes_string = match mes {
Line(line) => line,
_ => panic!("message parsing error"),
};

(adr_string, mes_string)
}
7 changes: 0 additions & 7 deletions src/interpreter/opcode_result_type.rs

This file was deleted.

27 changes: 26 additions & 1 deletion src/interpreter/print_file.rs → src/interpreter/print.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
use super::opcode_result_type::*;
use crate::files::write_content_to_file;
use crate::opcode::OpCodeResultType;
use crate::opcode::OpCodeResultType::*;
use crate::opcode::ValueType;
use crate::opcode::ValueType::*;
use std::collections::HashMap;

pub fn print_value(
key: &String,
storage: &HashMap<&String, ValueType>,
) -> Result<OpCodeResultType, String> {
let value = storage.get(key);

match value {
Some(s) => match s {
Int(i) => println!("{}", i),
Line(s) => println!("{}", s),
Arr(_a) => return Err("you can't print an array".to_string()),
},
None => println!("{}", key),
};

Ok(Empty)
}

pub fn print_file(
key: &String,
path: &String,
Expand All @@ -25,3 +44,9 @@ pub fn print_file(

Ok(OpCodeResultType::Empty)
}

pub fn print_error(file: &String, line: usize, error: &String) {
println!("File -> {}", file);
println!("Line # {}", line);
println!("Error: {}", error);
}
22 changes: 0 additions & 22 deletions src/interpreter/print_value.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Parser;
mod code_runner;
mod files;
mod interpreter;
mod network;
mod opcode;
mod parser;
mod self_update;
Expand Down
12 changes: 12 additions & 0 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::io::Write;
use std::net::TcpStream;

pub fn send_tcp_message(addr: &String, message: &String) -> std::io::Result<()> {
// Подключаемся к серверу по указанному IP и порту
let mut stream = TcpStream::connect(addr)?;

// Отправляем сообщение
stream.write_all(message.as_bytes())?;

Ok(())
}
9 changes: 9 additions & 0 deletions src/opcode/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::thread::JoinHandle;

#[derive(PartialEq, Debug, Clone, PartialOrd)]
pub enum ValueType {
Int(f64),
Expand Down Expand Up @@ -32,6 +34,13 @@ pub enum OpCode {
ErrorCode(String),
Execute(String, String, Vec<String>),
Include(String, Vec<String>, bool),
SendTcp(String, String),
Sleep(u64),
EmptyLine,
}

pub enum OpCodeResultType {
Bool(bool),
Thread(Option<JoinHandle<()>>),
Empty,
}
1 change: 1 addition & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod condition;
mod exec;
mod file;
mod include;
mod network;
mod opcode_parser;
mod print;
mod sleep;
Expand Down
44 changes: 44 additions & 0 deletions src/parser/network.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
pub use crate::opcode::OpCode;
pub use crate::opcode::OpCode::*;

pub fn send_tcp(data: Vec<&str>) -> OpCode {
if data.len() < 3 {
return ErrorCode("the operation is not specified correctly".to_string());
}

let addr = data[1].to_string();
let message = data[2].to_string();

SendTcp(addr, message)
}

#[cfg(test)]
mod tests {
use super::send_tcp;
use crate::opcode::OpCode::*;
use pretty_assertions::assert_eq;

#[test]
fn test_send_tcp_one() {
let data = vec!["@", "a", "m"];
let result = send_tcp(data);
assert_eq!(result, SendTcp("a".to_string(), "m".to_string()));
}

#[test]
fn test_send_tcp_two() {
let data = vec!["@", "a", "m", "asdas", "asdsad"];
let result = send_tcp(data);
assert_eq!(result, SendTcp("a".to_string(), "m".to_string()));
}

#[test]
fn test_send_tcp_three() {
let data = vec!["@", "a"];
let result = send_tcp(data);
assert_eq!(
result,
ErrorCode("the operation is not specified correctly".to_string())
);
}
}
Loading

0 comments on commit 062a217

Please sign in to comment.