Diff
Not logged in

Differences From Artifact [908f0b94f1]:

To Artifact [6d780284b3]:


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
..
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
..
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
...
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
...
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
...
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
...
365
366
367
368
369
370
371


372
373

374
375
376
377
378
379
380
381
382
383
384
...
429
430
431
432
433
434
435

436
437
438
439
440
441
442
443
444
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net
// Commercial License, version 1.0 or later, or (2) The General Public License
// (GPL), version 3, depending on which licence you accepted on initial access
// to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project
// generally, you agree to be bound by the terms of the MaidSafe Contributor
// Agreement, version 1.0.
// This, along with the Licenses can be found in the root directory of this
// project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network
// Software distributed under the GPL Licence is distributed on an "AS IS"
// BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
//
// Please review the Licences for the specific language governing permissions
// and limitations relating to use of the SAFE Network Software.

//! FFI for mutable data entries, keys and values.

use App;
use errors::AppError;
use ffi::helper::send_sync;

use ffi_utils::{OpaqueCtx, catch_unwind_cb, vec_clone_from_raw_parts};
use ffi_utils::callback::Callback;
use object_cache::{MDataEntriesHandle, MDataKeysHandle, MDataValuesHandle};
use routing::{ClientError, Value};
use safe_core::CoreError;
use std::collections::{BTreeMap, BTreeSet};
use std::os::raw::c_void;

/// Create new empty entries.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_new(app: *const App,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void,
                                                               i32,
                                                               MDataEntriesHandle)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app,
                  user_data,
                  o_cb,
                  |_, context| Ok(context.object_cache().insert_mdata_entries(Default::default())))

    })
}

/// Insert an entry to the entries.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_insert(app: *const App,
                                              entries_h: MDataEntriesHandle,
                                              key_ptr: *const u8,
                                              key_len: usize,
                                              value_ptr: *const u8,
                                              value_len: usize,
                                              user_data: *mut c_void,
                                              o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        let key = vec_clone_from_raw_parts(key_ptr, key_len);
        let value = vec_clone_from_raw_parts(value_ptr, value_len);

        with_entries(app, entries_h, user_data, o_cb, |entries| {
            let _ = entries.insert(key,
                                   Value {
................................................................................
}

/// Returns the number of entries.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_len(app: *const App,
                                           entries_h: MDataEntriesHandle,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void, i32, usize)) {
    catch_unwind_cb(user_data,
                    o_cb,
                    || with_entries(app, entries_h, user_data, o_cb, |entries| Ok(entries.len())))
}

/// Get the entry value at the given key.
/// The callbacks arguments are: user data, error code, pointer to value,
................................................................................
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_get(app: *const App,
                                           entries_h: MDataEntriesHandle,
                                           key_ptr: *const u8,
                                           key_len: usize,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void,
                                                               i32,
                                                               *const u8,
                                                               usize,
                                                               u64)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);
        let key = vec_clone_from_raw_parts(key_ptr, key_len);

        (*app).send(move |_, context| {
            let entries = context.object_cache().get_mdata_entries(entries_h);
            let entries = try_cb!(entries, user_data, o_cb);

            let value = entries.get(&key)

                .ok_or(ClientError::NoSuchEntry)
                .map_err(CoreError::from)
                .map_err(AppError::from);
            let value = try_cb!(value, user_data, o_cb);

            o_cb(user_data.0,
                 0,
                 value.content.as_ptr(),
                 value.content.len(),
                 value.entry_version);

            None
        })
    })
}
................................................................................
                                                entry_cb: extern "C" fn(*mut c_void,
                                                                        *const u8,
                                                                        usize,
                                                                        *const u8,
                                                                        usize,
                                                                        u64),
                                                user_data: *mut c_void,
                                                o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        with_entries(app, entries_h, user_data.0, o_cb, move |entries| {
            for (key, value) in entries {
                entry_cb(user_data.0,
                         key.as_ptr(),
                         key.len(),
                         value.content.as_ptr(),
                         value.content.len(),
                         value.entry_version);
            }

            Ok(())
        })
    })
................................................................................
}

/// Free the entries from memory.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_free(app: *const App,
                                            entries_h: MDataEntriesHandle,
                                            user_data: *mut c_void,
                                            o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_entries(entries_h)?;
            Ok(())
        })
    })
}

