Skip to content
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
31 changes: 2 additions & 29 deletions s2energy-connection/src/pairing/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,7 @@ impl<'a> V1Session<'a> {

if response.status() == StatusCode::BAD_REQUEST {
let response_error: PairingResponseErrorMessage = response.json().await.map_err(|e| Error::new(ErrorKind::ProtocolError, e))?;
return Err(Error::new(
match response_error {
PairingResponseErrorMessage::InvalidCombinationOfRoles => ErrorKind::RemoteOfSameType,
PairingResponseErrorMessage::IncompatibleS2MessageVersions
| PairingResponseErrorMessage::IncompatibleHMACHashingAlgorithms
| PairingResponseErrorMessage::IncompatibleCommunicationProtocols => ErrorKind::NoSupportedVersion,
PairingResponseErrorMessage::S2NodeNotFound | PairingResponseErrorMessage::S2NodeNotProvided => ErrorKind::UnknownNode,
PairingResponseErrorMessage::InvalidPairingToken => ErrorKind::InvalidToken,
PairingResponseErrorMessage::ParsingError | PairingResponseErrorMessage::Other => ErrorKind::ProtocolError,
},
response_error,
));
return Err(Error::from(response_error));
}
if response.status() != StatusCode::NO_CONTENT {
return Err(ErrorKind::ProtocolError.into());
Expand Down Expand Up @@ -434,23 +423,7 @@ impl<'a> V1Session<'a> {
.json::<PairingResponseErrorMessage>()
.await
.map_err(|e| Error::new(ErrorKind::ProtocolError, e))?;
match error_response {
PairingResponseErrorMessage::InvalidCombinationOfRoles => {
return Err(Error::new(ErrorKind::RemoteOfSameType, error_response));
}
PairingResponseErrorMessage::IncompatibleS2MessageVersions
| PairingResponseErrorMessage::IncompatibleHMACHashingAlgorithms
| PairingResponseErrorMessage::IncompatibleCommunicationProtocols => {
return Err(Error::new(ErrorKind::NoSupportedVersion, error_response));
}
PairingResponseErrorMessage::S2NodeNotFound | PairingResponseErrorMessage::S2NodeNotProvided => {
return Err(Error::new(ErrorKind::UnknownNode, error_response));
}
PairingResponseErrorMessage::InvalidPairingToken => return Err(Error::new(ErrorKind::InvalidToken, error_response)),
PairingResponseErrorMessage::ParsingError | PairingResponseErrorMessage::Other => {
return Err(Error::new(ErrorKind::ProtocolError, error_response));
}
}
return Err(Error::from(error_response));
}
if response.status() != StatusCode::OK {
debug!(status = ?response.status(), "Unexpected status code in response to requestPairing.");
Expand Down
14 changes: 7 additions & 7 deletions s2energy-connection/src/pairing/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,27 +151,27 @@ pub enum PrePairingResponse {

/// Handler for pre-pairing requests.
///
/// This allows notification of other components when a prepairing request
/// This allows notification of other components when a pre-pairing request
/// comes in. The methods of this trait are called during request handling,
/// and should thus return rapidly.
//
// Note: As these methods are called during request handling, we conciously
// make them not async to encourage not doing long-lasting operations.
// Note: Because these methods are called during request handling, we
// deliberately make them not async to encourage not doing long-lasting
// operations.
pub trait PrePairingHandler: Send + Sync + 'static {
/// Handle a request for prepairing, and indicate our willingness.
/// Handle a request for pre-pairing, and indicate our willingness.
fn prepairing_requested(
&self,
endpoint: S2EndpointDescription,
node: S2NodeDescription,
target_node: Option<S2NodeId>,
) -> PrePairingResponse;
/// Handle a cancel event for prepairing. Note that not every pre-pairing
/// Handle a cancel event for pre-pairing. Note that not every pre-pairing
/// request will result in a cancel or a pairing interaction, so timeouts
/// may be needed.
fn prepairing_cancelled(&self, id: S2NodeId, target_node: Option<S2NodeId>);
}

/// A pre-pairing handler that does nothing on receiving a prepairing request.
/// A pre-pairing handler that does nothing on receiving a pre-pairing request.
///
/// The requests will always indicate willingness to pair to the client.
pub struct NoopPrePairingHandler;
Expand Down
21 changes: 20 additions & 1 deletion s2energy-connection/src/pairing/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
common::wire::{AccessToken, CommunicationProtocol, MessageVersion, S2EndpointDescription, S2NodeDescription},
};

#[derive(Error, Debug, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[derive(Error, Debug, Serialize, Deserialize, PartialEq, Eq, Hash, Clone, Copy)]
pub(crate) enum PairingResponseErrorMessage {
#[error("Invalid combination of roles")]
InvalidCombinationOfRoles,
Expand All @@ -34,6 +34,25 @@ pub(crate) enum PairingResponseErrorMessage {
Other,
}

impl From<PairingResponseErrorMessage> for super::Error {
fn from(value: PairingResponseErrorMessage) -> Self {
use super::ErrorKind;
use PairingResponseErrorMessage::*;

let error_kind = match value {
InvalidCombinationOfRoles => ErrorKind::RemoteOfSameType,
IncompatibleS2MessageVersions | IncompatibleHMACHashingAlgorithms | IncompatibleCommunicationProtocols => {
ErrorKind::NoSupportedVersion
}
S2NodeNotFound | S2NodeNotProvided => ErrorKind::UnknownNode,
InvalidPairingToken => ErrorKind::InvalidToken,
ParsingError | PairingResponseErrorMessage::Other => ErrorKind::ProtocolError,
};

super::Error::new(error_kind, value)
}
}

impl IntoResponse for PairingResponseErrorMessage {
fn into_response(self) -> axum::response::Response {
(StatusCode::BAD_REQUEST, Json(self)).into_response()
Expand Down
Loading