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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
use {ErrorCode, IndyHandle};

use std::ffi::CString;
use std::ptr::null;
use std::time::Duration;

use utils::results::ResultHandler;
use utils::callbacks::ClosureHandler;

use ffi::pool;
use ffi::{ResponseEmptyCB,
          ResponseStringCB,
          ResponseI32CB};

pub struct Pool {}

impl Pool {
    /// Creates a new local pool ledger configuration that can be used later to connect pool nodes.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration.
    /// * `config`  (required)- Pool configuration json. Example:
    /// {
    ///     "genesis_txn": string (required), A path to genesis transaction file.
    /// }
    pub fn create_ledger_config(pool_name: &str, pool_config: Option<&str>) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_create_ledger_config(command_handle, pool_name, pool_config, cb);

        ResultHandler::empty(err, receiver)
    }

    /// Creates a new local pool ledger configuration that can be used later to connect pool nodes.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration.
    /// * `config`  (required)- Pool configuration json. Example:
    /// {
    ///     "genesis_txn": string (required), A path to genesis transaction file.
    /// }
    /// * `timeout` - the maximum time this function waits for a response
    pub fn create_ledger_config_timeout(pool_name: &str, pool_config: Option<&str>, timeout: Duration) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_create_ledger_config(command_handle, pool_name, pool_config, cb);

        ResultHandler::empty_timeout(err, receiver, timeout)
    }

    /// Creates a new local pool ledger configuration that can be used later to connect pool nodes.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration.
    /// * `config`  (required)- Pool configuration json. Example:
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn create_ledger_config_async<F: 'static>(pool_name: &str, pool_config: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));

        Pool::_create_ledger_config(command_handle, pool_name, pool_config, cb)
    }

    fn _create_ledger_config(command_handle: IndyHandle, pool_name: &str, pool_config: Option<&str>, cb: Option<ResponseEmptyCB>) -> ErrorCode {
        let pool_name = c_str!(pool_name);
        let pool_config_str = opt_c_str!(pool_config);

        ErrorCode::from(unsafe { pool::indy_create_pool_ledger_config(command_handle, pool_name.as_ptr(), opt_c_ptr!(pool_config, pool_config_str), cb) })
    }

    /// Opens pool ledger and performs connecting to pool nodes.
    ///
    /// Pool ledger configuration with corresponded name must be previously created
    /// with indy_create_pool_ledger_config method.
    /// It is impossible to open pool with the same name more than once.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration.
    /// * `config`  (optional)- Runtime pool configuration json.
    ///                         if NULL, then default config will be used. Example:
    /// {
    ///     "refresh_on_open": bool (optional), Forces pool ledger to be refreshed immediately after opening.
    ///                      Defaults to true.
    ///     "auto_refresh_time": int (optional), After this time in minutes pool ledger will be automatically refreshed.
    ///                        Use 0 to disable automatic refresh. Defaults to 24*60.
    ///     "network_timeout": int (optional), Network timeout for communication with nodes in milliseconds.
    ///                       Defaults to 20000.
    /// }
    ///
    /// # Returns
    /// Handle to opened pool to use in methods that require pool connection.
    pub fn open_ledger(pool_name: &str, config: Option<&str>) -> Result<IndyHandle, ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_i32();

        let err = Pool::_open_ledger(command_handle, pool_name, config, cb);

        ResultHandler::one(err, receiver)
    }

    /// Opens pool ledger and performs connecting to pool nodes.
    ///
    /// Pool ledger configuration with corresponded name must be previously created
    /// with indy_create_pool_ledger_config method.
    /// It is impossible to open pool with the same name more than once.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration.
    /// * `config`  (optional)- Runtime pool configuration json.
    ///                         if NULL, then default config will be used. Example:
    /// {
    ///     "refresh_on_open": bool (optional), Forces pool ledger to be refreshed immediately after opening.
    ///                      Defaults to true.
    ///     "auto_refresh_time": int (optional), After this time in minutes pool ledger will be automatically refreshed.
    ///                        Use 0 to disable automatic refresh. Defaults to 24*60.
    ///     "network_timeout": int (optional), Network timeout for communication with nodes in milliseconds.
    ///                       Defaults to 20000.
    /// }
    /// * `timeout` - the maximum time this function waits for a response
    ///
    /// # Returns
    /// Handle to opened pool to use in methods that require pool connection.
    pub fn open_ledger_timeout(pool_name: &str, config: Option<&str>, timeout: Duration) -> Result<IndyHandle, ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_i32();

        let err = Pool::_open_ledger(command_handle, pool_name, config, cb);

        ResultHandler::one_timeout(err, receiver, timeout)
    }

    /// Opens pool ledger and performs connecting to pool nodes.
    ///
    /// Pool ledger configuration with corresponded name must be previously created
    /// with indy_create_pool_ledger_config method.
    /// It is impossible to open pool with the same name more than once.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration.
    /// * `config`  (optional)- Runtime pool configuration json.
    ///                         if NULL, then default config will be used. Example:
    /// {
    ///     "refresh_on_open": bool (optional), Forces pool ledger to be refreshed immediately after opening.
    ///                      Defaults to true.
    ///     "auto_refresh_time": int (optional), After this time in minutes pool ledger will be automatically refreshed.
    ///                        Use 0 to disable automatic refresh. Defaults to 24*60.
    ///     "network_timeout": int (optional), Network timeout for communication with nodes in milliseconds.
    ///                       Defaults to 20000.
    /// }
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn open_ledger_async<F: 'static>(pool_name: &str, config: Option<&str>, closure: F) -> ErrorCode where F: FnMut(ErrorCode, IndyHandle) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec_i32(Box::new(closure));

        Pool::_open_ledger(command_handle, pool_name, config, cb)
    }

    fn _open_ledger(command_handle: IndyHandle, pool_name: &str, config: Option<&str>, cb: Option<ResponseI32CB>) -> ErrorCode {
        let pool_name = c_str!(pool_name);
        let config_str = opt_c_str!(config);

        ErrorCode::from(unsafe { pool::indy_open_pool_ledger(command_handle, pool_name.as_ptr(), opt_c_ptr!(config, config_str), cb) })
    }

    /// Refreshes a local copy of a pool ledger and updates pool nodes connections.
    ///
    /// # Arguments
    /// * `handle` - pool handle returned by Pool::open_ledger
    pub fn refresh(pool_handle: IndyHandle) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_refresh(command_handle, pool_handle, cb);

        ResultHandler::empty(err, receiver)
    }

    /// Refreshes a local copy of a pool ledger and updates pool nodes connections.
    ///
    /// # Arguments
    /// * `handle` - pool handle returned by Pool::open_ledger
    /// * `timeout` - the maximum time this function waits for a response
    pub fn refresh_timeout(pool_handle: IndyHandle, timeout: Duration) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_refresh(command_handle, pool_handle, cb);

        ResultHandler::empty_timeout(err, receiver, timeout)
    }

    /// Refreshes a local copy of a pool ledger and updates pool nodes connections.
    ///
    /// # Arguments
    /// * `handle` - pool handle returned by Pool::open_ledger
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn refresh_async<F: 'static>(pool_handle: IndyHandle, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));

        Pool::_refresh(command_handle, pool_handle, cb)
    }

    fn _refresh(command_handle: IndyHandle, pool_handle: IndyHandle, cb: Option<ResponseEmptyCB>) -> ErrorCode {
        ErrorCode::from(unsafe { pool::indy_refresh_pool_ledger(command_handle, pool_handle, cb) })
    }

    /// Lists names of created pool ledgers
    pub fn list() -> Result<String, ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();

        let err = Pool::_list(command_handle, cb);

        ResultHandler::one(err, receiver)
    }

    /// Lists names of created pool ledgers
    /// * `timeout` - the maximum time this function waits for a response
    pub fn list_timeout(timeout: Duration) -> Result<String, ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec_string();

        let err = Pool::_list(command_handle, cb);

        ResultHandler::one_timeout(err, receiver, timeout)
    }

    /// Lists names of created pool ledgers
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn list_async<F: 'static>(closure: F) -> ErrorCode where F: FnMut(ErrorCode, String) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec_string(Box::new(closure));

        Pool::_list(command_handle, cb)
    }

    fn _list(command_handle: IndyHandle, cb: Option<ResponseStringCB>) -> ErrorCode {
        ErrorCode::from(unsafe { pool::indy_list_pools(command_handle, cb) })
    }

    /// Closes opened pool ledger, opened nodes connections and frees allocated resources.
    ///
    /// # Arguments
    /// * `handle` - pool handle returned by Pool::open_ledger.
    pub fn close(pool_handle: IndyHandle) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_close(command_handle, pool_handle, cb);

        ResultHandler::empty(err, receiver)
    }

    /// Closes opened pool ledger, opened nodes connections and frees allocated resources.
    ///
    /// # Arguments
    /// * `handle` - pool handle returned by Pool::open_ledger.
    /// * `timeout` - the maximum time this function waits for a response
    pub fn close_timeout(pool_handle: IndyHandle, timeout: Duration) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_close(command_handle, pool_handle, cb);

        ResultHandler::empty_timeout(err, receiver, timeout)
    }

    /// Closes opened pool ledger, opened nodes connections and frees allocated resources.
    ///
    /// # Arguments
    /// * `handle` - pool handle returned by Pool::open_ledger.
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn close_async<F: 'static>(pool_handle: IndyHandle, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));

        Pool::_close(command_handle, pool_handle, cb)
    }

    fn _close(command_handle: IndyHandle, pool_handle: IndyHandle, cb: Option<ResponseEmptyCB>) -> ErrorCode {
        ErrorCode::from(unsafe { pool::indy_close_pool_ledger(command_handle, pool_handle, cb) })
    }

    /// Deletes created pool ledger configuration.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration to delete.
    pub fn delete(pool_name: &str) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_delete(command_handle, pool_name, cb);

        ResultHandler::empty(err, receiver)
    }

    /// Deletes created pool ledger configuration.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration to delete.
    /// * `timeout` - the maximum time this function waits for a response
    pub fn delete_timeout(pool_name: &str, timeout: Duration) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_delete(command_handle, pool_name, cb);

        ResultHandler::empty_timeout(err, receiver, timeout)
    }

    /// Deletes created pool ledger configuration.
    ///
    /// # Arguments
    /// * `config_name` - Name of the pool ledger configuration to delete.
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn delete_async<F: 'static>(pool_name: &str, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));

        Pool::_delete(command_handle, pool_name, cb)
    }

    fn _delete(command_handle: IndyHandle, pool_name: &str, cb: Option<ResponseEmptyCB>) -> ErrorCode {
        let pool_name = c_str!(pool_name);

        ErrorCode::from(unsafe { pool::indy_delete_pool_ledger_config(command_handle, pool_name.as_ptr(), cb) })
    }

    /// Set PROTOCOL_VERSION to specific version.
    ///
    /// There is a global property PROTOCOL_VERSION that used in every request to the pool and
    /// specified version of Indy Node which Libindy works.
    ///
    /// By default PROTOCOL_VERSION=1.
    ///
    /// # Arguments
    /// * `protocol_version` - Protocol version will be used:
    ///     1 - for Indy Node 1.3
    ///     2 - for Indy Node 1.4
    pub fn set_protocol_version(protocol_version: usize) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_set_protocol_version(command_handle, protocol_version, cb);

        ResultHandler::empty(err, receiver)
    }

    /// Set PROTOCOL_VERSION to specific version.
    ///
    /// There is a global property PROTOCOL_VERSION that used in every request to the pool and
    /// specified version of Indy Node which Libindy works.
    ///
    /// By default PROTOCOL_VERSION=1.
    ///
    /// # Arguments
    /// * `protocol_version` - Protocol version will be used:
    ///     1 - for Indy Node 1.3
    ///     2 - for Indy Node 1.4
    /// * `timeout` - the maximum time this function waits for a response
    pub fn set_protocol_version_timeout(protocol_version: usize, timeout: Duration) -> Result<(), ErrorCode> {
        let (receiver, command_handle, cb) = ClosureHandler::cb_ec();

        let err = Pool::_set_protocol_version(command_handle, protocol_version, cb);

        ResultHandler::empty_timeout(err, receiver, timeout)
    }

    /// Set PROTOCOL_VERSION to specific version.
    ///
    /// There is a global property PROTOCOL_VERSION that used in every request to the pool and
    /// specified version of Indy Node which Libindy works.
    ///
    /// By default PROTOCOL_VERSION=1.
    ///
    /// # Arguments
    /// * `protocol_version` - Protocol version will be used:
    ///     1 - for Indy Node 1.3
    ///     2 - for Indy Node 1.4
    /// * `closure` - the closure that is called when finished
    ///
    /// # Returns
    /// * `errorcode` - errorcode from calling ffi function. The closure receives the return result
    pub fn set_protocol_version_async<F: 'static>(protocol_version: usize, closure: F) -> ErrorCode where F: FnMut(ErrorCode) + Send {
        let (command_handle, cb) = ClosureHandler::convert_cb_ec(Box::new(closure));

        Pool::_set_protocol_version(command_handle, protocol_version, cb)
    }

    fn _set_protocol_version(command_handle: IndyHandle, protocol_version: usize, cb: Option<ResponseEmptyCB>) -> ErrorCode {

        ErrorCode::from(unsafe {
          pool::indy_set_protocol_version(command_handle, protocol_version, cb)
        })
    }
}