/// Returns the number of keys.
#[no_mangle]
pub unsafe extern "C" fn mdata_keys_len(app: *const App,
                                        keys_h: MDataKeysHandle,
                                        user_data: *mut c_void,
                                        o_cb: extern "C" fn(*mut c_void, i32, usize)) {
    catch_unwind_cb(user_data,
                    o_cb,
                    || with_keys(app, keys_h, user_data, o_cb, |keys| Ok(keys.len())))
}

/// Iterate over the keys.
///
................................................................................
#[no_mangle]
pub unsafe extern "C" fn mdata_keys_for_each(app: *const App,
                                             keys_h: MDataKeysHandle,
                                             key_cb: unsafe extern "C" fn(*mut c_void,
                                                                          *const u8,
                                                                          usize),
                                             user_data: *mut c_void,
                                             o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        with_keys(app, keys_h, user_data.0, o_cb, move |keys| {
            for key in keys {
                key_cb(user_data.0, key.as_ptr(), key.len());
            }

            Ok(())
        })
    })
}

/// Free the keys from memory.
#[no_mangle]
pub unsafe extern "C" fn mdata_keys_free(app: *const App,
                                         keys_h: MDataKeysHandle,
                                         user_data: *mut c_void,
                                         o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_keys(keys_h)?;
            Ok(())
        })
    })
}

/// Returns the number of values.
#[no_mangle]
pub unsafe extern "C" fn mdata_values_len(app: *const App,
                                          values_h: MDataValuesHandle,
                                          user_data: *mut c_void,
                                          o_cb: extern "C" fn(*mut c_void, i32, usize)) {
    catch_unwind_cb(user_data,
                    o_cb,
                    || with_values(app, values_h, user_data, o_cb, |values| Ok(values.len())))
}

/// Iterate over the values.
///
................................................................................
pub unsafe extern "C" fn mdata_values_for_each(app: *const App,
                                               values_h: MDataValuesHandle,
                                               value_cb: unsafe extern "C" fn(*mut c_void,
                                                                              *const u8,
                                                                              usize,
                                                                              u64),
                                               user_data: *mut c_void,
                                               o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        with_values(app, values_h, user_data.0, o_cb, move |values| {
            for value in values {
                value_cb(user_data.0,
                         value.content.as_ptr(),
                         value.content.len(),
                         value.entry_version);
            }

            Ok(())
        })
    })
................................................................................
}

/// Free the values from memory.
#[no_mangle]
pub unsafe extern "C" fn mdata_values_free(app: *const App,
                                           values_h: MDataValuesHandle,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void, i32)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_values(values_h)?;
            Ok(())
        })
    })
}
................................................................................
        });

        let len: usize =
            unsafe { unwrap!(call_1(|ud, cb| mdata_entries_len(&app, handle, ud, cb))) };
        assert_eq!(len, 2);

        let (tx, rx) = mpsc::channel::<Value>();


        extern "C" fn get_cb(user_data: *mut c_void,
                             error_code: i32,

                             ptr: *const u8,
                             len: usize,
                             version: u64) {
            assert_eq!(error_code, 0);

            unsafe {
                let value = vec_clone_from_raw_parts(ptr, len);
                let value = Value {
                    content: value,
                    entry_version: version,
                };
................................................................................
                };

                let user_data = user_data as *mut (Sender<()>, BTreeMap<_, _>);
                let _ = (*user_data).1.insert(key, value);
            }
        }


        extern "C" fn done_cb(user_data: *mut c_void, error_code: i32) {
            assert_eq!(error_code, 0);
            let user_data = user_data as *mut (Sender<_>, BTreeMap<Vec<u8>, Value>);

            unsafe {
                unwrap!((*user_data).0.send(()));
            }
        }



|
|
<
|

|
|
<
<
|

|
|
|
<

|
|






>
|












|


|
|
|
|
>












|







 







|







 







|











|
>






|
|







 







|






|

|







 







|













|







 







|





|












|













|







 







|






|







 







|







 







>
>

<
>



|







 







>
|
|







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
..
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
..
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
...
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
...
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
...
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
...
364
365
366
367
368
369
370
371
372
373

