Changeset - 4469bd99f7a9
[Not reviewed]
0 5 1
Christopher Esterhuyse - 5 years ago 2020-02-20 11:10:13
christopher.esterhuyse@gmail.com
one buffer, multiple ranges
6 files changed with 191 insertions and 18 deletions:
0 comments (0 inline, 0 general)
cbindgen.toml
Show inline comments
 
language = "C"
 

	
 
header = "/* CBindgen generated */"
 
include_guard = "REOWOLF_HEADER_DEFINED"
 
\ No newline at end of file
huang
Show inline comments
 
new file 100644
 
/* CBindgen generated */
 

	
 
#ifndef REOWOLF_HEADER_DEFINED
 
#define REOWOLF_HEADER_DEFINED
 

	
 
#include <stdarg.h>
 
#include <stdbool.h>
 
#include <stdint.h>
 
#include <stdlib.h>
 

	
 
typedef struct Connector Connector;
 

	
 
typedef uint32_t ControllerId;
 

	
 
/**
 
 * Provides a binding annotation for the port with the given index with "active":
 
 * (The port will conenct to a "passive" port at the given address during connect())
 
 * Returns:
 
 * - 0 for success
 
 * - 1 if the port was already bound and was left unchanged
 
 * # Safety
 
 * TODO
 
 */
 
int connector_bind_active(Connector *connector, unsigned int proto_port_index, const char *address);
 

	
 
/**
 
 * Provides a binding annotation for the port with the given index with "native":
 
 * (The port is exposed for reading and writing from the application)
 
 * Returns:
 
 * # Safety
 
 * TODO
 
 */
 
int connector_bind_native(Connector *connector, uintptr_t proto_port_index);
 

	
 
/**
 
 * Provides a binding annotation for the port with the given index with "native":
 
 * (The port is exposed for reading and writing from the application)
 
 * Returns:
 
 * # Safety
 
 * TODO
 
 */
 
int connector_bind_passive(Connector *connector,
 
                           unsigned int proto_port_index,
 
                           const char *address);
 

	
 
/**
 
 * Configures the given Reowolf connector with a protocol description in PDL.
 
 * Returns:
 
 * # Safety
 
 * TODO
 
 */
 
int connector_configure(Connector *connector, char *pdl, char *main);
 

	
 
/**
 
 * Provides a binding annotation for the port with the given index with "active":
 
 * (The port will conenct to a "passive" port at the given address during connect())
 
 * Returns:
 
 * - 0 SUCCESS: connected successfully
 
 * - TODO error codes
 
 * # Safety
 
 * TODO
 
 */
 
int connector_connect(Connector *connector, uint64_t timeout_millis);
 

	
 
/**
 
 * Destroys the given connector, freeing its underlying resources.
 
 * # Safety
 
 * TODO
 
 */
 
void connector_destroy(Connector *connector);
 

	
 
/**
 
 * Resets the error message buffer.
 
 * Returns:
 
 * - 0 if an error was cleared
 
 * - 1 if there was no error to clear
 
 * # Safety
 
 * TODO
 
 */
 
int connector_error_clear(void);
 

	
 
/**
 
 * Returns a pointer into the error buffer for reading as a null-terminated string
 
 * Returns null if there is no error in the buffer.
 
 * # Safety
 
 * TODO
 
 */
 
const char *connector_error_peek(void);
 

	
 
/**
 
 * Prepares to synchronously put a message at the given port, writing it to the given buffer.
 
 * - 0 SUCCESS
 
 * - 1 this port has the wrong direction
 
 * - 2 this port is already marked to get
 
 * # Safety
 
 * TODO
 
 */
 
int connector_get(Connector *connector, unsigned int proto_port_index);
 

	
 
/**
 
 * # Safety
 
 * TODO
 
 */
 
int connector_gotten(Connector *connector,
 
                     unsigned int proto_port_index,
 
                     const unsigned char **buf_ptr_outptr,
 
                     unsigned int *len_outptr);
 

	
 
/**
 
 * Creates and returns Reowolf Connector structure allocated on the heap.
 
 */
 
Connector *connector_new(void);
 

	
 
/**
 
 * # Safety
 
 * TODO
 
 */
 
int connector_next_batch(Connector *connector);
 

	
 
/**
 
 * Prepares to synchronously put a message at the given port, reading it from the given buffer.
 
 * # Safety
 
 * TODO
 
 */
 
int connector_put(Connector *connector,
 
                  unsigned int proto_port_index,
 
                  unsigned char *buf_ptr,
 
                  unsigned int msg_len);
 

	
 
/**
 
 * # Safety
 
 * TODO
 
 */
 
int connector_sync(Connector *connector, uint64_t timeout_millis);
 

	
 
/**
 
 * Creates and returns Reowolf Connector structure allocated on the heap.
 
 */
 
Connector *connector_with_controller_id(ControllerId controller_id);
 

	
 
/**
 
 * # Safety
 
 * TODO
 
 */
 
int port_close(Connector *connector, unsigned int _proto_port_index);
 

	
 
#endif /* REOWOLF_HEADER_DEFINED */
src/lib.rs
Show inline comments
 
@@ -7,8 +7,10 @@ mod runtime; // chris' stuff
 

	
 
#[cfg(test)]
 
mod test;
 

	
 
pub use runtime::{errors, Connector, PortBinding};
 

	
 
pub use crate::runtime::experimental::api::Port;
 

	
 
#[cfg(feature = "ffi")]
 
pub use runtime::ffi;
src/runtime/experimental/api.rs
Show inline comments
 
@@ -47,12 +47,13 @@ impl<'a> From<&'a mut [u8]> for MsgBuffer<'a> {
 
    fn from(slice: &'a mut [u8]) -> Self {
 
        Self { slice, len: 0 }
 
    }
 
}
 

	
 
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
 
