Files @ 9ed6d091a817
Branch filter:

Location: CSY/reowolf/src/ffi/mod.rs

9ed6d091a817 14.8 KiB application/rls-services+xml Show Annotation Show as Raw Download as Raw
Christopher Esterhuyse
more benchmarks and some more error cases
  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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
use crate::{common::*, runtime::*};
use core::{cell::RefCell, convert::TryFrom};
use std::os::raw::c_int;
use std::slice::from_raw_parts as slice_from_raw_parts;

#[cfg(all(target_os = "linux", feature = "ffi_pseudo_socket_api"))]
/// cbindgen:ignore
pub mod pseudo_socket_api;

// Temporary simplfication: ignore ipv6. To revert, just refactor this structure and its usages
#[repr(C)]
pub struct FfiSocketAddr {
    pub ipv4: [u8; 4],
    pub port: u16,
}
impl Into<SocketAddr> for FfiSocketAddr {
    fn into(self) -> SocketAddr {
        (self.ipv4, self.port).into()
    }
}

///////////////////////////////////////////////
#[derive(Default)]
struct StoredError {
    // invariant: len is zero IFF its occupied
    // contents are 1+ bytes because we also store the NULL TERMINATOR
    buf: Vec<u8>,
}
impl StoredError {
    const NULL_TERMINATOR: u8 = 0;
    fn clear(&mut self) {
        // no null terminator either!
        self.buf.clear();
    }
    fn debug_store<E: Debug>(&mut self, error: &E) {
        let _ = write!(&mut self.buf, "{:?}", error);
        self.buf.push(Self::NULL_TERMINATOR);
    }
    fn tl_debug_store<E: Debug>(error: &E) {
        STORED_ERROR.with(|stored_error| {
            let mut stored_error = stored_error.borrow_mut();
            stored_error.clear();
            stored_error.debug_store(error);
        })
    }
    fn bytes_store(&mut self, bytes: &[u8]) {
        let _ = self.buf.write_all(bytes);
        self.buf.push(Self::NULL_TERMINATOR);
    }
    fn tl_bytes_store(bytes: &[u8]) {
        STORED_ERROR.with(|stored_error| {
            let mut stored_error = stored_error.borrow_mut();
            stored_error.clear();
            stored_error.bytes_store(bytes);
        })
    }
    fn tl_clear() {
        STORED_ERROR.with(|stored_error| {
            let mut stored_error = stored_error.borrow_mut();
            stored_error.clear();
        })
    }
    fn tl_bytes_peek() -> (*const u8, usize) {
        STORED_ERROR.with(|stored_error| {
            let stored_error = stored_error.borrow();
            match stored_error.buf.len() {
                0 => (core::ptr::null(), 0), // no error!
                n => {
                    // stores an error of length n-1 AND a NULL TERMINATOR
                    (stored_error.buf.as_ptr(), n - 1)
                }
            }
        })
    }
}
thread_local! {
    static STORED_ERROR: RefCell<StoredError> = RefCell::new(StoredError::default());
}

pub const RW_OK: c_int = 0;
pub const RW_TL_ERR: c_int = -1;
pub const RW_WRONG_STATE: c_int = -2;
pub const RW_LOCK_POISONED: c_int = -3;
pub const RW_CLOSE_FAIL: c_int = -4;
pub const RW_BAD_FD: c_int = -5;
pub const RW_CONNECT_FAILED: c_int = -6;
pub const RW_WOULD_BLOCK: c_int = -7;
pub const RW_BAD_SOCKADDR: c_int = -8;

///////////////////// REOWOLF //////////////////////////

/// Returns length (via out pointer) and pointer (via return value) of the last Reowolf error.
/// - pointer is NULL iff there was no last error
/// - data at pointer is null-delimited
/// - len does NOT include the length of the null-delimiter
/// If len is NULL, it will not written to.
#[no_mangle]
pub unsafe extern "C" fn reowolf_error_peek(len: *mut usize) -> *const u8 {
    let (err_ptr, err_len) = StoredError::tl_bytes_peek();
    if !len.is_null() {
        len.write(err_len);
    }
    err_ptr
}