374
375
376
377
378
379
380
381
382
383
384
385
...
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// Copyright 2016 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under (1) the MaidSafe.net Commercial License,
// version 1.0 or later, or (2) The General Public License (GPL), version 3, depending on which

// licence you accepted on initial access to the Software (the "Licences").
//
// By contributing code to the SAFE Network Software, or to this project generally, you agree to be
// bound by the terms of the MaidSafe Contributor Agreement.  This, along with the Licenses can be


// found in the root directory of this project at LICENSE, COPYING and CONTRIBUTOR.
//
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.

//
// Please review the Licences for the specific language governing permissions and limitations
// relating to use of the SAFE Network Software.

//! FFI for mutable data entries, keys and values.

use App;
use errors::AppError;
use ffi::helper::send_sync;
use ffi_utils::{FFI_RESULT_OK, FfiResult, OpaqueCtx, SafePtr, catch_unwind_cb,
                vec_clone_from_raw_parts};
use ffi_utils::callback::Callback;
use object_cache::{MDataEntriesHandle, MDataKeysHandle, MDataValuesHandle};
use routing::{ClientError, Value};
use safe_core::CoreError;
use std::collections::{BTreeMap, BTreeSet};
use std::os::raw::c_void;

/// Create new empty entries.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_new(app: *const App,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void,
                                                               FfiResult,
                                                               MDataEntriesHandle)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, |_, context| {
            Ok(context
                   .object_cache()
                   .insert_mdata_entries(Default::default()))
        })
    })
}

/// Insert an entry to the entries.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_insert(app: *const App,
                                              entries_h: MDataEntriesHandle,
                                              key_ptr: *const u8,
                                              key_len: usize,
                                              value_ptr: *const u8,
                                              value_len: usize,
                                              user_data: *mut c_void,
                                              o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        let key = vec_clone_from_raw_parts(key_ptr, key_len);
        let value = vec_clone_from_raw_parts(value_ptr, value_len);

        with_entries(app, entries_h, user_data, o_cb, |entries| {
            let _ = entries.insert(key,
                                   Value {
................................................................................
}

/// Returns the number of entries.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_len(app: *const App,
                                           entries_h: MDataEntriesHandle,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void, FfiResult, usize)) {
    catch_unwind_cb(user_data,
                    o_cb,
                    || with_entries(app, entries_h, user_data, o_cb, |entries| Ok(entries.len())))
}

/// Get the entry value at the given key.
/// The callbacks arguments are: user data, error code, pointer to value,
................................................................................
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_get(app: *const App,
                                           entries_h: MDataEntriesHandle,
                                           key_ptr: *const u8,
                                           key_len: usize,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void,
                                                               FfiResult,
                                                               *const u8,
                                                               usize,
                                                               u64)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);
        let key = vec_clone_from_raw_parts(key_ptr, key_len);

        (*app).send(move |_, context| {
            let entries = context.object_cache().get_mdata_entries(entries_h);
            let entries = try_cb!(entries, user_data, o_cb);

            let value = entries
                .get(&key)
                .ok_or(ClientError::NoSuchEntry)
                .map_err(CoreError::from)
                .map_err(AppError::from);
            let value = try_cb!(value, user_data, o_cb);

            o_cb(user_data.0,
                 FFI_RESULT_OK,
                 value.content.as_safe_ptr(),
                 value.content.len(),
                 value.entry_version);

            None
        })
    })
}
................................................................................
                                                entry_cb: extern "C" fn(*mut c_void,
                                                                        *const u8,
                                                                        usize,
                                                                        *const u8,
                                                                        usize,
                                                                        u64),
                                                user_data: *mut c_void,
                                                o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        with_entries(app, entries_h, user_data.0, o_cb, move |entries| {
            for (key, value) in entries {
                entry_cb(user_data.0,
                         key.as_safe_ptr(),
                         key.len(),
                         value.content.as_safe_ptr(),
                         value.content.len(),
                         value.entry_version);
            }

            Ok(())
        })
    })
................................................................................
}

/// Free the entries from memory.
#[no_mangle]
pub unsafe extern "C" fn mdata_entries_free(app: *const App,
                                            entries_h: MDataEntriesHandle,
                                            user_data: *mut c_void,
                                            o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_entries(entries_h)?;
            Ok(())
        })
    })
}

