124 lines
3.8 KiB
Rust
Raw Normal View History

2020-06-05 23:12:02 +03:00
/*
* Copyright 2020 Fluence Labs Limited
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use super::FaaSError;
use super::faas_interface::FaaSInterface;
use super::faas_interface::FaaSModuleInterface;
2020-06-05 23:12:02 +03:00
use fce::FCE;
use super::IValue;
2020-06-05 23:12:02 +03:00
use fce::FCEModuleConfig;
2020-06-12 00:05:04 +03:00
2020-06-05 23:12:02 +03:00
use std::fs;
use std::path::PathBuf;
pub struct FluenceFaaS {
2020-06-16 09:53:47 +03:00
fce: FCE,
2020-06-15 22:21:08 +03:00
// names of core modules loaded to FCE
2020-06-05 23:12:02 +03:00
module_names: Vec<String>,
2020-06-15 22:21:08 +03:00
// config for code loaded by call_code function
faas_code_config: FCEModuleConfig,
2020-06-05 23:12:02 +03:00
}
impl FluenceFaaS {
2020-06-10 18:00:07 +03:00
pub fn new<P: Into<PathBuf>>(
core_modules_dir: P,
config_file_path: P,
) -> Result<Self, FaaSError> {
2020-06-16 09:53:47 +03:00
let mut fce = FCE::new();
2020-06-05 23:12:02 +03:00
let mut module_names = Vec::new();
let mut core_modules_config = crate::misc::parse_config_from_file(config_file_path.into())?;
2020-06-05 23:12:02 +03:00
2020-06-10 14:56:15 +03:00
for entry in fs::read_dir(core_modules_dir.into())? {
2020-06-05 23:12:02 +03:00
let path = entry?.path();
2020-06-06 21:34:13 +03:00
if path.is_dir() {
2020-06-12 01:59:50 +03:00
// just skip directories
2020-06-06 21:34:13 +03:00
continue;
2020-06-05 23:12:02 +03:00
}
2020-06-06 21:34:13 +03:00
let module_name = path.file_name().unwrap();
let module_name = module_name
.to_os_string()
.into_string()
.map_err(|e| FaaSError::IOError(format!("failed to read from {:?} file", e)))?;
2020-06-06 21:34:13 +03:00
let module_bytes = fs::read(path.clone())?;
2020-06-16 09:53:47 +03:00
let core_module_config = crate::misc::make_fce_config(
2020-06-10 18:00:07 +03:00
core_modules_config.modules_config.remove(&module_name),
)?;
2020-06-16 09:53:47 +03:00
fce.load_module(module_name.clone(), &module_bytes, core_module_config)?;
2020-06-06 21:34:13 +03:00
module_names.push(module_name);
2020-06-05 23:12:02 +03:00
}
2020-06-10 18:00:07 +03:00
let rpc_module_config =
2020-06-16 09:53:47 +03:00
crate::misc::make_fce_config(core_modules_config.rpc_module_config)?;
2020-06-06 21:34:13 +03:00
2020-06-05 23:12:02 +03:00
Ok(Self {
2020-06-16 09:53:47 +03:00
fce,
2020-06-05 23:12:02 +03:00
module_names,
faas_code_config: rpc_module_config,
2020-06-05 23:12:02 +03:00
})
}
2020-06-15 22:21:08 +03:00
/// Executes provided Wasm code in the internal environment (with access to module exports).
pub fn call_code(
2020-06-11 03:10:37 +03:00
&mut self,
wasm_rpc: &[u8],
func_name: &str,
args: &[IValue],
) -> Result<Vec<IValue>, FaaSError> {
2020-06-05 23:12:02 +03:00
let rpc_module_name = "ipfs_rpc";
2020-06-16 09:53:47 +03:00
self.fce
.load_module(rpc_module_name, wasm_rpc, self.faas_code_config.clone())?;
2020-06-07 22:57:30 +03:00
2020-06-16 09:53:47 +03:00
let call_result = self.fce.call(rpc_module_name, func_name, args)?;
self.fce.unload_module(rpc_module_name)?;
2020-06-05 23:12:02 +03:00
Ok(call_result)
}
2020-06-15 22:21:08 +03:00
/// Call a specified function of loaded on a startup module by its name.
pub fn call_module(
2020-06-12 02:04:52 +03:00
&mut self,
module_name: &str,
func_name: &str,
args: &[IValue],
) -> Result<Vec<IValue>, FaaSError> {
2020-06-16 09:53:47 +03:00
self.fce
2020-06-12 02:04:52 +03:00
.call(module_name, func_name, args)
.map_err(Into::into)
}
2020-06-15 22:21:08 +03:00
/// Return all export functions (name and signatures) of loaded on a startup modules.
pub fn get_interface(&self) -> FaaSInterface {
2020-06-05 23:12:02 +03:00
let mut modules = Vec::with_capacity(self.module_names.len());
for module_name in self.module_names.iter() {
2020-06-16 09:53:47 +03:00
let functions = self.fce.get_interface(module_name).unwrap();
modules.push(FaaSModuleInterface {
2020-06-05 23:12:02 +03:00
name: module_name,
functions,
})
}
FaaSInterface { modules }
2020-06-05 23:12:02 +03:00
}
}