openethereum/ipc/tests/service.rs.in

78 lines
2.1 KiB
Rust
Raw Normal View History

2016-03-24 22:07:01 +01:00
// Copyright 2015, 2016 Ethcore (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
use std::sync::RwLock;
use std::ops::*;
2016-03-24 22:07:01 +01:00
pub struct Service {
pub commits: RwLock<usize>,
pub rollbacks: RwLock<usize>,
}
#[derive(Ipc)]
impl Service {
fn commit(&self, f: u32) -> u32 {
let mut lock = self.commits.write().unwrap();
*lock = *lock + f as usize;
f
}
pub fn rollback(&self, a: u32, b: u32) -> i32 {
let mut lock = self.rollbacks.write().unwrap();
*lock = *lock + a as usize - b as usize;
(a - b) as i32
}
}
impl<S: ::ipc::IpcSocket> ServiceProxy<S> {
pub fn commit(&self, f: u32) -> u32 {
#[derive(Serialize)]
struct Request<'a> {
f: &'a u32,
}
let payload = Request{ f: &f, };
let mut socket_ref = self.socket.borrow_mut();
let mut socket = socket_ref.deref_mut();
let serialized_payload = ::bincode::serde::serialize(&payload, ::bincode::SizeLimit::Infinite).unwrap();
::ipc::invoke(0, &Some(serialized_payload), &mut socket);
while !socket.ready().load(::std::sync::atomic::Ordering::Relaxed) { }
::bincode::serde::deserialize_from::<_, u32>(&mut socket, ::bincode::SizeLimit::Infinite).unwrap()
}
pub fn new(socket: S) -> ServiceProxy<S> {
ServiceProxy {
socket: ::std::cell::RefCell::new(socket),
phantom: ::std::marker::PhantomData,
}
}
#[cfg(test)]
pub fn socket(&self) -> &::std::cell::RefCell<S> {
&self.socket
}
}
2016-03-24 22:07:01 +01:00
impl Service {
pub fn new() -> Service {
Service {
commits: RwLock::new(0usize),
rollbacks: RwLock::new(0usize),
}
}
}