/// Returns the number of keys.
#[no_mangle]
pub unsafe extern "C" fn mdata_keys_len(app: *const App,
                                        keys_h: MDataKeysHandle,
                                        user_data: *mut c_void,
                                        o_cb: extern "C" fn(*mut c_void, FfiResult, usize)) {
    catch_unwind_cb(user_data,
                    o_cb,
                    || with_keys(app, keys_h, user_data, o_cb, |keys| Ok(keys.len())))
}

/// Iterate over the keys.
///
................................................................................
#[no_mangle]
pub unsafe extern "C" fn mdata_keys_for_each(app: *const App,
                                             keys_h: MDataKeysHandle,
                                             key_cb: unsafe extern "C" fn(*mut c_void,
                                                                          *const u8,
                                                                          usize),
                                             user_data: *mut c_void,
                                             o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        with_keys(app, keys_h, user_data.0, o_cb, move |keys| {
            for key in keys {
                key_cb(user_data.0, key.as_safe_ptr(), key.len());
            }

            Ok(())
        })
    })
}

/// Free the keys from memory.
#[no_mangle]
pub unsafe extern "C" fn mdata_keys_free(app: *const App,
                                         keys_h: MDataKeysHandle,
                                         user_data: *mut c_void,
                                         o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_keys(keys_h)?;
            Ok(())
        })
    })
}

/// Returns the number of values.
#[no_mangle]
pub unsafe extern "C" fn mdata_values_len(app: *const App,
                                          values_h: MDataValuesHandle,
                                          user_data: *mut c_void,
                                          o_cb: extern "C" fn(*mut c_void, FfiResult, usize)) {
    catch_unwind_cb(user_data,
                    o_cb,
                    || with_values(app, values_h, user_data, o_cb, |values| Ok(values.len())))
}

/// Iterate over the values.
///
................................................................................
pub unsafe extern "C" fn mdata_values_for_each(app: *const App,
                                               values_h: MDataValuesHandle,
                                               value_cb: unsafe extern "C" fn(*mut c_void,
                                                                              *const u8,
                                                                              usize,
                                                                              u64),
                                               user_data: *mut c_void,
                                               o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        let user_data = OpaqueCtx(user_data);

        with_values(app, values_h, user_data.0, o_cb, move |values| {
            for value in values {
                value_cb(user_data.0,
                         value.content.as_safe_ptr(),
                         value.content.len(),
                         value.entry_version);
            }

            Ok(())
        })
    })
................................................................................
}

/// Free the values from memory.
#[no_mangle]
pub unsafe extern "C" fn mdata_values_free(app: *const App,
                                           values_h: MDataValuesHandle,
                                           user_data: *mut c_void,
                                           o_cb: extern "C" fn(*mut c_void, FfiResult)) {
    catch_unwind_cb(user_data, o_cb, || {
        send_sync(app, user_data, o_cb, move |_, context| {
            let _ = context.object_cache().remove_mdata_values(values_h)?;
            Ok(())
        })
    })
}
................................................................................
        });

        let len: usize =
            unsafe { unwrap!(call_1(|ud, cb| mdata_entries_len(&app, handle, ud, cb))) };
        assert_eq!(len, 2);

        let (tx, rx) = mpsc::channel::<Value>();

        #[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
        extern "C" fn get_cb(user_data: *mut c_void,

                             res: FfiResult,
                             ptr: *const u8,
                             len: usize,
                             version: u64) {
            assert_eq!(res.error_code, 0);

            unsafe {
                let value = vec_clone_from_raw_parts(ptr, len);
                let value = Value {
                    content: value,
                    entry_version: version,
                };
................................................................................
                };

                let user_data = user_data as *mut (Sender<()>, BTreeMap<_, _>);
                let _ = (*user_data).1.insert(key, value);
            }
        }

        #[cfg_attr(feature="cargo-clippy", allow(needless_pass_by_value))]
        extern "C" fn done_cb(user_data: *mut c_void, res: FfiResult) {
            assert_eq!(res.error_code, 0);
            let user_data = user_data as *mut (Sender<_>, BTreeMap<Vec<u8>, Value>);

            unsafe {
                unwrap!((*user_data).0.send(()));
            }
        }