-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🕸 Implementation of work with the network (#31)
- Loading branch information
Showing
21 changed files
with
179 additions
and
60 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ mod condition; | |
mod exec; | ||
mod file; | ||
mod include; | ||
mod network; | ||
mod opcode_parser; | ||
mod print; | ||
mod sleep; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
Oops, something went wrong.