///////////////////// PROTOCOL DESCRIPTION //////////////////////////

/// Parses the utf8-encoded string slice to initialize a new protocol description object.
/// - On success, initializes `out` and returns 0
/// - On failure, stores an error string (see `reowolf_error_peek`) and returns -1
#[no_mangle]
pub unsafe extern "C" fn protocol_description_parse(
    pdl: *const u8,
    pdl_len: usize,
) -> *mut Arc<ProtocolDescription> {
    StoredError::tl_clear();
    match ProtocolDescription::parse(&*slice_from_raw_parts(pdl, pdl_len)) {
        Ok(new) => Box::into_raw(Box::new(Arc::new(new))),
        Err(err) => {
            StoredError::tl_bytes_store(err.as_bytes());
            std::ptr::null_mut()
        }
    }
}

/// Destroys the given initialized protocol description and frees its resources.
#[no_mangle]
pub unsafe extern "C" fn protocol_description_destroy(pd: *mut Arc<ProtocolDescription>) {
    drop(Box::from_raw(pd))
}

/// Given an initialized protocol description, initializes `out` with a clone which can be independently created or destroyed.
#[no_mangle]
pub unsafe extern "C" fn protocol_description_clone(
    pd: &Arc<ProtocolDescription>,
) -> *mut Arc<ProtocolDescription> {
    Box::into_raw(Box::new(pd.clone()))
}

///////////////////// CONNECTOR //////////////////////////

#[no_mangle]
pub unsafe extern "C" fn connector_new_logging_with_id(
    pd: &Arc<ProtocolDescription>,
    path_ptr: *const u8,
    path_len: usize,
    connector_id: ConnectorId,
) -> *mut Connector {
    StoredError::tl_clear();
    let path_bytes = &*slice_from_raw_parts(path_ptr, path_len);
    let path_str = match std::str::from_utf8(path_bytes) {
        Ok(path_str) => path_str,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            return std::ptr::null_mut();
        }
    };
    match std::fs::File::create(path_str) {
        Ok(file) => {
            let file_logger = Box::new(FileLogger::new(connector_id, file));
            let c = Connector::new(file_logger, pd.clone(), connector_id);
            Box::into_raw(Box::new(c))
        }
        Err(err) => {
            StoredError::tl_debug_store(&err);
            std::ptr::null_mut()
        }
    }
}
#[no_mangle]
pub unsafe extern "C" fn connector_new_with_id(
    pd: &Arc<ProtocolDescription>,
    connector_id: ConnectorId,
) -> *mut Connector {
    let c = Connector::new(Box::new(DummyLogger), pd.clone(), connector_id);
    Box::into_raw(Box::new(c))
}
#[no_mangle]
pub unsafe extern "C" fn connector_new_logging(
    pd: &Arc<ProtocolDescription>,
    path_ptr: *const u8,
    path_len: usize,
) -> *mut Connector {
    connector_new_logging_with_id(pd, path_ptr, path_len, Connector::random_id())
}

/// Initializes `out` with a new connector using the given protocol description as its configuration.
/// The connector uses the given (internal) connector ID.
#[no_mangle]
pub unsafe extern "C" fn connector_new(pd: &Arc<ProtocolDescription>) -> *mut Connector {
    connector_new_with_id(pd, Connector::random_id())
}

/// Destroys the given a pointer to the connector on the heap, freeing its resources.
/// Usable in {setup, communication} states.
#[no_mangle]
pub unsafe extern "C" fn connector_destroy(connector: *mut Connector) {
    drop(Box::from_raw(connector))
}

#[no_mangle]
pub unsafe extern "C" fn connector_print_debug(connector: &mut Connector) {
    println!("Debug print dump {:#?}", connector);
}

