1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

#![allow(unused_variables, dead_code, missing_docs)]

use std::{
    collections::HashMap,
    sync::{Arc, RwLock},
};
use tracing::debug;

use crate::common::url::Url;

use super::{NotifyListener, Registry};

// 从url中获取服务注册的元数据
/// rawURL = fmt.Sprintf("%s://%s%s?%s", c.Protocol, host, c.Path, s)
/// dubboPath = fmt.Sprintf("/%s/%s/%s", r.URL.GetParam(constant.RegistryGroupKey, "dubbo"), r.service(c), common.DubboNodes[common.PROVIDER])

pub const REGISTRY_GROUP_KEY: &str = "registry.group";

#[derive(Debug, Default, Clone)]
pub struct MemoryRegistry {
    registries: Arc<RwLock<HashMap<String, String>>>,
}

impl MemoryRegistry {
    pub fn new() -> MemoryRegistry {
        MemoryRegistry {
            registries: Arc::new(RwLock::new(HashMap::new())),
        }
    }
}

impl Registry for MemoryRegistry {
    type NotifyListener = MemoryNotifyListener;

    fn register(&mut self, mut url: Url) -> Result<(), crate::StdError> {
        // define provider label: ${registry.group}/${service_name}/provider
        let registry_group = match url.get_param(REGISTRY_GROUP_KEY) {
            Some(key) => key,
            None => "dubbo".to_string(),
        };

        let dubbo_path = format!(
            "/{}/{}/{}",
            registry_group,
            url.get_service_name(),
            "provider",
        );

        url.params.insert("anyhost".to_string(), "true".to_string());
        // define triple url path
        let raw_url = url.raw_url_string();

        self.registries.write().unwrap().insert(dubbo_path, raw_url);
        Ok(())
    }

    fn unregister(&mut self, url: crate::common::url::Url) -> Result<(), crate::StdError> {
        let registry_group = match url.get_param(REGISTRY_GROUP_KEY) {
            Some(key) => key,
            None => "dubbo".to_string(),
        };

        let dubbo_path = format!(
            "/{}/{}/{}",
            registry_group,
            url.get_service_name(),
            "provider",
        );
        self.registries.write().unwrap().remove(&dubbo_path);

        Ok(())
    }

    fn subscribe(
        &self,
        url: crate::common::url::Url,
        listener: Self::NotifyListener,
    ) -> Result<(), crate::StdError> {
        todo!()
    }

    fn unsubscribe(
        &self,
        url: crate::common::url::Url,
        listener: Self::NotifyListener,
    ) -> Result<(), crate::StdError> {
        todo!()
    }
}

pub struct MemoryNotifyListener {
    pub service_instances: Arc<RwLock<HashMap<String, Vec<Url>>>>,
}

impl NotifyListener for MemoryNotifyListener {
    fn notify(&self, event: super::ServiceEvent) {
        debug!("notify {:?}", event);
        let mut map = self.service_instances.write().expect("msg");
        match event.action.as_str() {
            "ADD" => map.insert(event.key, event.service),
            &_ => todo!(),
        };
    }

    fn notify_all(&self, event: super::ServiceEvent) {
        todo!()
    }
}