Skip to content

Commit

Permalink
Refactor Config.get_endpoints() (#46)
Browse files Browse the repository at this point in the history
Not sure why I decided to have that function return a HashMap, but to
implement filters, we need access to the underlying Endpoints, since
they get passed into several Filter functions.

So refactored the function to return a Vec<Endpoint>, which will make
things much easier to work with.

Work on #1
  • Loading branch information
markmandel authored Apr 29, 2020
1 parent b49bc20 commit ac36845
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
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

0 comments on commit ac36845

Please sign in to comment.