1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//! # Config
//!
//! `config` is the module providing the type used to configure the Mitrid applications.
use std::fs::{File, OpenOptions};
use std::path::Path;
use std::io::{Read, Write};
use base::Result;
use base::{ConstantSize, VariableSize};
use base::Serializable;
use base::Datable;
use util::Version;
use base::Stage;
pub trait Config<D, MnP, A, StP, SvP, ClP, CP>
where D: Datable + ConstantSize,
MnP: Datable,
A: Datable + VariableSize,
StP: Datable,
SvP: Datable,
ClP: Datable,
CP: Datable,
Self: Datable + Serializable
{
/// Returs the chain of the config.
fn chain(&self) -> String;
/// Returs the version of the config chain.
fn version(&self) -> Version;
/// Returs the stage of the config chain.
fn stage(&self) -> Stage;
/// Returs the hash of the chain node password.
fn pswd_hash(&self) -> D;
/// Returs the buffer size of the applications channels.
fn buffer_size(&self) -> u64;
/// Returs the maximum number of threads per application.
fn max_threads(&self) -> u64;
/// Returs the manager parameters.
fn manager_params(&self) -> MnP;
/// Returs the store parameters.
fn store_params(&self) -> StP;
/// Returs the local network addresses.
fn addresses(&self) -> Vec<A>;
/// Returs the seed network addresses.
fn seed(&self) -> Vec<A>;
/// Returs the network server parameters.
fn server_params(&self) -> SvP;
/// Returs the network client parameters.
fn client_params(&self) -> ClP;
/// Returs the custom parameters.
fn custom_params(&self) -> CP;
/// Reads a `Config` from a json file.
fn read_json_file<P: 'static + Send + AsRef<Path>>(path: &P) -> Result<Self> {
File::open(path)
.or_else(|e| {
Err(format!("{}", e))
})
.and_then(|mut file| {
let mut json = String::new();
file.read_to_string(&mut json)
.map_err(|e| format!("{:?}", e))?;
let config = Self::from_json(&json)?;
config.check()?;
Ok(config)
})
}
/// Writes the `Config` to a json file.
fn write_json_file<P: 'static + Send + AsRef<Path>>(&self, path: &P) -> Result<()> {
self.check()?;
OpenOptions::new()
.write(true)
.create(true)
.open(path)
.or_else(|e| {
Err(format!("{}", e))
})
.and_then(|mut file| {
let json = self.to_json()?;
file.write_all(json.as_bytes())
.map_err(|e| format!("{:?}", e))
})
}
/// Reads a `Config` from a binary file.
fn read_binary_file<P: 'static + Send + AsRef<Path>>(path: &P) -> Result<Self> {
File::open(path)
.or_else(|e| {
Err(format!("{}", e))
})
.and_then(|mut file| {
let mut buf = Vec::new();
file.read_to_end(&mut buf)
.map_err(|e| format!("{:?}", e))?;
let config = Self::from_bytes(&buf)?;
config.check()?;
Ok(config)
})
}
/// Writes the `Config` to a binary file.
fn write_binary_file<P: 'static + Send + AsRef<Path>>(&self, path: &P) -> Result<()> {
self.check()?;
OpenOptions::new()
.write(true)
.create(true)
.open(path)
.or_else(|e| {
Err(format!("{}", e))
})
.and_then(|mut file| {
let buf = self.to_bytes()?;
file.write_all(&buf)
.map_err(|e| format!("{:?}", e))
})
}
/// Reads a `Config` from a hex file.
fn read_hex_file<P: 'static + Send + AsRef<Path>>(path: &P) -> Result<Self> {
File::open(path)
.or_else(|e| {
Err(format!("{}", e))
})
.and_then(|mut file| {
let mut hex = String::new();
file.read_to_string(&mut hex)
.map_err(|e| format!("{:?}", e))?;
let config = Self::from_hex(&hex)?;
config.check()?;
Ok(config)
})
}
/// Writes the `Config` to a hex file.
fn write_hex_file<P: 'static + Send + AsRef<Path>>(&self, path: &P) -> Result<()> {
self.check()?;
OpenOptions::new()
.write(true)
.create(true)
.open(path)
.or_else(|e| {
Err(format!("{}", e))
})
.and_then(|mut file| {
let hex = self.to_hex()?;
file.write_all(hex.as_bytes())
.map_err(|e| format!("{:?}", e))
})
}
}