Skip to content

Commit

Permalink
refactor: use Paths iterator type
Browse files Browse the repository at this point in the history
  • Loading branch information
mlegner committed Nov 21, 2023
1 parent b313ccc commit 01d1c5f
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions crates/scion/src/daemon/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,7 @@ impl DaemonClient {
}

/// Request a set of end-to-end paths between the source and destination AS
pub async fn paths(
&self,
request: &PathRequest,
) -> Result<impl Iterator<Item = Path>, DaemonClientError> {
pub async fn paths(&self, request: &PathRequest) -> Result<Paths, DaemonClientError> {
let src_isd_asn = if request.source.is_wildcard() {
self.local_isd_asn
} else {
Expand All @@ -69,18 +66,16 @@ impl DaemonClient {
source: src_isd_asn,
destination: request.destination,
};
Ok(self
.client()
.paths(daemon_grpc::PathsRequest::from(request))
.await?
.into_inner()
.paths
.into_iter()
.map(move |grpc_path| Path::try_from_grpc_with_endpoints(grpc_path, isd_asn))
.filter_map(|x| {
x.map_err(|e| warn!(?e, "a parse error occurred for a path"))
.ok()
}))
Ok(Paths {
isd_asn,
grpc_paths: self
.client()
.paths(daemon_grpc::PathsRequest::from(request))
.await?
.into_inner()
.paths
.into_iter(),
})
}

#[inline]
Expand All @@ -95,3 +90,23 @@ impl DaemonClient {
DaemonServiceClient::new(self.connection.clone())
}
}

/// Iterator for SCION [Path]s obtained from the SCION Daemon via gRPC
pub struct Paths {
isd_asn: ByEndpoint<IsdAsn>,
grpc_paths: std::vec::IntoIter<daemon_grpc::Path>,
}

impl Iterator for Paths {
type Item = Path;

fn next(&mut self) -> Option<Self::Item> {
for grpc_path in self.grpc_paths.by_ref() {
match Path::try_from_grpc_with_endpoints(grpc_path, self.isd_asn) {
Ok(path) => return Some(path),
Err(e) => warn!(?e, "a parse error occurred for a path"),
}
}
None
}
}

0 comments on commit 01d1c5f

Please sign in to comment.