separate some functions by folders.

This commit is contained in:
Horhik 2021-02-28 00:58:55 +03:00
parent 3b8c3c9438
commit 1ddc42721c
17 changed files with 283 additions and 82 deletions

0
.cargo/config Normal file
View file

13
.idea/hole.iml Normal file
View file

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/examples" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

8
.idea/modules.xml Normal file
View file

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/hole.iml" filepath="$PROJECT_DIR$/.idea/hole.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View file

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

View file

@ -1,15 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" ENABLED="true" />
</configurations>
</component>
<component name="CargoProjects">
<cargoProject FILE="$PROJECT_DIR$/Cargo.toml" />
</component>
<component name="ChangeListManager">
<list default="true" id="e434c6bf-d215-476e-8569-84080f63e0f9" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Cargo.lock" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.lock" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Cargo.toml" beforeDir="false" afterPath="$PROJECT_DIR$/Cargo.toml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/front_conn.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/front_conn.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/mod.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/serv_conn.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/serv_conn.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/fcp/basic.rs" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/fcp/mod.rs" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/fcp/types.rs" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/chat/types.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/chat/types.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/db/mod.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/db/mod.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/lib.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/lib.rs" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main.rs" beforeDir="false" afterPath="$PROJECT_DIR$/src/main.rs" afterDir="false" />
</list>
@ -18,19 +26,57 @@
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="ClangdSettings">
<option name="formatViaClangd" value="false" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="MacroExpansionManager">
<option name="directoryName" value="8w8syaev" />
</component>
<component name="ProjectId" id="1mg1VEtaMSKRg5zbRaD9vETlRRi" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="ASKED_SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="cf.first.check.clang-format" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/../../../olymp/mosh" />
<property name="nodejs_package_manager_path" value="npm" />
<property name="org.rust.cargo.project.model.PROJECT_DISCOVERY" value="true" />
</component>
<component name="RunManager">
<configuration name="Run hole" type="CargoCommandRunConfiguration" factoryName="Cargo Command" temporary="true">
<option name="command" value="run --package hole --bin hole" />
<option name="workingDirectory" value="file://$PROJECT_DIR$" />
<option name="channel" value="DEFAULT" />
<option name="requiredFeatures" value="true" />
<option name="allFeatures" value="false" />
<option name="emulateTerminal" value="false" />
<option name="backtrace" value="SHORT" />
<envs />
<option name="isRedirectInput" value="false" />
<option name="redirectInputPath" value="" />
<method v="2">
<option name="CARGO.BUILD_TASK_PROVIDER" enabled="true" />
</method>
</configuration>
<recent_temporary>
<list>
<item itemvalue="Cargo.Run hole" />
</list>
</recent_temporary>
</component>
<component name="RustProjectSettings">
<option name="toolchainHomeDirectory" value="/usr/bin" />
<option name="version" value="2" />
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="e434c6bf-d215-476e-8569-84080f63e0f9" name="Default Changelist" comment="" />
@ -38,7 +84,14 @@
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1609900445316</updated>
<workItem from="1613568843781" duration="346000" />
<workItem from="1613570542642" duration="4470000" />
<workItem from="1614190476350" duration="9000" />
<workItem from="1614277959074" duration="31000" />
</task>
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
</project>

93
Cargo.lock generated
View file

