From 58c8d7d91f25c3708cbfcdb5c1edf6fd33b087b2 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 8 May 2019 11:26:54 +0200 Subject: [PATCH] rc/api2/admin/datastore/h2upload.rs: implement BackupEnvironment To pass arbitrary information/state to api methods. --- src/api2/admin/datastore/h2upload.rs | 73 ++++++++++++++++++++++++---- src/server/environment.rs | 1 - 2 files changed, 64 insertions(+), 10 deletions(-) diff --git a/src/api2/admin/datastore/h2upload.rs b/src/api2/admin/datastore/h2upload.rs index 38af0f11..5e51ccd2 100644 --- a/src/api2/admin/datastore/h2upload.rs +++ b/src/api2/admin/datastore/h2upload.rs @@ -3,6 +3,7 @@ use lazy_static::lazy_static; use std::collections::HashMap; use std::sync::Arc; +use std::any::Any; use futures::*; use hyper::header::{HeaderValue, UPGRADE}; @@ -15,7 +16,7 @@ use crate::tools; use crate::api_schema::router::*; use crate::api_schema::*; use crate::server::formatter::*; -use crate::server::{WorkerTask, RestEnvironment}; +use crate::server::WorkerTask; pub fn api_method_upgrade_h2upload() -> ApiAsyncMethod { ApiAsyncMethod::new( @@ -29,14 +30,62 @@ lazy_static!{ static ref BACKUP_ROUTER: Router = backup_api(); } +/// `RpcEnvironmet` implementation for backup service +#[derive(Clone)] +pub struct BackupEnvironment { + env_type: RpcEnvironmentType, + result_attributes: HashMap, + user: String, + worker: Arc, + +} + +impl BackupEnvironment { + pub fn new(env_type: RpcEnvironmentType, user: String, worker: Arc) -> Self { + Self { + result_attributes: HashMap::new(), + env_type, + user, + worker, + } + } + + pub fn log>(&self, msg: S) { + self.worker.log(msg); + } +} + +impl RpcEnvironment for BackupEnvironment { + + fn set_result_attrib(&mut self, name: &str, value: Value) { + self.result_attributes.insert(name.into(), value); + } + + fn get_result_attrib(&self, name: &str) -> Option<&Value> { + self.result_attributes.get(name) + } + + fn env_type(&self) -> RpcEnvironmentType { + self.env_type + } + + fn set_user(&mut self, user: Option) { + panic!("unable to change user"); + } + + fn get_user(&self) -> Option { + Some(self.user.clone()) + } +} + pub struct BackupService { - rpcenv: RestEnvironment, + rpcenv: BackupEnvironment, worker: Arc, } impl BackupService { - fn new(rpcenv: RestEnvironment, worker: Arc) -> Self { + fn new(rpcenv: BackupEnvironment, worker: Arc) -> Self { Self { rpcenv, worker } } @@ -155,12 +204,12 @@ fn upgrade_h2upload( let worker_id = String::from("test2workerid"); + let username = rpcenv.get_user().unwrap(); + let env_type = rpcenv.env_type(); - let mut rpcenv1 = RestEnvironment::new(rpcenv.env_type()); - rpcenv1.set_user(rpcenv.get_user()); - - WorkerTask::spawn("test2_download", Some(worker_id), &rpcenv.get_user().unwrap(), true, move |worker| { - let service = BackupService::new(rpcenv1, worker.clone()); + WorkerTask::spawn("test2_download", Some(worker_id), &username.clone(), true, move |worker| { + let backup_env = BackupEnvironment::new(env_type, username.clone(), worker.clone()); + let service = BackupService::new(backup_env, worker.clone()); let abort_future = worker.abort_future(); @@ -222,9 +271,15 @@ fn backup_api() -> Router { fn test1_get ( _param: Value, _info: &ApiMethod, - _rpcenv: &mut RpcEnvironment, + rpcenv: &mut RpcEnvironment, ) -> Result { + println!("TYPEID {:?}", (*rpcenv).type_id()); + + let env = rpcenv.as_any().downcast_ref::().unwrap(); + + env.log("Inside test1_get()"); + Ok(Value::Null) } diff --git a/src/server/environment.rs b/src/server/environment.rs index 04cab81e..b002b24f 100644 --- a/src/server/environment.rs +++ b/src/server/environment.rs @@ -4,7 +4,6 @@ use std::collections::HashMap; use serde_json::Value; /// Encapsulates information about the runtime environment -#[derive(Clone)] pub struct RestEnvironment { env_type: RpcEnvironmentType, result_attributes: HashMap,