first
parent
f7c613f03b
commit
c4eb3b4753
@ -0,0 +1,8 @@
|
|||||||
|
[package]
|
||||||
|
name = "Manager"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2021"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
postgres = "*"
|
||||||
|
lazy_static = "*"
|
||||||
@ -0,0 +1,99 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::sync::Mutex;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
use crate::traits::{Action, ActionType};
|
||||||
|
|
||||||
|
type RecieverID = i32;
|
||||||
|
|
||||||
|
// Трейт IReciever (аналог интерфейса в C++)
|
||||||
|
pub trait IReciever {
|
||||||
|
fn id(&self) -> RecieverID;
|
||||||
|
fn recieve(&self, action: Action);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Реализация Reciever
|
||||||
|
pub struct Reciever {
|
||||||
|
id: RecieverID,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Reciever {
|
||||||
|
pub fn new(id: RecieverID) -> Self {
|
||||||
|
Reciever { id }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IReciever for Reciever {
|
||||||
|
fn id(&self) -> RecieverID {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn recieve(&self, action: Action) {
|
||||||
|
println!("Reciever: action id={} action data={}", action.typ, action.payload);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Глобальные статические переменные для subscriptions и actions
|
||||||
|
lazy_static! {
|
||||||
|
static ref SUBSCRIPTIONS: Mutex<HashMap<ActionType, HashMap<RecieverID, Box<dyn IReciever + Send>>>> =
|
||||||
|
Mutex::new(HashMap::new());
|
||||||
|
static ref ACTIONS: Mutex<HashMap<ActionType, Action>> = Mutex::new(HashMap::new());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Трейт IDispatcher
|
||||||
|
pub trait IDispatcher {
|
||||||
|
fn subscribe(&self, action_id: ActionType, reciever: Box<dyn IReciever + Send>);
|
||||||
|
fn unsubscribe(&self, action_id: ActionType, reciever_id: RecieverID);
|
||||||
|
fn proccess(&self);
|
||||||
|
fn send(&self, action: Action);
|
||||||
|
fn erase(&self, action_id: ActionType);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Реализация Dispatcher
|
||||||
|
pub struct Dispatcher;
|
||||||
|
|
||||||
|
impl Dispatcher {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Dispatcher
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IDispatcher for Dispatcher {
|
||||||
|
fn subscribe(&self, action_id: ActionType, reciever: Box<dyn IReciever + Send>) {
|
||||||
|
SUBSCRIPTIONS
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.entry(action_id)
|
||||||
|
.or_insert_with(HashMap::new)
|
||||||
|
.insert(reciever.id(), reciever);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unsubscribe(&self, action_id: ActionType, reciever_id: RecieverID) {
|
||||||
|
if let Some(recievers) = SUBSCRIPTIONS.lock().unwrap().get_mut(&action_id) {
|
||||||
|
recievers.remove(&reciever_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn proccess(&self) {
|
||||||
|
let mut actions = ACTIONS.lock().unwrap();
|
||||||
|
let subscriptions = SUBSCRIPTIONS.lock().unwrap();
|
||||||
|
|
||||||
|
let actions_clone = actions.clone(); // Клонируем actions для итерации
|
||||||
|
for (action_id, action) in actions_clone {
|
||||||
|
if let Some(recievers) = subscriptions.get(&action_id) {
|
||||||
|
for reciever in recievers.values() {
|
||||||
|
reciever.recieve(action.clone());
|
||||||
|
}
|
||||||
|
actions.remove(&action_id); // Удаляем обработанное действие
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send(&self, action: Action) {
|
||||||
|
ACTIONS.lock().unwrap().insert(action.typ, action);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn erase(&self, action_id: ActionType) {
|
||||||
|
ACTIONS.lock().unwrap().remove(&action_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
use crate::traits;
|
||||||
|
|
||||||
|
pub struct Listener {
|
||||||
|
id: ListenerID,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IListener for Listener {
|
||||||
|
fn id(&self) -> ListenerID {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn listen(&self, store: IStore) {
|
||||||
|
println!("listen getState: ", store.getState());
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,29 @@
|
|||||||
|
use crate::traits::{Action, IListener, State, IStore};
|
||||||
|
use crate::dispatcher::{IDispatcher,Dispatcher};
|
||||||
|
|
||||||
|
pub struct Store {
|
||||||
|
state: State,
|
||||||
|
dispatcher: Box<dyn IDispatcher + Send>
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Store {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Store{
|
||||||
|
state: State {},
|
||||||
|
dispatcher:Box::new(Dispatcher::new())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IStore for Store {
|
||||||
|
fn getState(&self) -> State {
|
||||||
|
return self.state.clone();
|
||||||
|
}
|
||||||
|
fn dispatch(&self, action: Action) {
|
||||||
|
|
||||||
|
}
|
||||||
|
fn subscribe(&self, listener: Box<dyn IListener + Send>){
|
||||||
|
}
|
||||||
|
fn unsubscribe(&self, listener: Box<dyn IListener + Send>){
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,26 @@
|
|||||||
|
pub type ListenerID = i32;
|
||||||
|
pub type ActionType = i32;
|
||||||
|
pub type Payload = String;
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Action {
|
||||||
|
pub typ: ActionType,
|
||||||
|
pub payload: Payload,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct State {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub trait IListener {
|
||||||
|
fn id(&self) -> ListenerID;
|
||||||
|
fn listen(&self, store: Box<dyn IListener + Send>);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait IStore {
|
||||||
|
fn getState(&self) -> State;
|
||||||
|
fn dispatch(&self, action: Action);
|
||||||
|
fn subscribe(&self, listener: Box<dyn IListener + Send>);
|
||||||
|
fn unsubscribe(&self, listener: Box<dyn IListener + Send>);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue