#provider #wasmcloud #codec

wasmcloud-provider-core

构建wasmcloud能力提供者使用的核心类型和特质

2个版本

0.1.1 2021年3月26日
0.1.0 2021年2月25日

#1612WebAssembly

Download history 151/week @ 2024-04-06 166/week @ 2024-04-13 84/week @ 2024-04-20 136/week @ 2024-04-27 130/week @ 2024-05-04 109/week @ 2024-05-11 86/week @ 2024-05-18 100/week @ 2024-05-25 135/week @ 2024-06-01 196/week @ 2024-06-08 88/week @ 2024-06-15 181/week @ 2024-06-22 57/week @ 2024-06-29 115/week @ 2024-07-06 167/week @ 2024-07-13 195/week @ 2024-07-20

每月561次下载
用于 13 个Crate(12个直接使用)

Apache-2.0

11KB
105

wasmcloud Provider Core

此Crate提供了用于构建能力提供者的共享特性和原语。有关创建能力提供者的更多信息,请参阅我们的wasmcloud.dev文档。


lib.rs:

wasmcloud Provider Core

此库提供了wasmcloud宿主操作能力提供者和开发者创建自己的提供者所需的核心类型和关联函数。

示例

以下是最简单的能力提供者的示例


 use wasmcloud_provider_core as provider;
 use wasmcloud_actor_core as actor;
 use provider::{CapabilityProvider, Dispatcher, NullDispatcher, serialize,
             core::{OP_BIND_ACTOR, OP_HEALTH_REQUEST, OP_REMOVE_ACTOR, SYSTEM_ACTOR}};
 use actor::{CapabilityConfiguration, HealthCheckResponse};
 use std::sync::{Arc, RwLock};
 use std::error::Error;

 // Hello world implementation of the `demo:hello` capability provider
 #[derive(Clone)]
 pub struct HelloProvider {
     dispatcher: Arc<RwLock<Box<dyn Dispatcher>>>,
 }

 const OP_HELLO: &str = "DoHello";

 impl Default for HelloProvider {
     fn default() -> Self {
         HelloProvider {
             dispatcher: Arc::new(RwLock::new(Box::new(NullDispatcher::new()))),
         }
     }
 }

 impl CapabilityProvider for HelloProvider {
     // Invoked by the runtime host to give this provider plugin the ability to communicate
     // with actors
     fn configure_dispatch(
         &self,
         dispatcher: Box<dyn Dispatcher>,
         ) -> Result<(), Box<dyn Error + Sync + Send>> {
        
         let mut lock = self.dispatcher.write().unwrap();
         *lock = dispatcher;
         Ok(())
     }

     // Invoked by host runtime to allow an actor to make use of the capability
     // All providers MUST handle the "configure" message, even if no work will be done
     fn handle_call(
            &self,
            actor: &str,
            op: &str,
            msg: &[u8],
        ) -> Result<Vec<u8>, Box<dyn Error + Sync + Send>> {

        match op {
            OP_BIND_ACTOR if actor == SYSTEM_ACTOR => Ok(vec![]),
            OP_REMOVE_ACTOR if actor == SYSTEM_ACTOR => Ok(vec![]),
            OP_HEALTH_REQUEST if actor == SYSTEM_ACTOR =>
                Ok(serialize(HealthCheckResponse {
                  healthy: true,
                  message: "".to_string(),
                })
               .unwrap()),
            OP_HELLO => Ok(b"Hello, World".to_vec()),
            _ => Err(format!("Unknown operation: {}", op).into()),
         }
     }

        // No cleanup needed on stop
        fn stop(&self) {}
    }

依赖关系

~0.8–1.5MB
~31K SLoC