1use std::fs::File;
6use std::io::{Read, Write};
7use std::path::PathBuf;
8
9use error::*;
10use error::ErrorKind::*;
11
12use {ControllIdentifier, ControllerInternal, Controllers, Resources, Subsystem};
13
14#[derive(Debug, Clone)]
19pub struct RdmaController {
20 base: PathBuf,
21 path: PathBuf,
22}
23
24impl ControllerInternal for RdmaController {
25 fn control_type(&self) -> Controllers {
26 Controllers::Rdma
27 }
28 fn get_path(&self) -> &PathBuf {
29 &self.path
30 }
31 fn get_path_mut(&mut self) -> &mut PathBuf {
32 &mut self.path
33 }
34 fn get_base(&self) -> &PathBuf {
35 &self.base
36 }
37
38 fn apply(&self, _res: &Resources) -> Result<()> {
39 Ok(())
40 }
41}
42
43impl ControllIdentifier for RdmaController {
44 fn controller_type() -> Controllers {
45 Controllers::Rdma
46 }
47}
48
49impl<'a> From<&'a Subsystem> for &'a RdmaController {
50 fn from(sub: &'a Subsystem) -> &'a RdmaController {
51 unsafe {
52 match sub {
53 Subsystem::Rdma(c) => c,
54 _ => {
55 assert_eq!(1, 0);
56 ::std::mem::uninitialized()
57 }
58 }
59 }
60 }
61}
62
63fn read_string_from(mut file: File) -> Result<String> {
64 let mut string = String::new();
65 match file.read_to_string(&mut string) {
66 Ok(_) => Ok(string.trim().to_string()),
67 Err(e) => Err(Error::with_cause(ReadFailed, e)),
68 }
69}
70
71impl RdmaController {
72 pub fn new(oroot: PathBuf) -> Self {
74 let mut root = oroot;
75 root.push(Self::controller_type().to_string());
76 Self {
77 base: root.clone(),
78 path: root,
79 }
80 }
81
82 pub fn current(&self) -> Result<String> {
84 self.open_path("rdma.current", false)
85 .and_then(read_string_from)
86 }
87
88 pub fn set_max(&self, max: &str) -> Result<()> {
90 self.open_path("rdma.max", true).and_then(|mut file| {
91 file.write_all(max.as_ref())
92 .map_err(|e| Error::with_cause(WriteFailed, e))
93 })
94 }
95}