2018-05-25 15:57:47 -07:00
|
|
|
//! The `request_processor` processes thin client Request messages.
|
2018-05-12 16:57:28 -07:00
|
|
|
|
2018-05-14 14:33:11 -07:00
|
|
|
use bank::Bank;
|
2018-05-14 08:53:57 -07:00
|
|
|
use request::{Request, Response};
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
2018-05-12 16:57:28 -07:00
|
|
|
|
|
|
|
pub struct RequestProcessor {
|
2018-05-14 14:33:11 -07:00
|
|
|
bank: Arc<Bank>,
|
2018-05-12 16:57:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestProcessor {
|
2018-05-14 14:33:11 -07:00
|
|
|
/// Create a new Tpu that wraps the given Bank.
|
|
|
|
pub fn new(bank: Arc<Bank>) -> Self {
|
|
|
|
RequestProcessor { bank }
|
2018-05-12 16:57:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Process Request items sent by clients.
|
|
|
|
fn process_request(
|
|
|
|
&self,
|
|
|
|
msg: Request,
|
|
|
|
rsp_addr: SocketAddr,
|
|
|
|
) -> Option<(Response, SocketAddr)> {
|
|
|
|
match msg {
|
|
|
|
Request::GetBalance { key } => {
|
2018-05-14 14:33:11 -07:00
|
|
|
let val = self.bank.get_balance(&key);
|
2018-05-12 16:57:28 -07:00
|
|
|
let rsp = (Response::Balance { key, val }, rsp_addr);
|
|
|
|
info!("Response::Balance {:?}", rsp);
|
|
|
|
Some(rsp)
|
|
|
|
}
|
2018-05-14 08:35:10 -07:00
|
|
|
Request::GetLastId => {
|
2018-05-14 14:33:11 -07:00
|
|
|
let id = self.bank.last_id();
|
2018-05-14 08:35:10 -07:00
|
|
|
let rsp = (Response::LastId { id }, rsp_addr);
|
|
|
|
info!("Response::LastId {:?}", rsp);
|
|
|
|
Some(rsp)
|
|
|
|
}
|
2018-05-14 06:16:39 -07:00
|
|
|
Request::GetTransactionCount => {
|
2018-05-14 14:33:11 -07:00
|
|
|
let transaction_count = self.bank.transaction_count() as u64;
|
2018-05-14 06:16:39 -07:00
|
|
|
let rsp = (Response::TransactionCount { transaction_count }, rsp_addr);
|
|
|
|
info!("Response::TransactionCount {:?}", rsp);
|
|
|
|
Some(rsp)
|
|
|
|
}
|
2018-06-28 11:58:33 -07:00
|
|
|
Request::GetSignature { signature } => {
|
2018-06-29 09:30:09 -07:00
|
|
|
let signature_status = self.bank.has_signature(&signature);
|
2018-06-28 11:58:33 -07:00
|
|
|
let rsp = (Response::SignatureStatus { signature_status }, rsp_addr);
|
|
|
|
info!("Response::Signature {:?}", rsp);
|
|
|
|
Some(rsp)
|
|
|
|
}
|
2018-05-12 16:57:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process_requests(
|
|
|
|
&self,
|
|
|
|
reqs: Vec<(Request, SocketAddr)>,
|
|
|
|
) -> Vec<(Response, SocketAddr)> {
|
|
|
|
reqs.into_iter()
|
|
|
|
.filter_map(|(req, rsp_addr)| self.process_request(req, rsp_addr))
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
}
|