diff --git a/Cargo.lock b/Cargo.lock index 4c0aadf..d893fe2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -819,6 +819,7 @@ dependencies = [ "git2", "giterated-models", "giterated-plugin", + "giterated-protocol", "jsonwebtoken", "log", "rand", @@ -885,6 +886,7 @@ name = "giterated-plugin" version = "0.1.0" dependencies = [ "anyhow", + "async-trait", "dlopen2", "giterated-models", "semver", diff --git a/giterated-daemon/Cargo.toml b/giterated-daemon/Cargo.toml index 9dfb55a..ef03847 100644 --- a/giterated-daemon/Cargo.toml +++ b/giterated-daemon/Cargo.toml @@ -31,6 +31,7 @@ aes-gcm = "0.10" semver = {version = "1.0", features = ["serde"]} giterated-models = { path = "../giterated-models" } giterated-plugin = { path = "../giterated-plugins/giterated-plugin" } +giterated-protocol = { path = "../giterated-plugins/giterated-protocol" } deadpool = "0.9" bincode = "1.3" tokio-util = {version = "0.7", features = ["rt"]} diff --git a/giterated-daemon/src/client.rs b/giterated-daemon/src/client.rs index 17e9fbb..cba648d 100644 --- a/giterated-daemon/src/client.rs +++ b/giterated-daemon/src/client.rs @@ -6,8 +6,11 @@ use giterated_models::{ instance::Instance, object_backend::ObjectBackend, }; -use giterated_plugin::new_stack::Runtime; -use giterated_protocol::{AuthenticatedPayload, NetworkedObject, NetworkedOperation}; +use giterated_plugin::new_stack::{runtime_handler::RuntimeHandle, Runtime}; +use giterated_protocol::{ + handlers::{NetworkedObject, NetworkedOperation}, + AuthenticatedPayload, StackOperationState, +}; use tokio::net::TcpStream; use tokio_tungstenite::{tungstenite::Message, WebSocketStream}; @@ -94,13 +97,13 @@ pub async fn client_wrapper( pub async fn handle_client_message( payload: AuthenticatedPayload, - // operation_state: StackOperationState, - runtime: Arc, + operation_state: StackOperationState, + runtime: RuntimeHandle, ) -> Result, OperationError>> { - // let mut networked_object = runtime - // .get_object::(&payload.object, &operation_state) - // .await - // .as_internal_error_with_context("handling client message")?; + let mut networked_object = runtime + .get_object::(&payload.object, &operation_state) + .await + .as_internal_error_with_context("handling client message")?; let message: giterated_protocol::GiteratedMessage = payload.into_message(); @@ -112,9 +115,7 @@ pub async fn handle_client_message( trace!("Calling handler for networked operation"); - // networked_object - // .request(networked_operation, &operation_state) - // .await - - todo!() + networked_object + .request(networked_operation, &operation_state) + .await } diff --git a/giterated-daemon/src/main.rs b/giterated-daemon/src/main.rs index 765d3ae..83c897b 100644 --- a/giterated-daemon/src/main.rs +++ b/giterated-daemon/src/main.rs @@ -4,7 +4,6 @@ use giterated_daemon::{authentication::AuthenticationTokenGranter, client::clien use giterated_models::instance::Instance; use giterated_plugin::new_stack::Runtime; -use giterated_protocol::NetworkedSubstack; use sqlx::{postgres::PgConnectOptions, ConnectOptions, PgPool}; use std::{net::SocketAddr, str::FromStr, sync::Arc}; use tokio::{ @@ -52,14 +51,6 @@ async fn main() -> Result<(), Error> { let mut runtime = Runtime::default(); - let networked_stack = NetworkedSubstack { - home_uri: Some( - Instance::from_str(config["giterated"]["instance"].as_str().unwrap()) - .unwrap() - .0, - ), - }; - let runtime = Arc::new(runtime); let pool = LocalPoolHandle::new(5); diff --git a/giterated-plugins/giterated-plugin/Cargo.toml b/giterated-plugins/giterated-plugin/Cargo.toml index 6e6f1cb..e1596af 100644 --- a/giterated-plugins/giterated-plugin/Cargo.toml +++ b/giterated-plugins/giterated-plugin/Cargo.toml @@ -12,4 +12,5 @@ thiserror = "1" tracing = "0.1" giterated-models = { path = "../../giterated-models" } semver = "*" -serde_json = "1.0" \ No newline at end of file +serde_json = "1.0" +async-trait = "0.1" diff --git a/giterated-plugins/giterated-plugin/src/new_stack/runtime_handler.rs b/giterated-plugins/giterated-plugin/src/new_stack/runtime_handler.rs index 343ce53..178bff6 100644 --- a/giterated-plugins/giterated-plugin/src/new_stack/runtime_handler.rs +++ b/giterated-plugins/giterated-plugin/src/new_stack/runtime_handler.rs @@ -1,9 +1,20 @@ -use giterated_models::error::OperationError; +use std::fmt::Debug; +use std::sync::Arc; + +use giterated_models::{ + error::OperationError, + object::{GiteratedObject, Object, ObjectRequestError}, + object_backend::ObjectBackend, + operation::GiteratedOperation, +}; use crate::vtable::{AnyFailure, AnySuccess, OperationVTable}; +use super::PluginState; + +#[derive(Clone)] pub struct RuntimeHandle { - inner: RuntimeHandleInner, + inner: Arc, } impl RuntimeHandle { @@ -19,6 +30,7 @@ impl RuntimeHandle { #[repr(C)] struct RuntimeHandleInner { + state: PluginState, handle_serialized: unsafe extern "C" fn( object_kind: &str, operation_name: &str, @@ -27,8 +39,38 @@ struct RuntimeHandleInner { ) -> HandlerResult, } +unsafe impl Send for RuntimeHandleInner {} +unsafe impl Sync for RuntimeHandleInner {} + #[repr(C)] struct HandlerResult { operation_vtable: OperationVTable, result: Result>, } + +#[async_trait::async_trait(?Send)] +impl ObjectBackend for RuntimeHandle { + async fn object_operation( + &self, + object: O, + operation: &str, + payload: D, + operation_state: &S, + ) -> Result> + where + O: GiteratedObject + Debug + 'static, + D: GiteratedOperation + Debug + 'static, + D::Success: Clone, + D::Failure: Clone, + { + todo!() + } + + async fn get_object( + &self, + object_str: &str, + operation_state: &S, + ) -> Result, OperationError> { + todo!() + } +} diff --git a/giterated-plugins/giterated-protocol/Cargo.toml b/giterated-plugins/giterated-protocol/Cargo.toml index 13ddc40..0d52246 100644 --- a/giterated-plugins/giterated-protocol/Cargo.toml +++ b/giterated-plugins/giterated-protocol/Cargo.toml @@ -4,7 +4,7 @@ version = "0.1.0" edition = "2021" [lib] -name = "giterated_protocol_plugin" +name = "giterated_protocol" path = "src/lib.rs" crate-type = ["dylib", "rlib"] diff --git a/giterated-plugins/giterated-protocol/src/lib.rs b/giterated-plugins/giterated-protocol/src/lib.rs index 56f5600..4eacc08 100644 --- a/giterated-plugins/giterated-protocol/src/lib.rs +++ b/giterated-plugins/giterated-protocol/src/lib.rs @@ -1,4 +1,4 @@ -use std::{str::FromStr, sync::Arc}; +use std::{ops::Deref, str::FromStr, sync::Arc}; use giterated_models::{ authenticated::{InstanceSignature, UserAuthenticationToken}, @@ -28,6 +28,47 @@ pub mod operations; extern crate tracing; #[derive(Clone)] +pub struct StackOperationState { + pub our_instance: Instance, + pub instance: Option, + pub user: Option, +} + +#[derive(Clone, Debug)] +pub struct AuthenticatedInstance(Instance); + +impl AuthenticatedInstance { + pub fn new(instance: Instance) -> Self { + AuthenticatedInstance(instance) + } +} + +impl Deref for AuthenticatedInstance { + type Target = Instance; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[derive(Clone, Debug)] +pub struct AuthenticatedUser(User); + +impl AuthenticatedUser { + pub fn new(user: User) -> Self { + AuthenticatedUser(user) + } +} + +impl Deref for AuthenticatedUser { + type Target = User; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +#[derive(Clone)] pub struct NetworkOperationState { authentication: Vec>, }