#[repr(C)]
 
pub struct Port(pub u32);
 
impl From<InPort> for Port {
 
    fn from(x: InPort) -> Self {
 
        x.0
 
    }
 
}
 
@@ -60,16 +61,12 @@ impl From<OutPort> for Port {
 
    fn from(x: OutPort) -> Self {
 
        x.0
 
    }
 
}
 
pub struct InPort(Port);
 
pub struct OutPort(Port);
 
pub enum PortOp<'a> {
 
    In { port: &'a InPort, poll: bool, msg: Option<&'a mut [u8]> },
 
    Out { port: &'a OutPort, offer: bool, msg: Option<&'a [u8]> },
 
}
 

	
 
#[derive(Default)]
 
struct ChannelIndexStream {
 
    next: u32,
 
}
 
impl ChannelIndexStream {
 
@@ -173,18 +170,19 @@ impl Connected {
 
        }
 
        self.native_ports.retain(|e| !moved_ports.contains(e));
 
        self.components.push(ComponentExt { ports: moved_ports, protocol: protocol.clone(), name });
 
        // TODO add a singleton machine
 
        Ok(())
 
    }
 
    pub fn sync_set(&mut self, _ops: &mut [PortOp]) -> Result<(), ()> {
 
    pub fn sync_set(&mut self, _inbuf: &mut [u8], _ops: &mut [PortOpRs]) -> Result<(), ()> {
 
        Ok(())
 
    }
 
    pub fn sync_subsets(
 
        &mut self,
 
        _ops: &mut [PortOp],
 
        _inbuf: &mut [u8],
 
        _ops: &mut [PortOpRs],
 
        bit_subsets: &[&[usize]],
 
    ) -> Result<usize, ()> {
 
        for (batch_index, bit_subset) in bit_subsets.iter().enumerate() {
 
            println!("batch_index {:?}", batch_index);
 
            use super::bits::BitChunkIter;
 
            let chunk_iter = bit_subset.iter().copied();
 
@@ -193,26 +191,53 @@ impl Connected {
 
            }
 
        }
 
        Ok(0)
 
    }
 
}
 

	
 
macro_rules! bitslice {
 
    ($( $num:expr  ),*) => {{
 
        &[0 $( | (1usize << $num)  )*]
 
    }};
 
}
 

	
 
#[test]
 
fn api_new_test() {
 
    let mut c = Connecting::default();
 
    let net_out: OutPort = c.bind(Coupling::Active, "127.0.0.1:8001".parse().unwrap());
 
    let net_out: OutPort = c.bind(Coupling::Active, "127.0.0.1:8000".parse().unwrap());
 
    let net_in: InPort = c.bind(Coupling::Active, "127.0.0.1:8001".parse().unwrap());
 
    let proto_0 = Arc::new(Protocol::parse(b"").unwrap());
 
    let mut c = c.connect(None).unwrap();
 
    let (mem_out, mem_in) = c.new_channel();
 
    let mut inbuf = [0u8; 64];
 
    c.new_component(&proto_0, b"sync".to_vec(), &[net_in.into(), mem_out.into()]).unwrap();
 

	
 
    let mut buf = vec![0; 32];
 
    let mut ops = [
 
        PortOp::Out { port: &net_out, offer: false, msg: Some(b"hi!") },
 
        PortOp::Out { port: &net_out, offer: false, msg: Some(b"hey!") },
 
        PortOp::Out { port: &net_out, offer: false, msg: Some(b"hello, there!") },
 
        PortOp::In { port: &mem_in, poll: false, msg: Some(&mut buf) },
 
        PortOpRs::In { msg_range: None, port: &mem_in },
 
        PortOpRs::Out { msg: b"hey", port: &net_out, optional: false },
 
        PortOpRs::Out { msg: b"hi?", port: &net_out, optional: true },
 
        PortOpRs::Out { msg: b"yo!", port: &net_out, optional: false },
 
    ];
 
    c.sync_subsets(&mut ops, &[&[0b001], &[0b010], &[0b100]]).unwrap();
 
    c.sync_set(&mut ops).unwrap();
 
    c.sync_set(&mut inbuf, &mut ops).unwrap();
 
    c.sync_subsets(&mut inbuf, &mut ops, &[bitslice! {0,1,2}]).unwrap();
 
}
 

	
 
#[repr(C)]
 
pub struct PortOp {
 
    msgbuf: *mut u8,
 
    buflen: usize,
 
    msglen: usize,
 
    optional: bool,
 
}
 

	
 
pub enum PortOpRs<'a> {
 
    In { msg_range: Option<Range<usize>>, port: &'a InPort },
 
    Out { msg: &'a [u8], port: &'a OutPort, optional: bool },
 
}
 
pub struct InPortOp<'a> {
 
    msg_range: Option<Range<usize>>, // written by sync
 
    port: &'a InPort,
 
}
 
pub struct OutPortOp<'a> {
 
    msg: &'a [u8],
 
    port: &'a OutPort,
 
    optional: bool,
 
}
src/runtime/experimental/mod.rs
Show inline comments
 
mod api;
 
pub mod api;
 
mod bits;
src/runtime/mod.rs
Show inline comments
 
@@ -3,13 +3,13 @@ pub mod ffi;
 

	
 
mod actors;
 
pub(crate) mod communication;
 
pub(crate) mod connector;
 
pub(crate) mod endpoint;
 
pub mod errors;
 
mod experimental;
 
pub mod experimental;
 
mod serde;
 
pub(crate) mod setup;
 

	
 
pub(crate) type ProtocolD = crate::protocol::ProtocolDescriptionImpl;
 
pub(crate) type ProtocolS = crate::protocol::ComponentStateImpl;
 

	
0 comments (0 inline, 0 general)