2018-02-28 09:07:54 -08:00
|
|
|
use std::io;
|
|
|
|
use accountant::Accountant;
|
2018-03-06 11:48:26 -08:00
|
|
|
use transaction::Transaction;
|
|
|
|
use signature::PublicKey;
|
2018-03-05 10:11:00 -08:00
|
|
|
use log::{Entry, Sha256Hash};
|
2018-03-04 06:28:51 -08:00
|
|
|
use std::net::UdpSocket;
|
|
|
|
use bincode::{deserialize, serialize};
|
2018-02-28 09:07:54 -08:00
|
|
|
|
|
|
|
pub struct AccountantSkel {
|
2018-03-05 11:48:09 -08:00
|
|
|
pub acc: Accountant,
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
|
2018-02-28 13:16:50 -08:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2018-02-28 09:07:54 -08:00
|
|
|
pub enum Request {
|
2018-03-06 10:54:45 -08:00
|
|
|
Transaction(Transaction<i64>),
|
2018-03-06 10:03:41 -08:00
|
|
|
GetBalance { key: PublicKey },
|
|
|
|
GetEntries { last_id: Sha256Hash },
|
|
|
|
GetId { is_last: bool },
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
|
2018-02-28 13:16:50 -08:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
2018-02-28 09:07:54 -08:00
|
|
|
pub enum Response {
|
2018-03-05 16:29:32 -08:00
|
|
|
Balance { key: PublicKey, val: Option<i64> },
|
|
|
|
Entries { entries: Vec<Entry<i64>> },
|
2018-03-05 11:48:09 -08:00
|
|
|
Id { id: Sha256Hash, is_last: bool },
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl AccountantSkel {
|
2018-03-05 11:48:09 -08:00
|
|
|
pub fn new(acc: Accountant) -> Self {
|
|
|
|
AccountantSkel { acc }
|
2018-02-28 13:16:50 -08:00
|
|
|
}
|
|
|
|
|
2018-02-28 17:04:35 -08:00
|
|
|
pub fn process_request(self: &mut Self, msg: Request) -> Option<Response> {
|
2018-02-28 09:07:54 -08:00
|
|
|
match msg {
|
2018-03-06 10:43:53 -08:00
|
|
|
Request::Transaction(tr) => {
|
2018-03-06 10:54:45 -08:00
|
|
|
if let Err(err) = self.acc.process_transaction(tr) {
|
|
|
|
eprintln!("Transaction error: {:?}", err);
|
2018-03-03 09:23:31 -08:00
|
|
|
}
|
2018-02-28 09:07:54 -08:00
|
|
|
None
|
|
|
|
}
|
|
|
|
Request::GetBalance { key } => {
|
2018-03-05 14:34:15 -08:00
|
|
|
let val = self.acc.get_balance(&key);
|
2018-02-28 09:07:54 -08:00
|
|
|
Some(Response::Balance { key, val })
|
|
|
|
}
|
2018-03-05 10:11:00 -08:00
|
|
|
Request::GetEntries { .. } => Some(Response::Entries { entries: vec![] }),
|
2018-03-05 11:48:09 -08:00
|
|
|
Request::GetId { is_last } => Some(Response::Id {
|
|
|
|
id: if is_last {
|
|
|
|
self.acc.sync();
|
|
|
|
self.acc.last_id
|
|
|
|
} else {
|
|
|
|
self.acc.first_id
|
|
|
|
},
|
|
|
|
is_last,
|
|
|
|
}),
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:23:27 -08:00
|
|
|
/// UDP Server that forwards messages to Accountant methods.
|
2018-02-28 09:07:54 -08:00
|
|
|
pub fn serve(self: &mut Self, addr: &str) -> io::Result<()> {
|
2018-03-01 11:23:27 -08:00
|
|
|
let socket = UdpSocket::bind(addr)?;
|
2018-02-28 13:16:50 -08:00
|
|
|
let mut buf = vec![0u8; 1024];
|
2018-02-28 09:07:54 -08:00
|
|
|
loop {
|
2018-03-01 11:23:27 -08:00
|
|
|
//println!("skel: Waiting for incoming packets...");
|
|
|
|
let (_sz, src) = socket.recv_from(&mut buf)?;
|
2018-02-28 09:07:54 -08:00
|
|
|
|
|
|
|
// TODO: Return a descriptive error message if deserialization fails.
|
2018-02-28 13:16:50 -08:00
|
|
|
let req = deserialize(&buf).expect("deserialize request");
|
|
|
|
|
2018-02-28 17:04:35 -08:00
|
|
|
if let Some(resp) = self.process_request(req) {
|
2018-03-01 11:23:27 -08:00
|
|
|
socket.send_to(&serialize(&resp).expect("serialize response"), &src)?;
|
2018-02-28 09:07:54 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|