/// Given an initialized connector in setup or connecting state,
/// - Creates a new directed port pair with logical channel putter->getter,
/// - adds the ports to the native component's interface,
/// - and returns them using the given out pointers.
/// Usable in {setup, communication} states.
#[no_mangle]
pub unsafe extern "C" fn connector_add_port_pair(
    connector: &mut Connector,
    out_putter: *mut PortId,
    out_getter: *mut PortId,
) {
    let [o, i] = connector.new_port_pair();
    if !out_putter.is_null() {
        out_putter.write(o);
    }
    if !out_getter.is_null() {
        out_getter.write(i);
    }
}

/// Given
/// - an initialized connector in setup or connecting state,
/// - a string slice for the component's identifier in the connector's configured protocol description,
/// - a set of ports (represented as a slice; duplicates are ignored) in the native component's interface,
/// the connector creates a new (internal) protocol component C, such that the set of native ports are moved to C.
/// Usable in {setup, communication} states.
#[no_mangle]
pub unsafe extern "C" fn connector_add_component(
    connector: &mut Connector,
    ident_ptr: *const u8,
    ident_len: usize,
    ports_ptr: *const PortId,
    ports_len: usize,
) -> c_int {
    StoredError::tl_clear();
    match connector.add_component(
        &*slice_from_raw_parts(ident_ptr, ident_len),
        &*slice_from_raw_parts(ports_ptr, ports_len),
    ) {
        Ok(()) => RW_OK,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR
        }
    }
}

/// Given
/// - an initialized connector in setup or connecting state,
/// - a utf-8 encoded socket address,
/// - the logical polarity of P,
/// - the "physical" polarity in {Active, Passive} of the endpoint through which P's peer will be discovered,
/// returns P, a port newly added to the native interface.
#[no_mangle]
pub unsafe extern "C" fn connector_add_net_port(
    connector: &mut Connector,
    port: *mut PortId,
    addr: FfiSocketAddr,
    port_polarity: Polarity,
    endpoint_polarity: EndpointPolarity,
) -> c_int {
    StoredError::tl_clear();
    match connector.new_net_port(port_polarity, addr.into(), endpoint_polarity) {
        Ok(p) => {
            if !port.is_null() {
                port.write(p);
            }
            RW_OK
        }
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR
        }
    }
}

/// Given
/// - an initialized connector in setup or connecting state,
/// - a utf-8 encoded BIND socket addresses (i.e., "local"),
/// - a utf-8 encoded CONNECT socket addresses (i.e., "peer"),
/// returns [P, G] via out pointers [putter, getter],
/// - where P is a Putter port that sends messages into the socket
/// - where G is a Getter port that recvs messages from the socket
#[no_mangle]
pub unsafe extern "C" fn connector_add_udp_mediator_component(
    connector: &mut Connector,
    putter: *mut PortId,
    getter: *mut PortId,
    local_addr: FfiSocketAddr,
    peer_addr: FfiSocketAddr,
) -> c_int {
    StoredError::tl_clear();
    match connector.new_udp_mediator_component(local_addr.into(), peer_addr.into()) {
        Ok([p, g]) => {
            if !putter.is_null() {
                putter.write(p);
            }
            if !getter.is_null() {
                getter.write(g);
            }
            RW_OK
        }
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR
        }
    }
}

/// Connects this connector to the distributed system of connectors reachable through endpoints,
/// Usable in setup state, and changes the state to communication.
#[no_mangle]
pub unsafe extern "C" fn connector_connect(
    connector: &mut Connector,
    timeout_millis: i64,
) -> c_int {
    StoredError::tl_clear();
    let option_timeout_millis: Option<u64> = TryFrom::try_from(timeout_millis).ok();
    let timeout = option_timeout_millis.map(Duration::from_millis);
    match connector.connect(timeout) {
        Ok(()) => RW_OK,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR
        }
    }
}

