Add Iroh peer ping
This commit is contained in:
parent
8ab3ef004e
commit
679eeb48a3
15 changed files with 619 additions and 18 deletions
|
|
@ -28,6 +28,9 @@ pub enum ControlRequest {
|
|||
path: PathBuf,
|
||||
},
|
||||
PeerCardList,
|
||||
PeerPing {
|
||||
node: String,
|
||||
},
|
||||
ResourceList,
|
||||
ResourceCreate {
|
||||
kind: String,
|
||||
|
|
@ -228,6 +231,13 @@ pub enum ControlResponse {
|
|||
peers: Vec<DiscoveredPeer>,
|
||||
note: String,
|
||||
},
|
||||
PeerPinged {
|
||||
peer_node_id: String,
|
||||
peer_agent_id: String,
|
||||
endpoint_id: String,
|
||||
alpn: String,
|
||||
note: String,
|
||||
},
|
||||
ResourceList {
|
||||
resources: Vec<ResourceDescriptor>,
|
||||
},
|
||||
|
|
@ -428,6 +438,29 @@ pub struct CasBlob {
|
|||
pub pinned: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "kebab-case")]
|
||||
pub enum PeerControlRequest {
|
||||
Ping { peer_card: PeerCard, nonce: String },
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(tag = "type", rename_all = "kebab-case")]
|
||||
pub enum PeerControlResponse {
|
||||
Pong {
|
||||
node_id: String,
|
||||
agent_id: String,
|
||||
endpoint_id: String,
|
||||
remote_endpoint_id: String,
|
||||
alpn: String,
|
||||
nonce: String,
|
||||
note: String,
|
||||
},
|
||||
Error {
|
||||
message: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ControlError {
|
||||
#[error("json error: {0}")]
|
||||
|
|
@ -454,6 +487,26 @@ pub fn decode_response(line: &str) -> Result<ControlResponse, ControlError> {
|
|||
serde_json::from_str(line).map_err(ControlError::from)
|
||||
}
|
||||
|
||||
pub fn encode_peer_request(request: &PeerControlRequest) -> Result<String, ControlError> {
|
||||
let mut line = serde_json::to_string(request)?;
|
||||
line.push('\n');
|
||||
Ok(line)
|
||||
}
|
||||
|
||||
pub fn decode_peer_request(line: &str) -> Result<PeerControlRequest, ControlError> {
|
||||
serde_json::from_str(line).map_err(ControlError::from)
|
||||
}
|
||||
|
||||
pub fn encode_peer_response(response: &PeerControlResponse) -> Result<String, ControlError> {
|
||||
let mut line = serde_json::to_string(response)?;
|
||||
line.push('\n');
|
||||
Ok(line)
|
||||
}
|
||||
|
||||
pub fn decode_peer_response(line: &str) -> Result<PeerControlResponse, ControlError> {
|
||||
serde_json::from_str(line).map_err(ControlError::from)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
|
@ -557,5 +610,28 @@ mod tests {
|
|||
decode_request(&encode_request(&request).expect("encode")).expect("decode"),
|
||||
request
|
||||
);
|
||||
|
||||
let request = ControlRequest::PeerPing {
|
||||
node: "node:peer".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
decode_request(&encode_request(&request).expect("encode")).expect("decode"),
|
||||
request
|
||||
);
|
||||
|
||||
let response = PeerControlResponse::Pong {
|
||||
node_id: "node:peer".to_owned(),
|
||||
agent_id: "agent:peer".to_owned(),
|
||||
endpoint_id: "endpoint:peer".to_owned(),
|
||||
remote_endpoint_id: "endpoint:caller".to_owned(),
|
||||
alpn: "/geth/control/1".to_owned(),
|
||||
nonce: "nonce".to_owned(),
|
||||
note: "candidate only".to_owned(),
|
||||
};
|
||||
assert_eq!(
|
||||
decode_peer_response(&encode_peer_response(&response).expect("encode"))
|
||||
.expect("decode"),
|
||||
response
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue