Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Config.get_endpoints() #46

Merged
merged 1 commit into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 25 additions & 26 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* limitations under the License.
*/

use std::collections::HashMap;
use std::io;
use std::net::SocketAddr;

Expand All @@ -37,22 +36,17 @@ pub struct Config {
impl Config {
/// get_endpoints get a list of all endpoints as a HashMap. For a Client proxy,
/// the key is "address", for a Server proxy the key is the name provided.
pub fn get_endpoints(&self) -> HashMap<String, SocketAddr> {
pub fn get_endpoints(&self) -> Vec<EndPoint> {
return match &self.connections {
ConnectionConfig::Client {
address,
connection_id: _,
} => {
let mut map: HashMap<String, SocketAddr> = HashMap::new();
map.insert(String::from(CLIENT_ENDPOINT), *address);
return map;
}
ConnectionConfig::Server { endpoints } => {
endpoints.iter().fold(HashMap::new(), |mut m, entrypoint| {
m.insert(entrypoint.name.clone(), entrypoint.address);
return m;
})
}
} => vec![EndPoint {
name: String::from(CLIENT_ENDPOINT),
address: *address,
connection_ids: vec![],
}],
ConnectionConfig::Server { endpoints } => endpoints.clone(),
};
}
}
Expand Down Expand Up @@ -101,8 +95,6 @@ pub fn from_reader<R: io::Read>(input: R) -> Result<Config, Error> {

#[cfg(test)]
mod tests {
use std::collections::HashMap;

use serde_yaml::Value;

use crate::config::{from_reader, Config, ConnectionConfig, EndPoint, Local, CLIENT_ENDPOINT};
Expand Down Expand Up @@ -257,8 +249,11 @@ server:
},
};

let mut expected = HashMap::new();
expected.insert(String::from(CLIENT_ENDPOINT), expected_addr);
let expected = vec![EndPoint {
name: String::from(CLIENT_ENDPOINT),
address: expected_addr,
connection_ids: vec![],
}];
assert_eq!(expected, config.get_endpoints());
}

Expand All @@ -280,15 +275,19 @@ server:
connection_ids:
- nkuy70x";
let config = from_reader(yaml.as_bytes()).unwrap();
let mut expected = HashMap::new();
expected.insert(
String::from("Game Server No. 1"),
"127.0.0.1:26000".parse().unwrap(),
);
expected.insert(
String::from("Game Server No. 2"),
"127.0.0.1:26001".parse().unwrap(),
);

let expected = vec![
EndPoint {
name: String::from("Game Server No. 1"),
address: "127.0.0.1:26000".parse().unwrap(),
connection_ids: vec!["1x7ijy6".to_string(), "8gj3v2i".to_string()],
},
EndPoint {
name: String::from("Game Server No. 2"),
address: "127.0.0.1:26001".parse().unwrap(),
connection_ids: vec!["nkuy70x".to_string()],
},
];

assert_eq!(expected, config.get_endpoints());
}
Expand Down
6 changes: 3 additions & 3 deletions src/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ impl Server {
from_utf8(packet).unwrap()
);

for (_, dest) in endpoints.iter() {
for endpoint in endpoints.iter() {
if let Err(err) = Server::ensure_session(
&log,
sessions.clone(),
recv_addr,
*dest,
endpoint.address,
send_packets.clone(),
)
.await
Expand All @@ -154,7 +154,7 @@ impl Server {
}

let map = sessions.read().await;
let key = (recv_addr, *dest);
let key = (recv_addr, endpoint.address);
match map.get(&key) {
Some(mtx) => {
let mut session = mtx.lock().await;
Expand Down