// #[no_mangle]
// pub unsafe extern "C" fn connector_put_payload(
//     connector: &mut Connector,
//     port: PortId,
//     payload: *mut Payload,
// ) -> c_int {
//     match connector.put(port, payload.read()) {
//         Ok(()) => 0,
//         Err(err) => {
//             StoredError::tl_debug_store(&err);
//             -1
//         }
//     }
// }

// #[no_mangle]
// pub unsafe extern "C" fn connector_put_payload_cloning(
//     connector: &mut Connector,
//     port: PortId,
//     payload: &Payload,
// ) -> c_int {
//     match connector.put(port, payload.clone()) {
//         Ok(()) => 0,
//         Err(err) => {
//             StoredError::tl_debug_store(&err);
//             -1
//         }
//     }
// }

/// Convenience function combining the functionalities of
/// "payload_new" with "connector_put_payload".
#[no_mangle]
pub unsafe extern "C" fn connector_put_bytes(
    connector: &mut Connector,
    port: PortId,
    bytes_ptr: *const u8,
    bytes_len: usize,
) -> c_int {
    StoredError::tl_clear();
    let bytes = &*slice_from_raw_parts(bytes_ptr, bytes_len);
    match connector.put(port, Payload::from(bytes)) {
        Ok(()) => RW_OK,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn connector_get(connector: &mut Connector, port: PortId) -> c_int {
    StoredError::tl_clear();
    match connector.get(port) {
        Ok(()) => RW_OK,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn connector_next_batch(connector: &mut Connector) -> isize {
    StoredError::tl_clear();
    match connector.next_batch() {
        Ok(n) => n as isize,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR as isize
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn connector_sync(connector: &mut Connector, timeout_millis: i64) -> isize {
    StoredError::tl_clear();
    let option_timeout_millis: Option<u64> = TryFrom::try_from(timeout_millis).ok();
    let timeout = option_timeout_millis.map(Duration::from_millis);
    match connector.sync(timeout) {
        Ok(n) => n as isize,
        Err(err) => {
            StoredError::tl_debug_store(&err);
            RW_TL_ERR as isize
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn connector_gotten_bytes(
    connector: &mut Connector,
    port: PortId,
    out_len: *mut usize,
) -> *const u8 {
    StoredError::tl_clear();
    match connector.gotten(port) {
        Ok(payload_borrow) => {
            let slice = payload_borrow.as_slice();
            if !out_len.is_null() {
                out_len.write(slice.len());
            }
            slice.as_ptr()
        }
        Err(err) => {
            StoredError::tl_debug_store(&err);
            std::ptr::null()
        }
    }
}

// #[no_mangle]
// unsafe extern "C" fn connector_gotten_payload(
//     connector: &mut Connector,
//     port: PortId,
// ) -> *const Payload {
//     StoredError::tl_clear();
//     match connector.gotten(port) {
//         Ok(payload_borrow) => payload_borrow,
//         Err(err) => {
//             StoredError::tl_debug_store(&err);
//             std::ptr::null()
//         }
//     }
// }

///////////////////// PAYLOAD //////////////////////////
// #[no_mangle]
// unsafe extern "C" fn payload_new(
//     bytes_ptr: *const u8,
//     bytes_len: usize,
//     out_payload: *mut Payload,
// ) {
//     let bytes: &[u8] = &*slice_from_raw_parts(bytes_ptr, bytes_len);
//     out_payload.write(Payload::from(bytes));
// }

// #[no_mangle]
// unsafe extern "C" fn payload_destroy(payload: *mut Payload) {
//     drop(Box::from_raw(payload))
// }

// #[no_mangle]
// unsafe extern "C" fn payload_clone(payload: &Payload, out_payload: *mut Payload) {
//     out_payload.write(payload.clone())
// }

// #[no_mangle]
// unsafe extern "C" fn payload_peek_bytes(payload: &Payload, bytes_len: *mut usize) -> *const u8 {
//     let slice = payload.as_slice();
//     bytes_len.write(slice.len());
//     slice.as_ptr()
// }