pub use crate::node_info::NodeContact;
use crate::{
packet::Packet,
rpc::{Request, RequestBody},
};
use super::HandlerReqId;
#[derive(Debug)]
pub(super) struct RequestCall {
contact: NodeContact,
packet: Packet,
request_id: HandlerReqId,
request: RequestBody,
handshake_sent: bool,
retries: u8,
remaining_responses: Option<u64>,
initiating_session: bool,
}
impl RequestCall {
pub fn new(
contact: NodeContact,
packet: Packet,
request_id: HandlerReqId,
request: RequestBody,
initiating_session: bool,
) -> Self {
RequestCall {
contact,
packet,
request_id,
request,
handshake_sent: false,
retries: 1,
remaining_responses: None,
initiating_session,
}
}
pub fn contact(&self) -> &NodeContact {
&self.contact
}
pub fn id(&self) -> &HandlerReqId {
&self.request_id
}
pub fn body(&self) -> &RequestBody {
&self.request
}
pub fn packet(&self) -> &Packet {
&self.packet
}
pub fn encode(&self) -> Vec<u8> {
match &self.request_id {
HandlerReqId::Internal(id) | HandlerReqId::External(id) => {
let request = Request {
id: id.clone(),
body: self.request.clone(),
};
request.encode()
}
}
}
pub fn retries(&self) -> u8 {
self.retries
}
pub fn increment_retries(&mut self) {
self.retries += 1;
}
pub fn handshake_sent(&self) -> bool {
self.handshake_sent
}
pub fn set_handshake_sent(&mut self) {
self.handshake_sent = true;
}
pub fn set_initiating_session(&mut self, state: bool) {
self.initiating_session = state;
}
pub fn initiating_session(&self) -> bool {
self.initiating_session
}
pub fn update_packet(&mut self, packet: Packet) {
self.packet = packet;
}
pub fn remaining_responses_mut(&mut self) -> &mut Option<u64> {
&mut self.remaining_responses
}
}