-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): binding listener on fragment request
- Loading branch information
1 parent
9e19a82
commit 7b729c2
Showing
10 changed files
with
131 additions
and
17 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod handler; | ||
pub mod messages; | ||
pub mod image; | ||
pub mod services; | ||
pub use serde::{Deserialize, Serialize}; | ||
|
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,9 @@ | ||
use shared::types::messages::{FragmentRequest, FragmentTask, FragmentResult}; | ||
|
||
pub fn create_task_for_request(_request: FragmentRequest) -> FragmentTask { | ||
todo!() | ||
} | ||
|
||
pub fn process_result(_result: FragmentResult) { | ||
todo!() | ||
} |
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,3 @@ | ||
pub mod handler; | ||
pub mod serialization; | ||
pub mod fragment_maker; |
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,51 @@ | ||
use serde_json::json; | ||
use shared::types::messages::{FragmentRequest, Message, FragmentTask}; | ||
use serde::de::Error as SerdeError; | ||
|
||
pub fn serialize_request(request: &FragmentRequest) -> Result<String, serde_json::Error> { | ||
let request_details = json!({ | ||
"worker_name": &request.worker_name, | ||
"maximal_work_load": request.maximal_work_load | ||
}); | ||
|
||
let request = json!({ | ||
"FragmentRequest": request_details | ||
}); | ||
|
||
serde_json::to_string(&request) | ||
} | ||
|
||
pub fn deserialize_request(response: &str) -> serde_json::Result<FragmentRequest> { | ||
let response_value: serde_json::Value = serde_json::from_str(response)?; | ||
let response_details = response_value.get("FragmentRequest").ok_or_else(|| SerdeError::custom("FragmentRequest not found"))?; | ||
|
||
let worker_name = response_details.get("worker_name").and_then(|c| c.as_str()).ok_or_else(|| SerdeError::custom("Invalid worker name"))?; | ||
let maximal_work_load = response_details.get("maximal_work_load").and_then(|c| c.as_u64()).ok_or_else(|| SerdeError::custom("Invalid maximal work load"))?; | ||
|
||
let response = FragmentRequest { | ||
worker_name: worker_name.to_string(), | ||
maximal_work_load: maximal_work_load as u32, | ||
}; | ||
|
||
Ok(response) | ||
} | ||
|
||
pub fn deserialize_message(response: &str) -> serde_json::Result<Message> { | ||
let response_value: serde_json::Value = serde_json::from_str(response)?; | ||
match response_value.as_object().and_then(|obj| obj.keys().next()) { | ||
Some(key) if key == "FragmentRequest" => { | ||
deserialize_request(response).map(Message::FragmentRequest) | ||
} | ||
Some(key) if key == "FragmentTask" => { | ||
todo!() | ||
} | ||
Some(key) if key == "FragmentResult" => { | ||
todo!() | ||
} | ||
_ => Err(serde_json::Error::custom("No recognizable message type found")) | ||
} | ||
} | ||
|
||
pub fn serialize_task(_task: &FragmentTask) -> Result<String, serde_json::Error> { | ||
todo!() | ||
} |
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