apache_dubbo/protocol/triple/
triple_server.rs

1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18use std::{net::ToSocketAddrs, str::FromStr};
19
20use crate::triple::transport::DubboServer;
21
22#[derive(Default, Clone)]
23pub struct TripleServer {
24    s: DubboServer,
25    service_names: Vec<String>,
26}
27
28impl TripleServer {
29    pub fn new(names: Vec<String>) -> TripleServer {
30        Self {
31            service_names: names,
32            s: DubboServer::new(),
33        }
34    }
35
36    pub async fn serve(mut self, url: String) {
37        {
38            let lock = super::TRIPLE_SERVICES.read().unwrap();
39            for name in self.service_names.iter() {
40                if lock.get(name).is_none() {
41                    tracing::warn!("service {} not register", name);
42                    continue;
43                }
44                let svc = lock.get(name).unwrap();
45
46                self.s = self.s.add_service(name.clone(), svc.clone());
47            }
48        }
49
50        let uri = match http::Uri::from_str(&url) {
51            Ok(v) => v,
52            Err(err) => {
53                tracing::error!("http uri parse error: {}, url: {:?}", err, &url);
54                return;
55            }
56        };
57
58        let authority = match uri.authority() {
59            Some(v) => v.to_owned(),
60            None => {
61                tracing::error!("http authority is none");
62                return;
63            }
64        };
65
66        self.s
67            .with_listener("tcp".to_string())
68            .serve(
69                authority
70                    .to_string()
71                    .to_socket_addrs()
72                    .unwrap()
73                    .next()
74                    .unwrap(),
75            )
76            .await
77            .unwrap();
78    }
79}