partialy implement Glue Logic
This commit is contained in:
@ -4,13 +4,23 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::model::*;
|
||||
use crate::persistency::*;
|
||||
use crate::protocol::*;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::path::Path;
|
||||
use std::sync::*;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ServerState {
|
||||
// ...
|
||||
data_store: Arc<RwLock<DataStore>>,
|
||||
}
|
||||
|
||||
impl ServerState {
|
||||
pub fn new(storage_path: &Path) -> std::io::Result<Self> {
|
||||
Ok(Self {
|
||||
data_store: Arc::new(RwLock::new(DataStore::new(&storage_path)?)),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl ProtocolHandler for ServerState {
|
||||
@ -19,15 +29,21 @@ impl ProtocolHandler for ServerState {
|
||||
}
|
||||
|
||||
fn get_texture_by_id(&mut self, id: &str) -> ProtocolResult<Option<Texture>> {
|
||||
unimplemented!()
|
||||
let data_store = self.data_store.read().unwrap();
|
||||
Ok(data_store.texture_by_id(id))
|
||||
}
|
||||
|
||||
fn get_texture_by_name(&mut self, name: &str) -> ProtocolResult<Option<Texture>> {
|
||||
unimplemented!()
|
||||
let data_store = self.data_store.read().unwrap();
|
||||
Ok(data_store.texture_by_name(name))
|
||||
}
|
||||
|
||||
fn get_texture_file(&mut self, hash: Sha256) -> ProtocolResult<Vec<u8>> {
|
||||
unimplemented!()
|
||||
let data_store = self.data_store.read().unwrap();
|
||||
|
||||
let data = data_store.read_texture_file_by_hash(&hash)?;
|
||||
|
||||
Ok(data)
|
||||
}
|
||||
|
||||
fn get_texture_preview(
|
||||
@ -35,7 +51,18 @@ impl ProtocolHandler for ServerState {
|
||||
hash: Sha256,
|
||||
format: TextureFormat,
|
||||
) -> ProtocolResult<Vec<u8>> {
|
||||
unimplemented!()
|
||||
let mut data_store = self.data_store.write().unwrap();
|
||||
|
||||
match data_store.get_texture_preview(&hash, format) {
|
||||
Ok(data) => Ok(data),
|
||||
Err(TextureFileError::NotFound) => Err(ProtocolError::FileNotFound(
|
||||
"Texture not found!".to_string(),
|
||||
)),
|
||||
Err(TextureFileError::ImageError(_)) => Err(ProtocolError::FileNotFound(
|
||||
"Didn't Find valid Texture File".to_string(),
|
||||
)),
|
||||
Err(TextureFileError::IoError(err)) => Err(ProtocolError::InternalServerError(err)),
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_texture(
|
||||
@ -44,7 +71,63 @@ impl ProtocolHandler for ServerState {
|
||||
insert: Option<Texture>,
|
||||
insert_texture_data: Option<Vec<u8>>,
|
||||
) -> ProtocolResult<ReplaceTextureStatus> {
|
||||
// NOTE: must also check if insert_texture_data fits sha256!
|
||||
unimplemented!()
|
||||
let mut data_store = self.data_store.write().unwrap();
|
||||
|
||||
match insert_texture_data {
|
||||
Some(data) => {
|
||||
data_store.store_texture_file(&data)?;
|
||||
}
|
||||
None => (),
|
||||
}
|
||||
|
||||
match (delete, insert) {
|
||||
(Some(delete), Some(insert)) => {
|
||||
if !data_store.is_texture_file_on_disk(&insert.texture_hash) {
|
||||
return Ok(ReplaceTextureStatus::NeedTextureData(insert.texture_hash));
|
||||
}
|
||||
|
||||
if !data_store.delete(&delete) {
|
||||
return Err(ProtocolError::Conflict(
|
||||
"Delete Texture was modified!".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
if !data_store.insert(insert) {
|
||||
// undo delete
|
||||
// panics if texture file is delete during delete and reinsert.
|
||||
// unlikely to happen.
|
||||
assert!(data_store.insert(delete));
|
||||
return Err(ProtocolError::Conflict(
|
||||
"Name or Id already taken.".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(ReplaceTextureStatus::Ok)
|
||||
}
|
||||
(Some(delete), None) => {
|
||||
if !data_store.delete(&delete) {
|
||||
return Err(ProtocolError::Conflict(
|
||||
"Delete Texture was modified!".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(ReplaceTextureStatus::Ok)
|
||||
}
|
||||
(None, Some(insert)) => {
|
||||
if !data_store.is_texture_file_on_disk(&insert.texture_hash) {
|
||||
return Ok(ReplaceTextureStatus::NeedTextureData(insert.texture_hash));
|
||||
}
|
||||
|
||||
if !data_store.insert(insert) {
|
||||
// undo delete
|
||||
return Err(ProtocolError::Conflict(
|
||||
"Name or Id already taken.".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
Ok(ReplaceTextureStatus::Ok)
|
||||
}
|
||||
(None, None) => Ok(ReplaceTextureStatus::Ok),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user