reactive_graph_net_http/
plugin.rs

1use reactive_graph_plugin_api::EntityComponentBehaviourRegistry;
2use reactive_graph_plugin_api::prelude::plugin::*;
3use reactive_graph_plugin_api::prelude::providers::*;
4
5use crate::behaviour::component::http::HttpFactory;
6use crate::behaviour::component::json_rpc::JsonRpcFactory;
7use reactive_graph_net_http_model::BEHAVIOUR_HTTP;
8use reactive_graph_net_http_model::BEHAVIOUR_JSON_RPC;
9use reactive_graph_net_http_model::COMPONENT_BEHAVIOUR_HTTP;
10use reactive_graph_net_http_model::COMPONENT_BEHAVIOUR_JSON_RPC;
11
12export_plugin!({
13    "dependencies": [
14        { "name": "reactive-graph-std-base", "version": ">=0.10.0, <0.11.0" },
15        { "name": "reactive-graph-std-result", "version": ">=0.10.0, <0.11.0" },
16        { "name": "reactive-graph-std-trigger", "version": ">=0.10.0, <0.11.0" }
17    ]
18});
19
20#[injectable]
21pub trait HttpPlugin: Plugin + Send + Sync {}
22
23#[derive(Component)]
24pub struct HttpPluginImpl {
25    component_provider: Arc<dyn TypeProvider<Components> + Send + Sync>,
26
27    #[component(default = "component_provider_registry")]
28    component_provider_registry: Arc<dyn ComponentProviderRegistry + Send + Sync>,
29
30    entity_types_provider: Arc<dyn TypeProvider<EntityTypes> + Send + Sync>,
31
32    #[component(default = "entity_types_provider_registry")]
33    entity_type_provider_registry: Arc<dyn EntityTypeProviderRegistry + Send + Sync>,
34
35    #[component(default = "entity_component_behaviour_registry")]
36    entity_component_behaviour_registry: Arc<dyn EntityComponentBehaviourRegistry + Send + Sync>,
37}
38
39#[async_trait]
40#[component_alias]
41impl Plugin for HttpPluginImpl {
42    async fn activate(&self) -> Result<(), PluginActivationError> {
43        self.component_provider_registry.register_provider(self.component_provider.clone()).await;
44        self.entity_type_provider_registry.register_provider(self.entity_types_provider.clone()).await;
45
46        // HTTP
47        let factory = Arc::new(HttpFactory::new(BEHAVIOUR_HTTP.clone()));
48        self.entity_component_behaviour_registry
49            .register(COMPONENT_BEHAVIOUR_HTTP.clone(), factory)
50            .await;
51
52        // JSON_RPC
53        let factory = Arc::new(JsonRpcFactory::new(BEHAVIOUR_JSON_RPC.clone()));
54        self.entity_component_behaviour_registry
55            .register(COMPONENT_BEHAVIOUR_JSON_RPC.clone(), factory)
56            .await;
57
58        Ok(())
59    }
60
61    async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
62        self.entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_HTTP).await;
63        self.entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_JSON_RPC).await;
64        self.entity_type_provider_registry.unregister_provider(self.entity_types_provider.id()).await;
65        self.component_provider_registry.unregister_provider(self.component_provider.id()).await;
66        Ok(())
67    }
68}
69
70// use std::sync::Arc;
71// use std::sync::RwLock;
72//
73// use async_trait::async_trait;
74//
75// use crate::behaviour::component::http::HttpFactory;
76// use crate::behaviour::component::json_rpc::JsonRpcFactory;
77// use crate::di::*;
78// use reactive_graph_net_http_model::BEHAVIOUR_HTTP;
79// use reactive_graph_net_http_model::BEHAVIOUR_JSON_RPC;
80// use reactive_graph_net_http_model::COMPONENT_BEHAVIOUR_HTTP;
81// use reactive_graph_net_http_model::COMPONENT_BEHAVIOUR_JSON_RPC;
82// use crate::plugins::component_provider;
83// use crate::plugins::entity_type_provider;
84// use crate::plugins::plugin_context::PluginContext;
85// use crate::plugins::ComponentProvider;
86// use crate::plugins::ComponentProviderError;
87// use crate::plugins::EntityTypeProvider;
88// use crate::plugins::EntityTypeProviderError;
89// use crate::plugins::Plugin;
90// use crate::plugins::PluginActivationError;
91// use crate::plugins::PluginContextDeinitializationError;
92// use crate::plugins::PluginContextInitializationError;
93// use crate::plugins::PluginDeactivationError;
94// use crate::providers::HttpComponentProviderImpl;
95// use crate::providers::HttpEntityTypeProviderImpl;
96//
97// #[wrapper]
98// pub struct PluginContextContainer(RwLock<Option<Arc<dyn PluginContext>>>);
99//
100// #[provides]
101// fn create_empty_plugin_context_container() -> PluginContextContainer {
102//     PluginContextContainer(RwLock::new(None))
103// }
104//
105// pub trait HttpPlugin: Plugin + Send + Sync {}
106//
107// #[component]
108// pub struct HttpPluginImpl {
109//     component_provider: Wrc<HttpComponentProviderImpl>,
110//     entity_type_provider: Wrc<HttpEntityTypeProviderImpl>,
111//
112//     context: PluginContextContainer,
113// }
114//
115// impl HttpPluginImpl {}
116//
117// impl HttpPluginImpl {}
118//
119// interfaces!(HttpPluginImpl: dyn Plugin);
120//
121// #[provides]
122// #[async_trait]
123// impl HttpPlugin for HttpPluginImpl {}
124//
125// #[async_trait]
126// impl Plugin for HttpPluginImpl {
127//     async fn activate(&self) -> Result<(), PluginActivationError> {
128//         let guard = self.context.0.read().unwrap();
129//         if let Some(context) = guard.clone() {
130//             let entity_component_behaviour_registry = context.get_entity_component_behaviour_registry();
131//             // HTTP
132//             let factory = Arc::new(HttpFactory::new(BEHAVIOUR_HTTP.clone()));
133//             entity_component_behaviour_registry.register(COMPONENT_BEHAVIOUR_HTTP.clone(), factory);
134//
135//             // JSON_RPC
136//             let factory = Arc::new(JsonRpcFactory::new(BEHAVIOUR_JSON_RPC.clone()));
137//             entity_component_behaviour_registry.register(COMPONENT_BEHAVIOUR_JSON_RPC.clone(), factory);
138//         }
139//         Ok(())
140//     }
141//
142//     async fn deactivate(&self) -> Result<(), PluginDeactivationError> {
143//         let guard = self.context.0.read().unwrap();
144//         if let Some(context) = guard.clone() {
145//             let entity_component_behaviour_registry = context.get_entity_component_behaviour_registry();
146//             entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_HTTP);
147//             entity_component_behaviour_registry.unregister(&COMPONENT_BEHAVIOUR_JSON_RPC);
148//         }
149//         Ok(())
150//     }
151//
152//     fn set_context(&self, context: Arc<dyn PluginContext>) -> Result<(), PluginContextInitializationError> {
153//         self.context.0.write().unwrap().replace(context.clone());
154//         Ok(())
155//     }
156//
157//     fn remove_context(&self) -> Result<(), PluginContextDeinitializationError> {
158//         let mut writer = self.context.0.write().unwrap();
159//         *writer = None;
160//         Ok(())
161//     }
162//
163//     fn get_component_provider(&self) -> Result<Option<Arc<dyn ComponentProvider>>, ComponentProviderError> {
164//         component_provider!(self.component_provider)
165//     }
166//
167//     fn get_entity_type_provider(&self) -> Result<Option<Arc<dyn EntityTypeProvider>>, EntityTypeProviderError> {
168//         entity_type_provider!(self.entity_type_provider)
169//     }
170// }