From 88fae2cba4204183a70840b4b56a78bcf5e52029 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Sat, 20 Apr 2019 01:24:19 +0200 Subject: [PATCH] create skeleton for server from design --- doc/grobdesign/server/server_architektur.rs | 245 ------------------ server/.gitkeep | 0 server/texture-sync-server/Cargo.toml | 10 + server/texture-sync-server/src/main.rs | 19 ++ server/texture-sync-server/src/model/mod.rs | 20 ++ .../src/persistency/image_convert/mod.rs | 19 ++ .../src/persistency/mod.rs | 75 ++++++ .../src/persistency/search/mod.rs | 38 +++ .../texture-sync-server/src/protocol/mod.rs | 52 ++++ .../texture-sync-server/src/server_state.rs | 44 ++++ 10 files changed, 277 insertions(+), 245 deletions(-) delete mode 100644 doc/grobdesign/server/server_architektur.rs delete mode 100644 server/.gitkeep create mode 100644 server/texture-sync-server/Cargo.toml create mode 100644 server/texture-sync-server/src/main.rs create mode 100644 server/texture-sync-server/src/model/mod.rs create mode 100644 server/texture-sync-server/src/persistency/image_convert/mod.rs create mode 100644 server/texture-sync-server/src/persistency/mod.rs create mode 100644 server/texture-sync-server/src/persistency/search/mod.rs create mode 100644 server/texture-sync-server/src/protocol/mod.rs create mode 100644 server/texture-sync-server/src/server_state.rs diff --git a/doc/grobdesign/server/server_architektur.rs b/doc/grobdesign/server/server_architektur.rs deleted file mode 100644 index 72617d3..0000000 --- a/doc/grobdesign/server/server_architektur.rs +++ /dev/null @@ -1,245 +0,0 @@ -// main author: Robin Willmann -// date : see git. -// WIP, changes welcome - -extern crate image; - -pub mod model { - pub struct Sha256(pub [u8; 64]); - - pub struct Texture { - pub id: String, - pub name: String, - pub tags: Vec, - pub format: TextureFormat, - pub resolution: (usize, usize), - pub texture_hash: Sha256, - } - - pub enum TextureFormat { - PNG, - JPG, - } -} - -pub mod protocol { - - use crate::model::*; - - use std::io; - - pub enum ReplaceTextureStatus { - // Call Again With Texture Binary - NeedTextureData, - // Done. - Ok, - } - - pub type ProtocolResult = Result; - pub enum ProtocolError { - BadRequest(String), - FileNotFound(String), - Conflict(String), - InternalServerError(std::io::Error), - NotImplemented, - } - - pub trait ProtocolHandler: Send + Sync { - fn query(&mut self, query: &[String]) -> ProtocolResult>; - - fn get_texture_by_id(&mut self, id: String) -> ProtocolResult>; - - fn get_texture_by_name(&mut self, id: String) -> ProtocolResult>; - - fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult>; - - fn get_texture_preview(&mut self, hash: Sha256) -> ProtocolResult>; - - fn replace_texture( - &mut self, - delete: Option, - insert: Option, - insert_texture_data: Option>, - ) -> ProtocolResult; - } - - pub struct ProtocolConfig { - pub port: u16, - } - - pub fn listen_forever(handler: &ProtocolHandler) -> io::Result<()> { - unimplemented!() - } -} - -pub mod persistency { - - use crate::model::*; - - use std::collections::HashMap; - use std::io; - use std::path::{Path, PathBuf}; - use std::sync::Arc; - - pub type TextureFileResult = Result< Arc> , TextureFileError> - pub enum TextureFileError { - NotFound, - IOError(io::Error), - ImgError(::image::Error), - } - - pub struct DataStore { - // private attributes - // may change - data_dir: PathBuf, - texture: Vec, - preview_cache: HashMap<(TextureFormat, Sha256), Arc>>, - } - - impl DataStore { - pub fn new(path: &Path) -> io::Result { - unimplemented!() - } - - pub fn garbage_collect(&mut self) -> io::Result<()> { - unimplemented!() - } - - pub fn query(&mut self, query: &self::search::Query) -> Vec { - unimplemented!(); - // calls self::search::search(... ) - } - - /// returns true if successful - pub fn delete(&mut self, tex: &Texture) -> bool { - unimplemented!(); - } - - pub fn insert(&mut self, tex: Texture) -> bool { - unimplemented!(); - } - - pub fn by_name<'a>(&'a self, name: &str) -> &'a Texture { - unimplemented!(); - } - - pub fn by_id<'a, 'b>(&'a self, id: &'b str) -> &'a Texture { - unimplemented!(); - } - - pub fn has_hash(&self, hash: &Sha256) -> bool { - unimplemented!(); - } - - pub fn get_texture_file(&mut self, hash: &Sha256) -> TextureFileResult { - unimplemented!(); - } - - pub fn get_texture_preview(&mut self, hash: &Sha256) -> TextureFileResult { - unimplemented!(); - } - } - - pub use self::search::Query; - - mod search { - - use crate::model::*; - - pub struct Query { - filters: Vec, - } - - pub type QueryParserResult = Result; - pub enum QuerySyntaxError { - UnknownFilter, - } - - impl Query { - pub fn parse(input: &[String]) -> QueryParserResult { - unimplemented!() - } - } - - pub fn search(input: &[Texture], query: &Query) -> Vec { - unimplemented!() - } - - //private - enum QueryFilterModifier { - None(QueryFilter), - Not(QueryFilter), - } - - //private - enum QueryFilter { - TagName(String), - InName(String), - MinResolution(usize), - BeforeDate { year: u16, month: u16, day: u16 }, - } - } - - pub mod image_convert { - use crate::model::*; - - pub struct ConvertConfig { - pub desired_size: (usize, usize), - } - - pub fn generate_preview( - input: &[u8], - format: TextureFormat, - config: &ConvertConfig, - ) -> ::image::ImageResult> { - unimplemented!() - } - } -} - -pub mod main { - - use crate::model::*; - use crate::protocol::*; - - use std::sync::Arc; - - struct ServerState { - // private attributes - } - - impl ProtocolHandler for ServerState { - fn query(&mut self, query: &[String]) -> ProtocolResult> { - unimplemented!() - } - - fn get_texture_by_id(&mut self, id: String) -> ProtocolResult> { - unimplemented!() - } - - fn get_texture_by_name(&mut self, id: String) -> ProtocolResult> { - unimplemented!() - } - - fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult>> { - unimplemented!() - } - - fn get_texture_preview(&mut self, hash: Sha256) -> ProtocolResult>> { - unimplemented!() - } - - fn replace_texture( - &mut self, - delete: Option, - insert: Option, - insert_texture_data: Option>, - ) -> ProtocolResult { - unimplemented!() - } - } - - pub fn main() { - unimplemented!() - } -} diff --git a/server/.gitkeep b/server/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/server/texture-sync-server/Cargo.toml b/server/texture-sync-server/Cargo.toml new file mode 100644 index 0000000..3058b99 --- /dev/null +++ b/server/texture-sync-server/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "texture-sync-server" +version = "0.1.0" +authors = ["CodeSteak "] +edition = "2018" + +[dependencies] +image = "0.21.1" +serde = "1.0.90" +serde_json = "1.0.39" \ No newline at end of file diff --git a/server/texture-sync-server/src/main.rs b/server/texture-sync-server/src/main.rs new file mode 100644 index 0000000..61e8625 --- /dev/null +++ b/server/texture-sync-server/src/main.rs @@ -0,0 +1,19 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + +extern crate serde_json; +extern crate serde; +extern crate image; + +pub mod model; +pub mod persistency; +pub mod protocol; + +mod server_state; +use server_state::*; + +fn main() { + println!("Hello, world!"); +} diff --git a/server/texture-sync-server/src/model/mod.rs b/server/texture-sync-server/src/model/mod.rs new file mode 100644 index 0000000..133fd04 --- /dev/null +++ b/server/texture-sync-server/src/model/mod.rs @@ -0,0 +1,20 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + +pub struct Sha256(pub [u8; 64]); + +pub struct Texture { + pub id: String, + pub name: String, + pub tags: Vec, + pub format: TextureFormat, + pub resolution: (usize, usize), + pub texture_hash: Sha256, +} + +pub enum TextureFormat { + PNG, + JPEG, +} \ No newline at end of file diff --git a/server/texture-sync-server/src/persistency/image_convert/mod.rs b/server/texture-sync-server/src/persistency/image_convert/mod.rs new file mode 100644 index 0000000..a06f0ab --- /dev/null +++ b/server/texture-sync-server/src/persistency/image_convert/mod.rs @@ -0,0 +1,19 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + + +use crate::model::*; + +pub struct ConvertConfig { + pub desired_size: (usize, usize), +} + +pub fn generate_preview( + input: &[u8], + format: TextureFormat, + config: &ConvertConfig, +) -> ::image::ImageResult> { + unimplemented!() +} \ No newline at end of file diff --git a/server/texture-sync-server/src/persistency/mod.rs b/server/texture-sync-server/src/persistency/mod.rs new file mode 100644 index 0000000..d78e3db --- /dev/null +++ b/server/texture-sync-server/src/persistency/mod.rs @@ -0,0 +1,75 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + +use crate::model::*; + +use std::collections::HashMap; +use std::io; +use std::path::{Path, PathBuf}; +use std::sync::Arc; + +pub use self::search::Query; +mod search; +mod image_convert; + +pub type TextureFileResult = Result< Arc> , TextureFileError>; +pub enum TextureFileError { + NotFound, + IoError(io::Error), + ImageError(::image::ImageError), +} + +pub struct DataStore { + // private attributes + // may change + data_dir: PathBuf, + texture: Vec, + preview_cache: HashMap<(TextureFormat, Sha256), Arc>>, +} + +impl DataStore { + pub fn new(path: &Path) -> io::Result { + unimplemented!() + } + + pub fn garbage_collect(&mut self) -> io::Result<()> { + unimplemented!() + } + + pub fn query(&mut self, query: &self::search::Query) -> Vec { + unimplemented!(); + // calls self::search::search(... ) + } + + /// returns true if successful + pub fn delete(&mut self, tex: &Texture) -> bool { + unimplemented!(); + } + + pub fn insert(&mut self, tex: Texture) -> bool { + unimplemented!(); + } + + pub fn by_name<'a>(&'a self, name: &str) -> Option<&'a Texture> { + unimplemented!(); + } + + pub fn by_id<'a, 'b>(&'a self, id: &'b str) -> Option<&'a Texture> { + unimplemented!(); + } + + pub fn has_hash(&self, hash: &Sha256) -> bool { + unimplemented!(); + } + + pub fn get_texture_file(&mut self, hash: &Sha256) -> TextureFileResult { + unimplemented!(); + } + + pub fn get_texture_preview(&mut self, hash: &Sha256) -> TextureFileResult { + unimplemented!(); + } +} + diff --git a/server/texture-sync-server/src/persistency/search/mod.rs b/server/texture-sync-server/src/persistency/search/mod.rs new file mode 100644 index 0000000..611258b --- /dev/null +++ b/server/texture-sync-server/src/persistency/search/mod.rs @@ -0,0 +1,38 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + + +use crate::model::*; + +pub struct Query { + filters: Vec, +} + +pub type QueryParserResult = Result; +pub enum QuerySyntaxError { + UnknownFilter, +} + +impl Query { + pub fn parse(input: &[String]) -> QueryParserResult { + unimplemented!() + } +} + +pub fn search(input: &[Texture], query: &Query) -> Vec { + unimplemented!() +} + +enum QueryFilterModifier { + None(QueryFilter), + Not(QueryFilter), +} + +enum QueryFilter { + TagName(String), + InName(String), + MinResolution(usize), + BeforeDate { year: u16, month: u16, day: u16 }, +} \ No newline at end of file diff --git a/server/texture-sync-server/src/protocol/mod.rs b/server/texture-sync-server/src/protocol/mod.rs new file mode 100644 index 0000000..df7ff2a --- /dev/null +++ b/server/texture-sync-server/src/protocol/mod.rs @@ -0,0 +1,52 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + +use crate::model::*; + +use std::sync::Arc; +use std::io; + +pub enum ReplaceTextureStatus { + // Call Again With Texture Binary + NeedTextureData, + // Done. + Ok, +} + +pub type ProtocolResult = Result; +pub enum ProtocolError { + BadRequest(String), + FileNotFound(String), + Conflict(String), + InternalServerError(std::io::Error), + NotImplemented, +} + +pub trait ProtocolHandler: Send + Sync { + fn query(&mut self, query: &[String]) -> ProtocolResult>; + + fn get_texture_by_id(&mut self, id: String) -> ProtocolResult>; + + fn get_texture_by_name(&mut self, id: String) -> ProtocolResult>; + + fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult>>; + + fn get_texture_preview(&mut self, hash: Sha256) -> ProtocolResult>>; + + fn replace_texture( + &mut self, + delete: Option, + insert: Option, + insert_texture_data: Option>, + ) -> ProtocolResult; +} + +pub struct ProtocolConfig { + pub port: u16, +} + +pub fn listen_forever(handler: &ProtocolHandler) -> io::Result<()> { + unimplemented!() +} \ No newline at end of file diff --git a/server/texture-sync-server/src/server_state.rs b/server/texture-sync-server/src/server_state.rs new file mode 100644 index 0000000..02a5095 --- /dev/null +++ b/server/texture-sync-server/src/server_state.rs @@ -0,0 +1,44 @@ +// TODO: remove on implementation +#![allow(unused_imports)] +#![allow(unused_variables)] +#![allow(dead_code)] + +use crate::model::*; +use crate::protocol::*; + +use std::sync::Arc; + +pub struct ServerState { + // ... +} + +impl ProtocolHandler for ServerState { + fn query(&mut self, query: &[String]) -> ProtocolResult> { + unimplemented!() + } + + fn get_texture_by_id(&mut self, id: String) -> ProtocolResult> { + unimplemented!() + } + + fn get_texture_by_name(&mut self, id: String) -> ProtocolResult> { + unimplemented!() + } + + fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult>> { + unimplemented!() + } + + fn get_texture_preview(&mut self, hash: Sha256) -> ProtocolResult>> { + unimplemented!() + } + + fn replace_texture( + &mut self, + delete: Option, + insert: Option, + insert_texture_data: Option>, + ) -> ProtocolResult { + unimplemented!() + } +}