rust_libindy_wrapper/
pool.rs

1use {ErrorCode, IndyHandle};
2
3use std::ffi::CString;
4use std::ptr::null;
5use std::time::Duration;
6
7use utils::results::ResultHandler;
8use utils::callbacks::ClosureHandler;
9
10use native::pool;
11use native::{ResponseEmptyCB,
12          ResponseStringCB,
13          ResponseI32CB};
14
15pub struct Pool {}
16
17impl Pool {
18    /// Creates a new local pool ledger configuration that can be used later to connect pool nodes.
19    ///
20    /// # Arguments
21    /// * `config_name` - Name of the pool ledger configuration.
22    /// * `config`  (optional)- Pool configuration json. if NULL, then default config will be used. Example:
23    /// {
24    ///     "genesis_txn": string (optional), A path to genesis transaction file. If NULL, then a default one will be used.
25    ///                    If file doesn't exists default one will be created.
26    /// }
27    pub fn create_ledger_config(pool_name: &str, pool_config: Option<&str>) -> Result<(), ErrorCode> {
28        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
29
30        let err = Pool::_create_ledger_config(command_handle, pool_name, pool_config, cb);
31
32        ResultHandler::empty(err, receiver)
33    }
34
35    /// Creates a new local pool ledger configuration that can be used later to connect pool nodes.
36    ///
37    /// # Arguments
38    /// * `config_name` - Name of the pool ledger configuration.
39    /// * `config`  (optional)- Pool configuration json. if NULL, then default config will be used. Example:
40    /// {
41    ///     "genesis_txn": string (optional), A path to genesis transaction file. If NULL, then a default one will be used.
42    ///                    If file doesn't exists default one will be created.
43    /// }
44    /// * `timeout` - the maximum time this function waits for a response
45    pub fn create_ledger_config_timeout(pool_name: &str, pool_config: Option<&str>, timeout: Duration) -> Result<(), ErrorCode> {
46        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
47
48        let err = Pool::_create_ledger_config(command_handle, pool_name, pool_config, cb);
49
50        ResultHandler::empty_timeout(err, receiver, timeout)
51    }
52
53    /// Creates a new local pool ledger configuration that can be used later to connect pool nodes.
54    ///
55    /// # Arguments
56    /// * `config_name` - Name of the pool ledger configuration.
57    /// * `config`  (optional)- Pool configuration json. if NULL, then default config will be used. Example:
58    /// * `closure` - the closure that is called when finished
59    ///
60    /// # Returns
61    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
62    pub fn create_ledger_config_async<F: 'static>(pool_name: &str, pool_config: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
63        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
64
65        Pool::_create_ledger_config(command_handle, pool_name, pool_config, cb)
66    }
67
68    fn _create_ledger_config(command_handle: IndyHandle, pool_name: &str, pool_config: Option<&str>, cb: Option<ResponseEmptyCB>) -> ErrorCode {
69        let pool_name = c_str!(pool_name);
70        let pool_config_str = opt_c_str!(pool_config);
71
72        ErrorCode::from(unsafe { pool::indy_create_pool_ledger_config(command_handle, pool_name.as_ptr(), opt_c_ptr!(pool_config, pool_config_str), cb) })
73    }
74
75    /// Opens pool ledger and performs connecting to pool nodes.
76    ///
77    /// Pool ledger configuration with corresponded name must be previously created
78    /// with indy_create_pool_ledger_config method.
79    /// It is impossible to open pool with the same name more than once.
80    ///
81    /// # Arguments
82    /// * `config_name` - Name of the pool ledger configuration.
83    /// * `config`  (optional)- Runtime pool configuration json.
84    ///                         if NULL, then default config will be used. Example:
85    /// {
86    ///     "refresh_on_open": bool (optional), Forces pool ledger to be refreshed immediately after opening.
87    ///                      Defaults to true.
88    ///     "auto_refresh_time": int (optional), After this time in minutes pool ledger will be automatically refreshed.
89    ///                        Use 0 to disable automatic refresh. Defaults to 24*60.
90    ///     "network_timeout": int (optional), Network timeout for communication with nodes in milliseconds.
91    ///                       Defaults to 20000.
92    /// }
93    ///
94    /// # Returns
95    /// Handle to opened pool to use in methods that require pool connection.
96    pub fn open_ledger(pool_name: &str, config: Option<&str>) -> Result<IndyHandle, ErrorCode> {
97        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_i32();
98
99        let err = Pool::_open_ledger(command_handle, pool_name, config, cb);
100
101        ResultHandler::one(err, receiver)
102    }
103
104    /// Opens pool ledger and performs connecting to pool nodes.
105    ///
106    /// Pool ledger configuration with corresponded name must be previously created
107    /// with indy_create_pool_ledger_config method.
108    /// It is impossible to open pool with the same name more than once.
109    ///
110    /// # Arguments
111    /// * `config_name` - Name of the pool ledger configuration.
112    /// * `config`  (optional)- Runtime pool configuration json.
113    ///                         if NULL, then default config will be used. Example:
114    /// {
115    ///     "refresh_on_open": bool (optional), Forces pool ledger to be refreshed immediately after opening.
116    ///                      Defaults to true.
117    ///     "auto_refresh_time": int (optional), After this time in minutes pool ledger will be automatically refreshed.
118    ///                        Use 0 to disable automatic refresh. Defaults to 24*60.
119    ///     "network_timeout": int (optional), Network timeout for communication with nodes in milliseconds.
120    ///                       Defaults to 20000.
121    /// }
122    /// * `timeout` - the maximum time this function waits for a response
123    ///
124    /// # Returns
125    /// Handle to opened pool to use in methods that require pool connection.
126    pub fn open_ledger_timeout(pool_name: &str, config: Option<&str>, timeout: Duration) -> Result<IndyHandle, ErrorCode> {
127        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_i32();
128
129        let err = Pool::_open_ledger(command_handle, pool_name, config, cb);
130
131        ResultHandler::one_timeout(err, receiver, timeout)
132    }
133
134    /// Opens pool ledger and performs connecting to pool nodes.
135    ///
136    /// Pool ledger configuration with corresponded name must be previously created
137    /// with indy_create_pool_ledger_config method.
138    /// It is impossible to open pool with the same name more than once.
139    ///
140    /// # Arguments
141    /// * `config_name` - Name of the pool ledger configuration.
142    /// * `config`  (optional)- Runtime pool configuration json.
143    ///                         if NULL, then default config will be used. Example:
144    /// {
145    ///     "refresh_on_open": bool (optional), Forces pool ledger to be refreshed immediately after opening.
146    ///                      Defaults to true.
147    ///     "auto_refresh_time": int (optional), After this time in minutes pool ledger will be automatically refreshed.
148    ///                        Use 0 to disable automatic refresh. Defaults to 24*60.
149    ///     "network_timeout": int (optional), Network timeout for communication with nodes in milliseconds.
150    ///                       Defaults to 20000.
151    /// }
152    /// * `closure` - the closure that is called when finished
153    ///
154    /// # Returns
155    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
156    pub fn open_ledger_async<F: 'static>(pool_name: &str, config: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode, IndyHandle) + Send {
157        let (command_handle, cb) = ClosureHandler::convert_cb_ec_i32(Box::new(closure));
158
159        Pool::_open_ledger(command_handle, pool_name, config, cb)
160    }
161
162    fn _open_ledger(command_handle: IndyHandle, pool_name: &str, config: Option<&str>, cb: Option<ResponseI32CB>) -> ErrorCode {
163        let pool_name = c_str!(pool_name);
164        let config_str = opt_c_str!(config);
165
166        ErrorCode::from(unsafe { pool::indy_open_pool_ledger(command_handle, pool_name.as_ptr(), opt_c_ptr!(config, config_str), cb) })
167    }
168
169    /// Refreshes a local copy of a pool ledger and updates pool nodes connections.
170    ///
171    /// # Arguments
172    /// * `handle` - pool handle returned by Pool::open_ledger
173    pub fn refresh(pool_handle: IndyHandle) -> Result<(), ErrorCode> {
174        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
175
176        let err = Pool::_refresh(command_handle, pool_handle, cb);
177
178        ResultHandler::empty(err, receiver)
179    }
180
181    /// Refreshes a local copy of a pool ledger and updates pool nodes connections.
182    ///
183    /// # Arguments
184    /// * `handle` - pool handle returned by Pool::open_ledger
185    /// * `timeout` - the maximum time this function waits for a response
186    pub fn refresh_timeout(pool_handle: IndyHandle, timeout: Duration) -> Result<(), ErrorCode> {
187        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
188
189        let err = Pool::_refresh(command_handle, pool_handle, cb);
190
191        ResultHandler::empty_timeout(err, receiver, timeout)
192    }
193
194    /// Refreshes a local copy of a pool ledger and updates pool nodes connections.
195    ///
196    /// # Arguments
197    /// * `handle` - pool handle returned by Pool::open_ledger
198    /// * `closure` - the closure that is called when finished
199    ///
200    /// # Returns
201    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
202    pub fn refresh_async<F: 'static>(pool_handle: IndyHandle, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
203        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
204
205        Pool::_refresh(command_handle, pool_handle, cb)
206    }
207
208    fn _refresh(command_handle: IndyHandle, pool_handle: IndyHandle, cb: Option<ResponseEmptyCB>) -> ErrorCode {
209        ErrorCode::from(unsafe { pool::indy_refresh_pool_ledger(command_handle, pool_handle, cb) })
210    }
211
212    /// Lists names of created pool ledgers
213    pub fn list() -> Result<String, ErrorCode> {
214        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
215
216        let err = Pool::_list(command_handle, cb);
217
218        ResultHandler::one(err, receiver)
219    }
220
221    /// Lists names of created pool ledgers
222    /// * `timeout` - the maximum time this function waits for a response
223    pub fn list_timeout(timeout: Duration) -> Result<String, ErrorCode> {
224        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();
225
226        let err = Pool::_list(command_handle, cb);
227
228        ResultHandler::one_timeout(err, receiver, timeout)
229    }
230
231    /// Lists names of created pool ledgers
232    /// * `closure` - the closure that is called when finished
233    ///
234    /// # Returns
235    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
236    pub fn list_async<F: 'static>(closure: F) -> ErrorCode where F: FnMut(ErrorCode, String) + Send {
237        let (command_handle, cb) = ClosureHandler::convert_cb_ec_string(Box::new(closure));
238
239        Pool::_list(command_handle, cb)
240    }
241
242    fn _list(command_handle: IndyHandle, cb: Option<ResponseStringCB>) -> ErrorCode {
243        ErrorCode::from(unsafe { pool::indy_list_pools(command_handle, cb) })
244    }
245
246    /// Closes opened pool ledger, opened nodes connections and frees allocated resources.
247    ///
248    /// # Arguments
249    /// * `handle` - pool handle returned by Pool::open_ledger.
250    pub fn close(pool_handle: IndyHandle) -> Result<(), ErrorCode> {
251        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
252
253        let err = Pool::_close(command_handle, pool_handle, cb);
254
255        ResultHandler::empty(err, receiver)
256    }
257
258    /// Closes opened pool ledger, opened nodes connections and frees allocated resources.
259    ///
260    /// # Arguments
261    /// * `handle` - pool handle returned by Pool::open_ledger.
262    /// * `timeout` - the maximum time this function waits for a response
263    pub fn close_timeout(pool_handle: IndyHandle, timeout: Duration) -> Result<(), ErrorCode> {
264        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
265
266        let err = Pool::_close(command_handle, pool_handle, cb);
267
268        ResultHandler::empty_timeout(err, receiver, timeout)
269    }
270
271    /// Closes opened pool ledger, opened nodes connections and frees allocated resources.
272    ///
273    /// # Arguments
274    /// * `handle` - pool handle returned by Pool::open_ledger.
275    /// * `closure` - the closure that is called when finished
276    ///
277    /// # Returns
278    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
279    pub fn close_async<F: 'static>(pool_handle: IndyHandle, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
280        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
281
282        Pool::_close(command_handle, pool_handle, cb)
283    }
284
285    fn _close(command_handle: IndyHandle, pool_handle: IndyHandle, cb: Option<ResponseEmptyCB>) -> ErrorCode {
286        ErrorCode::from(unsafe { pool::indy_close_pool_ledger(command_handle, pool_handle, cb) })
287    }
288
289    /// Deletes created pool ledger configuration.
290    ///
291    /// # Arguments
292    /// * `config_name` - Name of the pool ledger configuration to delete.
293    pub fn delete(pool_name: &str) -> Result<(), ErrorCode> {
294        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
295
296        let err = Pool::_delete(command_handle, pool_name, cb);
297
298        ResultHandler::empty(err, receiver)
299    }
300
301    /// Deletes created pool ledger configuration.
302    ///
303    /// # Arguments
304    /// * `config_name` - Name of the pool ledger configuration to delete.
305    /// * `timeout` - the maximum time this function waits for a response
306    pub fn delete_timeout(pool_name: &str, timeout: Duration) -> Result<(), ErrorCode> {
307        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
308
309        let err = Pool::_delete(command_handle, pool_name, cb);
310
311        ResultHandler::empty_timeout(err, receiver, timeout)
312    }
313
314    /// Deletes created pool ledger configuration.
315    ///
316    /// # Arguments
317    /// * `config_name` - Name of the pool ledger configuration to delete.
318    /// * `closure` - the closure that is called when finished
319    ///
320    /// # Returns
321    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
322    pub fn delete_async<F: 'static>(pool_name: &str, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
323        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
324
325        Pool::_delete(command_handle, pool_name, cb)
326    }
327
328    fn _delete(command_handle: IndyHandle, pool_name: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
329        let pool_name = c_str!(pool_name);
330
331        ErrorCode::from(unsafe { pool::indy_delete_pool_ledger_config(command_handle, pool_name.as_ptr(), cb) })
332    }
333
334    /// Set PROTOCOL_VERSION to specific version.
335    ///
336    /// There is a global property PROTOCOL_VERSION that used in every request to the pool and
337    /// specified version of Indy Node which Libindy works.
338    ///
339    /// By default PROTOCOL_VERSION=1.
340    ///
341    /// # Arguments
342    /// * `protocol_version` - Protocol version will be used:
343    ///     1 - for Indy Node 1.3
344    ///     2 - for Indy Node 1.4
345    pub fn set_protocol_version(protocol_version: usize) -> Result<(), ErrorCode> {
346        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
347
348        let err = Pool::_set_protocol_version(command_handle, protocol_version, cb);
349
350        ResultHandler::empty(err, receiver)
351    }
352
353    /// Set PROTOCOL_VERSION to specific version.
354    ///
355    /// There is a global property PROTOCOL_VERSION that used in every request to the pool and
356    /// specified version of Indy Node which Libindy works.
357    ///
358    /// By default PROTOCOL_VERSION=1.
359    ///
360    /// # Arguments
361    /// * `protocol_version` - Protocol version will be used:
362    ///     1 - for Indy Node 1.3
363    ///     2 - for Indy Node 1.4
364    /// * `timeout` - the maximum time this function waits for a response
365    pub fn set_protocol_version_timeout(protocol_version: usize, timeout: Duration) -> Result<(), ErrorCode> {
366        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();
367
368        let err = Pool::_set_protocol_version(command_handle, protocol_version, cb);
369
370        ResultHandler::empty_timeout(err, receiver, timeout)
371    }
372
373    /// Set PROTOCOL_VERSION to specific version.
374    ///
375    /// There is a global property PROTOCOL_VERSION that used in every request to the pool and
376    /// specified version of Indy Node which Libindy works.
377    ///
378    /// By default PROTOCOL_VERSION=1.
379    ///
380    /// # Arguments
381    /// * `protocol_version` - Protocol version will be used:
382    ///     1 - for Indy Node 1.3
383    ///     2 - for Indy Node 1.4
384    /// * `closure` - the closure that is called when finished
385    ///
386    /// # Returns
387    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
388    pub fn set_protocol_version_async<F: 'static>(protocol_version: usize, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
389        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));
390
391        Pool::_set_protocol_version(command_handle, protocol_version, cb)
392    }
393
394    fn _set_protocol_version(command_handle: IndyHandle, protocol_version: usize, cb: Option<ResponseEmptyCB>) -> ErrorCode {
395
396        ErrorCode::from(unsafe {
397          pool::indy_set_protocol_version(command_handle, protocol_version, cb)
398        })
399    }
400}
401