@ -1,5 +1,20 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "ahash"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "739f4a8db6605981345c5654f3a85b056ce52f37a39d34da03f25bf2151ea16e"
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]]
name = "arc-swap"
version = "0.4.7"
@ -346,6 +361,13 @@ dependencies = [
"instant",
]
[[package]]
name = "fcpv2"
version = "0.0.1"
dependencies = [
"regex",
]
[[package]]
name = "fnv"
version = "1.0.7"
@ -528,6 +550,24 @@ dependencies = [
"web-sys",
]
[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
dependencies = [
"ahash",
]
[[package]]
name = "hashlink"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d99cf782f0dc4372d26846bec3de7804ceb5df083c2d4462c0b8d2330e894fa8"
dependencies = [
"hashbrown",
]
[[package]]
name = "hermit-abi"
version = "0.1.17"
@ -545,6 +585,7 @@ dependencies = [
"async-tls 0.10.0",
"async-trait",
"async-tungstenite",
"fcpv2",
"futures",
"futures-util",
"log",
@ -651,20 +692,14 @@ checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]]
name = "libsqlite3-sys"
version = "0.20.0"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a245984b1b06c291f46e27ebda9f369a94a1ab8461d0e845e23f9ced01f5db"
checksum = "64d31059f22935e6c31830db5249ba2b7ecd54fd73a9909286f0a67aa55c2fbd"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]]
name = "linked-hash-map"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
[[package]]
name = "lock_api"
version = "0.4.2"
@ -683,15 +718,6 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "lru-cache"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
dependencies = [
"linked-hash-map",
]
[[package]]
name = "matches"
version = "0.1.8"
@ -1066,6 +1092,24 @@ version = "0.1.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
[[package]]
name = "regex"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
"thread_local",
]
[[package]]
name = "regex-syntax"
version = "0.6.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5eb417147ba9860a96cfe72a0b93bf88fee1744b5636ec99ab20c1aa9376581"
[[package]]
name = "remove_dir_all"
version = "0.5.3"
@ -1092,15 +1136,15 @@ dependencies = [
[[package]]
name = "rusqlite"
version = "0.24.0"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c78c3275d9d6eb684d2db4b2388546b32fdae0586c20a82f3905d21ea78b9ef"
checksum = "d5f38ee71cbab2c827ec0ac24e76f82eca723cee92c509a65f67dee393c25112"
dependencies = [
"bitflags",
"fallible-iterator",
"fallible-streaming-iterator",
"hashlink",
"libsqlite3-sys",
"lru-cache",
"memchr",
"smallvec",
]
@ -1291,6 +1335,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "thread_local"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8018d24e04c95ac8790716a5987d0fec4f8b27249ffa0f7d33f1369bdfb88cbd"
dependencies = [
"once_cell",
]
[[package]]
name = "tinyvec"
version = "0.3.4"

View file

@ -8,7 +8,7 @@ edition = "2018"
[dependencies]
ntru = "0.5.6"
rusqlite = "0.24.0"
rusqlite = "0.24.2"
toml = "0.5.6"
tungstenite = "0.12.0"
tokio = { version = "0.3.3", features = ["full"] }
@ -22,6 +22,10 @@ serde = "1.0.116"
serde_derive = "1.0.116"
async-trait = "0.1.42"
[dependencies.fcpv2]
path = "../FCPv2"
[dependencies.async-tungstenite]
version = "0.11.0"
features = ["tokio-runtime", "tokio-native-tls", "async-tls", "async-std"]

BIN
hole.db Normal file

Binary file not shown.

View file

@ -1,4 +1,5 @@
use crate::chat::types::PackedMessage;
use crate::db;
use async_std::{
io,
net::{TcpListener, TcpStream},
@ -13,6 +14,8 @@ use serde_derive::Deserialize;
use std::env;
use std::sync::mpsc::{Receiver, Sender};
use super::stay_awake::request_repeater;
type SP = Sender<PackedMessage>;
type RP = Receiver<PackedMessage>;
@ -28,25 +31,6 @@ pub fn listen_client(server_sender: SP, client_receiver: RP) -> io::Result<()> {
task::block_on(connect_to_client(server_sender, client_receiver))
}
async fn request_repeater(ss: SP) -> io::Result<()> {
loop {
let time = std::time::Duration::from_millis(1000);
std::thread::sleep(time);
match ss.send(PackedMessage {
message: format!(
"ClientGet\n\
URI=KSK@msg23.txt\n\
Identifier=doesnt_matter?\n\
Verbosity=0\n\
ReturnType=direct\n\
EndMessage\n\n"
),
}) {
Ok(_) => {}
Err(e) => println!("{:?}", e),
}
}
}
async fn connect_to_client(server_sender: SP, client_receiver: RP) -> io::Result<()> {
let addr = env::args()
@ -110,6 +94,8 @@ async fn connection_for_sending(
serde_json::from_str(jsoned.to_text().unwrap());
if let Ok(received_msg) = res {
let msg = received_msg.message;
db::start_db().unwrap();
server_sender.send(PackedMessage { message: msg }).unwrap();
/* message example
{

View file

@ -1,4 +1,6 @@
pub mod chat_api_base;
pub mod front_conn;
pub mod serv_conn;
pub mod stay_awake;
pub mod serv_handler;
pub mod types;

View file

@ -1,20 +1,13 @@
use crate::chat::types::PackedMessage;
use crate::fcpv2;
use super::serv_handler::to_server_sender;
use crate::chat::types::{PackedMessage, RP, SP};
use async_std::task;
use serde_derive::Deserialize;
use std::env;
use std::sync::mpsc::{Receiver, Sender};
use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::{
tcp::{OwnedReadHalf, OwnedWriteHalf},
TcpStream,
},
io::{self, AsyncReadExt},
net::{tcp::OwnedReadHalf, TcpStream},
};
type SP = Sender<PackedMessage>;
type RP = Receiver<PackedMessage>;
#[tokio::main]
pub async fn listen_server(client_sender: SP, server_receiver: RP) -> io::Result<()> {
task::block_on(connect_to_server(client_sender, server_receiver))
@ -26,7 +19,7 @@ async fn connect_to_server(client_sender: SP, server_receiver: RP) -> io::Result
.unwrap_or_else(|| "127.0.0.1:9481".to_string());
let stream = TcpStream::connect(&addr).await.expect("weeror here");
let (mut receiver, mut sender) = stream.into_split();
let (receiver, sender) = stream.into_split();
let t = task::spawn(server_responce_getter(receiver, client_sender));
to_server_sender(sender, server_receiver).await?;
match t.await {
@ -50,31 +43,6 @@ async fn server_responce_getter(mut receiver: OwnedReadHalf, client_sender: SP)
Err(e) => println!("Error: {} ", e),
}
}
Ok(())
}
async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP) -> io::Result<()> {
while let Ok(res) = server_receiver.recv() {
//TODO from_core_to_server_handler
if res.message == "STARTAPP!" {
let _ = sender
.write(("ClientHello\nName=ggg\nExpectedVersion=2.0\nEndMessage\n\n").as_bytes())
.await?;
} else if res.message.lines().next() == Some("ClientGet") {
let _ = sender.write(res.message.as_bytes()).await?;
} else {
//println!("{:?}", res.message);
let _ = sender.write(
format!(
"ClientPut\nIdentifier=hello\nURI=KSK@msg23.txt\nDataLength={}\nUploadFrom=direct\nEndMessage\n{}\n\n",
res.message.len(),
res.message
)
.as_bytes(),
).await;
}
}
Ok(())
}
#[derive(Deserialize, Debug)]

30
src/chat/serv_handler.rs Normal file
View file

@ -0,0 +1,30 @@
use crate::chat::types::{RP, SP};
use tokio::{
io::{self, AsyncReadExt, AsyncWriteExt},
net::{
tcp::{OwnedReadHalf, OwnedWriteHalf},
TcpStream,
},
};
use fcpv2::client::fcp_types::{ClientHello, ClientPut, FcpParser, SSK};
use fcpv2::client::types::FcpRequest;
pub async fn to_server_sender(mut sender: OwnedWriteHalf, server_receiver: RP) -> io::Result<()> {
while let Ok(res) = server_receiver.recv() {
//TODO from_core_to_server_handler
if res.message == "STARTAPP!" {
let _ = sender
.write((ClientHello::new("name".to_string(), 2.0).convert()).as_bytes())
.await?;
} else if res.message.lines().next() == Some("ClientGet") {
let _ = sender.write(res.message.as_bytes()).await?;
} else {
//println!("{:?}", res.message);
let key = SSK::parse("KSK@msg23.txt").unwrap();
let cp = ClientPut::new_default(key, "msg23.txt", "hello", &res.message[..]).convert();
let _ = sender.write(cp.as_bytes()).await;
}
}
Ok(())
}

25
src/chat/stay_awake.rs Normal file
View file

@ -0,0 +1,25 @@
use crate::chat::types::PackedMessage;
use async_std::io;
use std::sync::mpsc::Sender;
type SP = Sender<PackedMessage>;
pub async fn request_repeater(ss: SP) -> io::Result<()> {
loop {
let time = std::time::Duration::from_millis(1000);
std::thread::sleep(time);
match ss.send(PackedMessage {
message: format!(
"ClientGet\n\
URI=KSK@msg23.txt\n\
Identifier=doesnt_matter?\n\
Verbosity=0\n\
ReturnType=direct\n\
EndMessage\n\n"
),
}) {
Ok(_) => {}
Err(e) => println!("{:?}", e),
}
}
}

View file

@ -1,3 +1,4 @@
use std::sync::mpsc::{Receiver, Sender};
//type Decoded = String;
//type Encoded = String;
@ -34,3 +35,6 @@
pub struct PackedMessage {
pub message: String,
}
pub type SP = Sender<PackedMessage>;
pub type RP = Receiver<PackedMessage>;

View file

@ -1,4 +1,50 @@
use rusqlite::{params, Connection, Result};
pub mod messaging;
fn main () {
#[derive(Debug)]
pub struct Person {
id: i32,
name: String,
data: Option<Vec<u8>>,
}
pub fn start_db() -> Result<()> {
let conn = Connection::open("../hole.db")?;
println!("{}", conn.is_autocommit());
match conn.execute(
"CREATE TABLE person (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
data BLOB
)",
params![],
) {
Ok(_) => {}
Err(e) => println!("{:?}", e),
}
let me = Person {
id: 0,
name: "Steven".to_string(),
data: None,
};
conn.execute(
"INSERT INTO person (name, data) VALUES (?1, ?2)",
params![me.name, me.data],
)?;
let mut stmt = conn.prepare("SELECT id, name, data FROM person")?;
let person_iter = stmt.query_map(params![], |row| {
Ok(Person {
id: row.get(0)?,
name: row.get(1)?,
data: row.get(2)?,
})
})?;
for person in person_iter {
println!("Found person {:?}", person.unwrap());
}
Ok(())
}

View file

@ -1,5 +1,6 @@
mod chat;
pub mod db;
pub mod encrypting;
pub mod fcpv2;
pub use chat::front_conn;

View file

@ -43,6 +43,8 @@ use std::{
*/
fn main() -> io::Result<()> {
db::start_db().unwrap();
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();